Ping Pong Game Using Turtle in Python

The ping pong game is one of the most famous games of arcadia, which is quite similar to table tennis. The rules of this game are simple:

  • There are two players.
  • Both have control over the paddle of their side.
  • They can move the paddle on vertical sides from up to down and down to up.

Players can strike back and forth on the ball by using the paddle.

Turtle: The turtle is a built-in module in Python which is used for graphical illustrations. The users can use it as a pen and panel to illustrate the graphics.

The following are the steps used for illustrating Ping Pong game:

  • Step 1: The user has to create two Paddles for the "Left Side" and "Right Side" of the screen.
  • Step 2: The user then has to create the ball.
  • Step 3: Then, create an event for moving the paddles vertically by pressing the particular keys, that is, for "Left Paddle": "R" for "Up" and "C" for "Down", for "Right Paddle": "Up key ()" for "Up" and "Down key ()" for "Down".
  • Step 4: At last, create the function for updating the score after each player misses the strike on the ball.

Code 1: For Creating Illustrating the Ball and Paddle:

Output:

Ping Pong Game Using Turtle in Python

Explanation:

First, we imported the turtle library. Then we wrote the code for the background screen by using turtle.Screen() function. We created the Left and Right paddle by using the turtle.Turtle() function and positioned them by using goto() function. at last, we created the ball by using a turtle.Turtle() function and mentioned its moving speed on screen by using speed() function and other required parameters. The illustration of the ball and paddle on the screen is ready.

Code 2: The Complete Code for Illustrating the Ping Pong Game with Two Players.

Output:

Ping Pong Game Using Turtle in Python

Explanation:

In the above code, after illustrating the outlook of the screen with paddles and ball, we will first initialize the score of both the players, that is, Zero. Then, we will display the score on the screen, which will increase with the opposite player missing the strike. We will then implement the functions for moving the paddle vertically. We will bind the keys for each function, that is, for "Left Paddle": "R" for "Up" and "C" for "Down", for "Right Paddle": "Up key" for "Up" and "Down key" for "Down". Then, we will initiate how the ball and striking of the paddle will work, as if the ball hit the paddle, it will go for the opposite player, but if the paddle missed the ball, then the ball will hit the border, and the opposite player will get the score. If the code is correct and no error is found, the screen of Ping Pong Game will pop up after running the code, and the players can play. The ball will first go for the right player.

Conclusion

In this tutorial, we have created a simple illustration of a ping pong game on the screen by using the built-in library of Python, that is, Turtle. The user can create different moving illustrations by using the turtle library and its different functions.






Latest Courses