GAMES TWO: LESSON ONE: Scoring TMAN
The TMAN game which we have developed up to this point is far from complete.
There are still a few features we need to add before we have a working game.
The feature we will add in this lesson is scoring. We want the TMAN to get
points for each oval cell it clears and to get bonus points for each T cell
which it clears. Also we want to display the current point totals as the
TMAN accumulates points. Finally, we want to declare the TMAN the game
winner when all possible points have been accumulated. (Obviously, without
active enemies accumulating points will be really easy.)
Basically to add scoring to this applet, you just have to perform tests at
the appropriate locations in the code. To score after moving with the down
arrow, you use the following code:
if(map[(ytorn+1)%12][xtorn]==1) score+=10;
if(map[(ytorn+1)%12][xtorn]==4) score+=100;
Moving in each direction requires a slight variation of this technique. In
fact, it would be more efficient to write a separate method for scoring.
Besides scoring this applet also detects when the player has accumulated all
720 points and displays a WIN!! message and it also displays the score as it
being accumulated.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class TMAN3 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;
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(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){
if(map[(ytorn-1)%12][xtorn]==1) score+=10;
if(map[(ytorn-1)%12][xtorn]==4) score+=100;
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){
if(map[11][xtorn]==1) score+=10;
if(map[11][xtorn]==4) score+=100;
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){
if(map[(ytorn+1)%12][xtorn]==1) score+=10;
if(map[(ytorn+1)%12][xtorn]==4) score+=100;
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){
if(map[ytorn][(xtorn-1)%12]==1) score+=10;
if(map[ytorn][(xtorn-1)%12]==4) score+=100;
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){
if(map[ytorn][11]==1) score+=10;
if(map[ytorn][11]==4) score+=100;
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){
if(map[ytorn][(xtorn+1)%12]==1)score+=10;
if(map[ytorn][(xtorn+1)%12]==4)score+=100;
map[ytorn][xtorn]=2;
xtorn++;
xtorn%=12;
map[ytorn][xtorn]=5;
}
}
else{
if(k.getKeyChar() == 'r') resetValues();
}
repaint();
}
});
}
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;
}
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);
for(int y=0; y=720) offG.drawString("WIN!!!", d.width-100, d.height-10);
g.drawImage(offI, 0, 0, this);
}
}
ASSIGNMENT: