Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

code pong in pygame

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-2025-04-14’s answer.
  1. User

    code pong in pygame

  2. Assistant

    Here’s a simple implementation of classic “Pong” using Pygame. Just make sure you’ve installed pygame first:

    pip install pygame
    

    Then save the following as, say, pong.py and run with python pong.py:

    import pygame
    import sys
    
    # --- Constants ---
    WIDTH, HEIGHT = 800, 600
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    FPS = 60
    PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
    BALL_SIZE = 15
    PADDLE_SPEED = 7
    BALL_SPEED_X, BALL_SPEED_Y = 5, 5
    
    # --- Pygame Setup ---
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Pong")
    clock = pygame.time.Clock()
    font = pygame.font.SysFont(None, 48)
    
    # --- Game Objects ---
    left_paddle = pygame.Rect(10, (HEIGHT - PADDLE_HEIGHT)//2, PADDLE_WIDTH, PADDLE_HEIGHT)
    right_paddle = pygame.Rect(WIDTH - 20, (HEIGHT - PADDLE_HEIGHT)//2, PADDLE_WIDTH, PADDLE_HEIGHT)
    ball = pygame.Rect((WIDTH - BALL_SIZE)//2, (HEIGHT - BALL_SIZE)//2, BALL_SIZE, BALL_SIZE)
    
    ball_vel = [BALL_SPEED_X, BALL_SPEED_Y]
    score_left, score_right = 0, 0
    
    def reset_ball():
        ball.center = (WIDTH//2, HEIGHT//2)
        ball_vel[0] *= -1  # Send back toward last scorer
    
    # --- Main Loop ---
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        # --- Input Handling ---
        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_vel[0]
        ball.y += ball_vel[1]
    
        # --- Collisions with top/bottom ---
        if ball.top <= 0 or ball.bottom >= HEIGHT:
            ball_vel[1] *= -1
    
        # --- Collisions with paddles ---
        if ball.colliderect(left_paddle) and ball_vel[0] < 0:
            ball_vel[0] *= -1
        if ball.colliderect(right_paddle) and ball_vel[0] > 0:
            ball_vel[0] *= -1
    
        # --- Score Update ---
        if ball.left <= 0:
            score_right += 1
            reset_ball()
        if ball.right >= WIDTH:
            score_left += 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))
    
        # Draw scores
        score_surf = font.render(f"{score_left}    {score_right}", True, WHITE)
        screen.blit(score_surf, ((WIDTH - score_surf.get_width())//2, 20))
    
        pygame.display.flip()
        clock.tick(FPS)
    
    pygame.quit()
    sys.exit()
    

    How it works:

    • Two paddles on left/right, moved by W/S and ↑/↓.
    • Ball bounces off walls and paddles.
    • Scores when ball leaves left or right edge.
    • Center line and scores are drawn each frame.

    Feel free to tweak BALL_SPEED_X/Y, paddle size, window size, or add sound effects! 🏓

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.