In this game, you are a snake and have to eat as many green squares as possible to get as big as possible! If you come in contact with yourself or touch the wall, you die.
Here’s an example: HERE
In order to play the game, you must copy and paste the code below into your python file, then run the code and enjoy!
Copy the code for FREE!:
import pygame import time import random pygame.init() # Constants WIDTH, HEIGHT = 800, 600 SNAKE_SIZE = 20 FOOD_SIZE = 20 FPS = 15 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) # Screen setup screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Snake Game') clock = pygame.time.Clock() font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def draw_score(score): value = score_font.render("Score: " + str(score), True, BLACK) screen.blit(value, [0, 0]) def draw_snake(snake_list): for x in snake_list: pygame.draw.rect(screen, BLACK, [x[0], x[1], SNAKE_SIZE, SNAKE_SIZE]) def game_over_message(): message = font_style.render("Game Over", True, RED) screen.blit(message, [WIDTH // 4, HEIGHT // 3]) pygame.display.update() time.sleep(2) 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 - FOOD_SIZE) / 20.0) * 20.0 foody = round(random.randrange(0, HEIGHT - FOOD_SIZE) / 20.0) * 20.0 while not game_over: while game_close: screen.fill(WHITE) game_over_message() draw_score(length_of_snake - 1) pygame.display.update() 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: 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_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 x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(WHITE) pygame.draw.rect(screen, GREEN, [foodx, foody, FOOD_SIZE, FOOD_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] for x in snake_list[:-1]: if x == snake_head: game_close = True draw_snake(snake_list) draw_score(length_of_snake - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, WIDTH - FOOD_SIZE) / 20.0) * 20.0 foody = round(random.randrange(0, HEIGHT - FOOD_SIZE) / 20.0) * 20.0 length_of_snake += 1 clock.tick(FPS) pygame.quit() quit() game_loop()