Sign in to follow this  
Chad Smith

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just a quick hint:

You should put 'score = false' in 'if (keysHeld[SDLK_SPACE])', not in the do {} while () loop. Putting it there will only mess it up and make it an 'if()'. Also, I suggest 'while(!keysHeld[SDLK_SPACE])', and then when the user hits 'space' you can have this outside the loop. Let me clear it up a little.

if(KeysHeld[SDLK_SPACE])//while(!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;//-----------\
// |
} // |
// V
score = false;



Instead, have this:

while (!KeyHeld[SDLK_SPACE])
;

//And THEN have all that other stuff.



[Edited by - dudedbz1 on October 25, 2005 7:17:57 PM]

Share this post


Link to post
Share on other sites
Hey, this one person I talk to on AIM decided to help me, and he said for me to take out my do...while loops, and just do an if, and that should work. I did that, and the project wouldn't compile(a freaking } error)and now I just saw the error. Let me run the game, and see if it worked real fast....


*goes and compiles project again, and runs it*


Ok...right when it started the game, the game just cancled, and crashed, so I debugged it, and a segmatation fault happened. Let me look over code real fast!

*goes and looks over code*

Ok, I did't see anything in my code. This is weird.


Alright, I am going to post my code, which is pretty long(for a Pong Clone). The code is VERY messy, and that will be fixed later on. I have been trying all kinds of stuff, but it is not working at all. So I am turning to y'all once again! lol.
First thing you will see is their are ALOT of Globals, and I know those are bad, but it is the only way I can get things to work sometimes(some might beable to go away now, as my code has changed in some places, and I might have kept it their. I just haven't checked it yet. Sorry. :|

Remember, my code is pretty messy.


#include <SDL.h>
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <iostream>
// For my sake ;)
#pragma comment ( lib, "SDLmain.lib" )
#pragma comment ( lib, "SDL.lib" )
using namespace std;
// These are good! Const ints over #defines ;)
const int WINDOW_WIDTH = 750;
const int WINDOW_HEIGHT = 600;
const char* WINDOW_TITLE = "Paddle Ball";
bool gamerunning = true;
bool menurunning=true;
int FRAME_RATE=1000/30;
int Timer;
const int FPS=30;
int ballXVel = 0;
int ballYVel = 0;
int boxYVel = 0;
int missleXVel=0;
int missle2XVel=0;
SDL_Surface* ball=SDL_LoadBMP("Paddle_Ball.bmp");
SDL_Rect ball_position;
int player1_score=0;
int player2_score=0;
int p2hit=0;
int p1hit=0;
//function prototypes
void Draw_Text(char* string, int size, int x, int y, int ft, int fg, int fb, int br, int bg, int bb);
void LoadBall();
void Player1Score();
void Player2Score();
void Player2Win();
void InitPauseMenu();
void Pause_Menu();
void Init_Game();
void Main_Menu();

//stuff we will need for sound effects
Mix_Chunk *hit = NULL;
Mix_Chunk *misslehit = NULL;
// This needs to be declared
SDL_Surface* Continue;
// As of these
SDL_Rect Continue_source;
SDL_Rect Continue_dest;
//state flags. lol, no...not those state flags! lol.
bool score = true;
bool paused = 0;
//start main
int main(int argc, char *argv[])
{
Main_Menu();
return 0;
}
//now I am decalring the functions
void DrawBall()
{
ball_position.x=WINDOW_WIDTH/2;
ball_position.y=WINDOW_HEIGHT/2;
ball_position.w=WINDOW_WIDTH/2;
ball_position.h=WINDOW_HEIGHT/2;
}
void Draw_Text(char* string, int size, int x, int y, int fr, int fg, int fb, int br, int bg, int bb)
{
TTF_Font* font = TTF_OpenFont("arial.ttf", size);
SDL_Color fgc={fr, fg, fb};
SDL_Color bgc={br, bg, bb};
SDL_Surface* textsurface= TTF_RenderText_Shaded(font, string, fgc, bgc);
SDL_Rect textlocation={x, y, 0, 0};
SDL_BlitSurface(textsurface, NULL, SDL_GetVideoSurface(), &textlocation);
TTF_CloseFont(font);
}

void Player1Scored()
{
player1_score++;
Uint8* KeysHeld = SDL_GetKeyState(0);
if(!KeysHeld[SDLK_SPACE])
{
Draw_Text("Player 1 has won!", 24, 100, 500, 255, 255, 255, 0, 0, 0);
Draw_Text("Press Space to Contine.", 24, 100, 530, 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;
}
}
}

void Player2Scored()
{
printf("Player2 has scored");
player2_score++;
Uint8* KeysHeld = SDL_GetKeyState(0);
if(!KeysHeld[SDLK_SPACE])
{
Draw_Text("Player 2 has won!", 24, 100, 500, 255, 255, 255, 0, 0, 0);
Draw_Text("Press Space to Contine.", 24, 100, 530, 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;
}
}
}

void Player2Win()
{
printf("Player 2 has one!");
if ( (SDL_GetTicks() - Timer) >= FRAME_RATE )
{
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, 90, 250, 255, 244, 55, 0, 0, 0);
Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);
Draw_Text("(P)lay Again", 12, 110, 300, 255, 244, 55, 0, 0, 0);
player1_score=0;
player2_score=0;
SDL_Flip(SDL_GetVideoSurface());
Timer=SDL_GetTicks();
}
}

