Photo by Steven Skerritt on Unsplash
- We’ll deploy a ball with an initial velocity and a direction at the center.
- We move the paddles up and down using the keys on the keyboard. W & S for the left player to move up and down respectively. UP & DOWN arrow keys for the right player to move up and down respectively.
- The direction of the ball is changed and speed is increased a little when the ball touches a paddle.
- The up and down boundaries of the canvas will reflect the ball when the ball hits.
- Once a player misses the ball, the other player’s score is incremented by one. It is checked whether the player has reached the score that is needed to win and if it is reached, then the player wins and the game ends.
- The game is continued until a player wins.
Explanation of Source code
Let’s go through the code to understand the logic better and see how we can build ping pong.
Want to connect?
Follow me on Twitter.