簡體   English   中英

Java“小行星”之類的游戲-放慢飛船的速度

[英]Java “Asteroids” Like Game - Slowing Down Ship

因此,我有一個Java搖擺應用程序,其中有一個太空船,該宇宙飛船獲取旋轉角度,然后在該方向上加速(繞其軸旋轉並移動到船頭所在的位置)。 我遇到的麻煩是,當船舶指向與先前方向相反的方向時,它會減速。

會發生什么,我需要修復什么

如果您查看圖像,您會發現當我嘗試通過完全相反的方向旋轉來補償我的速度時,速度會增加,而我需要做的就是在飛船駛過的情況下降低速度補償以前的速度。

例1:

import java.util.Set;

/**
* Created by griffin on 12/7/2015.
*/
public class Example1 extends JPanel {

public enum Input {
    ROTATE_LEFT,
    ROTATE_RIGHT,
    UP,
    DOWN
}

private final int B_WIDTH = 640;
private final int B_HEIGHT = 480;
private Set<Input> inputs;

Ship player = new Ship(320, 240);

public Example1() {
    initExample1();
}

private void initExample1() {
    inputs = new HashSet<>(25);
    setFocusable(true);
    setBackground(Color.BLACK);
    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));

    addKeyBinding("rotate-left", KeyEvent.VK_A, Input.ROTATE_LEFT);
    addKeyBinding("rotate-right", KeyEvent.VK_D, Input.ROTATE_RIGHT);
    addKeyBinding("up", KeyEvent.VK_W, Input.UP);
    addKeyBinding("down", KeyEvent.VK_S, Input.DOWN);

    Timer timer = new Timer(40, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (inputs.contains(Input.ROTATE_LEFT)) {
                player.angle -= 5;
            }

            if (inputs.contains(Input.UP)) {

                player.thrust = true;
                player.moveForwards();
            }
            else
                player.moveForwards();

            if (inputs.contains(Input.ROTATE_RIGHT)) {
                player.angle += 5;
            }

            player.checkAngle();
            player.screenWrap();

            repaint();
        }
    });
    timer.start();
}


public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);
    g2d.drawString("Speed " + (int) player.speed, 15, 15);
    AffineTransform old = AffineTransform.getTranslateInstance(player.x, player.y);
    old.rotate(Math.toRadians(player.angle), player.getWidth() / 2, player.getHeight() / 2);
    g2d.setTransform(old);
    g2d.drawImage(player.ship, 0, 0, this);

    Toolkit.getDefaultToolkit().sync();
}

private void addKeyBinding(String name, int keyCode, Input input) {
    InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = getActionMap();

    inputMap.put(KeyStroke.getKeyStroke(keyCode, 0, false), name + ".pressed");
    actionMap.put(name + ".pressed", new InputAction(input, true));

    inputMap.put(KeyStroke.getKeyStroke(keyCode, 0, true), name + ".released");
    actionMap.put(name + ".released", new InputAction(input, false));
}

private class InputAction extends AbstractAction {

    private Input input;
    private boolean pressed;

    public InputAction(Input input, boolean pressed) {
        this.input = input;
        this.pressed = pressed;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (pressed) {
            inputs.add(input);
        } else {
            inputs.remove(input);
        }
    }
}
}

船:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
* Created by griffin on 12/7/2015.
*/
public class Ship {
float directionX, directionY;
boolean thrust = false;
int x, y;
float speed = 1;
int angle = 0, vAngle = 0;

BufferedImage ship;
private String path = "rocketc.png";

public Ship(int x, int y) {
    this.x = x;
    this.y = y;
    getImage();
}

private void getImage() {
    try {
        ship = ImageIO.read(new File(path));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public int getWidth() {
    return ship.getWidth();
}

public int getHeight() {
    return ship.getHeight();
}

public void moveForwards() {

    directionX = (float) (Math.cos(Math.toRadians(vAngle))) * speed;
    directionY = (float) (Math.sin(Math.toRadians(vAngle))) * speed;

    if (thrust) {
        speed++;

        vAngle = angle;
        thrust = false;
    }

    x -= directionX;
    y -= directionY;

}

    public void checkAngle () {

        if (angle > 360) {
            angle = 0;
        }
        if (angle < 0) {
            angle = 360;
        }
    }


    public void screenWrap () {
        if (x > 640) {
            x = 0;
        }

        if (x < 0) {
            x = 640;
        }

        if (y > 480) {
            y = 0;
        }

        if (y < 0) {
            y = 480;
        }
    }
}

您可能現在已經知道了這一點,但是在檢查是否按下向上鍵時,尚未將player.thrust設置回false

if (inputs.contains(Input.UP)) {
    player.thrust = true;
    player.moveForwards();
}
else{
    player.thrust = false; // added this line
    player.moveForwards();
}

另外,如果要拖動(即,不按向上鍵時減速),則可以在Ship.moveForwards()Ship.moveForwards() speed

if (thrust) {
    speed++;

    vAngle = angle;
    thrust = false;
}
else{ // added this block
    speed--;
}

您可以用不同的值來增加/減少speed ,以得到不同的加速度/阻力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM