Below is a Python implementation of a Snake game using the pygame library. The game includes a start menu, controls, scoring, and is fully playable.
Make sure you have pygame installed before running the code. You can install it using pip install pygame.
import pygame
import time
import random
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
# Snake block size
BLOCK_SIZE = 20
SNAKE_SPEED = 15
# Initialize screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Clock for controlling the game speed
clock = pygame.time.Clock()
# Fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def display_score(score):
"""Display the current score."""
value = score_font.render(f"Score: {score}", True, WHITE)
screen.blit(value, [10, 10])
def draw_snake(block_size, snake_list):
"""Draw the snake on the screen."""
for block in snake_list:
pygame.draw.rect(screen, GREEN, [block[0], block[1], block_size, block_size])
def message(msg, color, position):
"""Display a message on the screen."""
mesg = font_style.render(msg, True, color)
screen.blit(mesg, position)
def game_loop():
"""Main game loop."""
game_over = False
game_close = False
# Initial position of the snake
x1, y1 = WIDTH // 2, HEIGHT // 2
# Change in position
x1_change, y1_change = 0, 0
# Snake body
snake_list = []
length_of_snake = 1
# Food position
food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0
while not game_over:
while game_close:
screen.fill(BLACK)
message("You Lost! Press Q-Quit or C-Play Again", RED, [WIDTH // 6, HEIGHT // 3])
display_score(length_of_snake - 1)
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 for boundaries
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
# Draw food
pygame.draw.rect(screen, BLUE, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])
# Update snake's head
snake_head = [x1, y1]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
# Check for collision with itself
for block in snake_list[:-1]:
if block == snake_head:
game_close = True
draw_snake(BLOCK_SIZE, snake_list)
display_score(length_of_snake - 1)
pygame.display.update()
# Check if the snake eats the food
if x1 == food_x and y1 == food_y:
food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0
length_of_snake += 1
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
def start_menu():
"""Display the start menu."""
menu = True
while menu:
screen.fill(BLACK)
message("Welcome to Snake Game!", GREEN, [WIDTH // 4, HEIGHT // 3])
message("Press S to Start or Q to Quit", WHITE, [WIDTH // 4, HEIGHT // 2])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
menu = False
if event.key == pygame.K_q:
pygame.quit()
quit()
# Run the game
start_menu()
game_loop()
Features:
- Start Menu: The game starts with a menu where you can press
S to start or Q to quit.
- Game Over Screen: When the snake collides with itself or the wall, a game over screen appears with options to restart (
C) or quit (Q).
- Score Display: The score is displayed at the top-left corner of the screen.
- Snake Movement: The snake moves in the direction of the arrow keys (
UP, DOWN, LEFT, RIGHT).
How to Play:
- Run the script.
- On the start menu, press
S to start the game.
- Use the arrow keys to control the snake.
- Avoid colliding with the walls or yourself.
- Eat the blue food to grow the snake and increase your score.
Enjoy the game! 🎮