Citadel Game Club Basic User Input This exercise builds off the
Transcription
Citadel Game Club Basic User Input This exercise builds off the
Citadel Game Club Basic User Input This exercise builds off the BouncingBall project from the "Introduction to Animation" worksheet. You need to complete that exercise before you can proceed with this worksheet. The goal of this exercise is to learn basic user input within an animation. You will add a paddle to your BouncingBall animation to make a simple Pong game. 1.) Create the Paddle Class Open your BouncingBall project. In the Project Explorer pane on the right side of your screen, expand the BouncingBall src folder. Right-click New -> Class. Name your new class Paddle. First let's create some basic class variables. int x = 50; int y = 300; int dx = 0; int dy = 0; private Game game; 2.) Add Functions to the Paddle Class First let's create a simple constructor. public Paddle(Game game) { this.game = game; } We want to draw our paddle on the screen. Let's add a paint function to the Paddle class to draw a 50x20 blue rectangle on the screen. public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(x, y, 50, 20); } Just like the Ball class, we want our paddle to move from position (x,y) to position (x+dx,y+dy). Let's add a move function. (You can copy and paste this from the Ball class, if you like.) public void move() { x = x + dx; y = y + dy; } Finally, we want a way to locate the current position of our paddle. Let's add a function getRect that returns a Rectangle object indicating the bounds of our paddle. To make the Rectangle class available, add the import line to the top of your class file. import java.awt.Rectangle; public Rectangle getRect() { return new Rectangle (x, y, 50, 20); } 3.) Detect User Input in the Game Class In the beginning of the Game class, right after you create a new Ball object let's also create a new Paddle object. Paddle paddle = new Paddle (this); Update the Game class's moveObjects function to also move the paddle. paddle.move(); And update the Game class's paint function to also draw the paddle. paddle.draw(); Now comes the tricky part. We need to add a KeyListener object to the Game class to detect the user's keystrokes. Modify the Game constructor to react when the user presses the left arrow key. When left arrow is pressed, we want the paddle to start moving left (dx=-1). When the arrow key is released, we want the paddle to stop moving (dx=0). I'll leave it up to you figure out how to move the paddle to the right. You could let the user move the paddle up & down too, if you wish. public Game() { addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_LEFT) paddle.dx = -1; } @Override public void keyReleased(KeyEvent e) { paddle.dx = 0; } }); setFocusable(true); } If you run your program now, you should see a bouncing red ball and a user-controlled blue paddle. But the ball passes right through the paddle, which totally sucks. 4.) Detect Collisions in the Ball Class We want our ball to interact with the paddle. First add a getRect function to the Ball class, similar to the one for the Paddle class. Add the import line to the top of the Ball class. import java.awt.Rectangle; public Rectangle getRect() { return new Rectangle (x, y, DIAMETER, DIAMETER); } This function returns a bounding rectangle around the circular ball. The Rectangle class has an intersects boolean function which tells us when one Rectangle overlaps with another Rectangle. We can use this to detect when the ball hits the paddle. If a collision is detected, we want to reverse the y-direction of movement. Add the following if statement to the Ball's move function, before you update the (x,y) position. if ( getRect().intersects(game.paddle.getRect()) ) dy = -dy;