Snake game
- User
Snake game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Snake game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a simple Snake game in Python using Pygame. Here's a basic implementation:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width = 800
height = 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Snake properties
snake_block = 20
snake_speed = 15
# Initialize snake
snake_x = width // 2
snake_y = height // 2
snake_dx = 0
snake_dy = 0
snake_body = []
snake_length = 1
# Initialize food
food_x = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
food_y = round(random.randrange(0, height - snake_block) / snake_block) * snake_block
# Game clock
clock = pygame.time.Clock()
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and snake_dx != snake_block:
snake_dx = -snake_block
snake_dy = 0
elif event.key == pygame.K_RIGHT and snake_dx != -snake_block:
snake_dx = snake_block
snake_dy = 0
elif event.key == pygame.K_UP and snake_dy != snake_block:
snake_dx = 0
snake_dy = -snake_block
elif event.key == pygame.K_DOWN and snake_dy != -snake_block:
snake_dx = 0
snake_dy = snake_block
# Move snake
snake_x += snake_dx
snake_y += snake_dy
# Check boundaries
if snake_x >= width or snake_x < 0 or snake_y >= height or snake_y < 0:
running = False
# Update snake body
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_body.append(snake_head)
if len(snake_body) > snake_length:
del snake_body[0]
# Check self-collision
for segment in snake_body[:-1]:
if segment == snake_head:
running = False
# Check food collision
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
food_y = round(random.randrange(0, height - snake_block) / snake_block) * snake_block
snake_length += 1
# Draw everything
window.fill(BLACK)
for segment in snake_body:
pygame.draw.rect(window, GREEN, [segment[0], segment[1], snake_block, snake_block])
pygame.draw.rect(window, RED, [food_x, food_y, snake_block, snake_block])
pygame.display.update()
# Control game speed
clock.tick(snake_speed)
pygame.quit()
This code creates a classic Snake game where you:
To run this game, you'll need to have Pygame installed. You can install it using:
pip install pygame
Would you like me to explain any part of the code in more detail?