import pygame
import random
import  os

pygame.init()
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'img')
player_img = pygame.image.load(os.path.join(img_folder, "New Piskel-19.png.png"))
# === ЦВЕТА ===
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0,0,255)

dis = pygame.display.set_mode((800, 600))

visota_block = 25
kopania_speed = 15
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((visota_block, visota_block))
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.x = 400  # старт по центру
        self.rect.y = 100
        self.dx = 0  # изменение по X (как x1_change в шаблоне)
        self.dy = 0  # изменение по Y
        self.body = []  # тело змейки (список координат)
        self.length = 1  # длина
        self.score = 0
    def grow(self, type_sokrovisha):
        if type_sokrovisha == "sokrovisha":
            self.score += 1
        else:
            self.score += 5
    def update(self):
        self.rect.x += self.dx
        self.rect.y += self.dy

class Sokrovisha(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        #self.image = pygame.image.load("land/sokrovisha.png").convert_alpha()
        self.image = pygame.Surface((visota_block, visota_block))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.respawn()
    def respawn(self):
        self.rect.x = random.randrange(0, 800 - visota_block, visota_block)
        self.rect.y = random.randrange(0, 600 - visota_block, visota_block)

class Almaz(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        #self.image = pygame.image.load('land/almaz.png').convert_alpha()
        # self.image = pygame.transform.scale(self.image, (visota_block, visota_block))
        self.image = pygame.Surface((visota_block, visota_block))
        self.image.fill(blue)
        self.rect = self.image.get_rect()

        self.respawn
    def respawn(self):
        self.rect.x = random.randrange(0, 800 - visota_block, visota_block)
        self.rect.y = random.randrange(0, 600 - visota_block, visota_block)
class Fon(pygame.sprite.Sprite):
    def __init__(self):
        super.__init__()
    self.image.so
all_sprites = pygame.sprite.Group()
Sokrovish = pygame.sprite.Group()
almas = pygame.sprite.Group()
player = Player()
almaz = Almaz()
all_sprites.add(player, almaz)
almas.add(almaz)

kolishestvo_sokrovish = 5
# for i in range(kolishestvo_sokrovish):
sokrovishe = Sokrovisha()
all_sprites.add(sokrovishe)
Sokrovish.add(sokrovishe)
game_over = False

while not game_over:
    # 1. Обработка событий
    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 = -visota_block
                player.dy = 0
            elif event.key == pygame.K_RIGHT and player.dx == 0:
                player.dx = visota_block
                player.dy = 0
            elif event.key == pygame.K_UP and player.dy == 0:
                player.dy = -visota_block
                player.dx = 0
            elif event.key == pygame.K_DOWN and player.dy == 0:
                player.dy = visota_block
                player.dx = 0
    # 4. Отрисовка
    dis.fill(white)

    # Рисуем все спрайты из группы
    all_sprites.draw(dis)
    # Рисуем тело змейки отдельно (оно не в группе)
    # player.draw_body(dis)

    pygame.display.update()
    clock.tick(kopania_speed)

pygame.quit()
