Jump to content
  • Advertisement
Sign in to follow this  
Xrid3r

multiple objects - shots

This topic is 2849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi you guys!

I've been working on a ripoff of the game Space invaders!
I'm using CodeBlocks as IDE with openGL and glut. It has been working pretty well until now.

The problem is laying in this function that handles the shots.. You see it's just spwning one shot.
I want the program to spawn a shot every time you hit a button (I know how to do that part) and have the same x coordinate as the player position was when the shot was fired.

Oh, I almost forgot.. I based the movment on the time elapsed since the program was started.

Here is the code:

void Shots(int shots)
{
shot s[shots];
const double t = glutGet(GLUT_ELAPSED_TIME) / 15000.0f;
float t2 = t;
for(int k = 0;k<=shots;k++)
{
s[k].y = t2-2.8f;
s[k].x = p.x;
glTranslatef(s[k].x,s[k].y,-6.0f);
glPushMatrix();
//Draws the shot..
glPointSize(6.0f);
glBegin(GL_POINTS);
glVertex2f(0.0000000000005f,0.00000000000002f);
glEnd();
glPopMatrix();
glLoadIdentity();
}
}




Hope you guys can help me out with this one.
~Xrid3r

[Edited by - Xrid3r on December 28, 2010 12:35:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
You see it's just spwning one shot.

Actually, it looks like you generate 2 shots per call.

for(int k = 0;k<=shots;k++) // for shots==1, generate shot for k=0 and k=1

In any case, your shots array should be a global or somehow independent of the Shots function. Using std::vector or a similar container class should serve.

When you hit a button, add a shot to the shots array. When the shot is terminated (by time or collision), erase/delete it from the shots array.

It appears you intend your Shots function to render the shot. Make creation of the shot itself independent of rendering it.

Share this post


Link to post
Share on other sites
Quote:
Make creation of the shot itself independent of rendering it.


And can you please explain a little further, how I'm suppose to do that?

~Xrid3r

Share this post


Link to post
Share on other sites
Quote:
Original post by Xrid3r
Quote:
Make creation of the shot itself independent of rendering it.


And can you please explain a little further, how I'm suppose to do that?

Create a global array to hold the shot information. When the "fire" button is pressed, add a shot to that array. Then call Shots to render them.

Pseudo-code:

int numShots = 0;
shot s[12]; // maximum of 12 shots total allowed
//...
if( fireButton && numShots < 12 )
{
s[numShots] = newShotWithPosition;
numShots++;
}
Shots(numShots);
//...
void Shots(int numberOfShotsInArray)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 15000.0f;
float t2 = t;
for(int k=0; k < numberOfShotsInArray; k++)
{
s[k].y = t2-2.8f; // advance the shot
glTranslatef(s[k].x,s[k].y,-6.0f);
glPushMatrix();
//Draws the shot..
glPointSize(6.0f);
glBegin(GL_POINTS);
glVertex2f(0.0000000000005f,0.00000000000002f);
glEnd();
glPopMatrix();
glLoadIdentity();
}
}

Somewhere in your code you'll need to go through the shots array and reset shots that have collided or timed out.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!