PDA

View Full Version : showing "lives" of the user



glcrazy
12-13-2003, 12:44 PM
Hello ;

In the game I wrote now . I give the user the choice of the number of lives he want to begin the game with (1,2, or 3(


void showlife1 (void)
{

glPushMatrix(); //save matrix
glEnable(GL_LIGHTING);
glTranslatef(-66.0f,70.0f,15.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidSphere( 3.5,15,15);
glPopMatrix(); //restore matrix

}

void showlife2 (void)
{

glPushMatrix(); //save matrix
glEnable(GL_LIGHTING);
glTranslatef(-60.0f,70.0f,15.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidSphere( 3.5,10,10);
glPopMatrix(); //restore matrix
}

void showlife3 (void)
{
glPushMatrix(); //save matrix
glEnable(GL_LIGHTING);
glTranslatef(-54.0f,70.0f,15.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidSphere( 3.5,10,10);
glPopMatrix(); //restore matrix
}

if (nLifes == 2) {showlife1();}
if (nLifes == 3) {showlife1(); showlife2 (); }
if (nLifes == 4) {showlife1(); showlife2 ();showlife3 ();}

If he chooses two lives for example .. Two spheres will appear in the top of the game to remind the user about how many lives he chose..

The number of lives must reduced whenever the ball missed the user bar ( i.e, the number of collision increases )

nBall1Collision = nBall1Collision +1 ;

if ( nBall1Collision = 1) {dontshowlife1(); }
else if ( nBall1Collision = 2) {dontshowlife2();}
else if ( nBall1Collision = 3) {dontshowlife3();}

// dontshowlife# is the same of showlife# but the color is similar to the background

if( nBall1Collision == nLifes )
{
show_Ball1 = false;
nBall1Collision = 0;
}

Unfortunately, I have no result .. I don't know where is if I follow the right way to code "Appearance and Disppearance of number of lives" .. or where is my mistake here ..

Any help will be appreciated ..

nexusone
12-13-2003, 08:01 PM
How about a variable?
I am guessing you ask for number of balls at the start of each game then just use the following

int player_lives;

if( missed_ball ) player_lives--; // reduce number of lives

If you want to know what number of lives the user pick at the start of game then add something like this.

int Init_player_lives; // holds the users pick of lives for game.

player_lives = Init_player_lives; // after player selects number of lives store is in variable for game use.

In your display routine add sometime like this:

Display_Lives( player_lives )


// This routine displays number of spheres based on L value.
void Display_Lives( int L )
{
int i;


for( i = 0; i < L; i++)
{
glPushMatrix();
glTranslatef( Start_Location + distance_between_spheres * i, 70, 15); in your case distance_between sphere's would be 6
Draw_sphere();
glPopMatrix();
}

}

Hope this helps


[This message has been edited by nexusone (edited 12-13-2003).]

glcrazy
12-14-2003, 10:11 AM
Hi nexusone,

Thank you very much ..

What you wrote works perfectly with me except that the number of sphere doesn't change .. I mean when the player choose three lives to begin with .. the code display three spheres .. when one ball is missed the number is spheres must be two but it still draw three spheres ..

As my knowledge .. diplay function clear the screen and redraw the objects every frame so each time I call Display_Lives( player_lives ) function it must draw as same number of shperes as "player_Lives" which is reduced every missed ball .. But this not happen .. ? I don't know why ..??

nexusone
12-15-2003, 04:47 AM
Hard to say, sounds like a variable problem, something not being set correctly.

Make sure that you ball count is kept in a globle variable and not a local one.



Originally posted by glcrazy:

Hi nexusone,

Thank you very much ..

What you wrote works perfectly with me except that the number of sphere doesn't change .. I mean when the player choose three lives to begin with .. the code display three spheres .. when one ball is missed the number is spheres must be two but it still draw three spheres ..

As my knowledge .. diplay function clear the screen and redraw the objects every frame so each time I call Display_Lives( player_lives ) function it must draw as same number of shperes as "player_Lives" which is reduced every missed ball .. But this not happen .. ? I don't know why ..??

glcrazy
12-16-2003, 12:30 AM
Hi nexusone,

I don't think its a variable problem because I check player_lives & nBall1Collision , player_lives decreases every missed ball I mean when nBall1Collision increases ..

I'm thinking that the problem is in hiding the spheres I mean do I have to call a sphere with backgraound color whenever there is missed ball ??

glcrazy
12-16-2003, 12:32 AM
Here is an illustration images :

http://nomatterwhat.jeeran.com/lives1.gif
http://nomatterwhat.jeeran.com/lives2.gif

nexusone
12-16-2003, 05:03 AM
You should not need to hide the spheres, when the screen is updated, only the amount of spheres needed should be drawn.

Sometime like this:

void my_display( void )
{
// Setup stuff

Draw_balls();

Draw_player_lives( player_lives );

glutSwapBuffers();
}



Originally posted by glcrazy:

Hi nexusone,

I don't think its a variable problem because I check player_lives & nBall1Collision , player_lives decreases every missed ball I mean when nBall1Collision increases ..

I'm thinking that the problem is in hiding the spheres I mean do I have to call a sphere with backgraound color whenever there is missed ball ??

nexusone
12-16-2003, 05:07 AM
You can send your program to me to look at if you like, but use this e-mail address since the other is my home and I am at work.

nexusone@netzero.net

glcrazy
12-16-2003, 07:03 AM
Hi nexusone ..

I sent the program to you with many thanks ..

12-31-2003, 10:34 AM
Yeah, the problem is definitely that you should only render the # of spheres for the # of lives the player has left. There is no reason to render a sphere with the background color, the loop from the posts above do this correctly.