在完成编写和附加 Tron 单播放器版本的代码后,我想知道我将如何成为一个 cpu 播放器:
public class Tron{
public static void main(String[] args){
new Tron();
}
public Tron(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Tron");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel{
private int xVelocity;
private int yVelocity;
protected static final int PLAYER_SIZE = 0;
protected static final int DELTA = 4;
public static int timer = 0;
private static List xList = new ArrayList();
private static List yList = new ArrayList();
private Point player;
private Point lastTurn;
private Path2D playerPath;
private final Random random = new Random();
public TestPane(){
setBackground(Color.BLACK);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
am.put("left", new VelocityAction(-DELTA, 0));
am.put("right", new VelocityAction(DELTA, 0));
am.put("up", new VelocityAction(0, -DELTA));
am.put("down", new VelocityAction(0, DELTA));
xVelocity = DELTA;
player = new Point(40, 40);
lastTurn = new Point(player);
playerPath = new Path2D.Float();
playerPath.moveTo(40, 40);
Timer playerTimer = new Timer(20, new ActionListener(){
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e){
player.x += xVelocity;
if (player.x > getWidth()){
playerPath.lineTo(getWidth(), player.y);
playerPath.moveTo(0, player.y);
player.x = 0;
lastTurn = new Point(player);
}
if (player.x + PLAYER_SIZE < 0){
playerPath.lineTo(0, player.y);
playerPath.moveTo(getWidth() - 1, player.y);
player.x = getWidth() - 1;
lastTurn = new Point(player);
}
xList.add(player.x);
player.y += yVelocity;
if (player.y > getHeight()){
playerPath.lineTo(player.x, getHeight());
playerPath.moveTo(player.x, 0);
player.y = 0;
lastTurn = new Point(player);
}
if (player.y + PLAYER_SIZE < 0){
playerPath.lineTo(player.x, 0);
playerPath.moveTo(player.x, getHeight() - 1);
player.y = getHeight() - 1;
lastTurn = new Point(player);
}
yList.add(player.y);
repaint();
}
});
playerTimer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(6));
g2d.setColor(Color.BLUE);
g2d.draw(playerPath);
g2d.draw(new Line2D.Float(lastTurn, player));
g2d.drawRect(player.x - (PLAYER_SIZE / 2), player.y - (PLAYER_SIZE / 2), PLAYER_SIZE, PLAYER_SIZE);
}
public class VelocityAction extends AbstractAction{
private final int xDelta;
private final int yDelta;
public VelocityAction(int xDelta, int yDelta){
this.xDelta = xDelta;
this.yDelta = yDelta;
};
@Override
public void actionPerformed(ActionEvent e){
xVelocity = xDelta;
yVelocity = yDelta;
lastTurn = new Point(player);
playerPath.lineTo(player.x, player.y);
}
}
}
}
我已经尝试为人类玩家复制所有方法和声明,并添加一个随机数生成器来确定它会转向哪个方向,但是 cpu 会有点吓坏,甚至不能直线移动。提前致谢。