如何使火箭游戏的动作平稳而又不会滞后?

时间:2019-02-28 18:39:30

标签: java keyevent

我正在创建游戏,但遇到一个问题。按键之间的延迟。例如,火箭向上移动,然后,如果您想向右旋转,它将停止,等待半秒钟,然后开始旋转。这使得游戏难以玩。我已经读到这与OS等有关。但是,例如,为什么其他游戏会有这种平稳的运动?有可能修复它吗?

这是我连接到类路径的外部JAR

https://drive.google.com/open?id=1y8sU8iAL2bnRq_oho8K_VeawjN-zhhsz

这是我用来运行程序的MANIFEST.MF文件。

https://drive.google.com/open?id=1PLlyzs2L5ilBftT87ywhPoVR5gxLLmeW

请帮助?

RocketTest.java

import blobz.SandBox;
import blobz.SandBoxMode;
import blobz.BlobGUI;

public class RocketTest implements BlobGUI {


private SandBox sandbox;

public static void main(String[] args) {
    // Run constructor for the RocketTest.
    new RocketTest();
}

private RocketTest() {
    // Create sandbox object.
    sandbox = new SandBox();

    // Set sandboxmode to FLOW.
    sandbox.setSandBoxMode( SandBoxMode.FLOW );

    // Set FramteRate to 15.
    sandbox.setFrameRate( 15L );

    // Initiate the sandbox.
    sandbox.init(this);
}

public void generate () {

    // Create an instance of the rocket.
    Rocket rocket = new Rocket ( 300, 300, sandbox );

    // Add it to the sandbox.
    sandbox.addBlob( rocket );
}
}

Rocket.java

import blobz.PolyBlob;
import blobz.BlobAction;
import blobz.SandBox;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;


public class Rocket extends PolyBlob implements BlobAction {

private double angle = 0.0;
private final double delta = 0.15;
private final double speed = 5.0;

Rocket( int x, int y, SandBox sandbox ) {

    // Run super class constructor.
    super( x, y, 0 );
    super.setLoc( x, y );

    // Create rocket shape using setPolygon.

    int vertices = 4;
    Point [] p = {
            new Point(10, 0),
            new Point(-10, -7),
            new Point(-5, 0),
            new Point(-10, 7),

    };
    setColor(Color.MAGENTA);
    setPolygon( p );

}

public void keyAction( KeyEvent e ) {

    // Left Arrow: Update angle.
    if ( e.getKeyCode() == 37 ) {
        if ( angle - delta < 2*Math.PI ) {
            angle = angle - delta + 2*Math.PI;
            super.setAngle(angle);
        }
        else {
            angle = angle - delta;
            super.setAngle(angle);
        }
    }

    // Up Arrow: Update x and y location.
    else if ( e.getKeyCode() == 38 ) {
        Point p = super.getLoc();
        p.x = p.x + (int) Math.round(speed * Math.cos(angle));
        p.y = p.y + (int) Math.round(speed * Math.sin(angle));
        super.setLoc(p.x, p.y);
    }

    // Right Arrow: Update angle.
    else if ( e.getKeyCode() == 39 ) {
        if ( angle + delta > 2*Math.PI ) {
            angle = angle + delta - 2*Math.PI;
            super.setAngle(angle);
        }
        else {
            angle = angle + delta;
        }   super.setAngle(angle);
    }

}

}

0 个答案:

没有答案