void Player1Win()
{
printf("Player 1 Win!");
if ( (SDL_GetTicks() - Timer) >= FRAME_RATE )
{
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, 90, 250, 255, 244, 55, 0, 0, 0);
Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);
Draw_Text("(P)lay Again", 12, 110, 300, 255, 244, 55, 0, 0, 0);
player1_score=0;
player2_score=0;
SDL_Flip(SDL_GetVideoSurface());
Timer=SDL_GetTicks();
}
}
void InitPauseMenu()
{
Continue = SDL_LoadBMP("pause.bmp");
// Error checking is your best friend!
if( !Continue )
printf("pause.bmp could not be loaded");

Continue_source.x=0;
Continue_source.y=1;
Continue_source.w=80;
Continue_source.h=34;

Continue_dest.x=WINDOW_WIDTH/2+20;
Continue_dest.y=WINDOW_HEIGHT/2+20;
Continue_dest.w=WINDOW_WIDTH/2+20;
Continue_dest.h=WINDOW_WIDTH/2+20;
}

void Pause_Menu()
{

Uint8* KeysHeld = SDL_GetKeyState(0);

if( KeysHeld[SDLK_DOWN] )
{
// Do something here later.

// Clear the key after press, important for menus, not for moving a paddle left and right
KeysHeld[SDLK_DOWN] = 0;
}

// Draw the paused graphics here

SDL_BlitSurface(Continue, &Continue_source, SDL_GetVideoSurface(), &Continue_dest);
}

