Javatpoint Logo
Javatpoint Logo

Create a Pong Game using Turtle in Python

One of the most well-known arcade games that simulates table tennis is called Pong. Dragging a paddle vertically across the left or right side of the screen gives each player control over that paddle. Players hit the ball back and forth using their paddles. If, one of them misses the ball, other one gets the point.

Python's built-in turtle module is used to create graphical representations of data. It can be used by users as a pen and a panel to draw on the visuals. This Turtle graphics package is a popular approach to get newcomers started with programming. It makes possible the possibilities of programming. It's an easy method to grasp Python's ideas while also being flexible.

In order to create the game, we must first create a rectangular GUI that stores the moving ball, the base object, and also shows the score. Such GUI libraries are abundant in Python. We will use Turtle for this example because it is one of the simplest and contain all the features that we require to create a pong game.

This is the procedure used in the Pong game :

  1. On the left and right sides of the screen, make two paddles, A and B.
  2. Construct a ball.
  3. Create an event that causes the paddle to travel vertically when a particular key is pressed.
  4. The code to update the score when each player misses a collision must be created in this step.

The game's simple rules are as follows :

  • There are two participants.
  • The paddle for each side is in their respective hands.
  • They have the ability to move the paddle up and down the vertical sides.

Using the paddle, players can strike the ball repeatedly.

Pong Game in Python :

The Python turtle module will be used to create this game. The left and right paddles in this game can be moved with the up, down, left, and right keys. As a player strikes the ball at a predetermined speed level, the ball's speed increases and so does the score.

If a player misses the hit, the ball will again start from the centre and go toward the other player, raising their score in the process.

In simpler terms, we will utilise the turtle module to build this Python game. We will utilise the up, down, W, and S keys in this game to move the left and right paddles. The ball's speed increases as a player strikes it to a certain level, and so does the score. The ball restarts from the centre towards the opposing player after a missed hit, with the opponent's score rising.

The logic used to move the ball and steer it in the appropriate direction after hitting the wall is the trickiest aspect of this pong game. Additionally, we need to figure out how to detect when the ball strikes the base object. All of them may be written using simple Python, which is detailed in the sections below.

Starting with the Code setup :

Firstly, we will start with checking If the Turtle module isn't already installed, use the command below to install it.

Steps to follow during the code setup :

1. Importing the modules comes first.

2. Next, we design the primary screen.

3. Next, the two paddles, ball, and scoreboard are created, and their characteristics are set.

4. After that, we design the functions that control the paddles and link the corresponding keyboard buttons to those functions.

5. Lastly, we create the game code, which is integrated into an endless loop. In this:

a. we move the ball,

b. check for wall collisions (to see if the player missed the ball),

c. check for ball-paddle collisions, and

d. Adjust the ball's direction in light of the previous two circumstances

1. Importing the modules comes first :

The turtle module must be imported first. The entire game will be constructed with this module.

2. Making the primary screen :

The primary screen for the "Python Pong game" is then created. We then determine its size.

3. Designing left and right-hand paddles

The left and right paddles must then be made. These characteristics of the paddles

a. They are square in shape and are vertically centred on either the left or right ends.

b. They move at zero speed. Only when the keyboard's keys are depressed do they move.

4. Making a ball and establishing its characteristics :

The time has come to make the ball. It is round in shape and starts off in the centre of the screen. Even though it moves according to the values of dx and dy, its speed is zero.

The position of the ball is updated by using dx and dy each time the main game loop executes, creating the illusion of mobility.

5. Establishing the scoreboard and starting the scores :

The scores are then reset to zero. Then make a scoreboard to display the scores at the top of the screen. The text displayed on the window in this case is displayed using the write() function.

6. Writing paddle-moving functions and matching them with keys :

At this point, we write paddle-moving functions that move the paddles vertically. We are adding 15 units to the paddle's y-coordinate to provide movement. They function as follows:

1. The left paddle travels up when the left arrow button on the keyboard is pressed, thanks to the movePad1Up() method.

2. The left paddle travels down when the right arrow key on the keyboard is pressed, thanks to the movePad1Down() method.

3. The right paddle travels up when the keyboard's up arrow button is pressed, thanks to the movePad2Up() method.

4. The right paddle travels down when the keyboard's down arrow button is pressed, thanks to the movePad2Down() method.

7. Writing the main code :

Finally, we are at the point where we will create the main code of the game. The code is created in an endless while loop that continues until the window is closed.

a. We begin by updating the screen.

b. After that, we move the ball by updating the dx and dy coordinates.

c. By adjusting the sign of dy, we can bounce the ball and see if it strikes the top and bottom barriers.

d. We are aware that the ball will strike the left or right barriers if any players miss the hit. To determine whether the ball has impacted the left or right walls and update the scoreboard and adjust the opponent's score.

Start the ball once more in the other direction e from the screen's centre.

e. Finally, we determine which paddles actually made contact with the ball.

Upgrading the score and scoreboard. Speed up while being careful not to go over the limit 7.

Change the ball's dx sign to direct it toward the other player.

Complete Code :

Output :

The output of the code is represented below in the form of a screenshot. The game is a dynamic version of this screenshot which can be run by executing the code we have explained above.

Create a Pong Game using Turtle in Python

Conclusion :

With the help of Python, we were able to create the Pong video game. We employed the turtle module for this. We learned how to construct shapes for blocks, balls, modify the speed and refresh the screen.

The project is now finished! We hope you've enjoyed playing this straightforward Pong game made using Python and that it has helped you learn new skills or have a greater knowledge of the ideas we covered. In this assignment, we gained knowledge of various Python modules, including turtle. To carry out more task, you can add as many features as you can. So, test your code right now to see how it performs. We hope you had fun creating this game!


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA