Game design : Smooth movement of an object

Hey guys,
I was coding brick breaker game and i have written code so as to move the paddle when the user presses LEFT/RIGHT arrow key.but it doesnt move smoothly.. please help me to code it so as to move smoothly and one more thing - whenever i move the paddle, the ball in the game gets redrawn. I guess bricks also gets redrawn. I dont want that to happen is there any way i could bypass it??

/*Re-post
one more thing, if you observe the output, some bricks tend a bit offside(though x cordinates are same for every row) example, just see the red-row . Do you know why this happens?? are there any corrections ??
Here is the code
*/

the loops in the case statements are useless, you will only see the result of the final px += 25 * move_unit * dx * cx; because there are never any images drawn with any of the intermediate values. Similarly, you don't want to call glutSwapBuffers() in the functions that draw pieces of your scene (e.g. paddle() or ball()). Instead, draw a full frame of your scene, then call glutSwapBuffers().

To move objects smoothly you could keep track of which keys are currently pressed and each frame move the paddle by a small amount based on which keys are down. You get even smoother motion if you apply some simple form of acceleration to the paddle so that in the first few frames a key is pressed the movement distance is smaller and slowly increases up to a max. You probably also want to make the movement speed independent of your framerate (search the forums, there are threads with more details on this). For that keep track of how much time has elapsed since the last frame and use that to scale the movement distance.

Many applications redraw the whole scene for each frame, that is quite a common approach as there is often not much/anything that is static between frames (especially if the camera moved). Your application probably has some static elements, so you could draw those into a texture using a FBO and then just draw the texture to restore the static elements. I would only recommend that if profiling shows it's worth the effort - your program has plenty of optimization potential before that, e.g. get rid of immediate mode rendering (glBegin/glEnd) and use buffer objects (keyword: VBO).

the loops in the case statements are useless, you will only see the result of the final px += 25 * move_unit * dx * cx; because there are never any images drawn with any of the intermediate values. Similarly, you don't want to call glutSwapBuffers() in the functions that draw pieces of your scene (e.g. paddle() or ball()). Instead, draw a full frame of your scene, then call glutSwapBuffers().

To move objects smoothly you could keep track of which keys are currently pressed and each frame move the paddle by a small amount based on which keys are down. You get even smoother motion if you apply some simple form of acceleration to the paddle so that in the first few frames a key is pressed the movement distance is smaller and slowly increases up to a max. You probably also want to make the movement speed independent of your framerate (search the forums, there are threads with more details on this). For that keep track of how much time has elapsed since the last frame and use that to scale the movement distance.

Many applications redraw the whole scene for each frame, that is quite a common approach as there is often not much/anything that is static between frames (especially if the camera moved). Your application probably has some static elements, so you could draw those into a texture using a FBO and then just draw the texture to restore the static elements. I would only recommend that if profiling shows it's worth the effort - your program has plenty of optimization potential before that, e.g. get rid of immediate mode rendering (glBegin/glEnd) and use buffer objects (keyword: VBO).

Hey, Thanks for your reply..
i was trying to make it smooth so i introduced for() which was of no use..
anyhow ... thanks for clearing me with glutSwapBuffers().(So, if i call glutSwapBuffers once at end of display() its ok right??. )currently, my ball getting redrawn has stopped since i removed glutSwapBuffers() everywhere else except display(). i will try to make it smooth as you said by modifying its code tomorrow Thanks bro