opengl game

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.

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.

here is some of my rendering code


	//alienone
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId_four);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glColor3f(1.0f, 1.0f, 1.0f);

	for(float i=-8.0f; i<=6.0f; i+=2.0f)
	{
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,1.0f);
	glVertex3f(i,8.0f, 0.0f);
	glTexCoord2f(0.0f,0.0f);
	glVertex3f(i+2.0f,8.0f, 0.0f);
	glTexCoord2f(1.0f,0.0f);
	glVertex3f(i+2.0f,7.0f, 0.0f);
	glTexCoord2f(1.0f,1.0f);
	glVertex3f(i,7.0f, 0.0f);
	glEnd();
	}

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 [thread=176139]Forum Posting Guidelines[/thread] 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)
    glBegin(GL_QUADS);
    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);
    glEnd();
}

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).

sorry I will try to ask better questions

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 ();

    etc.......
}

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.

cool thanks for all the help, my cannon is able to move form side to side using the arrow keys.

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.