2 SDL questions

1. I am trying to make a pong paddle move by holding down a button. Rigtht now, it doesnt move at all. Here are the functions that are suppose to move the paddle and draw it.

2. I am trying to make the ball move by itself a little bit each frame, but its not working. The code for moving the ball is at the bottom.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void Game::HandleEvents()
{
	while( SDL_PollEvent( &event ) )
	{
		switch( event.type )
		{
		case SDL_QUIT:
			nextState = STATE_EXIT;
			break;
		case SDL_KEYDOWN:
			if( User.yPos > 0 && User.yPos < 480 )
			{
				switch( event.key.keysym.sym )
				{
				case SDLK_UP:
					User.moving = true;
					while( User.moving == true )
					{
						User.yPos -= 5.f;
					}
					break;
				case SDLK_DOWN:
					User.moving = true;
					while( User.moving == true )
					{
						User.yPos += 5.f;
					}
					break;
				default:
					break;
				}
			}
			break;
		case SDL_KEYUP:
			switch( event.key.keysym.sym )
			{
			case SDLK_UP:
				User.moving = false;
				break;
			case SDLK_DOWN:
				User.moving = false;
				break;
			}

		}
		
	}
}



1
2
3
4
5
6
7
8
9
10
11
12
void Game::Render()
{
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
	ApplySurface( 0, 0 , background, screen );
	ApplySurface( 50, User.yPos, userPaddle, screen );


	MoveBall();
	ApplySurface(ball.xPos, ball.yPos, pongBall, screen );
	SDL_Flip( screen );
	SDL_Delay( 5 );
}


1
2
3
4
5
6
7
8
9
10
void Game::MoveBall()
{
	ball.xPos += ball.xVel;
	ball.yPos += ball.yVel;

	if( ball.yPos == 0 || ball.yPos == 480 )
	{
		ball.yVel -= ( ball.yVel * 2 );
	}
}
Check out the case SDLK_UP part... What happens in there? You set user.moving to true. After that you enter to while loop which never ends.
eraggo i dont understand what your saying. Here is my logic

if up pressed
set moving to true
while moving is true
move the paddle

if up released
set moving to false( so that it will stop the while loop above^^ )

EDIT: obviously something is wrong with my logic, but could you tell me how to fix it in code?
Last edited on
That would only work if you started another thread which went ahead and looked at the next event. Since this isn't multi-threaded, when it detects that the user pressed the up (or down) key, it will enter an infinite loop.
Okay i understand its wrong. How do i fix it?!
You only need to update the user paddle position:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case SDL_KEYDOWN:
     if( User.yPos > 0 && User.yPos < 480 )
     {
          switch( event.key.keysym.sym )
          {
               case SDLK_UP:
                    User.yPos -= 5.f;
                    break;
               case SDLK_DOWN:
                    User.yPos += 5.f;
                    break;
               default:
                    break;
          }
     }
     break;


The render function should show the paddle in its current position.

Also this: if( User.yPos > 0 && User.yPos < 480 ) would freeze the paddle if its position is <= 0 or >= 480. What you should do is not let the paddle go down if its new position would bring it below 480 (assuming y == 0 is top) and not let the paddle go up if its new position would bring it above 0.
Last edited on
okay shacktar thanks for that tip about freezing the paddle but i want the paddle to go up if up is held. what is the problem and how do i fix it?
Last edited on
what is the problem

Most likely your infinite loop.

how do i fix it?

Did you at least try the code I showed you in my previous post?

shacktar wrote:
You only need to update the user paddle position:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case SDL_KEYDOWN:
    if( User.yPos > 0 && User.yPos < 480 )     
    {          
          switch( event.key.keysym.sym )
          {
               case SDLK_UP:
                    User.yPos -= 5.f;
                    break;
               case SDLK_DOWN:
                    User.yPos += 5.f;
                    break;
               default:
                    break;
          }
     }
     break;


The render function should show the paddle in its current position.
Last edited on
Yes. my case SDL_KEYDOWN looks just like yours, and still the paddle is not showing up at all. and my render function looks like this
1
2
3
4
5
6
7
8
9
10
11
12
void Game::Render()
{
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
	ApplySurface( 0, 0 , background, screen );
	ApplySurface( 30, (User.yPos + User.yVel), userPaddle, screen );


	MoveBall();
	ApplySurface(ball.xPos, ball.yPos, pongBall, screen );
	SDL_Flip( screen );
}


Does the ball show up though?

Comment out your event handler except for the quit event. Does the paddle then show up? If not, it's either a problem with User.yPos not being initialized or a problem with the userPaddle surface. If the paddle does show up, then all I can think of (without seeing the rest of the code) is that the paddle moves so quick it goes beyond the screen before you have a chance to see it. If that's it, then you should make the paddle move slower (try 1, or 0.01 instead of 5). If you haven't already, you should also implement the proper collision detection for the boundary in your event handler:

shacktar wrote:
What you should do is not let the paddle go down if its new position would bring it below 480 (assuming y == 0 is top) and not let the paddle go up if its new position would bring it above 0.

You said you key down event looks just like mine so I assume you haven't implemented it yet. It's trivial code, so I didn't show it.
Last edited on
Does the ball show up though?

The ball shows up

Comment out your event handler except for the quit event. Does the paddle then show up?

The paddle does not show up

If not, it's either a problem with User.yPos not being initialized or a problem with the userPaddle surface.

User.yPos is initialized as "User.yPos = 190.f;" in Game's constructor. The surface is fine too. How do i know? It was working before and i havent messed with the surface at all. Plus, i checked everything about the surface :p.

If you haven't already, you should also implement the proper collision detection for the boundary in your event handler:


I did that


And still nothing comes up :(
Last edited on
Okay i fixed it yVel was never intialized lol. Thanks everyone for helping!
Topic archived. No new replies allowed.