gpt-4.1-2025-04-14· OpenAI LMArena Human Preference 140K
code pong in pygame
- User
code pong in pygame
- Assistant
Absolutely! Below is a simple implementation of Pong using Python and Pygame. This code creates a window, two paddles, a ball, and basic collision and scoring logic.
Make sure you have Pygame installed:
pip install pygame
import pygame import sys # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 WHITE = (255, 255, 255) BLACK = (0, 0, 0) PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100 BALL_SIZE = 20 PADDLE_SPEED = 7 BALL_SPEED_X, BALL_SPEED_Y = 5, 5 # Set up display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pong") # Paddles and Ball left_paddle = pygame.Rect(30, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT) right_paddle = pygame.Rect(WIDTH-40, 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_x = BALL_SPEED_X ball_speed_y = BALL_SPEED_Y # Scores left_score = 0 right_score = 0 font = pygame.font.Font(None, 74) clock = pygame.time.Clock() def reset_ball(): global ball_speed_x, ball_speed_y ball.center = (WIDTH//2, HEIGHT//2) ball_speed_x *= -1 # Game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Key presses keys = pygame.key.get_pressed() if keys[pygame.K_w] and left_paddle.top > 0: left_paddle.y -= PADDLE_SPEED if keys[pygame.K_s] and left_paddle.bottom < HEIGHT: left_paddle.y += PADDLE_SPEED if keys[pygame.K_UP] and right_paddle.top > 0: right_paddle.y -= PADDLE_SPEED if keys[pygame.K_DOWN] and right_paddle.bottom < HEIGHT: right_paddle.y += PADDLE_SPEED # Move ball ball.x += ball_speed_x ball.y += ball_speed_y # Collisions with top/bottom if ball.top <= 0 or ball.bottom >= HEIGHT: ball_speed_y *= -1 # Collisions with paddles if ball.colliderect(left_paddle) or ball.colliderect(right_paddle): ball_speed_x *= -1 # Score if ball.left <= 0: right_score += 1 reset_ball() if ball.right >= WIDTH: left_score += 1 reset_ball() # Drawing screen.fill(BLACK) pygame.draw.rect(screen, WHITE, left_paddle) pygame.draw.rect(screen, WHITE, right_paddle) pygame.draw.ellipse(screen, WHITE, ball) pygame.draw.aaline(screen, WHITE, (WIDTH//2, 0), (WIDTH//2, HEIGHT)) left_text = font.render(str(left_score), True, WHITE) right_text = font.render(str(right_score), True, WHITE) screen.blit(left_text, (WIDTH//4, 20)) screen.blit(right_text, (WIDTH*3//4, 20)) pygame.display.flip() clock.tick(60)
How to play:
- Left paddle:
W(up),S(down) - Right paddle:
Up Arrow(up),Down Arrow(down)
Let me know if you want to add features like sound, AI, or menus!
- Left paddle:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.