Archived

This topic is now archived and is closed to further replies.

Topace

Help with game...

Recommended Posts

I have a programming question about C++. I am currently wtiting a space "Shoot-Em-Up" game. It is a 3-person veiw, where you ship is on the bottom of the screen, and the enemies come from the top, i am having a problem with the laser firing. First of all i can get the first laser to fire, thats easy, but i need to get it to fire two lasers at a time. Im thinking i need an array that holds three numbers... laser[lasernumber][x][y]. Somthing like that where the lasernumber would be like 1, if it is the first laser, 2 if it is the second and so on... I dont really know if this would be the way i would want to hold the laser number and assosiate the x and y coordinates to them, if you know of a better way please tell me. Email Me!

Share this post


Link to post
Share on other sites
I''m assuming you''ll also want the bad guys to fire weapons? Right? So what I''d do is create an array of "shots" so then each time AYNONE fires a weapon it fills in the next available shot in the array specifiying the x and y co-ords and the type of weapon and probably destination x and y co-ords and the speed of the weapon, then in your main game loop you would:
1.)Paint your terrain
2.)Paint all the untis
3.)Paint all the shots

When you paint the shots you simply go through the list and depending on which type of shot it is Blit a different sprite like a missile or bomb or bullet or whatever, now when you get to type laser you simply draw a line (Maybe red?) from the start x and y to the dest x and y and then check for a collision with other ships. Also if you use bullets, and especially if you have anysort of network play, it''s ridiculous to shoot a bullet gun and actually create 100 bullet objects and update them all, so most people consider the whole shot of bullets as one line of bullets, so draw bullets from start x and y to destination x and y and move them along according to the speed variable...
Missile would be similar just move the missile towards it''s destination x and y according to it''s speed variable..
Hope this helps!
See ya,
Ben

Share this post


Link to post
Share on other sites
Personally, I would use classes, so you have something like:
laser[LaserNum].x and laser[LaserNum].y
It''s not really a good idea to have 3 dimensional arrays.

Martee
Magnum Games
http://MagnumGames.8m.com

Share this post


Link to post
Share on other sites
Using linked list for this would be much faster then using an array.......
if you used an array say of size 100.....for max of 100 bullets on screen at any given time......then you would be restricted to only 100 bullets on screen......even if you only had 10 bullets being firedt........you would still have to cycle through your array 100 times
on the other hand....a linked list would allow unlimited bullets on your screen (well only limited by your RAM).......and if you only had 10 bullets on screen......then it would only cycle through your bullets 10 times


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Of course, with an array of shots, you have the limit on
how many shots can be fired at once. A linked list would
probably kick a lot more ass in that regard.

typedef struct MyShotStruct {
int x, //The bullet''s Horizontal position
y, //The bullet''s Vertical position
target, //What it can hit (0=air unit,1=ground unit
//2=player)
speed; //How fast is it moving (negative is up,positive is
//down

MyShotStruct *Next,*Prev; //Pointers for the linked list
} ShotListStruct;

But maybe that''s just me...



----------
Disco Love For Everyone

Share this post


Link to post
Share on other sites
Just use an array, or you''re going to have to allocate memory for each new link in the list and thats going to slow your game down.

Share this post


Link to post
Share on other sites
the amount of time to allocate the memory is negligible.....
especially with the speed of todays computers, maybe if you have something pre-pentium....like an old 486 or something you MIGHT see a perofrmance hit.......but then if you are programming with an old 486 then that is a problem from the start.......
seriously......linked lists are much better to use in this case........the only place where I would use an array is for a gameMap because you have random access to all the elements in the map (i.e. gameMap[x][y] = whatever......)
but of course I would dynamically create the 2d array to save memory


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
Thanks for all your help, but im still confused... i could struct something to hold all the values, but wouldnt it be faster just to make an array?

int move_laser = 0;
int laser[50];
int laser_height = 100;
int i = 0; //number of lasers
int x = 100; //Laser X Value
int y = 100; //Laser Y Value

void movelaser(void);
void main(void);

void main(){
while(1){
blah;
blah;
blah;
blah;

if(move_laser == 1){
while(laser[x] >= 0){
for(a=0;a;a++){ //do it for as many times as there are //lasers
BLT(laser[x], laser[y],bltfast);
laser[x] = laser[x] - 1;
}
}
}
}

void movelaser(){
for(a=0;a laser[x] = ship_y - 10;
laser[y] = ship_x - 50;
}
move_laser = 1;
}

I was thinking something like that, no thats not my real code, its just a skeleton, but i think im resetting the value of the array too many times, i think i need an array thats like what i desribed the first time laser[#][X][Y] something like that, but im not sure, its not a 3-dimensional array, just one that can hold 2 others besides the first, if this helps any, please feel free to post more, sorry i wasnt very clear the first time...

Email Me!

Share this post


Link to post
Share on other sites
dont make one...use a STL vector (or list, whatever suits you best)

oh, and allocating memory for each laser is not a worry at all - reshuffling arrays is a much more time consuming task.

if you are really bothered about memory allocation you can tell a vector to pre-allocate space for x elements...

Share this post


Link to post
Share on other sites
dont make a linked list yourself...use a STL vector (or list, whatever suits you best)

oh, and allocating memory for each laser is not a worry at all - reshuffling arrays is a much more time consuming task.

if you are really bothered about memory allocation you can tell a vector to pre-allocate space for x elements...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Linked lists would be cool to hold the enemy ships but don''t use linked lists for the laser bullets! Lasers shoot faster(usually) then regular bullets and missles. In my game I only had 2 or 3 lasers on screen at a time, you might have 10 if your lasers bounce or are ultra-slow but still use an array for this. For the second laser you can cheese it by saying paint(x,y) then paint(x+offset,y). But the collision is not right so I would make them two swperate lasers, so you will have and array of 6 or 10 of them. Something like this

class Laser
{
int x;
int y;
boolean is_alive = false;
int speed = 5;
Color c = Color.red;
int height = 8,width = 2;

void fire_laser()
{
x = ship.x + ship.laser1_xoffset;
y = ship.y + ship.laser2_yoffset;
is_alive = true;
}

void draw_laser()
{
if(is_alive)
{
draw_rect[however using color,height,width,x,y]
y -= speed;//if 0 is at the top
if(y < 0 // y > screen_height)
is_alive = false;
}
}
}

then in your player ship class hav an array of these lasers and an index to remember which laser your on. Just remember to cycle the index variable back to zero so you don''t go out of bounds on your array when you are firing the missles. Then every frame I just go through the lasers in a for loop and call their draw_laser method which draws them if they are alive. Good luck!

Share this post


Link to post
Share on other sites
quote:
Original post by JonatanHedborg

Hum, i have a question!

How do you make/use a linked list?


========================
Game project(s):
www.fiend.cjb.net


Simple:



typedef struct ListElement_typ
{
int value;
ListElement_ptr next;
}ListElement, ListElement_ptr;


You just dynamically allocate memory for the next element, i.e. element1.next = new ListElement;
etc.. You can also have a link back to the previous element of the list. At the beginning and the end of the linked list you need to put a null pointer, or you can just link the list in a circle. You process the list iteratively.


------------------------------
#pragma twice

Edited by - furby100 on June 21, 2000 1:12:48 PM

Share this post


Link to post
Share on other sites