一个物体应该如何自行移动?我的下面的项目是一个简单的Ping Pong游戏翻拍的当前代码。我已成功通过用户输入使左侧Paddle移动,但我不知道如何编程Ball对象和AI Paddle对象独立移动。我怀疑我需要多个线程,但是我在哪里放置它们以及如何从线程内更新我的对象?
也许我把Ball和Paddle对象错放到错误的班级?
主要类别:
package com.N00744323.pingpong;
import javax.swing.JFrame;
public class Display
{
static JFrame frame;
public static void main(String[] args)
{
frame = new JFrame("Ping Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GUI());
frame.pack();
frame.setVisible(true);
}
}
GUI框架类:
package com.N00744323.pingpong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class GUI extends JPanel
{
private static final long serialVersionUID = 1L;
final private int WIDTH = 750, HEIGHT = 500;
private Paddle p1, p2;
private Ball b;
public GUI()
{
addKeyListener(new DirectionListener());
p1 = new Paddle(1);
p2 = new Paddle(2);
b = new Ball();
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
public int getWIDTH()
{
return WIDTH;
}
public int getHEIGHT()
{
return HEIGHT;
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.black);
page.fillRect(0, 0, WIDTH, HEIGHT);
page.setColor(Color.white);
page.fillRect(p1.getLocX(), p1.getLocY(), p1.getWIDTH(), p1.getHEIGHT());
page.fillRect(p2.getLocX(), p2.getLocY(), p2.getWIDTH(), p2.getHEIGHT());
page.fillOval(b.getLoc()[0], b.getLoc()[1], b.getSIZE()*2, b.getSIZE()*2);
}
private class DirectionListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == (KeyEvent.VK_UP))
{
p1.setLocY(-3);
System.out.println("Up!");
}
else
if(key == (KeyEvent.VK_DOWN))
{
p1.setLocY(3);
System.out.println("Down!");
}
repaint();
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == (KeyEvent.VK_UP))
{
System.out.println("Up stopped!");
}
else
if(key == (KeyEvent.VK_DOWN))
{
System.out.println("Down stopped!");
}
}
public void keyTyped(KeyEvent e)
{
int key = e.getKeyCode();
if(key == (KeyEvent.VK_UP))
{
p1.setLocY(-2);
System.out.println("Up!");
}
else
if(key == (KeyEvent.VK_DOWN))
{
p1.setLocY(2);
System.out.println("Down!");
}
repaint();
}
}
}
Paddle对象类:
package com.N00744323.pingpong;
public class Paddle
{
final private int WIDTH = 10, HEIGHT = 45;
private int locY, locX;
public Paddle(int player)
{
if(player == 1)
{
locY = (500/2) - HEIGHT;
locX = 20;
}
if(player == 2)
{
locY = (500/2) - HEIGHT;
locX = 750-WIDTH-20;
}
}
public void setLocY(int locY)
{
if(locY + this.locY >= 0 && this.locY + locY <= 500 - HEIGHT)
this.locY += locY;
}
public int getLocY()
{
return locY;
}
public int getLocX()
{
return locX;
}
public int getWIDTH()
{
return WIDTH;
}
public int getHEIGHT()
{
return HEIGHT;
}
}
球对象类:
package com.N00744323.pingpong;
public class Ball
{
final private int SIZE = 5;
private int x, y;
public Ball()
{
x = (750/2) - SIZE*2;
y = (500/2) - SIZE*2;
}
public int getSIZE()
{
return SIZE;
}
public int[] getLoc()
{
int[] pos = {x, y};
return pos;
}
public void setLoc(int[] pos)
{
x = pos[0];
y = pos[1];
}
}
谢谢!