Make a python code of Pong game
- User
Make a python code of Pong game
Assistant
I'll help you create a simple Pong game using Python and Pygame. Here's a basic implementation:
import pygame import random # Initialize Pygame pygame.init() # Constants WIDTH = 800 HEIGHT = 600 PADDLE_WIDTH = 15 PADDLE_HEIGHT = 90 BALL_SIZE = 15 PADDLE_SPEED = 5 BALL_SPEED = 7 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Create the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pong") # Create game objects player = pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT) opponent = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT) ball = pygame.Rect(WIDTH//2 - BALL_SIZE//2, HEIGHT//2 - BALL_SIZE//2, BALL_SIZE, BALL_SIZE) # Ball speed ball_speed_x = BALL_SPEED * random.choice((1, -1)) ball_speed_y = BALL_SPEED * random.choice((1, -1)) # Score player_score = 0 opponent_score = 0 font = pygame.font.Font(None, 74) def move_paddle(paddle, up=True): if up and paddle.top > 0: paddle.y -= PADDLE_SPEED if not up and paddle.bottom < HEIGHT: paddle.y += PADDLE_SPEED def reset_ball(): ball.center = (WIDTH//2, HEIGHT//2) global ball_speed_x, ball_speed_y ball_speed_x = BALL_SPEED * random.choice((1, -1)) ball_speed_y = BALL_SPEED * random.choice((1, -1)) # Game loop running = True clock = pygame.time.Clock() while running: # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Paddle movement keys = pygame.key.get_pressed() if keys[pygame.K_w]: move_paddle(player, up=True) if keys[pygame.K_s]: move_paddle(player, up=False) # Simple AI for opponent if opponent.centery < ball.centery and opponent.bottom < HEIGHT: move_paddle(opponent, up=False) if opponent.centery > ball.centery and opponent.top > 0: move_paddle(opponent, up=True) # Ball movement ball.x += ball_speed_x ball.y += ball_speed_y # Ball collision with top and bottom if ball.top <= 0 or ball.bottom >= HEIGHT: ball_speed_y *= -1 # Ball collision with paddles if ball.colliderect(player) or ball.colliderect(opponent): ball_speed_x *= -1 # Score points if ball.left <= 0: opponent_score += 1 reset_ball() if ball.right >= WIDTH: player_score += 1 reset_ball() # Drawing screen.fill(BLACK) pygame.draw.rect(screen, WHITE, player) pygame.draw.rect(screen, WHITE, opponent) pygame.draw.ellipse(screen, WHITE, ball) pygame.draw.aaline(screen, WHITE, (WIDTH//2, 0), (WIDTH//2, HEIGHT)) # Score display player_text = font.render(str(player_score), False, WHITE) opponent_text = font.render(str(opponent_score), False, WHITE) screen.blit(player_text, (WIDTH//4, 20)) screen.blit(opponent_text, (3*WIDTH//4, 20)) # Update display pygame.display.flip() clock.tick(60) pygame.quit()This code creates a classic Pong game with the following features:
- Two paddles (player on the left, AI-controlled opponent on the right)
- Score tracking
- Ball physics with bouncing
- Simple AI opponent
- Basic collision detection
To play:
- Use W key to move your paddle (left) up
- Use S key to move your paddle down
- Try to hit the ball past the opponent's paddle
- First to score wins!
The game includes:
- A center line dividing the court
- Score display at the top
- Smooth paddle and ball movement
- Basic AI for the opponent paddle
You'll need to have Pygame installed to run this. You can install it using:
pip install pygameWould you like me to explain any specific part of the code or add additional features?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.