void Init_Game()
{
printf("I am now in the Main Game");
SDL_Surface* paddle1 = SDL_LoadBMP("Paddle.bmp");
SDL_Surface* paddle2 = SDL_LoadBMP("Paddle.bmp");
SDL_Surface* box = SDL_LoadBMP("box.bmp");
SDL_Surface* missle = SDL_LoadBMP("missle.bmp");
SDL_Surface* missle2 = SDL_LoadBMP("missle.bmp");
// Initialize the pause menu, and other stuff
InitPauseMenu();
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
hit=Mix_LoadWAV("hit.wav");
misslehit=Mix_LoadWAV("misslehit.wav");
if(!paddle1)
printf("Could not load Paddle 1");
SDL_Rect paddle1_source;
paddle1_source.x = 0;
paddle1_source.y = 0;
paddle1_source.w = 20;
paddle1_source.h = 100;

SDL_Rect paddle1_destination;
paddle1_destination.x = 75;
paddle1_destination.y = 240;
paddle1_destination.w = 20;
paddle1_destination.h = 100;
if(!paddle2)
printf("Paddle 2 could not be loaded.");
SDL_Rect paddle2_source;
paddle2_source.x = 0;
paddle2_source.y = 0;
paddle2_source.w = 20;
paddle2_source.h = 100;

SDL_Rect paddle2_destination;
paddle2_destination.x = 645;
paddle2_destination.y = 240;
paddle2_destination.w = 20;
paddle2_destination.h = 100;


SDL_Rect ball_source;
ball_source.x = 0;
ball_source.y = 0;
ball_source.w = 25;
ball_source.h = 25;


SDL_Rect box_source;
box_source.x=0;
box_source.y=0;
box_source.w=35;
box_source.h=35;

SDL_Rect box_dest;
box_dest.x=WINDOW_WIDTH/2;
box_dest.y=WINDOW_HEIGHT/2;
box_dest.w=100;
box_dest.h=300;

SDL_Rect missle_source;
missle_source.x=0;
missle_source.y=0;
missle_source.w=12;
missle_source.h=13;

SDL_Rect missle_dest;
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;

SDL_Rect missle2_source;
missle2_source.x=0;
missle2_source.y=0;
missle2_source.w=12;
missle2_source.h=13;

SDL_Rect missle2_dest;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
SDL_Event event;
while (gamerunning==true)
{
if( (SDL_GetTicks() - Timer) >= FRAME_RATE)
{
DrawBall();
cout<<"FPS: " <<FPS<< FRAME_RATE<<endl;
printf("Now in the game loop");
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gamerunning = false;
}
}
// New improved method! Get all the keys now
Uint8 *keysHeld = SDL_GetKeyState(0);
if( keysHeld[SDLK_ESCAPE] )
{
gamerunning = false;

// Break from the loop
break;
}


if( keysHeld[SDLK_p] )
{
// Toggle the state
paused = !paused;

// Note that we do not need to set the vel's to 0 because we don't execute that code below!

// Want this to be triggered only once
keysHeld[SDLK_p] = 0;
}

if( paused )
{
// Clear the screen
SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
// Draw game objects
SDL_BlitSurface(paddle1, &paddle1_source, SDL_GetVideoSurface(), &paddle1_destination);
SDL_BlitSurface(paddle2, &paddle2_source, SDL_GetVideoSurface(), &paddle2_destination);
SDL_BlitSurface(ball, &ball_source, SDL_GetVideoSurface(), &ball_position);
SDL_BlitSurface(box, &box_source, SDL_GetVideoSurface(), &box_dest);
// Call the pause menu to draw the paused or w/e
Pause_Menu();
// Flip the screen
SDL_Flip(SDL_GetVideoSurface());

// We do not want to process anything else after this, so continue on in the loop
continue;
}

if( keysHeld[SDLK_SPACE] )
{
ballXVel=1;
ballYVel=1;
boxYVel=2;
}
if(keysHeld[SDLK_q])
{
player1_score=0;
player2_score=0;
Main_Menu();
keysHeld[SDLK_q] = 0;
}
if (keysHeld[SDLK_UP])
{
paddle1_destination.y -= 2;
missle_dest.y -= 2;
}
if (keysHeld[SDLK_DOWN])
{
paddle1_destination.y += 2;
missle_dest.y += 2;
if (paddle1_destination.y + paddle1_destination.h > WINDOW_HEIGHT)
{
paddle1_destination.y = 600 - paddle1_destination.h;
}

}
if (keysHeld[SDLK_KP2])
{
paddle2_destination.y += 2;
missle2_dest.y +=2;
if (paddle2_destination.y + paddle2_destination.h > WINDOW_HEIGHT)
{
paddle2_destination.y = 600 - paddle2_destination.h;
}
}
if (keysHeld[SDLK_KP8])
{
paddle2_destination.y -= 2;
missle2_dest.y -=2;
}
if(keysHeld[SDLK_s])
{
missleXVel=1;
missle_dest.x *= missleXVel;
}
if(keysHeld[SDLK_KP4])
{
missle2XVel=1;
missle2_dest.x -= missle2XVel;
}
ball_position.x += ballXVel;
ball_position.y += ballYVel;
box_dest.y += boxYVel;
missle_dest.x += missleXVel;
missle2_dest.x -= missle2XVel;
SDL_BlitSurface(missle, &missle_source, SDL_GetVideoSurface(), &missle_dest);
SDL_BlitSurface(missle2, &missle2_source, SDL_GetVideoSurface(), &missle2_dest);
if (ball_position.x + ball_position.w == WINDOW_WIDTH)
{
Player1Scored();

if(player1_score==5)
{
Player1Win();
ball_position.x=WINDOW_WIDTH/2;
ball_position.y=WINDOW_HEIGHT/2;
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;
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;
continue;
}
}
if (ball_position.x == 0 || ball_position.x > 0)
{
Player2Scored();
if(player2_score==5)
{
Player2Win();
ball_position.x=WINDOW_WIDTH/2;
ball_position.y=WINDOW_HEIGHT/2;
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;
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;
continue;
}
}
if(missle_dest.x + missle_dest.w == WINDOW_WIDTH)
{
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;
missleXVel=0;
}
if(missle2_dest.x + missle2_dest.w == WINDOW_WIDTH)
{
missle2XVel=0;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
}
if (ball_position.y + ball_position.h > WINDOW_HEIGHT)
{
ballYVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if (ball_position.y <0 )
{
ballYVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if(box_dest.y + ball_position.h > WINDOW_HEIGHT)
{
boxYVel *=-1;
}
if(box_dest.y <0)
{
boxYVel *=-1;
}
if (ball_position.x == paddle1_destination.x +
paddle1_destination.w && ball_position.y + ball_position.h >=
paddle1_destination.y && ball_position.y <= paddle1_destination.y +
paddle1_destination.h)
{
Mix_PlayChannel(-1, hit, 0);
ballXVel *= -1;
}
if(missle2_dest.x == paddle1_destination.x + paddle1_destination.w && missle2_dest.y
+ missle2_dest.h >= paddle1_destination.y && missle2_dest.y <= paddle1_destination.y +
paddle2_destination.h)
{
Mix_PlayChannel(-1, misslehit, 0);
p1hit++;
if(p1hit == 5)
{
p1hit=0;
p2hit=0;
Player2Win();
}
missle2XVel=0;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
}
if(missle_dest.x + missle_dest.w == paddle2_destination.x && missle_dest.y >= paddle2_destination.y
&& missle_dest.y <=paddle2_destination.y + paddle2_destination.h)
{
Mix_PlayChannel(-1, misslehit, 0);
p2hit++;
if(p2hit == 5)
{
p1hit=0;
p2hit=0;
Player1Win();
}
missleXVel=0;
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;
}
if (ball_position.x +ball_position.w == paddle2_destination.x &&
ball_position.y >= paddle2_destination.y && ball_position.y <=
paddle2_destination.y + paddle2_destination.h)
{
ballXVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if(ball_position.x == box_dest.x + box_dest.w && ball_position.y + ball_position.h
>= box_dest.y && ball_position.y <= box_dest.y + box_dest.h)
{
ballXVel *= -1;
ballYVel *= -2;
}
if(ball_position.x + ball_position.w == box_dest.x && ball_position.y >= box_dest.y
&& ball_position.y <= box_dest.y + box_dest.h)
{
ballXVel *= -1;
ballYVel *= -2;
}


SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
SDL_BlitSurface(paddle1, &paddle1_source, SDL_GetVideoSurface(),
&paddle1_destination);
SDL_BlitSurface(paddle2, &paddle2_source, SDL_GetVideoSurface(),
&paddle2_destination);
SDL_BlitSurface(ball, &ball_source, SDL_GetVideoSurface(), &ball_position);
SDL_BlitSurface(box, &box_source, SDL_GetVideoSurface(), &box_dest);
SDL_BlitSurface(missle, &missle_source, SDL_GetVideoSurface(), &missle_dest);
SDL_BlitSurface(missle2, &missle2_source, SDL_GetVideoSurface(), &missle2_dest);
SDL_Flip(SDL_GetVideoSurface());
SDL_Delay(1);
// Don't forget to free the surfaces!
SDL_FreeSurface(ball);
SDL_FreeSurface(paddle1);
SDL_FreeSurface(paddle2);
SDL_FreeSurface(Continue);
SDL_FreeSurface(box);
Mix_FreeChunk(hit);
SDL_Quit();
}
}
}
void Main_Menu()
{
bool menu=true;
printf("Now in the Main Menu");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
TTF_Init();
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_DOUBLEBUF );
SDL_WM_SetCaption(WINDOW_TITLE, 0);
Timer=SDL_GetTicks();
SDL_Event event;

while(menurunning)
{
if( (SDL_GetTicks() - Timer) >= FRAME_RATE)
{
Draw_Text("(P)lay Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 255, 255, 255, 0, 0, 0);
Draw_Text("(E)xit Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2+30, 255, 255, 255, 0, 0, 0);
Uint8* KeysHeld= SDL_GetKeyState(0);
SDL_Flip(SDL_GetVideoSurface());
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
menurunning = false;
}

}
if(KeysHeld[SDLK_p])
{
SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 77, 77, 255));
Init_Game();
KeysHeld[SDLK_p]=0;
menurunning=false;
Timer=SDL_GetTicks();
}
if(KeysHeld[SDLK_e])
{
SDL_Quit();
KeysHeld[SDLK_e]=0;
menurunning=false;
Timer=SDL_GetTicks();
}
Timer=SDL_GetTicks();
}
}
}





