Combat
remember the game combat on the atari2600? i'm tryng to make a mockery :) of it in an applet, and am having a little problem.
it is a one on one top view tank combat game. i want to make the Tank it's own class. i want to to draw itself and listen for keyevents to move itself. how can i do this?
i tried this,
class Tankextends Rectangleimplements KeyListener{
int height=40;
int width=40;
double posX=10,posY=0;
boolean forward=false,reverse=false,right=false,left=false;
Image image;
Tank(Image tankImage){
image=tankImage;
setSize(width,height);
setLocation((int)posX,(int)posY);
addKeyListener(this);
}
publicvoid draw(Graphics g){
g.drawImage(image,(int)posX,(int)posY,40,40,null);
setLocation((int)posX,(int)posY);
}
publicvoid move(){
if(forward){posY=posY-3;}
if(reverse){posY=posY+3;}
if(left){posX=posX-3;}
if(right){posX=posX+3;}
}
publicvoid keyPressed(KeyEvent e){
System.out.println(e.getKeyCode());
if (e.getKeyCode()==38){forward=true;}
if (e.getKeyCode()==40){reverse=true;}
if (e.getKeyCode()==37){left=true;}
if (e.getKeyCode()==39){right=true;}
move();
}
publicvoid keyReleased(KeyEvent e){
if (e.getKeyCode()==38){forward=false;}
if (e.getKeyCode()==40){reverse=false;}
if (e.getKeyCode()==37){left=false;}
if (e.getKeyCode()==39){right=false;}
}
publicvoid keyTyped(KeyEvent e){}
}
of course this doesn't work, because Rectangle cannot implement KeyListener. can anyone think of a way to make this work?

