SDL object moves too fast

Jan 2, 2013 at 11:26pm
closed account (96AX92yv)
hello,
I am currently making pong using visual C++ with the SDL libary. I find that the ball moves too fast for the players and needs to slow down. The code for the movement is in a while loop and vairies speeds 1 - 2 (X and Y).

CODE:

//ball movement{
ballX += speedX;
ballY += speedY;
//}

ballX and ballY is the position of the bsll.
speedX and speedY are the speeds. these change from 1 - 2

i am wondering if anyone could say how to make the ball move less than one pixel or something along those lines.

ask if u need more code to help

thanks in advance :)
Last edited on Jan 2, 2013 at 11:29pm
Jan 3, 2013 at 12:30am
There are a coupe of ways to do this. One is to change the speed from an int value to a float value and use a smaller increment value. the other is to slow the speed of the game down, since the ball moves at the speed of the loop.

its usually a good idea to never change the speed of the game.
Jan 3, 2013 at 5:52am
Using floats for speed and position is the way to go. Then use a rounding function whenever you need to get the position as an int, e.g. when blitting
Jan 3, 2013 at 12:30pm
closed account (96AX92yv)
thanks
Last edited on Jan 3, 2013 at 1:22pm
Jan 3, 2013 at 2:43pm
closed account (96AX92yv)
does any1 no how to set up the SDL_ttf libary. im trying to add scores and text onto the game but i cant seem to set up the libary.

Jan 3, 2013 at 3:23pm
Have you tried follow the instructions in the lazyfoo tutorials?
http://www.lazyfoo.net/SDL_tutorials/lesson07/index.php
Jan 3, 2013 at 3:37pm
closed account (96AX92yv)
thanks
Jan 3, 2013 at 5:04pm
closed account (96AX92yv)
here yet anouther problem, turning everything into float means that i cannot use the random function. i tried changing all the integers to floats on the random function but it didnt work is there a way to make a random float?

CODE:

float random (float max, float min)
{
if (max + min == 0)
{
return 0;
}
float randomNumber = rand() % max + min;
return randomNumber;
}

on the value max it says it must be an integer or enum type
Last edited on Jan 3, 2013 at 7:42pm
Jan 3, 2013 at 11:28pm
closed account (96AX92yv)
done it

float randomNumber = min + (float)rand()/((float)RAND_MAX/(max-min));
Topic archived. No new replies allowed.