Now, I am sure that I didn't need to post all of it, but I did anyways. Again, the code is bad. Sorry about the bad coding. lol.



What it does, is that it runs, but after I leave the Main Menu, it goes to the game, then it automatically exits. Then the windows box pops up and says..."Paddle Ball has encountered an error, and needs to close...bla bla bla." I debug it, and when it closes, a little window comes up and says..."An Access Violation (Segmetation Faultation) raised in your program."

So what is up? :|

Chad.

Share this post


Link to post
Share on other sites
In your Init_Game function, I think you have miss placed a } or two =>
There might be something else but this is a no-go in my book (though I might be mistaken, I'm not a guru of SDL...)

You have your global game loop:


void Init_Game()
{
...
while (gamerunning==true)
{
if( (SDL_GetTicks() - Timer) >= FRAME_RATE)
{
...


}//WHERE I WOULD PUT THE END OF THE IF

SDL_Delay(1);

}//WHERE I WOULD PUT THE END OF THE WHILE

// Don't forget to free the surfaces!
SDL_FreeSurface(ball);
SDL_FreeSurface(paddle1);
SDL_FreeSurface(paddle2);
SDL_FreeSurface(Continue);
SDL_FreeSurface(box);
Mix_FreeChunk(hit);
SDL_Quit();
}//YOUR end of if
}//YOUR end of while
}//YOUR end of function



Share this post


Link to post
Share on other sites
Ok, I changed that, and the game dosen't crash but right when the game runs, it says..."Player 1 Has won!" and when I press space it says..."Player 2 Has one!" I can tell that this is probaly in my if, so I will most likely change that. I will try the while loop like someone stated above and see if that works.


