GAMES LESSON Twenty-one: Tile Games: Game Action
In this lesson we will only enable player movement, we will not attempt to
deal with enemy movement. The applet shown here allows the user to move the
player up, down, left, and right through the use of the arrow keys. Also the
player can reset the game through use of the "r" key. The applet also allows
the player to wrap around from the left to right and vice versa. (BTW: The
X's represent the enemies and in a future lesson they will be activated.)
The portion of the code which is most relevant to player movement is
contained in the keyPressed method. As you can see there are five conditions
which are checked for: up, down, right, left, and "r". You should also
inspect the resetValues method to see how reseting the game is accomplished.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class TMAN2 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;
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){
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[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][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]=2;
xtorn--;
xtorn%=12;
map[ytorn][xtorn]=5;
}
}
else{
if(map[ytorn][11]==1 || map[ytorn][11]==2 ||
map[ytorn][11]==4){
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]=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;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
Graphics offG = offI.getGraphics();
offG.setColor(Color.black);
offG.fillRect(0,0,d.width,d.height);
for(int y=0; y
ASSIGNMENT: