import pygame
import os
import random
from os import path

img_dir = path.join(path.dirname(__file__),'img')

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'img')

WIDTH =360
HEIGHT = 480
FPS = 30

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
VIOLET = (238, 130, 238)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Змейка")
#player_img = pygame.image.load(os.path.join(img_folder, 'красный чел.png')).convert()
#player_img = pygame.transform.scale(player_img, (300, 300))
clock = pygame.time.Clock()

snake_block = 10
snake_speed = 15


class Snake(pygame.sprite.Sprite):
    def __init__(self):
        self.lenght = 1
        self.score = 0
    def update(self):
        self.rect.y += self.dy
        self.rect.x += self.dx


        self.body.append([self.rect.x, self.rect.y])
        if len(self.body)>self.lenght:
            del self.body[0]


        for segment in self.body[:-1]:
            if segment[0] == self.rect.x and segment[1] == self.rect.y:
                return False
        return True


    def draw_body(self, surface):
        for segment in self.body[:-1]:
            pygame.draw.rect(surface,BLACK, segment[0], segment[1], snake_block, snake_block)


    def grow(self):
        self.lenght += 1
        self.score += 1


class Apple(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, 360-snake_block, snake_block)
        self.rect.y = random.randrange(0, 480-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)

running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        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.dx = -snake_block
                player.dy = 0
            elif event.key == pygame.K_DOWN and player.dy == 0:
                player.dx = snake_block
                player.dy = 0

    if player.update():
        running = False


    if (player.rect.x < 0 or player.rect.x >= 800 or
            player.rect.y < 0 or player.rect.y >= 600):
        running = False


    if pygame.sprite.spritecollide(player, foods, True):
        player.grow()
        new_food = Food()
        all_sprites.add(new_food)
        foods.add(new_food)


    screen.fill(WHITE)

    all_sprites.draw(screen)

    player.draw_body(screen)

    pygame.display.update()
    clock.tick(snake_speed)


#    screen.fill(GREEN)
#    all_sprites.draw(screen)
#    draw_text(screen, str(score), 18, WIDTH/2, 10)
#    #pygame.draw.circle(screen,(0,0,0), (180,240), (100))
#    pygame.display.flip()

pygame.quit()