Write a snake game in Python
- User
Write a snake game 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 a snake game in Python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple implementation of the classic Snake game using Python and the Pygame library:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
# Define colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
# Define snake properties
snake_size = 20
snake_speed = 15
# Define font for displaying score
font = pygame.font.SysFont(None, 30)
def draw_snake(snake_body):
for block in snake_body:
pygame.draw.rect(window, white, [block[0], block[1], snake_size, snake_size])
def display_score(score):
score_text = font.render("Score: " + str(score), True, white)
window.blit(score_text, [10, 10])
def game_loop():
game_over = False
close_game = False
# Set initial position of the snake
x = width // 2
y = height // 2
# Set initial velocity of the snake
dx = 0
dy = 0
# Initialize snake body
snake_body = []
snake_length = 1
# Generate initial position of the food
food_x = round(random.randrange(0, width - snake_size) / snake_size) * snake_size
food_y = round(random.randrange(0, height - snake_size) / snake_size) * snake_size
while not game_over:
while close_game:
window.fill(black)
game_over_text = font.render("Game Over! Press Q to Quit or C to Play Again", True, white)
window.blit(game_over_text, [width // 4, height // 2])
display_score(snake_length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
close_game = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx = -snake_size
dy = 0
elif event.key == pygame.K_RIGHT:
dx = snake_size
dy = 0
elif event.key == pygame.K_UP:
dx = 0
dy = -snake_size
elif event.key == pygame.K_DOWN:
dx = 0
dy = snake_size
# Check for collision with the boundaries
if x >= width or x < 0 or y >= height or y < 0:
close_game = True
x += dx
y += dy
window.fill(black)
pygame.draw.rect(window, red, [food_x, food_y, snake_size, snake_size])
snake_head = []
snake_head.append(x)
snake_head.append(y)
snake_body.append(snake_head)
if len(snake_body) > snake_length:
del snake_body[0]
# Check for collision with the snake body
for block in snake_body[:-1]:
if block == snake_head:
close_game = True
draw_snake(snake_body)
display_score(snake_length - 1)
pygame.display.update()
# Check for collision with the food
if x == food_x and y == food_y:
food_x = round(random.randrange(0, width - snake_size) / snake_size) * snake_size
food_y = round(random.randrange(0, height - snake_size) / snake_size) * snake_size
snake_length += 1
pygame.time.delay(snake_speed)
pygame.quit()
quit()
game_loop()
This code sets up a basic Snake game using Pygame. Here's a breakdown of the main components:
draw_snake function is used to draw the snake body on the game window.display_score function is used to display the current score on the game window.game_loop function contains the main game logic:
game_loop function.To run this code, make sure you have Pygame installed (pip install pygame). When you run the script, a game window will open, and you can control the snake using the arrow keys. The objective is to eat the food and grow the snake without colliding with the boundaries or the snake's own body. The game ends when a collision occurs, and you can choose to quit or play again.
Feel free to customize and enhance the game further based on your requirements!