o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
write a snakae game
- User
write a snakae game
- Assistant
write a snakae game
Certainly! Here's a simple snake game written in Python using the pygame library. This game lets you control a snake using the arrow keys. The objective is to eat the red food squares to grow longer, and avoid running into the edges or yourself.
First, make sure you have pygame installed:
pip install pygame
Here's the code for the snake game:
import pygame
import time
import random
pygame.init()
# Window size
width = 400
height = 300
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (213, 50, 80)
# Create the display
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont(None, 25)
def message(msg, color):
mesg = font_style.render(msg, True, color)
game_display.blit(mesg, [width / 6, height / 3])
def game_loop():
game_over = False
game_close = False
x = width / 2
y = height / 2
x_change = 0
y_change = 0
snake_list = []
length_of_snake = 1
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
game_display.fill(black)
message("You Lost! Press C-Play Again or Q-Quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = 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:
x_change = -snake_block
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = snake_block
y_change = 0
elif event.key == pygame.K_UP:
y_change = -snake_block
x_change = 0
elif event.key == pygame.K_DOWN:
y_change = snake_block
x_change = 0
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += x_change
y += y_change
game_display.fill(black)
pygame.draw.rect(game_display, red, [food_x, food_y, snake_block, snake_block])
snake_head = [x, y]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
for segment in snake_list:
pygame.draw.rect(game_display, white, [segment[0], segment[1], snake_block, snake_block])
pygame.display.update()
if x == food_x and y == food_y:
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
game_loop()
Instructions to Play:
Note: This is a simple version of the snake game to help you understand the basics. You can enhance it by adding features like scoring, levels, or sound effects.
Feel free to modify and expand upon this code to create your own unique version of the snake game!
Not yet included in a published release.