gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
I want a code of snake game completed
- User
I want a code of snake game completed
- Assistant
I want a code of snake game completed
Certainly! Here's a complete, simple implementation of the classic Snake game using Python and the Pygame library. If you don't have Pygame installed, you can install it via pip:
pip install pygame
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')
# Clock to control game speed
clock = pygame.time.Clock()
SNAKE_SPEED = 15
# Snake block size
BLOCK_SIZE = 20
# Font style
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def your_score(score):
value = score_font.render("Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
def message(msg, color, position):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, position)
def game_loop():
game_over = False
game_close = False
x1 = SCREEN_WIDTH // 2
y1 = SCREEN_HEIGHT // 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# Random food location
foodx = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
foody = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
score = 0
while not game_over:
while game_close:
screen.fill(BLACK)
message("Game Over! Press C-Play Again or Q-Quit", RED, [SCREEN_WIDTH / 6, SCREEN_HEIGHT / 3])
your_score(score)
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:
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 and x1_change == 0:
x1_change = -BLOCK_SIZE
y1_change = 0
elif event.key == pygame.K_RIGHT and x1_change == 0:
x1_change = BLOCK_SIZE
y1_change = 0
elif event.key == pygame.K_UP and y1_change == 0:
y1_change = -BLOCK_SIZE
x1_change = 0
elif event.key == pygame.K_DOWN and y1_change == 0:
y1_change = BLOCK_SIZE
x1_change = 0
# Check boundaries
x1 += x1_change
y1 += y1_change
if x1 >= SCREEN_WIDTH or x1 < 0 or y1 >= SCREEN_HEIGHT or y1 < 0:
game_close = True
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [foodx, foody, BLOCK_SIZE, BLOCK_SIZE])
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]
# Check if snake collides with itself
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
# Draw snake
for x in snake_List:
pygame.draw.rect(screen, GREEN, [x[0], x[1], BLOCK_SIZE, BLOCK_SIZE])
your_score(score)
pygame.display.update()
# Check if snake has eaten food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
foody = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
Length_of_snake += 1
score += 10
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
if __name__ == "__main__":
game_loop()
If you'd like me to help you with a version in another language or more features, just ask!
Not yet included in a published release.