Sign in to follow this  

multiple objects - shots

This topic is 2546 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
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:
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

This topic is 2546 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this