   Ping Pong C++/Allegro code optimization
  I have created a Ping Pong game. I am a bit of a newbie at C++, Allegro and programming. After i corrected a few bugs, my code has got very messy. Also i will want to rewrite the code from scratch, to a new - better code. Advanced coding instead of newbie's. So i would like you to help me optimize the code a little bit, or give me advices on what i am doing wrong and what i am doing good.Things i want to improve, when i will rewrite the code (not optimization here, just questions if that is possible, and how is that possible):The collision between paddles and the ball feels unrealistic. It doesn't feel like a natural collision. Also, sometimes the ball returns to where it was a few times. At rand() reference, i haven't found stuff like don't allow it to give the same random value, as it just been.Thanks, and here is the code: http://pastebin.com/ixcWNnM7.   
  I wouldn't believe, that my code is perfect, lol. Hmmm, maybe there's some good code of pong example somewhere,, since no one wants to investigate my code? Would be pretty good too.   ;  To start with there are some variables that are never used, tmp_x and tmp_y to name two. then I'm not actually sure what these loops are doing in ball_movement()for(velocity_x = 0;velocity_x > -4; velocity_x --){   for(velocity_y = 0;velocity_y > -4; velocity_y --)   { velocity_x --; velocity_y --;   }}To me it just looks like they always get the same values? In the code above wouldn't it be easier to just set velocity_x and _y to -4? Since you already know the numbers from the start (the numbers in the loop test expression). The other loops are a bit different but they will always get the same values each time as well. That's because you reset velocity_x and _y to 0 each time you initialize the for loop and then loops a fixed number of times. I don't really get the meaning with if(velocity_pong1 < 12)   velocity_pong1 plus plus;  (why am I not able to have a plus sign in my text? :S)since you never do anything else than add 1 to the velocity_pong1 (and velocity_pong2) as long as they are less than 12. That would mean that the first 12 updates where the keys (up and down) are registered the velocity will increase and after that it's stuck on 12? And the second time you play the game, without restarting the application, they are still the number from the previous games since you don't reset them in startNew().And the "winning screen" or what I should call it are almost identical if player 1 or player 2 wins. All that differs are the text string printed.if(collision_pong1_lose == TRUE){   acquire_screen();   textout_ex(buffer, font, "Player 2 has won, congratulations!", 185, 240, makecol(255, 0, 0), makecol(0, 0, 0));   textout_ex(buffer, font, "Press SPACE to start a new game.", 194, 260, makecol(255, 0, 0), makecol(0, 0, 0));   draw_sprite(screen, buffer, 0, 0);   release_screen();   startNew();}Therefore would I extract that to a method taking the winning string as an parameter. Since your buffer is already a field you don't need to worry about that.What's the purpose of vel_x and vel_y? For me it just feels like the complicate things like doing so that the ball moves faster in positive x than negative x ? (since x = x plus (vel_x plus velocity_x) and x = x - (vel_x - velocity_x) )This is a brief look at your code and comment on the gameplay, collision detection and game loop I will let someone more experienced give his or her opinion on.   ;  You're asking for us to help you with optimization, which involves things like improving execution speed and lowering memory consumption. However, it seems that what you're really after is help with refactoring, which means reworking the code's structure to make it cleaner, more modular and easier to understand. So the collisions feel unrealistic. Unrealistic how? You can't reasonably expect us to download Allegro, set up a project, paste your code and try the game. Please elaborate so that we can understand the nature of the problem.   ;  Okay, sorry for that. You can download the game over here: http://frozenas.co.cc/?page_id=10&did=6.I also removed the unused variables. Thanks for that.As for velocity, i don't know how, but somehow it helps the paddles don't go under the screen. Both ball's and paddle's velocities. Otherwise the paddles can go a little after the screen, since they increase the y more than1 pixel, they let's say increase their y by 5 being at 4 pixels from the end of the screen. They either stop or increase the pixels after the screen. I found no better way to solve that yet.As for other things, that needs to be tried - feel free to download the game and try those things yourselves, sorry about the first post, haven't thought about posting a link.   