GAMES LESSON SIXTEEN: Pausing the Game

Sometimes you want to stop a game and keep action from advancing so that the user can take a break. (For instance, the user might have a phone or door that needs answering.) In the example the game can be paused through use of a combination of the ALT and P keys. If the user holds down the ALT key and presses the P key, the game will pause. Repeating the ALT-P combination will also unpause the game.

To keep track of the ALT key we use code in both the keyPressed and the keyReleased portion of our KeyListener. Here's the keyReleased method:

public void keyReleased(KeyEvent k){ if(k.getKeyCode() == KeyEvent.VK_ALT){ ALT_KEY=false; } }

Here's the keyPressed method.

public void keyPressed(KeyEvent k) { if(k.getKeyCode() == KeyEvent.VK_ALT){ ALT_KEY=true; } if(PAUSE){ if(ALT_KEY && k.getKeyChar()=='p'){ PAUSE=false; } } else{ if(ALT_KEY && k.getKeyChar()=='p'){ PAUSE=true; } else if(k.getKeyCode() != KeyEvent.VK_SHIFT){ if(!gameOver){ user+=k.getKeyChar(); check(); } else{ repaint(); } } } }

PAUSE.java

import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; public class PAUSE extends Applet implements Runnable{ Dimension d; Image offI; Thread tt; int x, y, index; String user = ""; String letter = ""; String letters = "abcdefghijklmnopqrstuvwxyz"; Color colors[] = { Color.black, Color.blue, Color.green, Color.red, Color.cyan }; int cc = 0;; boolean gameOver, PAUSE, ALT_KEY; Random r; public void init(){ d = getSize(); r=new Random(); spawn(); offI=createImage(d.width,d.height); tt = new Thread(this); tt.start(); requestFocus(); this.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent k) { if(k.getKeyCode() == KeyEvent.VK_ALT){ ALT_KEY=true; } if(PAUSE){ if(ALT_KEY && k.getKeyChar()=='p'){ PAUSE=false; } } else{ if(ALT_KEY && k.getKeyChar()=='p'){ PAUSE=true; } else if(k.getKeyCode() != KeyEvent.VK_SHIFT){ if(!gameOver){ user+=k.getKeyChar(); check(); } else{ repaint(); } } } } public void keyReleased(KeyEvent k){ if(k.getKeyCode() == KeyEvent.VK_ALT){ ALT_KEY=false; } } }); } public void check(){ if(user.equals(letter)){ if(index<letters.length()){ spawn(); cc++; cc%=colors.length; } else gameOver=true; } user=""; } public void spawn(){ letter = letters.substring(index,index+1); x = Math.abs(r.nextInt()%(d.width-30))+20; y = 0; index++; } public void run(){ while(!gameOver){ if(!ALT_KEY && !PAUSE) y+=3; if(y>d.height) y = 0; repaint(); try{ Thread.sleep(50); }catch(InterruptedException ie){ } } repaint(); } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.yellow); offG.fillRect(0,0,d.width,d.height); if(!gameOver){ offG.setColor(colors[cc]); offG.fillOval(x-7,y-15,20,20); offG.setColor(Color.white); if(!gameOver) offG.drawString(letter, x, y); } else { offG.setColor(Color.black); offG.drawString("GAME OVER", 50,50); } offG.drawString("INDEX: " + index, 10, 150); offG.setColor(Color.black); if(ALT_KEY&&PAUSE) offG.drawString("GAME PAUSED: alt down",5,d.height-5); else if(PAUSE) offG.drawString("GAME PAUSED",5,d.height-5); else if(ALT_KEY) offG.drawString("ALT DOWN", 5,d.height-5); g.drawImage(offI, 0,0, this); } }


ASSIGNMENT: