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.
To create a simple Snake game in Python, we can use the pygame library, which simplifies graphics and game development. If you don't have pygame installed, you can install it using pip:
pip install pygame
Here's a basic implementation of the Snake game:
import pygame
import time
import random
# Initialize pygame
pygame.init()
# Define some colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 155, 0)
# Set the width and height of the screen [width, height]
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# Set the title of the window
pygame.display.set_caption("Snake Game")
# Block size of the snake
block_size = 20
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Snake initial position and body
snake_pos = [100, 50]
snake_body = [[100, 50], [80, 50], [60, 50]]
# Fruit position
fruit_pos = [random.randrange(1, (screen_width // block_size)) * block_size,
random.randrange(1, (screen_height // block_size)) * block_size]
fruit_spawn = True
# Set the initial direction of the snake
direction = 'RIGHT'
change_to = direction
# Score
score = 0
# Function to display the score
def show_score(color, font, size):
score_font = pygame.font.SysFont(font, size)
score_surface = score_font.render("Score : " + str(score), True, color)
score_rect = score_surface.get_rect()
screen.blit(score_surface, score_rect)
# Main loop
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Whenever a key is pressed down
elif event.type == pygame.KEYDOWN:
# W -> Up; S -> Down; A -> Left; D -> Right
if event.key == pygame.K_UP or event.key == ord('w'):
change_to = 'UP'
elif event.key == pygame.K_DOWN or event.key == ord('s'):
change_to = 'DOWN'
elif event.key == pygame.K_LEFT or event.key == ord('a'):
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT or event.key == ord('d'):
change_to = 'RIGHT'
# Esc -> Create event to quit the game
elif event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Making sure the snake cannot move in the opposite direction instantaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Moving the snake
if direction == 'UP':
snake_pos[1] -= block_size
if direction == 'DOWN':
snake_pos[1] += block_size
if direction == 'LEFT':
snake_pos[0] -= block_size
if direction == 'RIGHT':
snake_pos[0] += block_size
# Snake body growing mechanism
snake_body.insert(0, list(snake_pos))
if snake_pos[0] == fruit_pos[0] and snake_pos[1] == fruit_pos[1]:
score += 1
fruit_spawn = False
else:
snake_body.pop()
# Spawning the fruit
if not fruit_spawn:
fruit_pos = [random.randrange(1, (screen_width // block_size)) * block_size,
random.randrange(1, (screen_height // block_size)) * block_size]
fruit_spawn = True
# GFX
screen.fill(black)
for pos in snake_body:
pygame.draw.rect(screen, green, pygame.Rect(pos[0], pos[1], block_size, block_size))
pygame.draw.rect(screen, red, pygame.Rect(fruit_pos[0], fruit_pos[1], block_size, block_size))
# Game Over conditions
# Getting out of bounds
if snake_pos[0] >= screen_width or snake_pos[0] < 0 or snake_pos[1] >= screen_height or snake_pos[1] < 0:
game_over = True
# Touching the snake body
for block in snake_body[1:]:
if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
game_over = True
show_score(white, 'consolas', 20)
# Refresh game screen
pygame.display.flip()
# Pause for 100 milliseconds
clock.tick(10)
# Quit pygame
pygame.quit()
quit()
This code sets up a basic Snake game where the player controls the snake using the arrow keys or 'WASD' keys to navigate the snake around the screen to eat fruit and grow. The game ends if the snake collides with itself or goes out of bounds.
Remember, this is a simple implementation. There are many ways to enhance this game, such as adding a start menu, high score tracking, different levels, etc. Experimenting with this code is a great way to learn more about Python and game development.