Sure! Here's an example Python code for a simple game of Pong using the Pygame library:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Pong Game")
# Set up the ball
BALL_WIDTH = 20
BALL_HEIGHT = 20
ball = pygame.Rect(WINDOW_WIDTH // 2 - BALL_WIDTH // 2, WINDOW_HEIGHT // 2 - BALL_HEIGHT // 2, BALL_WIDTH, BALL_HEIGHT)
ball_speed_x = 5 * random.choice((1, -1))
ball_speed_y = 5 * random.choice((1, -1))
# Set up the paddles
PADDLE_WIDTH = 15
PADDLE_HEIGHT = 100
paddle_speed = 5
left_paddle = pygame.Rect(50, WINDOW_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
right_paddle = pygame.Rect(WINDOW_WIDTH - 50 - PADDLE_WIDTH, WINDOW_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
# Set up the score
FONT_SIZE = 60
score_font = pygame.font.Font(None, FONT_SIZE)
left_score = 0
right_score = 0
# Set up the game loop
game_running = True
clock = pygame.time.Clock()
while game_running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
# Move the paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
left_paddle.move_ip(0, -paddle_speed)
if keys[pygame.K_s]:
left_paddle.move_ip(0, paddle_speed)
if keys[pygame.K_UP]:
right_paddle.move_ip(0, -paddle_speed)
if keys[pygame.K_DOWN]:
right_paddle.move_ip(0, paddle_speed)
# Move the ball
ball.move_ip(ball_speed_x, ball_speed_y)
# Handle collisions with the top and bottom walls
if ball.top <= 0 or ball.bottom >= WINDOW_HEIGHT:
ball_speed_y *= -1
# Handle collisions with the paddles
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
ball_speed_x *= -1
# Handle scoring
if ball.left <= 0:
right_score += 1
ball_speed_x = 5
ball_speed_y = 5 * random.choice((1, -1))
ball.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
elif ball.right >= WINDOW_WIDTH:
left_score += 1
ball_speed_x = -5
ball_speed_y = 5 * random.choice((1, -1))
ball.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
# Draw everything
window.fill((0, 0, 0))
pygame.draw.rect(window, (255, 255, 255), left_paddle)
pygame.draw.rect(window, (255, 255, 255), right_paddle)
pygame.draw.ellipse(window, (255, 255, 255), ball)
left_score_text = score_font.render(str(left_score), True, (255, 255, 255))
right_score_text = score_font.render(str(right_score), True, (255, 255, 255))
window.blit(left_score_text, (WINDOW_WIDTH // 4 - FONT_SIZE // 2, FONT_SIZE))
window.blit(right_score_text, (3 * WINDOW_WIDTH // 4 - FONT_SIZE // 2, FONT_SIZE))
pygame.display.flip()
# Limit the frame rate
clock.tick(60)
# Clean up
pygame.quit()
This code should create a window for the game, display the paddles and ball, handle collisions and scoring, and move everything around based on user input and the game logic. You can customize various aspects of the game, such as the size of the window, the speed of the ball and paddles, the font used for the score, and so on. I hope this helps you get started with creating your own game of Pong in Python!