我正在使用eclipse创建一个飞扬的鸟类游戏,我的键绑定向上或向下移动都没有任何效果,鸟只会跳到JFrame的底部。我可能需要帮助,如果语法是否正确,我甚至把addKeyListener(this);在JFrame中,椭圆形显示出来,但就是它,它只是沉到底部
public FlappyBird() {
// Creating the parameters of the size of the frame with a set size
Dimension d = new Dimension(FlappyBird.WIDTH, FlappyBird.HEIGHT);
setSize(d);
// The initial positions for the setting and birdie
setting = new Setting(100);
birdie = new Bird(20,FlappyBird.HEIGHT/2,setting.pipes);
addKeyListener(this);
requestFocus();
}
private void updateGame() {
// TODO Auto-generated method stub
setting.updateGame();
birdie.updateGame();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
birdie.isPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_SPACE ) {
birdie.isPressed = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
The second class is
public class Bird extends Rectangle {
/**
*
*/
// No idea what this does
private static final long serialVersionUID = 1L;
// Speed of the birdie
private int speed = 4;
// Checking if a certain key is pressed or not
public boolean isPressed = false;
private ArrayList <Rectangle> pipes;
public Bird(int x, int y, ArrayList <Rectangle> pipes) {
setBounds(x,y,32,32);
this.pipes = pipes;
}
public void updateGame() {
if(isPressed) {
y-=speed;
}
else {
y+=speed;
}
// If the birdie touches the pipes the window will close
for (int k = 0; k < pipes.size(); k++) {
if(this.intersects(pipes.get(k))) {
System.exit(1);
}
}
}
public void renderGame(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x, y, width, height);
}
}