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 )
{
paddle.yvel -= 2;
}
}
if ( event.type == SDL_KEYUP )
{
if ( event.key.keysym.sym == SDLK_UP )
{
paddle.yvel = 0;
}
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
void movePaddle(Tpaddle *pad)
{
pad->y += pad->yvel;
}
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)
{
movePaddle(&Paddle);
}
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]