im currently creating a game where you have a spaceship (movable) and has to shoot asteroids in its path. (Something similar to galaga, i guess)... the spaceship shoots lasers. Now, my problem is how to create a variable which has the lasers' coordinates and thus, must be infinite (by infinite i mean i can shoot infinite lasers). I thought of an array at first, but it wont make sense as i have to have infinite array (3d - one for 'on/off', one for x-coordinate, one for y-coordinate)...... this is a very tough solution......
can anyone give me an idea on how to create lasers as a quantity that carry both x coor, y coor, and whether its 'on/off' ....
or if there's a better solution, plzz tell me!!!!!
class LaserBeamShot {
int x,y; // relative coordinates
bool boActive; // on / off
...
}
class LaserBeam {
LaserBeamShot shots[MAX_BEAM_SHOT]; // say 100
int xStart,yStart; // relative startPos
int xEnd, yEnd; // relative aim-position of the laser beam
}
class Laser {
LaserBeam lasers[MAX_LASER]; // one laserbeam is sometimes not enough! ;)
int x,y; // relative position of the laser
}
class Gun {
Laser laser;
int x,y; // absolute position of the gun
}
A computer is delimited in its capacities. I am sorry, Rosen, but you can't have INFINITE arrays.
Hmm. Each time you shoot the laser, you create a laser shot. The shot moves up until it hits something or it reaches the end of the screen. To generalize a little bit, let's pretend that the shot's direction and speed can vary:
1 2 3 4
struct Shot {
unsigned x, y; // current position
int xSpeed, ySpeed; // current speed
};
You'll want to create a constructor for this.
The ship can shoot, move and shoot again before the first shot hits anything, so you can have multiple shots in play on the board. That means you need a collection of them. But what sort of collection? You'll need to iterate over them, add new ones, and delete them as they go off the board or hit something. That means you can delete from anywhere in the collection. A list would seem to do: list<Shot> shots;
This is just a sketch but it should get you going.