give me the snake code
- User
give me the snake code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
give me the snake code
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple Snake game implementation in Python using the Pygame library:
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 600, 600
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE
FPS = 10
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
def game_loop():
# Initial snake position and direction
snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
snake_direction = (1, 0)
# Initial food position
food = place_food(snake)
# Game state
score = 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake_direction != (0, 1):
snake_direction = (0, -1)
elif event.key == pygame.K_DOWN and snake_direction != (0, -1):
snake_direction = (0, 1)
elif event.key == pygame.K_LEFT and snake_direction != (1, 0):
snake_direction = (-1, 0)
elif event.key == pygame.K_RIGHT and snake_direction != (-1, 0):
snake_direction = (1, 0)
# Move the snake
head_x, head_y = snake[0]
new_head = (head_x + snake_direction[0], head_y + snake_direction[1])
# Check for collisions
if (new_head[0] < 0 or new_head[0] >= GRID_WIDTH or
new_head[1] < 0 or new_head[1] >= GRID_HEIGHT or
new_head in snake):
game_over = True
continue
# Add new head
snake.insert(0, new_head)
# Check if food eaten
if new_head == food:
score += 1
food = place_food(snake)
else:
snake.pop()
# Draw everything
screen.fill(BLACK)
# Draw snake
for segment in snake:
pygame.draw.rect(screen, GREEN, (segment[0] * GRID_SIZE, segment[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
# Draw food
pygame.draw.rect(screen, RED, (food[0] * GRID_SIZE, food[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
# Draw score
font = pygame.font.SysFont(None, 36)
score_text = font.render(f'Score: {score}', True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(FPS)
# Game over screen
font = pygame.font.SysFont(None, 72)
game_over_text = font.render('Game Over', True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 50))
font = pygame.font.SysFont(None, 36)
score_text = font.render(f'Final Score: {score}', True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 2 + 10))
pygame.display.flip()
# Wait for a key press to restart or quit
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
waiting = False
game_loop()
def place_food(snake):
while True:
food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
if food not in snake:
return food
# Start the game
game_loop()
Would you like me to explain or break down this code?