I want to make a game were a circle jumps over obstacles but I can't get it to jump
I've tried all keys on keyboard
import java.awt.event.KeyEvent;
public class Game {
public static void main(String[] args) {
StdDraw.setCanvasSize();
circle player = new circle(0, 0.05 , 0.05) ;
StdDraw.line(0, 0, 1, 0);
if(StdDraw.isKeyPressed(KeyEvent.VK_UP)){
System.out.println("hello");
double height = player.height;
while(height <= 0.2)
height = height + 0.05;
StdDraw.setCanvasSize();
StdDraw.line(0, 0, 1, 0);
StdDraw.filledCircle(0, height, 0.05);
StdDraw.show(100);
}
}
}
I put in print function to test if it goes in the "if" and it doesn't
You aren't listening for KeyEvents the right way. The old way was to use KeyListener, and the newerish way is to use Key bindings, but for me when I was coding Java I found in terms of writing keyboard controls for games the answer was using a KeyEventDispatcher subclass, like I did here