Chad.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Once you finish that, you should try an object oriented design. Is all that int your main file? Try splitting it up a bit. Makes it more readable.

Share this post


Link to post
Share on other sites
Ok, I have edited a FEW things, but nothing big, but now after the Main Menu, the game AUTOMATICALLY goes to a screen with a light blue background(I am not sure why, cause I am not even clearing the screen to blue)and it says..."Player 2 Has scored...Press Space to continue." It does not load up the images on the screen, BUT I did some error checking, and this is what it says on the error checking:


Now in the Main Menu
I am now in the Main Game
Paddle 1 is now loaded
Paddle 2 is now loaded
box is loaded
FPS:30




Here is the source:


#include <SDL.h>
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <iostream>
// For my sake ;)
#pragma comment ( lib, "SDLmain.lib" )
#pragma comment ( lib, "SDL.lib" )
using namespace std;
// These are good! Const ints over #defines ;)
const int WINDOW_WIDTH = 750;
const int WINDOW_HEIGHT = 600;
const char* WINDOW_TITLE = "Paddle Ball";
bool gamerunning = true;
bool menurunning=true;
int FRAME_RATE=1000/30;
int Timer;
const int FPS=30;
int ballXVel = 0;
int ballYVel = 0;
int boxYVel = 0;
int missleXVel=0;
int missle2XVel=0;
SDL_Surface* ball=SDL_LoadBMP("Paddle_Ball.bmp");
SDL_Rect ball_position;
int player1_score=0;
int player2_score=0;
int p2hit=0;
int p1hit=0;
//function prototypes
void Draw_Text(char* string, int size, int x, int y, int ft, int fg, int fb, int br, int bg, int bb);
void LoadBall();
void Player1Score();
void Player2Score();
void Player2Win();
void InitPauseMenu();
void Pause_Menu();
void Init_Game();
void Main_Menu();

//stuff we will need for sound effects
Mix_Chunk *hit = NULL;
Mix_Chunk *misslehit = NULL;
// This needs to be declared
SDL_Surface* Continue;
// As of these
SDL_Rect Continue_source;
SDL_Rect Continue_dest;
//state flags. lol, no...not those state flags! lol.
bool score = true;
bool paused = 0;
//start main
int main(int argc, char *argv[])
{
Main_Menu();
return 0;
}
//now I am decalring the functions
void Draw_Text(char* string, int size, int x, int y, int fr, int fg, int fb, int br, int bg, int bb)
{
TTF_Font* font = TTF_OpenFont("arial.ttf", size);
SDL_Color fgc={fr, fg, fb};
SDL_Color bgc={br, bg, bb};
SDL_Surface* textsurface= TTF_RenderText_Shaded(font, string, fgc, bgc);
SDL_Rect textlocation={x, y, 0, 0};
SDL_BlitSurface(textsurface, NULL, SDL_GetVideoSurface(), &textlocation);
TTF_CloseFont(font);
}

void Player1Scored()
{
player1_score++;
Uint8* KeysHeld = SDL_GetKeyState(0);
while(!KeysHeld[SDLK_SPACE])
{
Draw_Text("Player 1 has won!", 24, 100, 500, 255, 255, 255, 0, 0, 0);
Draw_Text("Press Space to Contine.", 24, 100, 530, 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;
}
}
}

void Player2Scored()
{
printf("Player2 has scored");
player2_score++;
Uint8* KeysHeld = SDL_GetKeyState(0);
while(!KeysHeld[SDLK_SPACE])
{
Draw_Text("Player 2 has won!", 24, 100, 500, 255, 255, 255, 0, 0, 0);
Draw_Text("Press Space to Contine.", 24, 100, 530, 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;
}
}
}

void Player2Win()
{
printf("Player 2 has one!");
if ( (SDL_GetTicks() - Timer) >= FRAME_RATE )
{
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, 90, 250, 255, 244, 55, 0, 0, 0);
Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);
Draw_Text("(P)lay Again", 12, 110, 300, 255, 244, 55, 0, 0, 0);
player1_score=0;
player2_score=0;
SDL_Flip(SDL_GetVideoSurface());
Timer=SDL_GetTicks();
}
}

void Player1Win()
{
printf("Player 1 Win!");
if ( (SDL_GetTicks() - Timer) >= FRAME_RATE )
{
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, 90, 250, 255, 244, 55, 0, 0, 0);
Draw_Text("(Q)uit Game", 12, 100, 300, 255, 244, 55, 0, 0, 0);
Draw_Text("(P)lay Again", 12, 110, 300, 255, 244, 55, 0, 0, 0);
player1_score=0;
player2_score=0;
SDL_Flip(SDL_GetVideoSurface());
Timer=SDL_GetTicks();
}
}
void InitPauseMenu()
{
Continue = SDL_LoadBMP("pause.bmp");
// Error checking is your best friend!
if( !Continue )
printf("pause.bmp could not be loaded");

Continue_source.x=0;
Continue_source.y=1;
Continue_source.w=80;
Continue_source.h=34;

Continue_dest.x=WINDOW_WIDTH/2+20;
Continue_dest.y=WINDOW_HEIGHT/2+20;
Continue_dest.w=WINDOW_WIDTH/2+20;
Continue_dest.h=WINDOW_WIDTH/2+20;
}

