import pygame import time import random # Initialize pygame pygame.init() # Define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (213, 50, 80) GREEN = (0, 255, 0) BLUE = (50, 153, 213) # Define screen size SCREEN_WIDTH = 600 SCREEN_HEIGHT = 400 # Create the screen object screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Snake Game") # Set up the clock clock = pygame.time.Clock() # Snake and food block size BLOCK_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 display_score(score): value = score_font.render("Your Score: " + str(score), True, WHITE) screen.blit(value, [0, 0]) # Function to draw the snake def draw_snake(block_size, snake_body): for segment in snake_body: pygame.draw.rect(screen, GREEN, [segment[0], segment[1], block_size, block_size]) # Function to display the message when the game ends def message(msg, color): mesg = font_style.render(msg, True, color) screen.blit(mesg, [SCREEN_WIDTH / 6, SCREEN_HEIGHT / 3]) # Main game loop def gameLoop(): game_over = False game_close = False # Starting position of the snake x1 = SCREEN_WIDTH / 2 y1 = SCREEN_HEIGHT / 2 # Movement speed x1_change = 0 y1_change = 0 # Snake body snake_body = [] snake_length = 1 # Randomly place the food foodx = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / 10.0) * 10.0 foody = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / 10.0) * 10.0 # Game loop while not game_over: while game_close: screen.fill(BLUE) message("You Lost! Press C-Continue or Q-Quit", RED) display_score(snake_length - 1) pygame.display.update() # Handle key events for restarting or quitting for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True game_close = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() # Handling key events 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 = -BLOCK_SIZE y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = BLOCK_SIZE y1_change = 0 elif event.key == pygame.K_UP: y1_change = -BLOCK_SIZE x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = BLOCK_SIZE x1_change = 0 # Check for boundary collisions if x1 >= SCREEN_WIDTH or x1 < 0 or y1 >= SCREEN_HEIGHT or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(BLUE) # Draw the food pygame.draw.rect(screen, RED, [foodx, foody, BLOCK_SIZE, BLOCK_SIZE]) # Update snake body snake_head = [] snake_head.append(x1) snake_head.append(y1) snake_body.append(snake_head) if len(snake_body) > snake_length: del snake_body[0] # Check if the snake collides with itself for segment in snake_body[:-1]: if segment == snake_head: game_close = True draw_snake(BLOCK_SIZE, snake_body) display_score(snake_length - 1) pygame.display.update() # Check if snake eats the food if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / 10.0) * 10.0 foody = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / 10.0) * 10.0 snake_length += 1 clock.tick(SNAKE_SPEED) pygame.quit() quit() # Start the game gameLoop()