import pygame import time import random # 15 snake_speed = 10 e_count = 0 display_x = 200 display_y = 150 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) pygame.init() pygame.display.set_caption("snak"+("e"*e_count)) game_window = pygame.display.set_mode((display_x, display_y)) fps = pygame.time.Clock() snake_pos = [100, 50] snake_body = [ [100, 50], [90, 50], [80, 50], [70, 50] ] food_pos = [random.randrange(1, (display_x//10)) * 10, random.randrange(1, (display_y//10)) * 10] food_spawn = True direction = 'RIGHT' change_to = direction score = 0 def show_score(choice, color, font, size): score_font = pygame.font.SysFont("Verdana", 20) score_surface = score_font.render("score: " + str(score), True, color) score_rect = score_surface.get_rect() game_window.blit(score_surface, score_rect) def game_over(): my_font = pygame.font.SysFont('times new roman', 50) game_over_surface = my_font.render("your score: " + str(score), True, red) game_over_rect = game_over_surface.get_rect() game_over_rect.midtop = (display_x/2, display_y/4) game_window.blit(game_over_surface, game_over_rect) pygame.display.flip() time.sleep(2) pygame.quit() quit() while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: change_to = 'UP' if event.key == pygame.K_s: change_to = 'DOWN' if event.key == pygame.K_a: change_to = 'LEFT' if event.key == pygame.K_d: change_to = 'RIGHT' if event.key == pygame.K_UP: change_to = 'UP' if event.key == pygame.K_DOWN: change_to = 'DOWN' if event.key == pygame.K_LEFT: change_to = 'LEFT' if event.key == pygame.K_RIGHT: change_to = 'RIGHT' if change_to == 'UP' and direction != 'DOWN': direction = 'UP' if change_to == 'DOWN' and direction != 'UP': direction = 'DOWN' if change_to == 'LEFT' and direction != 'RIGHT': direction = 'LEFT' if change_to == 'RIGHT' and direction != 'LEFT': direction = 'RIGHT' if direction == 'UP': snake_pos[1] -= 10 if direction == 'DOWN': snake_pos[1] += 10 if direction == 'LEFT': snake_pos[0] -= 10 if direction == 'RIGHT': snake_pos[0] += 10 snake_body.insert(0, list(snake_pos)) if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]: score += 10 e_count = e_count + 1 if display_x < 1500: display_x = display_x + 10 game_window = pygame.display.set_mode((display_x, display_y)) pygame.display.set_caption("snak"+("e"*e_count)) food_spawn = False else: snake_body.pop() if not food_spawn: food_pos = [random.randrange(1, (display_x//10)) * 10, random.randrange(1, (display_y//10)) * 10] food_spawn = True game_window.fill(black) for pos in snake_body: pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10)) pygame.draw.rect(game_window, white, pygame.Rect( food_pos[0], food_pos[1], 10, 10)) if snake_pos[0] < 0 or snake_pos[0] > display_x-10: game_over() if snake_pos[1] < 0 or snake_pos[1] > display_y-10: game_over() for block in snake_body[1:]: if snake_pos[0] == block[0] and snake_pos[1] == block[1]: game_over() show_score(1, white, 'times new roman', 20) pygame.display.update() fps.tick(snake_speed)