Player 1 Keeps on winning in one score!(SDL)

Started by
20 comments, last by Chad Smith 18 years, 5 months ago
Alright, Today was the day I was planning to release my Pong Clone. So today, I play the game, and write down all the known bugs. I found 4 bugs(3 of them could have been ignored, but I didn't want to.)and one of them was that Player 1 Could NOT win. So I made some changes to my code and now only PLayer 1 Wins even when Player 2 Scores. It says....Player 1 Scores! You win Player 1, even when player 2 scores. It never does show that Player 2 scored now. I am going to post some of my code here that I think you will need, and hopefully y'all can help me figure out why. I am sure it is simple, and it is on my part, but I have searched for 20 minutes and could not find out why this is happening. Player 1 Scored Function

void Player1Scored()
{
     player1_score++;
     Uint8* KeysHeld = SDL_GetKeyState(0);
     do {
             Draw_Text("Player 1 has scored!", 24, 100, 500, 255, 255, 255, 0, 0, 0);
             Draw_Text("Press Space to continue.", 12, 100, 160, 255, 255, 255, 0, 0, 0);
             ballXVel=0;
             ballYVel=0;
             ball_position.x=WINDOW_WIDTH/2;
             ball_position.y=WINDOW_HEIGHT/2;
             ball_position.w=WINDOW_WIDTH/2;
             ball_position.h=WINDOW_HEIGHT/2;
             SDL_Flip(SDL_GetVideoSurface());
            if(KeysHeld[SDLK_SPACE])
            {
                 ball_position.x=WINDOW_WIDTH/2;
                 ball_position.y=WINDOW_HEIGHT/2;
                 ball_position.w=WINDOW_WIDTH/2;
                 ball_position.h=WINDOW_HEIGHT/2;
                 KeysHeld[SDLK_SPACE] = 0;
                 
            }
             
            score = false;
        }
        while(score);
}

Player 1 Won function

void Player1Win()
{
          ballXVel=0;
          ballYVel=0;
          boxYVel=0;
          Uint8* KeysHeld = SDL_GetKeyState(0);
          Draw_Text("Congradulations Player 1!  You have won!", 24, 50, 200, 255, 244, 55, 0, 0, 0);
          Draw_Text("Thank you for playing!", 12, 80, 230, 255, 244, 55, 0, 0, 0);
          Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);
          player1_score=0;
          player2_score=0;
          SDL_Flip(SDL_GetVideoSurface());
     }
}

Code that I am using this at:

if (ball_position.x  + ball_position.w ==  WINDOW_WIDTH)
        {
            Player1Scored();
        
            if(player1_score==5)
            {
                 Player1Win();
            }
            missle_dest.x=paddle1_destination.x;
            missle_dest.y=paddle1_destination.y;
            missle_dest.w=paddle1_destination.w;
            missle_dest.h=paddle1_destination.h;
            boxYVel=0;
            box_dest.x=WINDOW_WIDTH/2;
            box_dest.y=WINDOW_HEIGHT/2;
            box_dest.w=WINDOW_WIDTH/2;
            box_dest.h=WINDOW_HEIGHT/2;
            continue;
       }

While I am waiting on a reply, I will be working on my other bugs, and those I should beable to fix. Chad.
Advertisement
Try posting the code for whenever player 2 scores.
Ok, here it is.



When Player 2 Scores:
if (ball_position.x  < 0)         {            Player2Scored();            if(player2_score==5)            {                 Player2Win();            }            missle2_dest.x=paddle2_destination.x;            missle2_dest.y=paddle2_destination.y;            missle2_dest.w=paddle2_destination.w;            missle2_dest.h=paddle2_destination.h;            boxYVel=0;            box_dest.x=WINDOW_WIDTH/2;            box_dest.y=WINDOW_HEIGHT/2;            box_dest.w=WINDOW_WIDTH/2;            box_dest.h=WINDOW_HEIGHT/2;            continue;                    }




Thanks to all who help!


Chad.
Just a quick overview- you have an extra bracket in your player1 won function- i dont know if that is a mistake but your compiler should have told you.

Try posting your entire code if you can, or if not, what goes on in your player2score() function.
OOps, that is my bad, as I thought that part didn't get posted, so I just added it real fast. lol.



My code is WAY to long to post here. I will post as much information as I can without just over crowding it.


