1

所以我正在尝试用java构建一个自上而下的太空射击游戏。

我还很新,但我已经很好地掌握了制作乒乓球的技巧。我的主要问题是我似乎无法降低轮换。

我希望能够在箭头键上按右或左,并让一个基本矩形沿用户输入的方向围绕其中心旋转。

一旦我把它放下,我希望能够在箭头键上向前或向后按,使其朝着当前所面对的方向前进。

到目前为止,我所尝试的一切都影响了框架中的一切,而不仅仅是一个单一的对象。来自as2,旋转和对象在java中非常不同。

有人可以帮我解释一下我会怎么做吗?

package spaceroyalgame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.geom.AffineTransform;



public class SpaceRoyalGame extends JPanel implements KeyListener {
        static double shipRotation=0, shipRotationSpeed=0, shipSpeed=0, TOPSPEED= 20;

        static int shipy=50, shipx=50, shipWidth = 10, shipHeight = 20;
        static int WorldWidth = 700, WorldHeight = 700;
        static boolean slowdown = true;

    public static void main(String[] args) throws InterruptedException {
        SpaceRoyalGame game = new SpaceRoyalGame();

        JFrame frame = new JFrame();
        frame.add(game);
        frame.setSize(WorldWidth,WorldHeight);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener(game);


        while(true){
            game.moveShip();

            game.repaint();

            Thread.sleep(10);
        }

    }



    public void moveShip(){
        if (shipSpeed != 0) {
            shipx += shipSpeed;
        }
        if (shipRotationSpeed != 0) {
            shipRotation += shipRotationSpeed;
        }
    }


    public void paint (Graphics g){
        super.paint(g);
        setBackground(Color.black);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
        Rectangle ship = new Rectangle(shipx,shipy,100,50);

        g2d.rotate(Math.toRadians(shipRotation));
        g2d.draw(ship);
        g2d.fill(ship);


    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        slowdown = false;

        if (keyCode == KeyEvent.VK_W) {
            //System.out.println("W is pressed");
            shipSpeed = -2;

        }
        if (keyCode == KeyEvent.VK_S) {
            //System.out.println("S is pressed");
            shipSpeed = 2;
        }
        if (keyCode == KeyEvent.VK_A) {
            System.out.println("A is pressed");
            shipRotationSpeed = 1;
            shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);

        }
        if (keyCode == KeyEvent.VK_D) {
            System.out.println("D is pressed");
            shipRotationSpeed = -1;
            shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);

        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        shipRotationSpeed = 0;

        shipSpeed = 0;

    }

}
4

1 回答 1

0

尝试使用向量。由于“A”和“D”键正在修改您当前的角度,因此您应该保留一个变量来跟踪角度。

从那里:

x += speed * cos(θ)
y += speed * sin(θ)

此外,如果您希望能够加速/减速:

Δx = speed * cos(θ) * Δt + 1/2 * acceleration * Δt^2
Δy = speed * * sin(θ) * Δt + 1/2 * acceleration * Δt^2

Δt 将是最后一帧之间的时间。

可能值得研究一下物理/运动学

于 2015-10-26T20:37:22.587 回答