我正在创建蛇游戏,我想从最基本的角度开始,即在屏幕上移动三角形。但是我在如何使矩形在屏幕上移动方面遇到麻烦

这是我到目前为止的代码:

Screen.java

public class Screen extends JPanel implements ActionListener, KeyListener {
    public static final JLabel statusbar = new JLabel("Default");
    public static final int WIDTH = 800, HEIGHT = 800;
    Timer t = new Timer(50, this);
    int x = 400;
    int y = 400;
    int velx = 0;
    int vely = 0;

    private BodyPart b;

    public Screen(){

        b = new BodyPart(x, y);
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public static void main(String[] args) {

    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(new Color(10, 50, 0));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.BLACK);
        for(int i = 0; i < WIDTH / 10; i++) {
            g.drawLine(i * 10, 0, i * 10, HEIGHT);
        }

        for(int i = 0; i < HEIGHT / 10; i++) {
            g.drawLine(0, i * 10, WIDTH, i * 10);
        }

        b.draw(g);
    }

    public void up(){
        vely = -10;
        velx = 0;
    }
    public void down(){
        vely = 10;
        velx = 0;
    }
    public void left(){
        vely = 0;
        velx = -10;
    }
    public void right(){
        vely = 0;
        velx = 10;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
        x += velx;
        y += vely;
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch(key){
        case KeyEvent.VK_DOWN: down();
            System.out.println("Down");
            break;
        case KeyEvent.VK_UP: up();
            System.out.println("up");
            break;
        case KeyEvent.VK_LEFT: left();
            System.out.println("left");
            break;
        case KeyEvent.VK_RIGHT: right();
            System.out.println("right");
            break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {}
}


BodyPart.java

public class BodyPart {

    int x;
    int y;

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

    public void draw(Graphics g) {
    g.setColor(Color.red);
        g.fillRect(x, y, 10, 10);
        g.setColor(Color.white);
        g.drawRect(x, y, 10, 10);
    }
}


框架

public class Frame extends JPanel {
    private static JLabel statusbar = new JLabel("Default");

    public Frame(){
        statusbar = Screen.statusbar;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        Screen s = new Screen();
        BodyPart b = new BodyPart(400,400);
        f.add(s);
        f.add(statusbar, BorderLayout.SOUTH);
        f.setSize(800, 800);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


因此,当我运行代码时,矩形仅显示在设置它的中心,并且不会移动。反正有解决办法吗?此外,状态栏也不会移动。 (有些奇怪,当我将所有内容放在一个文件中时,它确实起作用了)

另外,我计划使用linkedlist<Point>,是否可以将此程序与linkedlist<>混合使用?还是我必须更改代码才能使用linkedlist<>

谢谢

最佳答案

您的问题如下:

从那时起,您只用x = 400和y = 400创建了BodyPart对象,则只更改了Screen类中的变量x和y的值,这是错误的,因为Screen.x与BodyPart.x不同,所以当您这样做时如Screen.x + = 10之类的东西,您还需要从BodyPart更新x变量。

一个快速解决问题的方法可能是这样的:

将2个参数添加到您的draw方法中,并传递图形对象,x和y更新值。

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(new Color(10, 50, 0));
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setColor(Color.BLACK);
    for(int i = 0; i < WIDTH / 10; i++) {
        g.drawLine(i * 10, 0, i * 10, HEIGHT);
    }

    for(int i = 0; i < HEIGHT / 10; i++) {
        g.drawLine(0, i * 10, WIDTH, i * 10);
    }

    b.draw(g, x, y);
}


接收x和y更新的值,并更新BodyPart x和y变量。

  public void draw(Graphics g, int x, int y) {
    this.x = x;
    this.y = y;

    g.setColor(Color.red);
    g.fillRect(x, y, 10, 10);
    g.setColor(Color.white);
    g.drawRect(x, y, 10, 10);
}

07-26 09:29