Home >Backend Development >Python Tutorial >Ping Pong game in Pygame python
import pygame import sys
Pygame is the module we are using to make games. It provided us with tools for graphics, sound, and more.
sys is a module in Python that helps us interact with the Python interpreter.
pygame.init()
Initializes all the Pygame modules and makes them ready to use.
#dimensions WIDTH, HEIGHT=800,600 #frame rate FPS=60 #the paddles at the side of ping pong PADDLE_WIDTH, PADDLE_HEIGHT=15,90 #the balls radius BALL_RADIUS=15 #the color of the ball and paddle WHITE=(255, 255, 255)
screen=pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("Ping Pong")
you will have a window named Ping Pong with the assigned WIDTH and HEIGHT
left_paddle=pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT) right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT) ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
In Pygame the left top corner of the screen represents (0,0) in coordinates.
pygame.Rect(x, y, width, height)
pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
First, we position the left paddle 50px towards the right from the left side.
Then we do HEIGHT//2 - PADDLE_HEIGHT //2 because if you just did HEIGHT//2 it will look like the way it is in the picture. It goes down the screen. To center it we do - PADDLE_HEIGHT //2
This is what we did for the right paddle to center it.
right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
For the ball to center it, we subtracted by the radius.
ball_speed_x=7 ball_speed_y=7 paddle_speed=10
ball_speed_x and ball_speed_y controls the horizontal and vertical speed of the ball.
paddle_speed: Controls the movement speed of the paddles.
import pygame import sys
pygame.init()
#dimensions WIDTH, HEIGHT=800,600 #frame rate FPS=60 #the paddles at the side of ping pong PADDLE_WIDTH, PADDLE_HEIGHT=15,90 #the balls radius BALL_RADIUS=15 #the color of the ball and paddle WHITE=(255, 255, 255)
screen=pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("Ping Pong")
Renders the scores for both players and positions them on the screen.
left_paddle=pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT) right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT) ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
Updates the display with the latest changes.
pygame.Rect(x, y, width, height)
Keeps the game running indefinitely.
pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
This will go through all the events that can happen in pygame and if one of them is closing the window then quit pygame and close the window.
right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
Detects key presses:
ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
Moves the ball by adding its speed to its current position
ball_speed_x=7 ball_speed_y=7 paddle_speed=10
Reverses the ball's vertical direction if it hits the top or bottom of the screen
import pygame import sys
Reverses the ball's horizontal direction if it collides with a paddle.
pygame.init()
#dimensions WIDTH, HEIGHT=800,600 #frame rate FPS=60 #the paddles at the side of ping pong PADDLE_WIDTH, PADDLE_HEIGHT=15,90 #the balls radius BALL_RADIUS=15 #the color of the ball and paddle WHITE=(255, 255, 255)
Limits the game to run at a maximum of 60 frames per second, ensuring smooth gameplay.
screen=pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("Ping Pong")
The above is the detailed content of Ping Pong game in Pygame python. For more information, please follow other related articles on the PHP Chinese website!