void Pause_Menu()
{

Uint8* KeysHeld = SDL_GetKeyState(0);

if( KeysHeld[SDLK_DOWN] )
{
// Do something here later.

// Clear the key after press, important for menus, not for moving a paddle left and right
KeysHeld[SDLK_DOWN] = 0;
}

// Draw the paused graphics here

SDL_BlitSurface(Continue, &Continue_source, SDL_GetVideoSurface(), &Continue_dest);
}

void Init_Game()
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
printf("I am now in the Main Game");
SDL_Surface* paddle1 = SDL_LoadBMP("Paddle.bmp");
SDL_Surface* paddle2 = SDL_LoadBMP("Paddle.bmp");
SDL_Surface* box = SDL_LoadBMP("box.bmp");
SDL_Surface* missle = SDL_LoadBMP("missle.bmp");
SDL_Surface* missle2 = SDL_LoadBMP("missle.bmp");
// Initialize the pause menu, and other stuff
InitPauseMenu();
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
hit=Mix_LoadWAV("hit.wav");
misslehit=Mix_LoadWAV("misslehit.wav");
if(!paddle1)
printf("Could not load Paddle 1");
SDL_Rect paddle1_source;
paddle1_source.x = 0;
paddle1_source.y = 0;
paddle1_source.w = 20;
paddle1_source.h = 100;
cout<<"Paddle 1 is no loaded"<<endl;
SDL_Rect paddle1_destination;
paddle1_destination.x = 75;
paddle1_destination.y = 240;
paddle1_destination.w = 20;
paddle1_destination.h = 100;
if(!paddle2)
printf("Paddle 2 could not be loaded.");
SDL_Rect paddle2_source;
paddle2_source.x = 0;
paddle2_source.y = 0;
paddle2_source.w = 20;
paddle2_source.h = 100;
cout<<"Paddle 2 is now loaded"<<endl;
SDL_Rect paddle2_destination;
paddle2_destination.x = 645;
paddle2_destination.y = 240;
paddle2_destination.w = 20;
paddle2_destination.h = 100;


SDL_Rect ball_source;
ball_source.x = 0;
ball_source.y = 0;
ball_source.w = 25;
ball_source.h = 25;


SDL_Rect box_source;
box_source.x=0;
box_source.y=0;
box_source.w=35;
box_source.h=35;
if(!box)
cout<<"Box was NOT loaded"<<endl;
SDL_Rect box_dest;
box_dest.x=WINDOW_WIDTH/2;
box_dest.y=WINDOW_HEIGHT/2;
box_dest.w=100;
box_dest.h=300;
cout<<"box is loaded"<<endl;
SDL_Rect missle_source;
missle_source.x=0;
missle_source.y=0;
missle_source.w=12;
missle_source.h=13;

SDL_Rect missle_dest;
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;

SDL_Rect missle2_source;
missle2_source.x=0;
missle2_source.y=0;
missle2_source.w=12;
missle2_source.h=13;

SDL_Rect missle2_dest;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
SDL_Event event;
while (gamerunning==true)
{
if( (SDL_GetTicks() - Timer) >= FRAME_RATE)
{
cout<<"FPS: " <<FPS<< FRAME_RATE<<endl;
printf("Now in the game loop");
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gamerunning = false;
}
}
// New improved method! Get all the keys now
Uint8 *keysHeld = SDL_GetKeyState(0);
if( keysHeld[SDLK_ESCAPE] )
{
gamerunning = false;
Main_Menu();
// Break from the loop
break;
}


if( keysHeld[SDLK_p] )
{
// Toggle the state
paused = !paused;

// Note that we do not need to set the vel's to 0 because we don't execute that code below!

// Want this to be triggered only once
keysHeld[SDLK_p] = 0;
}

if( paused )
{
// Clear the screen
SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
// Draw game objects
SDL_BlitSurface(paddle1, &paddle1_source, SDL_GetVideoSurface(), &paddle1_destination);
SDL_BlitSurface(paddle2, &paddle2_source, SDL_GetVideoSurface(), &paddle2_destination);
SDL_BlitSurface(ball, &ball_source, SDL_GetVideoSurface(), &ball_position);
SDL_BlitSurface(box, &box_source, SDL_GetVideoSurface(), &box_dest);
// Call the pause menu to draw the paused or w/e
Pause_Menu();
// Flip the screen
SDL_Flip(SDL_GetVideoSurface());

// We do not want to process anything else after this, so continue on in the loop
continue;
}

