write the snake game in python
- User
write the 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 the snake game in python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a basic implementation of the Snake game in Python using the Pygame library:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 640, 480
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_size, snake_pixels):
for pixel in snake_pixels:
pygame.draw.rect(window, white, [pixel[0], pixel[1], snake_size, snake_size])
def display_score(score):
text = font.render("Score: " + str(score), True, white)
window.blit(text, [0, 0])
def run_game():
game_over = False
game_close = False
# Initial position of the snake
x = width / 2
y = height / 2
# Movement of the snake
x_speed = 0
y_speed = 0
# Snake body
snake_pixels = []
snake_length = 1
# Food position
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 game_close:
window.fill(black)
text = font.render("Game Over! Press Q to Quit or C to Play Again", True, white)
window.blit(text, [width / 3, height / 3])
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
game_close = False
if event.key == pygame.K_c:
run_game()
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:
x_speed = -snake_size
y_speed = 0
elif event.key == pygame.K_RIGHT:
x_speed = snake_size
y_speed = 0
elif event.key == pygame.K_UP:
y_speed = -snake_size
x_speed = 0
elif event.key == pygame.K_DOWN:
y_speed = snake_size
x_speed = 0
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += x_speed
y += y_speed
window.fill(black)
pygame.draw.rect(window, red, [food_x, food_y, snake_size, snake_size])
snake_pixels.append([x, y])
if len(snake_pixels) > snake_length:
del snake_pixels[0]
for pixel in snake_pixels[:-1]:
if pixel == [x, y]:
game_close = True
draw_snake(snake_size, snake_pixels)
display_score(snake_length - 1)
pygame.display.update()
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
clock = pygame.time.Clock()
clock.tick(snake_speed)
pygame.quit()
quit()
run_game()
This code sets up a basic Snake game using Pygame. Here's a breakdown of the main components:
We import the necessary modules: pygame for game development and random for generating random positions for the food.
We initialize Pygame and set up the game window with a specified width and height.
We define colors and snake properties such as size and speed.
The draw_snake function is used to draw the snake on the game window.
The display_score function is used to display the current score on the game window.
The run_game function contains the main game loop. It handles events, updates the game state, and redraws the game elements on each frame.
Inside the game loop, we handle user input for controlling the snake's movement.
We check for collisions with the boundaries of the game window and the snake's own body.
We update the position of the snake based on its current speed and direction.
We draw the food and the snake on the game window.
If the snake collides with the food, we increase the snake's length and generate a new random position for the food.
We update the display and control the game speed using a clock.
If the game is over, we display a "Game Over" message and allow the player to quit or play again.
To run the game, make sure you have Pygame installed (pip install pygame) and save the code in a Python file (e.g., snake_game.py). Then, run the file using the Python interpreter.