/*Enter Main Game Loop*/
while (done == false)
{
/***********************Begin Event Loop**********************/
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) done = true;
break;
}
/**********************End Event Loop*********************/
/**********************Begin Drawing Sequence*************/
SDL_BlitSurface(compPaddle, 0, screen, &cRectDest);
SDL_BlitSurface(playerPaddle, 0, screen, &pRectDest);
SDL_BlitSurface(ball, 0, screen, &bRectDest);
/*********************End Drawing Sequence****************/
/*Check for player input*/
SDL_PumpEvents();
if (keylist[SDLK_UP]) pRectDest.y = pRectDest.y - 2;
if (keylist[SDLK_DOWN]) pRectDest.y = pRectDest.y + 2;
/*Computer AI*/
if (bRectDest.y > cRectDest.y) cRectDest.y = cRectDest.y + 2;
if (bRectDest.y < cRectDest.y) cRectDest.y = cRectDest.y - 2;
/*Ball Physics*/
if ((bRectDest.x < (cRectDest.x + cRectDest.w) //check X
&& ((bRectDest.y >= cRectDest.y) && (bRectDest.y <= cRectDest.y + cRectDest.h))) //check Y
&& xBallSpeed < 0) //end if statement
xBallSpeed = -xBallSpeed;
if ((bRectDest.x + bRectDest.w) > pRectDest.x //check X
&& ((bRectDest.y >= pRectDest.y) && (bRectDest.y <= (pRectDest.y+pRectDest.h))) //check Y
&& xBallSpeed > 0)//end if statement
xBallSpeed = -xBallSpeed;
if (((bRectDest.y + bRectDest.h) > ScreenHeight-1) && yBallSpeed > 0)
yBallSpeed = -yBallSpeed;
if ((bRectDest.y < 1) && yBallSpeed < 0) /*****Check if hit bottom or top of screen*****/
yBallSpeed = -yBallSpeed;
/*Check to see if there is a win or a loss*/
if (bRectDest.x < 5)
{
std::cout<<"You win! Press any key to exit.";
done = true;
}
if (bRectDest.x > ScreenWidth-5)
{
std::cout<<"You lose. Press any key to exit.";
done = true;
}
/*Ball Movement*/
bRectDest.x += xBallSpeed;
bRectDest.y += yBallSpeed;
/*Update Screen*/
SDL_Flip(screen);
}
Pong clone help...
Hey again everyone. I'm almost done with my first graphic game, and it's a Pong clone. I've got everything down besides one thing: when the paddles move, the representation of their former position stays on screen. In other words, the paddles will move and the ball will collide off of them correctly, but a trail is left behind where the paddle once was. When you move the paddle back to where that trail is, you have no way of knowing where the paddle actually is anymore since it blends in.
So basically my question is this: how do I update the screen in a way that the paddle will only be drawn where it actually is, not where it used to be? Here's the relevant code, if it helps at all:
Yeah, the exit conditions don't work all that well yet, but I'll get to em =P Thanks for any help,
Try drawing a background image before everything else. If you don't have one, just make a black bitmap of the screen res, load it, and blit it before everything else.
Thanks, that worked perfectly. If you don't mind, could you explain why that works? Just trying to learn so I can apply it in the future. Thanks again.
What you were doing was just drawing pixels in space and just those pixels. When you have a background image, the previously drawn pixels are covered by that background image, everything is back to black. Then you draw more pixels on top of that image and so on and so fourth and the loop repeats. Everything is drawn in the order that you program it to.
Or you could use SDL_FillRect(screen, NULL, colour);
Basically you're drawing stuff to the screen, but you're never getting rid of it. Every time you draw another frame, it gets put on top of whatever was previously there [because setting the whole screen to black before you draw every frame might not be what you want - ie you may want to keep what you had before], so you need to manually clear everything. Drawing a texture worked because it just replaced whatever was on the screen with the texture. This works in much the same way, but is probably faster, and you may not need a texture on your background.
--CJM
Basically you're drawing stuff to the screen, but you're never getting rid of it. Every time you draw another frame, it gets put on top of whatever was previously there [because setting the whole screen to black before you draw every frame might not be what you want - ie you may want to keep what you had before], so you need to manually clear everything. Drawing a texture worked because it just replaced whatever was on the screen with the texture. This works in much the same way, but is probably faster, and you may not need a texture on your background.
--CJM
Hey,
NB, I'd put it right after SDL_Flip too. That way, as soon as you draw it to screen, you set the image black. This is equivelent to the solution Ekim_Gram posted in this example, but if you make your drawing more complex you'll most certainly want to draw to screen and then clear everything, otherwise you might be stuck wondering why half of your stuff didn't draw...
I personally think of the two [draw/clear] as the same call.
--CJM
NB, I'd put it right after SDL_Flip too. That way, as soon as you draw it to screen, you set the image black. This is equivelent to the solution Ekim_Gram posted in this example, but if you make your drawing more complex you'll most certainly want to draw to screen and then clear everything, otherwise you might be stuck wondering why half of your stuff didn't draw...
I personally think of the two [draw/clear] as the same call.
--CJM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement