First, we need to install Pygame if it's not already installed. You can do this by running:
pip install pygame
At the beginning of our script, we import necessary modules from Pygame:
import pygame
import sys
We initialize Pygame and set up the display window:
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pong")
Create classes for the ball and paddles. Each object has its own properties like position, speed, and dimensions.
class Ball:
def __init__(self):
self.x = 400
self.y = 300
self.radius = 10
self.speed_x = 5
self.speed_y = 5
def move(self):
self.x += self.speed_x
self.y += self.speed_y
# Bounce off walls
if self.x + self.radius > 800 or self.x - self.radius < 0:
self.speed_x *= -1
if self.y + self.radius > 600 or self.y - self.radius < 0:
self.speed_y *= -1
class Paddle:
def __init__(self, x, y):
self.width = 10
self.height = 100
self.speed = 5
self.x = x
self.y = y
def move(self, up=True):
if up:
self.y -= self.speed
else:
self.y += self.speed
# Keep paddle within bounds
self.y = max(0, min(600 - self.height, self.y))
The main game loop handles events, updates game state, and draws objects on the screen.
def main():
clock = pygame.time.Clock()
ball = Ball()
paddle_a = Paddle(0, 300)
paddle_b = Paddle(790, 300)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
paddle_a.move(up=True)
if keys[pygame.K_s]:
paddle_a.move(up=False)
if keys[pygame.K_UP]:
paddle_b.move(up=True)
if keys[pygame.K_DOWN]:
paddle_b.move(up=False)
ball.move()
# Check collision with paddles
if ball.x - ball.radius <= paddle_a.x + paddle_a.width and \
paddle_a.y <= ball.y <= paddle_a.y + paddle_a.height:
ball.speed_x *= -1
elif ball.x + ball.radius >= paddle_b.x and \
paddle_b.y <= ball.y <= paddle_b.y + paddle_b.height:
ball.speed_x *= -1
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 255, 255), (ball.x, ball.y), ball.radius)
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(paddle_a.x, paddle_a.y, paddle_a.width, paddle_a.height))
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(paddle_b.x, paddle_b.y, paddle_b.width, paddle_b.height))
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()
- Initialization: We start by initializing Pygame and creating the screen.
- Classes:
Ball
andPaddle
classes encapsulate the behavior and state of the game objects. - Game Loop: The main loop updates the positions of the objects based on user input and game rules, then redraws the screen.
- Event Handling: Keyboard inputs are used to move the paddles.
- Drawing: Pygame functions are used to draw the game objects on the screen.