Python Pong Game – A Game That Will Test Your Skills!
Pong is a game that is played by two players who control the two paddles on either side of the game window. The paddles are x1x1 moved up and down to hit the moving ball. A player’s score rises when he or she hits the ball or when the opponent misses the hit.
About Python Pong Game
We will work with the Turtle module to create this Python game. To move the left and right paddles in this game, we will use the up, down, s, and w keys, respectively. When a player misses a hit, the ball restarts from the center toward the other player.
Prerequisites for Pong Game using Python
- Fundamental knowledge of the Python programming language and how to make functions in it.
- How the game window is made using Turtle
Download Python Pong Game Project
Please download the source code of Python Pong Game Project from the following link: Python Pong Game Project Code
Steps to Create Pong Game Project Using Python
Following are the steps for developing the Python Pong Game Project:
Step 1: Importing the necessary modules
Step 2: Making a window for our project
Step 3: Functions
Step 4: The main game loop
Step 1: Importing the necessary modules
To use Python Tkinter, we need to import the turtle module to make our Pong game.
Code
#import packages import turtle
Step 2: Making a window for our project
- The title of our window –‘TechVidvan Pong Game’, and sets its dimensions using the .setup() function.
- The ball is then made into a circle. It begins at the center of the screen and moves along the horizontal axis. The variables dx and dy determine the speed of the ball, whose position is constantly updated by these values.
- Now we’ll make two square paddles that move just when we press the keyboard. When the player pushes the left and right keyboard buttons for Player 2 and the w/s keyboard buttons for Player 1, it goes in the respective direction.
- Now we make the scoreboard, with the score changing every time each player strikes the ball with the paddle. This is at the top of the screen.
Code
# set up the screen root = turtle.Screen() root.title("TechVidvan Pong Game") root.bgcolor('#f7f1ea') root.setup(width=1000, height=400) # create the ball, giving its shape, color, and size ball = turtle.Turtle() ball.speed(30) ball.shape("circle") ball.color('#89bfbd') ball.penup() ball.goto(0, 0) ball.dx = 5 ball.dy = -5 # create the paddles, giving their shapes, colors, and sizes left_pad = turtle.Turtle() left_pad.speed(0) left_pad.shape("square") left_pad.color("#679ba7") left_pad.shapesize(stretch_wid=6, stretch_len=2) left_pad.penup() left_pad.goto(-450, 0) right_pad = turtle.Turtle() right_pad.speed(0) right_pad.shape("square") right_pad.color("#679ba7") right_pad.shapesize(stretch_wid=6, stretch_len=2) right_pad.penup() right_pad.goto(450, 0) # score score_l = 0 score_r = 0 # create the score board pen = turtle.Turtle() pen.speed(0) pen.color("black") pen.penup() pen.hideturtle() pen.goto(0, 170) pen.write("Player A: {} Player B: {}".format(score_l, score_r), align="center", font=("Arial", 16, "bold"))
Step 3: Functions
Then we write the functions left_up() and left_down(), which move the left paddle up and down by 22 units each time the user pushes the W or S key, and the functions right_up() and right_down(), which move the right paddle up and down by 22 units each time the user pushes the up or down key.
The listen() function ensures that keyboard inputs are taken into account.
Code
# define the paddle functions def left_up(): y = left_pad.ycor() y += 22 left_pad.sety(y) def left_down(): y = left_pad.ycor() y -= 22 left_pad.sety(y) def right_up(): y = right_pad.ycor() y += 22 right_pad.sety(y) def right_down(): y = right_pad.ycor() y -= 22 right_pad.sety(y) # keyboard bindings root.listen() root.onkeypress(left_up, "w") root.onkeypress(left_down, "s") root.onkeypress(right_up, "Up") root.onkeypress(right_down, "Down")
Step 4: The main game loop
The entire game is run in an unending loop. (while True:)
We begin by updating the screen and setting the ball’s coordinates.We do certain checks to ensure that the ball strikes the screen’s bottom or top edges, and we invert the sign of the dx or dy. (ball.dy *= -1)
Following that, we check to see if the ball strikes the left or right border of the screen, indicating that the paddles missed the hit. If this condition is met, we pause the game and display the score. (pen.write)
Code
# main game loop while True: root.update() # move the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # check for collisions with the walls if ball.ycor() > 190 or ball.ycor() < -190: ball.dy *= -1 # check for collisions with the paddles if 430 < ball.xcor() < 440 and right_pad.ycor() + 60 > ball.ycor() > right_pad.ycor() - 60: ball.dx *= -1 score_r += 1 pen.clear() pen.write("Player1: {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold")) if -430 > ball.xcor() > -440 and left_pad.ycor() + 60 > ball.ycor() > left_pad.ycor() - 60: ball.dx *= -1 score_l += 1 pen.clear() pen.write("Player1: {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold")) # Checking if the players missed the hit if ball.xcor() > 480 or ball.xcor() < -480: if ball.xcor() < -480: score_r += 1 # Increasing the score of the right player else: score_l += 1 # Increasing the score of the left player ball.goto(0, 0) ball.dx *= -1 ball.dy *= -1 # Updating the score pen.clear() pen.write("Player1 : {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold"))
Full Code
import turtle # set up the screen root = turtle.Screen() root.title("TechVidvan Pong Game") root.bgcolor('#f7f1ea') root.setup(width=1000, height=400) # create the ball, giving its shape, color, and size ball = turtle.Turtle() ball.speed(30) ball.shape("circle") ball.color('#89bfbd') ball.penup() ball.goto(0, 0) ball.dx = 5 ball.dy = -5 # create the paddles, giving their shapes, colors, and sizes left_pad = turtle.Turtle() left_pad.speed(0) left_pad.shape("square") left_pad.color("#679ba7") left_pad.shapesize(stretch_wid=6, stretch_len=2) left_pad.penup() left_pad.goto(-450, 0) right_pad = turtle.Turtle() right_pad.speed(0) right_pad.shape("square") right_pad.color("#679ba7") right_pad.shapesize(stretch_wid=6, stretch_len=2) right_pad.penup() right_pad.goto(450, 0) # score score_l = 0 score_r = 0 # create the score board pen = turtle.Turtle() pen.speed(0) pen.color("black") pen.penup() pen.hideturtle() pen.goto(0, 170) pen.write("Player A: {} Player B: {}".format(score_l, score_r), align="center", font=("Arial", 16, "bold")) # define the paddle functions def left_up(): y = left_pad.ycor() y += 22 left_pad.sety(y) def left_down(): y = left_pad.ycor() y -= 22 left_pad.sety(y) def right_up(): y = right_pad.ycor() y += 22 right_pad.sety(y) def right_down(): y = right_pad.ycor() y -= 22 right_pad.sety(y) # keyboard bindings root.listen() root.onkeypress(left_up, "w") root.onkeypress(left_down, "s") root.onkeypress(right_up, "Up") root.onkeypress(right_down, "Down") # main game loop while True: root.update() # move the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # check for collisions with the walls if ball.ycor() > 190 or ball.ycor() < -190: ball.dy *= -1 # check for collisions with the paddles if 435 < ball.xcor() < 440 and right_pad.ycor() + 60 > ball.ycor() > right_pad.ycor() - 60: ball.dx *= -1 score_r += 1 pen.clear() pen.write("Player1: {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold")) ball.setx(360) if -435 > ball.xcor() > -440 and left_pad.ycor() + 60 > ball.ycor() > left_pad.ycor() - 60: ball.dx *= -1 score_l += 1 pen.clear() pen.write("Player1: {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold")) ball.setx(-360) # Checking if the players missed the hit if ball.xcor() > 480 or ball.xcor() < -480: if ball.xcor() < -480: score_r += 1 # Increasing the score of the right player else: score_l += 1 # Increasing the score of the left player ball.goto(0, 0) ball.dx *= -1 ball.dy *= -1 # Updating the score pen.clear() pen.write("Player1 : {} Player2: {}".format(score_l, score_r), align="center", font=("Arial", 15, "bold"))
Python Pong Game Output
Summary
We were able to complete the Pong game with this Python project. We utilized the turtle module for this. We learned how to make balls and blocks, as well as how to move them if they hit the surface. I hope you learned a lot from us.