if( keysHeld[SDLK_SPACE] )
{
ballXVel=1;
ballYVel=1;
boxYVel=2;
}
if(keysHeld[SDLK_q])
{
player1_score=0;
player2_score=0;
Main_Menu();
keysHeld[SDLK_q] = 0;
}
if (keysHeld[SDLK_UP])
{
paddle1_destination.y -= 2;
missle_dest.y -= 2;
}
if (keysHeld[SDLK_DOWN])
{
paddle1_destination.y += 2;
missle_dest.y += 2;
if (paddle1_destination.y + paddle1_destination.h > WINDOW_HEIGHT)
{
paddle1_destination.y = 600 - paddle1_destination.h;
}

}
if (keysHeld[SDLK_KP2])
{
paddle2_destination.y += 2;
missle2_dest.y +=2;
if (paddle2_destination.y + paddle2_destination.h > WINDOW_HEIGHT)
{
paddle2_destination.y = 600 - paddle2_destination.h;
}
}
if (keysHeld[SDLK_KP8])
{
paddle2_destination.y -= 2;
missle2_dest.y -=2;
}
if(keysHeld[SDLK_s])
{
missleXVel=1;
missle_dest.x *= missleXVel;
}
if(keysHeld[SDLK_KP4])
{
missle2XVel=1;
missle2_dest.x -= missle2XVel;
}
ball_position.x += ballXVel;
ball_position.y += ballYVel;
box_dest.y += boxYVel;
missle_dest.x += missleXVel;
missle2_dest.x -= missle2XVel;
SDL_BlitSurface(missle, &missle_source, SDL_GetVideoSurface(), &missle_dest);
SDL_BlitSurface(missle2, &missle2_source, SDL_GetVideoSurface(), &missle2_dest);
if (ball_position.x + ball_position.w == WINDOW_WIDTH)
{
Player1Scored();

if(player1_score==5)
{
Player1Win();
ball_position.x=WINDOW_WIDTH/2;
ball_position.y=WINDOW_HEIGHT/2;
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;
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;
continue;
}
}
if (ball_position.x == 0 || ball_position.x > 0)
{
Player2Scored();
if(player2_score==5)
{
Player2Win();
ball_position.x=WINDOW_WIDTH/2;
ball_position.y=WINDOW_HEIGHT/2;
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;
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;
continue;
}
}
if(missle_dest.x + missle_dest.w == WINDOW_WIDTH)
{
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;
missleXVel=0;
}
if(missle2_dest.x + missle2_dest.w == WINDOW_WIDTH)
{
missle2XVel=0;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
}
if (ball_position.y + ball_position.h > WINDOW_HEIGHT)
{
ballYVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if (ball_position.y <0 )
{
ballYVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if(box_dest.y + ball_position.h > WINDOW_HEIGHT)
{
boxYVel *=-1;
}
if(box_dest.y <0)
{
boxYVel *=-1;
}
if (ball_position.x == paddle1_destination.x +
paddle1_destination.w && ball_position.y + ball_position.h >=
paddle1_destination.y && ball_position.y <= paddle1_destination.y +
paddle1_destination.h)
{
Mix_PlayChannel(-1, hit, 0);
ballXVel *= -1;
}
if(missle2_dest.x == paddle1_destination.x + paddle1_destination.w && missle2_dest.y
+ missle2_dest.h >= paddle1_destination.y && missle2_dest.y <= paddle1_destination.y +
paddle2_destination.h)
{
Mix_PlayChannel(-1, misslehit, 0);
p1hit++;
if(p1hit == 5)
{
p1hit=0;
p2hit=0;
Player2Win();
}
missle2XVel=0;
missle2_dest.x=paddle2_destination.x;
missle2_dest.y=paddle2_destination.y;
missle2_dest.w=paddle2_destination.w;
missle2_dest.h=paddle2_destination.h;
}
if(missle_dest.x + missle_dest.w == paddle2_destination.x && missle_dest.y >= paddle2_destination.y
&& missle_dest.y <=paddle2_destination.y + paddle2_destination.h)
{
Mix_PlayChannel(-1, misslehit, 0);
p2hit++;
if(p2hit == 5)
{
p1hit=0;
p2hit=0;
Player1Win();
}
missleXVel=0;
missle_dest.x=paddle1_destination.x;
missle_dest.y=paddle1_destination.y;
missle_dest.w=paddle1_destination.w;
missle_dest.h=paddle1_destination.h;
}
if (ball_position.x +ball_position.w == paddle2_destination.x &&
ball_position.y >= paddle2_destination.y && ball_position.y <=
paddle2_destination.y + paddle2_destination.h)
{
ballXVel *= -1;
Mix_PlayChannel(-1, hit, 0);
}
if(ball_position.x == box_dest.x + box_dest.w && ball_position.y + ball_position.h
>= box_dest.y && ball_position.y <= box_dest.y + box_dest.h)
{
ballXVel *= -1;
ballYVel *= -2;
}
if(ball_position.x + ball_position.w == box_dest.x && ball_position.y >= box_dest.y
&& ball_position.y <= box_dest.y + box_dest.h)
{
ballXVel *= -1;
ballYVel *= -2;
}


SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
SDL_BlitSurface(paddle1, &paddle1_source, SDL_GetVideoSurface(),
&paddle1_destination);
SDL_BlitSurface(paddle2, &paddle2_source, SDL_GetVideoSurface(),
&paddle2_destination);
SDL_BlitSurface(ball, &ball_source, SDL_GetVideoSurface(), &ball_position);
SDL_BlitSurface(box, &box_source, SDL_GetVideoSurface(), &box_dest);
SDL_BlitSurface(missle, &missle_source, SDL_GetVideoSurface(), &missle_dest);
SDL_BlitSurface(missle2, &missle2_source, SDL_GetVideoSurface(), &missle2_dest);
SDL_Flip(SDL_GetVideoSurface());
}
}
SDL_Delay(1);
// Don't forget to free the surfaces!
SDL_FreeSurface(ball);
SDL_FreeSurface(paddle1);
SDL_FreeSurface(paddle2);
SDL_FreeSurface(Continue);
SDL_FreeSurface(box);
Mix_FreeChunk(hit);
SDL_Quit();
}
void Main_Menu()
{
bool menu=true;
printf("Now in the Main Menu");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
TTF_Init();
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_DOUBLEBUF );
SDL_WM_SetCaption(WINDOW_TITLE, 0);
Timer=SDL_GetTicks();
SDL_Event event;