void Player1Scored(){     player1_score++;     Uint8* KeysHeld = SDL_GetKeyState(0);     do {             Draw_Text("Player 1 has scored!", 24, 100, 500, 255, 255, 255, 0, 0, 0);             Draw_Text("Press Space to continue.", 12, 100, 160, 255, 255, 255, 0, 0, 0);             ballXVel=0;             ballYVel=0;             ball_position.x=WINDOW_WIDTH/2;             ball_position.y=WINDOW_HEIGHT/2;             ball_position.w=WINDOW_WIDTH/2;             ball_position.h=WINDOW_HEIGHT/2;             SDL_Flip(SDL_GetVideoSurface());            if(KeysHeld[SDLK_SPACE])            {                 ball_position.x=WINDOW_WIDTH/2;                 ball_position.y=WINDOW_HEIGHT/2;                 ball_position.w=WINDOW_WIDTH/2;                 ball_position.h=WINDOW_HEIGHT/2;                 KeysHeld[SDLK_SPACE] = 0;                             }                         score = false;        }        while(score);}void Player2Scored(){     player2_score++;     Uint8* KeysHeld = SDL_GetKeyState(0);     do {             Draw_Text("Player 2 has scored!", 24, 100, 500, 255, 255, 255, 0, 0, 0);             Draw_Text("Press Space to continue.", 12, 100, 560, 255, 255, 255, 0, 0, 0);             ballXVel=0;             ballYVel=0;             ball_position.x=WINDOW_WIDTH/2;             ball_position.y=WINDOW_HEIGHT/2;             ball_position.w=WINDOW_WIDTH/2;             ball_position.h=WINDOW_HEIGHT/2;             SDL_Flip(SDL_GetVideoSurface());            if(KeysHeld[SDLK_SPACE])            {                 ball_position.x=WINDOW_WIDTH/2;                 ball_position.y=WINDOW_HEIGHT/2;                 ball_position.w=WINDOW_WIDTH/2;                 ball_position.h=WINDOW_HEIGHT/2;                 KeysHeld[SDLK_SPACE] = 0;                             }                         score = false;        }        while(score);}void Player2Win(){              ballXVel=0;             ballYVel=0;             boxYVel=0;             Uint8* KeysHeld = SDL_GetKeyState(0);             Draw_Text("Congradulations Player 2!  You have won!", 24, 50, 200, 255, 244, 55, 0, 0, 0);             Draw_Text("Thank you for playing!", 12, 80, 230, 255, 244, 55, 0, 0, 0);             Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);             player1_score=0;             player2_score=0;             SDL_Flip(SDL_GetVideoSurface());       }void Player1Win(){                    ballXVel=0;          ballYVel=0;          boxYVel=0;          Uint8* KeysHeld = SDL_GetKeyState(0);          Draw_Text("Congradulations Player 1!  You have won!", 24, 50, 200, 255, 244, 55, 0, 0, 0);          Draw_Text("Thank you for playing!", 12, 80, 230, 255, 244, 55, 0, 0, 0);          Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);          player1_score=0;          player2_score=0;          SDL_Flip(SDL_GetVideoSurface());          }



if (ball_position.x  + ball_position.w ==  WINDOW_WIDTH)        {            Player1Scored();                    if(player1_score==5)            {                 Player1Win();            }            missle_dest.x=paddle1_destination.x;            missle_dest.y=paddle1_destination.y;            missle_dest.w=paddle1_destination.w;            missle_dest.h=paddle1_destination.h;            boxYVel=0;            box_dest.x=WINDOW_WIDTH/2;            box_dest.y=WINDOW_HEIGHT/2;            box_dest.w=WINDOW_WIDTH/2;            box_dest.h=WINDOW_HEIGHT/2;            continue;       }         if (ball_position.x  < 0)         {            Player2Scored();            if(player2_score==5)            {                 Player2Win();            }            missle2_dest.x=paddle2_destination.x;            missle2_dest.y=paddle2_destination.y;            missle2_dest.w=paddle2_destination.w;            missle2_dest.h=paddle2_destination.h;            boxYVel=0;            box_dest.x=WINDOW_WIDTH/2;            box_dest.y=WINDOW_HEIGHT/2;            box_dest.w=WINDOW_WIDTH/2;            box_dest.h=WINDOW_HEIGHT/2;            continue;                    }





I have gotten one thing of my error list. I really only have one more thing to do after this, then the game is done! YEA! lol.


Like I said, I am sure this is just a simple mistake.

Chad.
I dont know about thiss, but you are checking if the ball's x + ball's width equals the screen width. What if its bigger?

//Edit: Whats
score
? You have it in your
do{...}while(...);
loop. Maybe it is it?
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
The only thing I question is your use of "continue"- why do you need to go back to the top of the loop? Make sure you are resetting your variables like ballx or whatever it is. If you want, email your full source to me: ender7771@yahoo.com.
Ok, I took away the score variable in my do while loop, and it kinda worked. What I mean by kinda, is that it dosen't say that Player 1 wins everytime, and I think it works. I can't tell, because in the do while loop, it won't exit until score=false, which if I took out, then it would always loop.

So we know that the score varialbe is causing it, which means that I need to think up of something else to put in my while(...), so it won't just be an infinite loop. Anyone have any ideas? I can't think of anything now.



Also, this is the only thing that is wrong with my game now. I fixed everything else, except this.


Chad.
Sorry to double post, but I still can't figure out what is wrong! The same thing keeps on happening! Can anyone please help, or am I just going to have to make this game a time game instead? Anyone, please!



chad.
If nothing else helps, add a few 'fprintf' calls in your program to write things to disk, so that you can track which functions are being called, and what values the variables have as you're playing the game. Also, try simplifying your code - for example, remove those while loops that don't actually loop. Simpler code is easier to debug.

This topic is closed to new replies.

Advertisement