#### Archived

This topic is now archived and is closed to further replies.

# Pong - SDL- Input problem

This topic is 5177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I am nearing completion of my first pong clone, built using C++ and SDL. I am using SDL_EVENTs in main() to check for input, as such:
    SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_UP )

But, of course, if I put "paddle.y -= 5" which should move the paddle up 5 pixels, it will only happen once, so you have to keep pressing the up button to keep moving up. So I came across the solution of using "flags":

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_UP )
{
}
}
if ( event.type == SDL_KEYUP )
{
if ( event.key.keysym.sym == SDLK_UP )
{
}

So, in theory, when you press down the "UP" key, it will set the paddles y-velocity as -2. When you release the key it goes back to 0 velocity. Now, every game loop, "movePaddle" is run

{
}


This should simply change the y coordinate of the paddle (I store the x and y of the top left pixel of the paddle, then width and height) by adding on its current velocity. If the UP key was pressed down but not released, the yvel would be -2, therefor the paddle would move up 2 pixels. Now this works and all, I can move the paddle up and down, although you have to properly release the key before changing direction, because sometimes if you change from up to down too quickly it wont recognise it. So if I can find a way around that then that would be useful. The main problem though is this: I can move up and down, but once I'm near the top, where the paddle is in the furthest top position it can go, it gets stuck. If I release the key and press down, it won't move. The same applies for if I move it furthest down, it get's stuck and I can't move it back up. Now, I have tried getting round it. In the main game loop I tried:
	if(Paddle.y > 20 && Paddle.y < HEIGHT - 70)
{
}

So in theory, it should only move the paddle if its within the boundaries, 20 pixels from the top and 70 from the bottom. This works, as if I hold up, it will stop at 20 pixels from the top. But.. it STILL gets stuck, so it's basically whenever the paddle has reached its furthest point up and down, it get's stuck. I really am stuck and don't know what I can do, I have tried several things but am still a newbie. I can post more code if needed but any help is greatly appreciated EDIT: oh and also just an extra query. yvel and xvel are currently stored as integers. I initialise both velocities as 2 and that is about the right speed, 1 is really slow, 3 is fast and makes it look a bit crap. Could I use floats? I get errors when I use float, because I have -= and += and aparently that doesn't seem to work with floats [edited by - sanguineraven on October 16, 2003 8:00:43 AM] [edited by - sanguineraven on October 16, 2003 8:01:44 AM]

##### Share on other sites
Wow, thats a hefty post. Mind you, it's good to see people actually trying to make their problem clear unlike some muppets. As a result I shall do my best to help.

When a player switches direction really quickly your functions overlap each other. So switching from 'up' to 'down' will set your Y velocity in the opposite direction. However, upon releasing the up keys moments later, that command sets the velocity to zero. I would suggest that you add a check when the key is released. If they release the up key and the velocity is actually heading downward, do not set it to zero as the player has switched direction.

With regards to getting stuck in the boundaries, make sure you place the paddle just outside the boundary once a collision is detected. I would suggest you allow the paddle to move freely but call a checkCollision() function afterwards. That determines if the paddle has crossed a boundary and then moves it to just outside that boundary. Your paddle is probably getting stuck because you move beyond the boundary and then your code no longer allows you to move it at all, even if it is in the other direction.

Finally, you can use floats for all you location and velocity data because they will afford a slightly more accurate/sensitive amount of detail (adn you can still use += and -=). When you come to actually drawing the things, just cast(convert) the value to an int.

e.g
int myInteger = (int)myFloat;

I hope that is of some help and if it isn't I have a tutorial/example that may be of some use to you.

Pong.zip
Making Pong.doc

Best of luck.

[edited by - m_wherrett on October 16, 2003 8:27:47 AM]

##### Share on other sites
Yea thanks for the help, I have most of it working now. I have the paddle collision working, your post made me realise the really simple answer.
I implemented floats as the x/y coords and velocities of the ball but it doesn''t seem to do much, 1.9 will be same speed as 1, 2.9 will be same speed as 2 etc. I will try and fiddle about with it and will post the problem if I can''t solve it.

Still working on the movement aswell.

Also, off topic but I noticed the download was from lincoln university, then I noticed you were learning advisor there. I applied to the games course and got accepted to start the course last month but didn''t go... it''s a small world.