90 likes | 384 Views
KeyListener and Keyboard Events. Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) to implement KeyListener, you must implement three methods: keyPressed, keyReleased and keyTyped all three methods receive a KeyEvent
E N D
KeyListener and Keyboard Events • Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) • to implement KeyListener, you must implement three methods: keyPressed, keyReleased and keyTyped • all three methods receive a KeyEvent • to determine which key was pressed you can pass the event the message getKeyChar( ) • in order for your Java program to watch for key presses, you also have to get your container (probably a JPanel) to watch the keyboard, this is done by saying • panel.setFocusable(true); • We will look at code that uses KeyListener to implement an “etch-a-sketch” program • you might also use KeyListener for a computer game where the user uses the keyboard to control movement in the game like Pong
Building a Key-controlled Game • Let’s build a game where you use the keys to control an object on the screen (say an image) • We will use the keys ‘a’, ‘w’, ‘d’, ‘x’ to go left, up, right, down and ‘s’ to stop the motion • The game will require a Graphics JPanel which implements ActionListener for the Timer and KeyListener for keyboard interaction • In the constructor, instantiate the timer (t=new Timer(10, this); and start the timer (t.start( );) and add the keyboard listener (addKeyListener(this);) • In the main method, add a keyboard focus instruction panel.setFocusable(true); • Now we need to implement actionPerformed, keyPressed, keyTyped, keyReleased and paintComponent
Continued • For actionPerformed, move the object on the screen (x+=dx; y+=dy) and check to see if the object has hit a boundary, and then call repaint(); • In paintComponent, do super.paintComponent(g) and then redraw the object on the screen • For keyPressed, do the following: • char c=e.getKeyChar( ); • if(e==‘a’) dx--; • else if(e==‘w’) dy--; • … • we will leave keyTyped and keyReleased blank (that is, implement them as { })
Missile Defense Game • The game for today is to finish implementing the Missile game • In this game, you have a missile launcher at the bottom of the screen • Press the space bar to fire a missile • Press left and right arrows to control the motion of the launcher – it only moves left or right, so we don’t have to worry about up and down • Questions: should the missile launcher stop when it hits a boundary, wrap around or bounce off? • Should missiles wrap around or disappear? • Should the user be able to fire more than 1 missile at a time or wait until the previous missile goes off the screen?
How do You Move the Launcher? • We will have variable x for the position of the launcher, (it never goes up and down so we do not need a y) • We will use dx to indicate the rate of change of movement of the launcher • dx > 0 means it is moving to the right • dx < 0 means it is moving to the left • dx = 0 means it is stopped • When a key is pressed, we will see if that key is a left arrow or right arrow and if so, adjust dx accordingly (not x, but dx)
How do You Launch a Missile? • We will use the space bar to indicate “fire a missile” • The keyPressed method is invoked and we test to see if the key was the space bar, if so, fire the missile • If we have a single missile • then firing a new missile will not work until the current missile is gone • To have a lot of missiles, we need an array • each missile will need an x, y value, we will call them • mx[i] and my[i] to indicate it is the ith missile • if we want missiles to retain the momentum of the launcher (that is, allow the missile to move up toward the left or up toward the right), then each missile will need a mdx[i] which will be equal to the value of dx at the time the missile is launched (if the rocket launcher changes speed or direction, the missile will continue along its original path)
What do You Fire At? • Of course the intention of this game is to have targets to shoot at • Recall the Ship program that had a space ship randomly changing course • You need to determine, after you move each missile, if the missile impacts the ship (recall the collision algorithm from the GraphicBalls game • If yes, remove the missile from the screen (set my[i]=-1), add 1 to your score, and remove the ship from the screen (randomly place it somewhere else) • Note that if the missile goes off the screen (at the top), you remove it by setting my[i]=-1 • What else do we want in the game? • We could have the ship fire at us and if we get hit, we lose a life, give the player 3 lives • We could give your missile launcher a shield • We could try to let you shoot the alien missiles/bombs
Pong • You have already done bouncing balls, so you know how to bounce a ball off of a wall • You can also bounce the ball off of a paddle • You know how to move a paddle, it’s the same way you moved the missile launcher, except that the paddle goes up and down rather than left or right • The game consists of two paddles (two players), one on each side • The game “field” has a border on top and bottom so that the ball can bounce of either border • There is no border on left or right, if the ball gets past the paddle, the other player gains a point • Display the score at the top of the screen • As the game goes on, you can increase the ball speed or decrease a player’s paddle size!