o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
create a game of snake in python using arrow keys as input
- User
create a game of snake in python using arrow keys as input
- Assistant
create a game of snake in python using arrow keys as input
Here's a simple Snake game implemented in Python using the Pygame library. You can control the snake using the arrow keys.
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Set up the window
window_size = (600, 600)
cell_size = 20
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption('Snake Game')
# Define colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
# Clock to control the frame rate
clock = pygame.time.Clock()
# Initialize game variables
def init_game():
snake_pos = [100, 50]
snake_body = [[100, 50],
[80, 50],
[60, 50]]
food_pos = [random.randrange(1, (window_size[0] // cell_size)) * cell_size,
random.randrange(1, (window_size[1] // cell_size)) * cell_size]
food_spawn = True
direction = 'RIGHT'
change_to = direction
score = 0
return snake_pos, snake_body, food_pos, food_spawn, direction, change_to, score
snake_pos, snake_body, food_pos, food_spawn, direction, change_to, score = init_game()
# Game Over function
def game_over():
font = pygame.font.SysFont('Arial', 40)
game_over_surface = font.render('Game Over!', True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (window_size[0] / 2, window_size[1] / 4)
screen.fill(black)
screen.blit(game_over_surface, game_over_rect)
pygame.display.flip()
pygame.time.delay(2000)
pygame.quit()
sys.exit()
# Main game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# When a key is pressed down
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if direction != 'DOWN':
change_to = 'UP'
elif event.key == pygame.K_DOWN:
if direction != 'UP':
change_to = 'DOWN'
elif event.key == pygame.K_LEFT:
if direction != 'RIGHT':
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT:
if direction != 'LEFT':
change_to = 'RIGHT'
elif event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Update the direction
if change_to == 'UP':
direction = 'UP'
if change_to == 'DOWN':
direction = 'DOWN'
if change_to == 'LEFT':
direction = 'LEFT'
if change_to == 'RIGHT':
direction = 'RIGHT'
# Move the snake
if direction == 'UP':
snake_pos[1] -= cell_size
if direction == 'DOWN':
snake_pos[1] += cell_size
if direction == 'LEFT':
snake_pos[0] -= cell_size
if direction == 'RIGHT':
snake_pos[0] += cell_size
# Snake body growing mechanism
snake_body.insert(0, list(snake_pos))
if snake_pos == food_pos:
score += 1
food_spawn = False
else:
snake_body.pop()
# Spawn food
if not food_spawn:
food_pos = [random.randrange(1, (window_size[0] // cell_size)) * cell_size,
random.randrange(1, (window_size[1] // cell_size)) * cell_size]
food_spawn = True
# Clear screen
screen.fill(black)
# Draw snake
for pos in snake_body:
pygame.draw.rect(screen, green, pygame.Rect(pos[0], pos[1], cell_size, cell_size))
# Draw food
pygame.draw.rect(screen, red, pygame.Rect(food_pos[0], food_pos[1], cell_size, cell_size))
# Check for collisions
# Collision with boundaries
if (snake_pos[0] < 0 or snake_pos[0] >= window_size[0] or
snake_pos[1] < 0 or snake_pos[1] >= window_size[1]):
game_over()
# Collision with self
for block in snake_body[1:]:
if snake_pos == block:
game_over()
# Display score
font = pygame.font.SysFont('Arial', 20)
score_surface = font.render(f'Score: {score}', True, white)
score_rect = score_surface.get_rect(center=(window_size[0] / 2, 20))
screen.blit(score_surface, score_rect)
# Refresh game screen
pygame.display.update()
# Control the frame rate
clock.tick(10)
Instructions to Run the Game:
Install Pygame: Make sure you have Pygame installed. You can install it using pip:
pip install pygame
Save the Code: Copy the code into a file named snake_game.py.
Run the Game: Execute the game by running:
python snake_game.py
Game Controls:
Game Objective:
Enjoy the game!
Not yet included in a published release.