I am creating a flappy bird game using eclipse and my key bindings to move up or down aren't having any effect and the bird just plops to the bottom of the JFrame. I need help if maybe the syntax is correct or not, I even put addKeyListener(this); In the JFrame the oval shows up but thats it and it just sinks to the bottom
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);
}
}