write the game snake in python
- User
write the game snake in python
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write the game snake in python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure, here's a basic implementation of the classic game Snake in Python using the Pygame library:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
game_window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Snake Game")
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Define game constants
BLOCK_SIZE = 20
SNAKE_SPEED = 10
# Initialize the snake
snake_body = [(400, 300), (380, 300), (360, 300)]
snake_direction = "RIGHT"
# Initialize the food
food_x = random.randint(BLOCK_SIZE, WINDOW_WIDTH - BLOCK_SIZE)
food_y = random.randint(BLOCK_SIZE, WINDOW_HEIGHT - BLOCK_SIZE)
# 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 and snake_direction != "RIGHT":
snake_direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake_direction != "LEFT":
snake_direction = "RIGHT"
elif event.key == pygame.K_UP and snake_direction != "DOWN":
snake_direction = "UP"
elif event.key == pygame.K_DOWN and snake_direction != "UP":
snake_direction = "DOWN"
# Move the snake
new_head = list(snake_body[0])
if snake_direction == "RIGHT":
new_head[0] += BLOCK_SIZE
elif snake_direction == "LEFT":
new_head[0] -= BLOCK_SIZE
elif snake_direction == "UP":
new_head[1] -= BLOCK_SIZE
elif snake_direction == "DOWN":
new_head[1] += BLOCK_SIZE
# Check for collisions
if new_head[0] < BLOCK_SIZE or new_head[0] >= WINDOW_WIDTH - BLOCK_SIZE or \
new_head[1] < BLOCK_SIZE or new_head[1] >= WINDOW_HEIGHT - BLOCK_SIZE or \
new_head in snake_body:
running = False
# Add the new head to the snake
snake_body.insert(0, tuple(new_head))
# Check if the snake has eaten the food
if new_head[0] == food_x and new_head[1] == food_y:
food_x = random.randint(BLOCK_SIZE, WINDOW_WIDTH - BLOCK_SIZE)
food_y = random.randint(BLOCK_SIZE, WINDOW_HEIGHT - BLOCK_SIZE)
else:
snake_body.pop()
# Clear the game window
game_window.fill(WHITE)
# Draw the snake
for segment in snake_body:
pygame.draw.rect(game_window, BLACK, (segment[0], segment[1], BLOCK_SIZE, BLOCK_SIZE))
# Draw the food
pygame.draw.rect(game_window, RED, (food_x, food_y, BLOCK_SIZE, BLOCK_SIZE))
# Update the display
pygame.display.flip()
# Limit the frame rate
pygame.time.Clock().tick(SNAKE_SPEED)
# Quit Pygame
pygame.quit()
This implementation includes the following features:
You can customize this code further by adding features like a score system, high scores, and a game over screen. You can also experiment with different game mechanics, such as increasing the snake's speed as it grows longer, or adding obstacles or power-ups to the game.
Not yet included in a published release.