在使用 JFrame 编写一个简单的 Tron 游戏时,我遇到了一些问题。我不确定如何将“光周期”的显示保持在屏幕上显示的一条线上,但同时重新绘制屏幕以使移动仍然有效。这是我的代码:(仅供参考,一切都可以编译并且一切正常,因为它在这里写得很好)
public class Tron extends JPanel{
    public static int x = 40;
    public static int y = 40;
    public static int h = 360;
    public static int k = 360;
    public final static int size = 10;
    public static int move = 1;
    public static int dir = 0;
    static final Tron m = new Tron();
    static final JFrame frame = new JFrame("1P Tron");
    public static void main(String[] args){
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        m.setBackground(Color.black);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                dir = 1;
                x += 5;
                if(x > 390){
                    x = -5;
                };
                m.repaint();
            }
        };
        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                dir = 2;
                x -= 5;
                if(x < 0){
                    x = 395;
                };
                m.repaint();
            }
        };
        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                dir = 3;
                y -= 5;
                if(y < 0){
                    y = 375;
                };
                m.repaint();
            }
        };
        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                dir = 4;
                y += 5;
                if(y > 370){
                    y = 0;
                };
                m.repaint();
            }
        };
        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");
        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);
        m.getActionMap().put("UP", actionUp);
        m.getActionMap().put("DOWN", actionDown);
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        draw(g);
    }
    public void draw(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(x, y, size, size);
        g.drawString("Tron", 190, 390);
    }
}
另外,我想知道如何制作它,以便当我按下箭头键时,而不是只从 x 或 y 坐标中添加或减去一次,它会不断添加到它,直到按下另一个箭头。
编辑:为了让这个问题更容易理解,python 等价物将在curses库的 x 和 y 坐标处绘制一个字符,使用箭头键在屏幕上移动它,但从不调用stdscr.clear().
编辑:在互联网上搜索试图弄清楚如何g.drawPolygon()按照 MadProgrammer 的建议使用时,我遇到了一个有趣的想法,即添加一个变量来帮助程序“记住”最后一次按下哪个键并基于该添加到坐标中. 我认为这将消除对多边形的需求,并且我可以使用该Translate()方法,但是当我添加翻译时,程序停止工作。我需要使用其他方法吗,还是我做错了什么?这是新代码:
import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
public class Tron extends JPanel{
    public static int x = 40;
    public static int y = 40;
    public static int h = 360;
    public static int k = 360;
    public static int size = 10;
    public static int move = 1;
    public static int dir = 1;
    static final Tron m = new Tron();
    static final JFrame frame = new JFrame("1P Tron");
    public static void main(String[] args){
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        m.setBackground(Color.black);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                dir = 1;
            };
        };
        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                dir = 2;
            };
        };
        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                dir = 3;
            };
        };
        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                dir = 4;          
            };
        };
        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");
        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);
        m.getActionMap().put("UP", actionUp);
        m.getActionMap().put("DOWN", actionDown);
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        draw(g);
        p1_move(m);
    }
    public void p1_move(Tron m){
        if(dir == 1){
            if(x > 390){
                x = -5;
            };
            x += 5;
            m.repaint();
        }else if(dir == 2){
            if(x < 0){
                x = 395;
            };
            x -= 5;
            m.repaint();
        }else if(dir == 3){
            if(y < 0){
                y = 375;
            };
            y -= 5;
            m.repaint();
        }else if(dir == 4){
            if(y > 370){
                y = 0;
            };
            y += 5;
            m.repaint();
        }
    }
    public void draw(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(40, 40, size, size);
        g.translate(x, y);
    }
}
顺便说一句,如果有人想知道为什么所有的java.Util.*'s 都是单独导入的,我本来打算用它们来创建一个ArrayList,但后来决定反对它,从来没有把它们拿出来。
编辑:关于这个问题的一个列表编辑,我的程序几乎完成了。我想知道的最后一件事是如何为我的 cpu 播放器放置一个随机数生成器。最初它有一条固定的路径,如果你存活的时间足够长,它会在几秒钟后“杀死”自己,但我认为如果他只是走随机路径会更有趣。我已经知道如何使用 编写随机数生成器java.util.Random,但是这些代码最终总是比我为我的游戏所谎称的要长得多,所以我想知道是否可以将它添加到大约五个或六个陈述。
