Creating a basic Snake game using Python and Pygame is a fun project! Below is a simple implementation of the Snake game. Before running the code, ensure you have the Pygame library installed. You can install it using pip if you haven’t done so:
bash
pip install pygame
Here’s a simple implementation of the Snake game:
python
import pygame
import time
import random
# Settings
WIDTH, HEIGHT = 600, 400
SNAKE_BLOCK = 10
SNAKE_SPEED = 15
# 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)
# Initialize Pygame
pygame.init()
pygame.display.set_caption('Snake Game')
game_window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
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(game_window, BLACK, [x[0], x[1], snake_block, snake_block])
def your_score(score):
value = score_font.render("Score: " + str(score), True, BLUE)
game_window.blit(value, [0, 0])
def message(msg, color):
mesg = font_style.render(msg, True, color)
game_window.blit(mesg, [WIDTH / 6, HEIGHT / 3])
def game_loop():
game_over = False
game_close = False
x1 = WIDTH / 2
y1 = HEIGHT / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
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:
game_window.fill(WHITE)
message("You Lost! Press C-Play Again or Q-Quit", RED)
your_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:
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
game_window.fill(WHITE)
pygame.draw.rect(game_window, 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)
your_score(length_of_snake - 1)
pygame.display.update()
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
game_loop()
How to Play the Game
Use the arrow keys to control the direction of the snake.
Try to eat the green food to grow the snake and increase your score.
Avoid running into the walls or the snake's own body.
If the snake runs into a wall or itself, press 'C' to play again or 'Q' to quit the game.
You can copy the above code into a Python file (for example, snake_game.py) and run it to play the game! Enjoy!