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?

[4201 byte] By [outawatera] at [2007-9-28 7:45:46]
# 1
Implement the KeyListener class in the applet and add it to the applet. + You are extending here Rectangle class but you don't use anything from it.
welkeidkiezena at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 2

i'm going to use the rectangle for collision testing.

if(tank[1].intersects(tank[2]).....

i already had it working (as indicated in my post) with the listener in the applet, but i want each Tank object to listen for it's own events. is this possible? any ideas?

outawatera at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 3
Then if I'm not wrong, you must use super.width, super.height, super.x and super.y
myavuzselima at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 4

no. the rectangle is fine, the height and width are fine, everything is fine except that i would like each Tank to listen for it's own KeyEvents. i do not need to make the tank extend rectangle, i just did so out of convenience. i could just as easily instantiate a rectangle and make sure it wraps the image of the tank.

can anyone help me?

outawatera at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 5
is there another class i could extend which would provide the keylistener but not muck up my "tank"?i had this working fine with all the code in the applet, but i really would like each tank to listen...
outawatera at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 6

you need to call addKeyListener in your applet class with the tank as a paramater. your tank needs to listen for keyevents *from* the applet, as opposed to from itself (which is what your code above tries to do). since a Rectangle doesn't generate keyEvents, it's obviously going to cause some problems :D

markuskidda at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 7
but a tanks is-a rectangle not, maybe an attribute of a tank object though.> class Tank extends Rectangle implements KeyListener{
mchan0a at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 8
what do you mean?
outawatera at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 9
Combat of Atari ? Cool ! I used to play that game for ages. Will player 1 also have a small advantage over player 2 in your game (player one's bullet allways came a little bit further) ?jpwinne
jpw35a at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 10

you should have only one KeyListener, and from there you could call events of your tanks, for example:

public class MyKeyListener extends KeyAdapter {

private Tank tank1 = null;

private Tank tank2 = null;

public MyKeyListener(Tank tank1, Tank tank2) {

this tank1 = tank1;

this tank2 = tank2;

}

public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()) {

case 32: tank1.shoot();break;

case 17: tank2.shoot();break;

default:;

}

}

}

VaskoLa at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...
# 11
alright. thanks everyone. and you VaskoL. looks like i'll just have to rearrange my program a bit.
outawatera at 2007-7-9 18:59:11 > top of Java-index,Other Topics,Java Game Development...