import pygame
import random
# Initialize pygame
pygame.init()
# Set the screen dimensions and create the screen object
WIDTH = 640
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Define colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
BLUE = (50, 153, 213)
YELLOW = (255, 255, 102)
# Define the snake and food size
SNAKE_SIZE = 10
SNAKE_SPEED = 15
# Font for displaying score
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Function to display the current score
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
# Function to draw the snake
def our_snake(snake_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_size, snake_size])
# Function to display the game over message
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [WIDTH / 6, HEIGHT / 3])
# Main game loop
def gameLoop():
game_over = False
game_close = False
# Initial position of the snake
x1 = WIDTH / 2
y1 = HEIGHT / 2
# Snake movement
x1_change = 0
y1_change = 0
# Snake body
snake_List = []
Length_of_snake = 1
# Initial food position
foodx = round(random.randrange(0, WIDTH - SNAKE_SIZE) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - SNAKE_SIZE) / 10.0) * 10.0
# Game clock
clock = pygame.time.Clock()
while not game_over:
while game_close:
screen.fill(BLUE)
message("You Lost! Press Q-Quit or C-Play Again", RED)
Your_score(Length_of_snake - 1)
pygame.display.update()
# Check for key press to quit or restart
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()
# Game loop: check for events and control the snake
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_SIZE
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = SNAKE_SIZE
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -SNAKE_SIZE
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = SNAKE_SIZE
x1_change = 0
# If the snake hits the boundaries, end the game
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLUE)
# Draw food
pygame.draw.rect(screen, YELLOW, [foodx, foody, SNAKE_SIZE, SNAKE_SIZE])
# Add the new head to the snake body
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
# Keep the snake body from growing bigger than the current length
if len(snake_List) > Length_of_snake:
del snake_List[0]
# Check if the snake collided with itself
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
# Draw the snake
our_snake(SNAKE_SIZE, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
# Check if the snake eats the food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, WIDTH - SNAKE_SIZE) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - SNAKE_SIZE) / 10.0) * 10.0
Length_of_snake += 1
# Set the game speed (snake's movement)
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
# Start the game
if __name__ == "__main__":
gameLoop()