write snake game in python
- User
write snake game in python
Assistant
Sure! Here is a basic implementation of the classic "snake" game in Python:
import pygame import random # Initialize Pygame pygame.init() # Set the window size window_width = 500 window_height = 500 # Set the colors white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # Set the font font = pygame.font.SysFont(None, 30) # Create the window window = pygame.display.set_mode((window_width, window_height)) # Set the title of the window pygame.display.set_caption("Snake Game") # Set the clock clock = pygame.time.Clock() # Set the starting position of the snake snake_x = window_width / 2 snake_y = window_height / 2 # Set the length of the snake snake_length = 1 # Set the direction of the snake direction = "right" # Create the snake's body snake_body = [ (snake_x, snake_y - snake_length), ] # Set the starting position of the food food_x = random.randint(0, window_width - 10) food_y = random.randint(0, window_height - 10) # Set the number of points the player can get for eating the food points_per_food = 10 # Set the number of points the player has player_points = 0 # Main game loop running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: direction = "left" elif event.key == pygame.K_RIGHT: direction = "right" elif event.key == pygame.K_UP: direction = "up" elif event.key == pygame.K_DOWN: direction = "down" # Update the position of the snake if direction == "up": snake_y -= 10 elif direction == "down": snake_y += 10 elif direction == "left": snake_x -= 10 elif direction == "right": snake_x += 10 # Check if the snake hit the wall or itself if snake
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.