diff --git a/src/Q3.java b/src/Q3.java index 9db0cbde33fa6354dd816f9c14491d5706e66fda..3fac7aa49ae91039292520cec22e918549ac879d 100644 --- a/src/Q3.java +++ b/src/Q3.java @@ -22,13 +22,35 @@ public class Q3 extends AbstractGame { private final Image playerImage = new Image(PLAYER_IMAGE); private final Image ballImage = new Image(BALL_IMAGE); + // (b) The player should be able to move left, right, up, and down, using the respective arrow keys at a + //constant step size (in pixels per frame). Try different values for the step size (a constant named as + //STEP SIZE), starting at 1. + private static final double STEP_SIZE = 1; + + private double playerX = PLAYER_POSITION.x; + private double playerY = PLAYER_POSITION.y; + public static void main(String[] args) { new Q3().run(); } @Override protected void update(Input input) { - playerImage.draw(PLAYER_POSITION.x, PLAYER_POSITION.y); + // (b) + if (input.isDown(Keys.LEFT)) { + playerX -= STEP_SIZE; + } + if (input.isDown(Keys.RIGHT)) { + playerX += STEP_SIZE; + } + if (input.isDown(Keys.UP)) { + playerY -= STEP_SIZE; + } + if (input.isDown(Keys.DOWN)) { + playerY += STEP_SIZE; + } + + playerImage.draw(playerX, playerY); ballImage.draw(BALL_POSITION.x, BALL_POSITION.y); } }