Make Python Snake code with pygame
- User
Make Python Snake code with pygame
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Make Python Snake code with pygame
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a simple Snake game using Python's Pygame library is a great way to learn about game development. Below is a complete, minimalist implementation of the classic Snake game. First, make sure you have Pygame installed. If you haven't installed it yet, you can do so via pip:
pip install pygame
Once you have Pygame installed, you can use the following code to create the Snake game:
import pygame
import time
import random
# Initialize Pygame
pygame.init()
# Set colors
WHITE = (255, 255, 255)
YELLOW = (255, 255, 102)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
# Set display dimensions
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Snake Game by OpenAI')
# Set the clock
clock = pygame.time.Clock()
# Snake settings
SNAKE_BLOCK = 10
SNAKE_SPEED = 15
# Font styles
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, BLACK, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [WIDTH / 6, HEIGHT / 3])
def gameLoop():
game_over = False
game_close = False
x1 = WIDTH / 2
y1 = HEIGHT / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
# Generate food position
foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
while not game_over:
while game_close == True:
screen.fill(BLUE)
message("You Lost! Press C-Play Again or Q-Quit", RED)
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:
gameLoop()
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:
x1_change = -SNAKE_BLOCK
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = SNAKE_BLOCK
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -SNAKE_BLOCK
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = SNAKE_BLOCK
x1_change = 0
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLUE)
pygame.draw.rect(screen, GREEN, [foodx, foody, SNAKE_BLOCK, SNAKE_BLOCK])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
our_snake(SNAKE_BLOCK, snake_list)
pygame.display.update()
# Check if snake ate the food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
length_of_snake += 1
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
# Start the game
gameLoop()
Make sure you have Python and Pygame installed.
Copy the code into a Python file (e.g., snake_game.py).
Run the Python file using the command:
python snake_game.py
Feel free to modify the code and tweak it to create a more complex or visually appealing game!