View Full Version : opengl game

07-26-2013, 12:19 PM
I am working on a space invaders game I have rendered the cannon, shields, aliens and bullets to the screen. I am unsure of how to get the aliens to move left and right across the screen.

carsten neumann
07-26-2013, 12:42 PM
Your question is very vague, so will necessarily be the answer: Presumably you somewhere have some variables for each alien that hold its current position. Each frame you need to update those positions and render a new image with everything that moved now at those updated positions. If you've hardcoded the positions of everything the first thing you need to change is store the positions in variables.

This is more of a general programming/software engineering question, as such a general programming forum may be a better place to ask.

07-26-2013, 01:07 PM
here is some of my rendering code

glBindTexture(GL_TEXTURE_2D, _textureId_four);

glColor3f(1.0f, 1.0f, 1.0f);

for(float i=-8.0f; i<=6.0f; i+=2.0f)
glVertex3f(i,8.0f, 0.0f);
glVertex3f(i+2.0f,8.0f, 0.0f);
glVertex3f(i+2.0f,7.0f, 0.0f);
glVertex3f(i,7.0f, 0.0f);

carsten neumann
07-26-2013, 01:35 PM
Please try to ask concrete questions, do not just dump some code on people and have them figure out what the heck the problem is - see Forum Posting Guidelines for suggestions how to ask good questions.
I've already mentioned that instead of hardcoded positions you should use variables. You could for example do something like this:

struct Alien
float poxX;
float posY;

void drawAlien(Alien* a)
// draw a quad at a->posX, a->posY (lower left corner)
glVertex3f(a->posX, a->poxY, 0.f);

glVertex3f(a->posX + 2.f, a->poxY, 0.f);

glVertex3f(a->posX + 2.f, a->poxY + 2.f, 0.f);

glVertex3f(a->posX, a->poxY + 2.f, 0.f);

void moveAlien(Alien* a)
// calculate new position for alien a and update a->posX, a->posY

And for each alien in your game you allocate a variable of type Alien. In your render function you call drawAlien for each of them to draw them at their location. At the beginning of each frame you also call moveAlien for each one and calculate the position where they should be in this frame (probably based on the aliens movement direction and speed and how much time has elapsed since the last frame).

07-26-2013, 01:49 PM
sorry I will try to ask better questions

07-26-2013, 02:37 PM
The next thing (I would do) would be to put your alien drawing code into a subroutine, something like 'Draw_Alien'. Then call that subroutine from another higher level routine. It would look something like this -

void Display_Scene (float x_aln, float y_aln, float x_can, float y_can)
glPushMatrix ();
glTranslatef (x_aln, y_aln, 0.0);
Draw_Alien ();
glPopMatrix ();

glPushMatrix ();
glTranslatef (x_can, y_can, 0.0);
Draw_Cannon ();
glPopMatrix ();

The Push and Pop matrix routines cause the glTranslates to only affect the drawing routines directly below them. Without them the glTranslate function that moves the alien would also move the cannon, which you wouldn't want. It is up to you to figure out what values to assign to x_aln, y_can, etc. This could be done computationally or in response to a GUI element or an input device (like the mouse). The motion variables could all be global, in which case you wouldn't have to call Display_Scene with any arguments. But this is not recommended.

07-26-2013, 03:16 PM
cool thanks for all the help, my cannon is able to move form side to side using the arrow keys.

07-28-2013, 07:58 PM
basically I want to draw a sprite and erase it and redraw a different sprite and then erase it and then draw the previous sprite and have all the sprites to move to the right.