Prompt Detail:
Sure! Here's an example Python code using Pygame to create the classic Snake game:
```
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 600
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# Set up the colors
background_color = (0, 0, 0)
snake_color = (0, 255, 0)
food_color = (255, 0, 0)
# Set up the snake
snake_block_size = 10
snake_speed = 15
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, snake_color, [x[0], x[1], snake_block_size, snake_block_size])
# Set up the food
food_block_size = 10
def draw_food():
food_x = round(random.randrange(0, screen_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - food_block_size) / 10.0) * 10.0
return food_x, food_y
# Set up the game loop
def game_loop():
game_over = False
game_close = False
# Set up the starting position of the snake
snake_start_x = round(screen_width / 2 / 10.0) * 10.0
snake_start_y = round(screen_height / 2 / 10.0) * 10.0
snake_list = []
length_of_snake = 1
# Add the starting position of the snake to the snake list
snake_list.append([snake_start_x, snake_start_y])
# Set up the food
food_x, food_y = draw_food()
# Set up the initial direction of the snake
direction = "right"
while not game_over:
# Check for events
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:
direction = "left"
elif event.key == pygame.K_RIGHT:
direction = "right"
elif event.key == pygame.K_UP:
direction = "up"
elif event.key == pygame.K_DOWN:
direction = "down"
# Move the snake
if direction == "right":
snake_head = []
snake_head.append(snake_list[-1][0] + snake_block_size)
snake_head.append(snake_list[-1][1])
elif direction == "left":
snake_head = []
snake_head.append(snake_list[-1][0] - snake_block_size)
snake_head.append(snake_list[-1][1])
elif direction == "up":
snake_head = []
snake_head.append(snake_list[-1][0])
snake_head.append(snake_list[-1][1] - snake_block_size)
elif direction == "down":
snake_head = []
snake_head.append(snake_list[-1][0])
snake_head.append(snake_list[-1][1] + snake_block_size)
# Add the new head of the snake to the snake list
snake_list.append(snake_head)
# Check if the snake hits the wall
if snake_head[0] >= screen_width or snake_head[0] < 0 or snake_head[1] >= screen_height or snake_head[1] < 0:
game_close = True
Add a comment