while(menurunning)
{
if( (SDL_GetTicks() - Timer) >= FRAME_RATE)
{
Draw_Text("(P)lay Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 255, 255, 255, 0, 0, 0);
Draw_Text("(E)xit Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2+30, 255, 255, 255, 0, 0, 0);
Uint8* KeysHeld= SDL_GetKeyState(0);
SDL_Flip(SDL_GetVideoSurface());
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
menurunning = false;
}

}
if(KeysHeld[SDLK_p])
{
SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 77, 77, 255));
Init_Game();
KeysHeld[SDLK_p]=0;
menurunning=false;
Timer=SDL_GetTicks();
}
if(KeysHeld[SDLK_e])
{
SDL_Quit();
KeysHeld[SDLK_e]=0;
menurunning=false;
Timer=SDL_GetTicks();
}
Timer=SDL_GetTicks();
}
}
}




Again...I am VERY sorry for the bad code. This will be fixed when I don't have to worry about the game being released.



Chad.


PS:WHY IS IT DOING THAT? I CAN NOT TELL WHY. DEBUGGING DOSEN'T DO ANYTHING EITHEIR. I FEEL SO AMBARASSED CAUSE I AM ASKING TO MANY QUESTIONS THAT I CAN NOT FIGURE OUT ON MY OWN *gets an ambarassed look on his face*


Share this post


Link to post
Share on other sites
Your light blue seems to come from this line:

Quote:

SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 77, 77, 255));


Your player two scores happens because your ball is not initiated and the velocity is set to 0.

Quote:

int ballXVel = 0;



You'll waste less time restarting it all then trying to fix your bugs, I've seen more problems in your code (for example, you only reset the ball position when one player has won... Once a player has scored, you don't reset the ball position...)

Having your code in one file isn't a good idea but this is a small game so it can be accepted. What is less doable is all those global variables, the first thing you should do is pack them into classes/structures and then only use those. Put the definitions of the class/structures in a header file...

Secondly, I would really recommend breaking your Init_game function into a simple:


void Init_Game()
{
initObjects(); //Initialize objects
initPadsNBall(); //Place the pads and the ball
while(game_running)
{
updateObjectPositions(); //move objects
checkCollisions(); //check collisions between ball and pad

if(checkScoring()) //Checks to see if someone scored
initPadsNBall(); //Place the pads and the ball

game_running = checkScore(); //Checks to see if someone one the game
}

cleanUpObj();
}




Or something similar,
Jc

Share this post


Link to post
Share on other sites
I can look at this a bit more later, but from my early observations:

You are using keysHeld, and KeysHeld - two different variables. Something is probably going wrong in detecting input.

// Initialize the pause menu, and other stuff
InitPauseMenu();

Where is the definition of InitPauseMenu()?

Share this post


Link to post
Share on other sites
Lol, cool. I know I know enough SDL to try a Space Invaders Clone, but I don't want to leave this, till I know I can do this. Things are going pretty good right now. I am working on making the classes right now. :)



Chad

Share this post


Link to post
Share on other sites

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