How can i make the computer to be a player in a game ? RRS feed

  • General discussion

  • In my ping pong game i want to make two players but also one player against the pc.

    Now i dont want the computer to move random but with some logic.

    So im using a button and now i can move the button up down with the mouse.

    But i want somehow to make that the computer will play this side and will hit the ball automatic. Not 100% but not random too.




    Monday, June 21, 2010 8:07 PM

All replies

  • You have to program the "intelligence" of the computer player into the main game loop.  Somewhere in your loop code along with the rules of "physics" that move the ball around will be the rules of "artificial intelligence" that control how the computer's paddle will move around.  The computer's movements must be simulated, just like the ball's movements are simulated.

    A good first step is to make the computer move up if the ball is above the paddle, and down if the ball is below the paddle.

    If the computer can move arbitrarily fast, then this will unfortunately be unbeatable even with the simplest implementation.  So you might want to add some thing to limit the ability of the computer player:


    • Limit the maximum speed of the computer's paddle's movement.  Then a very steep shot is required to defeat the computer.  (Boring, it's been done before).
    • Program in a random "reaction time" from the time that the computer makes a decision until it can start moving.  You can also have the computer get tired and have its reaction time get slower as it fatigues.
    • You can also have the computer predict where the ball is going to go and move directly there.  You can program in statistical error in either the position of where it thinks the ball is going, or the position that it decides to move the ball.

    Good luck!  And be creative!


    Monday, June 21, 2010 8:23 PM
  • Thanks.
    Tuesday, June 22, 2010 8:56 AM