GAMES TWO: LESSON TWO: Death and Reset for TMAN

Now it is time to begin to deal with the enemies of TMAN. We will not allow the enemies to move in this lesson, but we will deal with what happens if TMAN runs into an enemy. When TMAN runs into an enemy, he loses a life and he gets sent back to his starting point. If he runs out of lives he loses the game.

To deal with these added game features a new method of type boolean was added to the game:

public boolean check_move(int y, int x){ if(map[y][x]==1) score+=10; else if(map[y][x]==4) score+=100; else if(map[y][x]==3){ lives--; map[ytorn][xtorn]=2; if(lives>0){ resetPlayer(); } return false; } return true; }

The check_move method is of type boolean because of the way it is used in the keyPressed method. Inspect this carefully to see how this method is used to control the game.

Another new method is the resetPlayer method, but the use of this method is quite simple and obvious.

Finally, notice that game play is not allowed when the value of the lives variable is at zero. The only option available when lives equals zero is to hit "r" for reset.

import java.awt.*; import java.applet.*; import java.awt.event.*; public class TMAN4 extends Applet{ Dimension d; //wall=0, dot=1, hall=2, tee=3, x=4, torn=5 int[][] map = { { 0,0,0,0,0,0,0,0,0,0,0,0}, { 0,1,1,4,1,1,1,1,1,1,1,0}, { 0,1,0,0,0,1,0,0,0,1,0,0}, { 0,1,0,0,0,1,0,0,0,3,0,0}, { 0,1,0,0,0,1,1,1,1,1,1,0}, { 1,1,1,0,0,0,0,0,0,0,1,1}, { 0,0,1,1,1,5,0,0,0,0,1,0}, { 0,1,1,0,0,1,1,1,1,1,1,0}, { 0,3,0,0,0,1,0,0,0,0,1,0}, { 0,1,0,0,0,1,0,0,0,0,1,0}, { 0,1,1,1,1,1,1,1,4,1,1,0}, { 0,0,0,0,0,0,0,0,0,0,0,0} }; int[][] reset = new int[12][12]; int xtorn=5; int ytorn=6; int score; Image[] sps = new Image[6]; Image offI; int lives = 3; public void init(){ d = getSize(); offI=createImage(d.width,d.height); sps[0] = getImage(getDocumentBase(), "wall.jpg"); sps[1] = getImage(getDocumentBase(), "dot.jpg"); sps[2] = getImage(getDocumentBase(), "hall.jpg"); sps[3] = getImage(getDocumentBase(), "x.jpg"); sps[4] = getImage(getDocumentBase(), "tee.jpg"); sps[5] = getImage(getDocumentBase(), "torn.jpg"); for(int a = 0; a<12; a++){ for(int b = 0; b<12; b++){ reset[a][b] = map[a][b]; } } requestFocus(); this.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent k) { if(lives>0){ if(k.getKeyCode() == KeyEvent.VK_UP){ if(ytorn>0){ if(map[(ytorn-1)%12][xtorn]==1 || map[(ytorn-1)%12][xtorn]==2 || map[(ytorn-1)%12][xtorn]==4 || map[(ytorn-1)%12][xtorn]==3){ if(check_move((ytorn-1)%12,xtorn)){ map[ytorn][xtorn]=2; ytorn--; ytorn%=12; map[ytorn][xtorn]=5; } } } else{ if(map[11][xtorn]==1 || map[11][xtorn]==2 || map[11][xtorn]==4 || map[11][xtorn]==3){ if(check_move(11,xtorn)){ map[ytorn][xtorn]=2; ytorn=11; map[ytorn][xtorn]=5; } } } } else if(k.getKeyCode() == KeyEvent.VK_DOWN){ if(map[(ytorn+1)%12][xtorn]==1 || map[(ytorn+1)%12][xtorn]==2 || map[(ytorn+1)%12][xtorn]==4 || map[(ytorn+1)%12][xtorn]==3){ if(check_move((ytorn+1)%12,xtorn)){ map[ytorn][xtorn]=2; ytorn++; ytorn%=12; map[ytorn][xtorn]=5; } } } else if(k.getKeyCode() == KeyEvent.VK_LEFT){ if(xtorn>0){ if(map[ytorn][(xtorn-1)%12]==1 || map[ytorn][(xtorn-1)%12]==2 || map[ytorn][(xtorn-1)%12]==4 || map[ytorn][(xtorn-1)%12]==3){ if(check_move(ytorn,(xtorn-1)%12)){ map[ytorn][xtorn]=2; xtorn--; xtorn%=12; map[ytorn][xtorn]=5; } } } else{ if(map[ytorn][11]==1 || map[ytorn][11]==2 || map[ytorn][11]==4 || map[ytorn][11]==3){ if(check_move(ytorn,11)){ map[ytorn][xtorn]=2; xtorn=11; map[ytorn][xtorn]=5; } } } } else if(k.getKeyCode() == KeyEvent.VK_RIGHT){ if(map[ytorn][(xtorn+1)%12]==1 || map[ytorn][(xtorn+1)%12]==2 || map[ytorn][(xtorn+1)%12]==4 || map[ytorn][(xtorn+1)%12]==3){ if(check_move(ytorn,(xtorn+1)%12)){ map[ytorn][xtorn]=2; xtorn++; xtorn%=12; map[ytorn][xtorn]=5; } } } } if(k.getKeyChar() == 'r') resetValues(); repaint(); } }); } public boolean check_move(int y, int x){ if(map[y][x]==1) score+=10; else if(map[y][x]==4) score+=100; else if(map[y][x]==3){ lives--; map[ytorn][xtorn]=2; if(lives>0){ resetPlayer(); } return false; } return true; } public void resetPlayer(){ xtorn=5; ytorn=6; map[ytorn][xtorn]=5; } public void resetValues(){ for(int a = 0; a<12; a++){ for(int b = 0; b<12; b++){ map[a][b] = reset[a][b]; } } xtorn=5; ytorn=6; score=0; lives=3; } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.white); offG.fillRect(0,0,d.width,d.height); offG.setColor(Color.black); offG.drawString("SCORE: " + score, 10, d.height-10); offG.drawString("LIVES: " + lives, d.width/2-10, d.height-10); for(int y=0; y<map.length; y++) for(int x=0; x<map[y].length; x++) offG.drawImage(sps[map[y][x]],10+30*x,10+30*y,this); if(score>=720) offG.drawString("WIN!!!", d.width-100, d.height-10); if(lives<=0) offG.drawString("LOSS!!!", d.width-100, d.height-10); g.drawImage(offI, 0, 0, this); } }


ASSIGNMENT: