Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

give me the snake code

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219’s answer.
  1. User

    give me the snake code

  2. Qwen Assistant

    Here's a simple implementation of the classic Snake game in Python using the pygame library. This code includes basic functionality like movement, food generation, collision detection, and scoring.


    🐍 Snake Game Code (Python + Pygame)

    import pygame
    import random
    import sys
    
    # Initialize Pygame
    pygame.init()
    
    # Screen dimensions
    WIDTH, HEIGHT = 600, 400
    CELL_SIZE = 20
    FPS = 10
    
    # Colors
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)
    BLACK = (0, 0, 0)
    
    # Set up the display
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Snake Game")
    clock = pygame.time.Clock()
    
    # Font for score
    font = pygame.font.SysFont(None, 25)
    
    def draw_text(text, color, x, y):
        text_surface = font.render(text, True, color)
        screen.blit(text_surface, (x, y))
    
    def main():
        # Initial snake and food
        snake = [(100, 50), (80, 50), (60, 50)]
        direction = (CELL_SIZE, 0)  # Start moving right
        food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
                random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)
        score = 0
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
    
                # Handle key presses
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP and direction != (0, CELL_SIZE):
                        direction = (0, -CELL_SIZE)
                    elif event.key == pygame.K_DOWN and direction != (0, -CELL_SIZE):
                        direction = (0, CELL_SIZE)
                    elif event.key == pygame.K_LEFT and direction != (CELL_SIZE, 0):
                        direction = (-CELL_SIZE, 0)
                    elif event.key == pygame.K_RIGHT and direction != (-CELL_SIZE, 0):
                        direction = (CELL_SIZE, 0)
    
            # Move the snake
            head_x, head_y = snake[0]
            new_head = (head_x + direction[0], head_y + direction[1])
            snake.insert(0, new_head)
    
            # Check for food collision
            if new_head == food:
                score += 1
                food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
                        random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)
            else:
                snake.pop()  # Remove the tail if no food is eaten
    
            # Check for wall or self collision
            if (new_head[0] < 0 or new_head[0] >= WIDTH or
                new_head[1] < 0 or new_head[1] >= HEIGHT or
                new_head in snake[1:]):
                print(f"Game Over! Final Score: {score}")
                pygame.quit()
                sys.exit()
    
            # Draw everything
            screen.fill(BLACK)
            for segment in snake:
                pygame.draw.rect(screen, GREEN, (*segment, CELL_SIZE, CELL_SIZE))
            pygame.draw.rect(screen, RED, (*food, CELL_SIZE, CELL_SIZE))
            draw_text(f"Score: {score}", WHITE, 10, 10)
    
            pygame.display.flip()
            clock.tick(FPS)
    
    if __name__ == "__main__":
        main()
    

    🔧 How to Run the Code

    1. Install Pygame (if you haven't already):

      pip install pygame
      
    2. Save the code to a file, e.g., snake.py.

    3. Run the script:

      python snake.py
      

    🕹️ Controls

    • Arrow Keys (↑ ↓ ← →) to move the snake.
    • The game ends if the snake hits the wall or itself.

    🧠 Notes

    • This is a basic version. You can enhance it with features like:
      • Increasing difficulty over time.
      • Sound effects.
      • A start/pause menu.
      • High score tracking.

    Let me know if you want to expand this! 🐍

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