game - lasers as separate variables

hello world,

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!!!!!


thnx in advance
closed account (48bpfSEw)
I think you need not a "variable" rather than a class which contains all the information of one of the laser-beam-shots.


example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
enum STATUS {ON, OFF};

struct Laser
{
  int m_X, m_Y;
  STATUS m_Status;

  Laser (int x, int y, STATUS status) : m_X (x), m_Y (y), m_Status (status)
  {
  }
};


If the ship fires multiple lasers you can store them in a vector<Laser>
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.
@necip...... im currently working with turbo c++, not codeblocks..... the compiler that i have lets me do this:
int a[][2].....

thats what i meant by infinite arrays....

THANK YOU SOOOOO MUCH EVERYONE!!!!! you have undoubtedly given a new angle....

thanx alot!!!
:) :) :)
Topic archived. No new replies allowed.