import pygame
import random

pygame.init()

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
DARK_VIOLETT = (51,15,98)
PTNK = (255,192,203)
YELLOW = (255,255,0)
PURPLE = (128,0,128)

dis = pygame.display.set_mode((800,600))
pygame.display.set_caption('Змейка')

snake_block = 10
snake_speed = 15
clock = pygame.time.Clock()


class Snake(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((snake_block, snake_block))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.x = 400
        self.rect.y = 300
        self.dx = 0
        self.dy = 0
        self.body = []
        self.length = 1
        self.score = 0

    def update(self):
        self.rect.x += self.dx
        self.rect.y += self.dy

        self.body.append([self.rect.x, self.rect.y])
        if len(self.body) > self.length:
            del self.body[0]

        for segment in self.body[:-1]:
            if segment[0] == self.rect.x and segment[1] == self.rect.y:
                return True
        return False

    def draw_body(self, surface):
        for segment in self.body[:-1]:
            pygame.draw.rect(surface, GREEN, [segment[0], segment[1], snake_block, snake_block])

    def grow(self):
        self.length += 1
        self.score += 1



class Food(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((snake_block, snake_block))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.respawn()

    def respawn(self):
        self.rect.x = random.randrange(0, 800 - snake_block, snake_block)
        self.rect.y = random.randrange(0, 600 - snake_block, snake_block)


all_sprites = pygame.sprite.Group()
foods = pygame.sprite.Group()

player = Snake()
apple = Food()
all_sprites.add(player, apple)
foods.add(apple)

game_over = False

while not game_over:
    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 and player.dx == 0:
                player.dx = -snake_block
                player.dy = 0
            elif event.key == pygame.K_RIGHT and player.dx == 0:
                player.dx = snake_block
                player.dy = 0
            elif event.key == pygame.K_UP and player.dy == 0:
                player.dy = -snake_block
                player.dx = 0
            elif event.key == pygame.K_DOWN and player.dy == 0:
                player.dy = snake_block
                player.dx = 0

    if player.update():
        game_over = True

    if (player.rect.x < 0 or player.rect.x >= 800 or
            player.rect.y < 0 or player.rect.y >= 600):
        game_over = True

    if pygame.sprite.spritecollide(player, foods, True):
        player.grow()
        new_food = Food()
        all_sprites.add(new_food)
        foods.add(new_food)

    dis.fill(DARK_VIOLETT)

    all_sprites.draw(dis)
    player.draw_body(dis)

    pygame.display.update()
    clock.tick(snake_speed)

pygame.quit()
