我可以制作随机障碍的最佳方式是什么,所以我的方块可以躲闪它们?有点像飞鸟,但小盒子像障碍物。
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Jumpy
extends Applet
implements KeyListener{
private Rectangle rect;
public void init()
{
this.addKeyListener(this);
rect = new Rectangle(50, 400, 50, 50);
}
public void paint(Graphics g)
{
setSize(500, 500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP)
{
rect.setLocation(rect.x, rect.y - 13);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
rect.setLocation(rect.x, rect.y + 13);
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}}
我不想让你写我的代码。我只是问一下什么方法/方式/如何最好地让移动障碍来到我的路上?我是java的新手,所以简单而流畅......谢谢