Загрузка данных


import pygame as pg

GESTURE_MODE = False
if GESTURE_MODE:
    from gesture import Gesture


pg.init()

SCREEN_WIDTH = 900
SCREEN_HEIGHT = 550

CHARACTER_WIDTH = 300
CHARATER_HEIGHT = 375

FPS = 60

font = pg.font.Font(None, 40)


def load_image(file, width, height):
    image = pg.image.load(file).convert_alpha()
    image = pg.transform.scale(image, (width, height))
    return image


def text_render(text):
    return font.render(str(text), True, "black")


class Player(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.idle_animation_right = None
        self.load_animations()

        self.image = self.idle_animation_right[0]
        self.current_image = 0
        self.current_animation = self.idle_animation_right

        self.rect = self.image.get_rect()
        self.rect.center = (100, SCREEN_HEIGHT // 2)

        self.timer = pg.time.get_ticks()
        self.interval = 300
        self.side = "right"
        self.animation_mode = True

        self.charge_power = 0
        self.charge_indicator = pg.Surface((self.charge_power, 10))
        self.charge_indicator.fill("red")

        self.charge_mode = False

        self.attack_mode = False
        self.attack_interval = 500
        self.fireballs = pg.sprite.Group()

    def load_animations(self):

        self.idle_animation_right = []
        for i in range(1, 4):
            self.idle_animation_right.append(load_image(f"images/fire wizard/idle{i}.png", CHARACTER_WIDTH, CHARATER_HEIGHT))

        self.idle_animation_left = []
        for image in self.idle_animation_right:
            self.idle_animation_left.append(pg.transform.flip(image, True, False))

        self.move_animation_right = []
        for i in range(1, 5):
            self.move_animation_right.append(load_image(f"images/fire wizard/move{i}.png", CHARACTER_WIDTH, CHARATER_HEIGHT))

        self.move_animation_left = []
        for image in self.move_animation_right:
            self.move_animation_left.append(pg.transform.flip(image, True, False))

        self.charge = [load_image("images/fire wizard/charge.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.charge.append(pg.transform.flip(self.charge[0], True, False))

        self.down = [load_image("images/fire wizard/down.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.down.append(pg.transform.flip(self.down[0], True, False))

        self.fireball = [load_image("images/fire wizard/magicball.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.fireball.append(pg.transform.flip(self.down[0], True, False))

        self.attack = [load_image("images/fire wizard/attack.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.attack.append(pg.transform.flip(self.down[0], True, False))


    def update(self, gesture):
        keys = pg.key.get_pressed()
        direction = 0

        if keys[pg.K_a]:
            direction = -1
            self.side = "left"
        elif keys[pg.K_d]:
            direction = 1
            self.side = "right"


        self.handle_attack_mode()
        self.handle_movement(direction, keys, gesture)
        self.handle_animation()

    def handle_attack_mode(self):
        if self.attack_mode:
            if pg.time.get_ticks() - self.timer > self.attack_interval:
                self.attack_mode = False
                self.timer = pg.time.get_ticks()


    def handle_movement(self, direction, keys, gesture):
        if self.attack_mode:
            return


        if direction != 0:
            self.animation_mode = True
            self.charge_mode = False
            self.rect.x += direction
            self.current_animation = self.move_animation_left if direction == -1 else self.move_animation_right
        elif (GESTURE_MODE and gesture == "live long") or (keys[pg.K_SPACE]):
            self.animation_mode = False
            self.image = self.charge[self.side != "right"]
            self.charge_mode = True
            self.charge_power += 1
        elif keys[pg.K_s]:
            self.animation_mode = False
            self.charge_mode = False
            self.image = self.down[self.side != "right"]
        else:
            self.animation_mode = True
            self.charge_mode = False
            self.current_animation = self.idle_animation_left if self.side == "left" else self.idle_animation_right

        if self.rect.left <= 0:
            self.rect.left = 0

        if self.rect.right >= SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH

    def handle_animation(self):
        if not self.charge_mode and self.charge_power > 0:
            self.attack_mode = True

        if self.animation_mode and not self.attack_mode:
            if pg.time.get_ticks() - self.timer > self.interval:
                self.current_image += 1
                if self.current_image >= len(self.current_animation):
                    self.current_image = 0
                self.image = self.current_animation[self.current_image]
                self.timer = pg.time.get_ticks()

        if self.charge_mode:
            self.charge_power += 1
            self.charge_indicator = pg.Surface((self.charge_power, 10))
            self.charge_indicator.fill("red")

            if self.charge_power >= 100:
                self.attack_mode = True

        if self.animation_mode:
            if pg.time.get_ticks() - self.timer > self.interval:
                self.current_image += 1
                if self.current_image >= len(self.current_animation):
                    self.current_image = 0
                self.image = self.current_animation[self.current_image]
                self.timer = pg.time.get_ticks()


        if self.attack_mode and self.charge_power > 0:
            fireball_position = self.rect.topright if self.side == "right" else self.rect.topleft
            self.fireballs.add(Fireball(fireball_position, self.side, self.charge_power))
            self.charge_power = 0
            self.charge_mode = False
            self.image = self.attack[self.side != "right"]
            self.timer = pg.time.get_ticks()


class Enemy(pg.sprite.Sprite):
    def __init__(self, folder):
        super().__init__()

        self.folder = folder
        self.load_enemy_animations()

        self.image = self.idle_animation_left[0]
        self.current_image = 0
        self.current_animation = self.idle_animation_right

        self.rect = self.image.get_rect()
        self.rect.center = (SCREEN_WIDTH - 100, SCREEN_HEIGHT // 2)

        self.timer = pg.time.get_ticks()
        self.interval = 300
        self.side = "left"
        self.animation_mode = True

        self.charge_power = 0
        self.charge_indicator = pg.Surface((self.charge_power, 10))
        self.charge_indicator.fill("red")

        self.charge_mode = False

        self.magic_balls = pg.sprite.Group()

        self.attack_mode = False
        self.attack_interval = 500

        self.move_interval = 800
        self.move_duration = 0
        self.durection = 0
        self.move_timer = pg.time.get_ticks()

        self.charge_power = 0

    def load_enemy_animations(self):

        self.idle_animation_right = []
        for i in range(1, 4):
            self.idle_animation_right.append(
                load_image(f"images/{self.folder}/idle{i}.png", CHARACTER_WIDTH, CHARATER_HEIGHT))

        self.idle_animation_left = []
        for image in self.idle_animation_right:
            self.idle_animation_left.append(pg.transform.flip(image, True, False))

        self.move_animation_right = []
        for i in range(1, 5):
            self.move_animation_right.append(
                load_image(f"images/{self.folder}/move{i}.png", CHARACTER_WIDTH, CHARATER_HEIGHT))

        self.move_animation_left = []
        for image in self.move_animation_right:
            self.move_animation_left.append(pg.transform.flip(image, True, False))

        self.charge = [load_image(f"images/{self.folder}/move{i}.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.charge.append(pg.transform.flip(self.charge[0], True, False))

        self.down = [load_image(f"images/{self.folder}/down.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.down.append(pg.transform.flip(self.down[0], True, False))

        self.attack = [load_image(f"images/{self.folder}/attack.png", CHARACTER_WIDTH, CHARATER_HEIGHT)]
        self.attack.append(pg.transform.flip(self.down[0], True, False))

    def update_enemy(self, gesture):
        self.handle_attack_mode(Player)
        self.handle_movement()
        self.handle_animation()

    def handle_attack_mode(self, player):
        ...

    def handle_movement(self):
        ...

    def handle_animation(self):
        ...



class Fireball(pg.sprite.Sprite):
    def __init__(self, coord, side, power):
        super().__init__()
        self.coord = coord
        self.side = side
        self.power = power
        self.image = load_image("images/fire wizard/magicball.png", 200, 150)
        if self.side == "right":
            self.image = pg.transform.flip(self.image, True, False)
        self.rect = self.image.get_rect()
        self.rect.x = coord[0]
        self.rect.y = coord[1] + 120

    def update(self):
        if self.side == "right":
            self.rect.x += 4
        else:
            self.rect.x -= 4

        if self.rect.right <= 0 or self.rect.left >= SCREEN_WIDTH:
            self.kill()

class Game:
    def __init__(self):

        # Создание окна
        self.screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pg.display.set_caption("Битва магов")

        self.gesture = None

        self.background = load_image("images/background.png", SCREEN_WIDTH, SCREEN_HEIGHT)

        self.player = Player()

        self.enemy = Enemy("earth monk")

        self.clock = pg.time.Clock()

        if GESTURE_MODE:
            print("Загрузка модуля жестов ... ")
            self.g = Gesture()
            print("Загрузка завершена.")

            self.GET_GESTURE = pg.USEREVENT + 1
            pg.time.set_timer(self.GET_GESTURE, 1000)

        self.run()

    def run(self):
        while True:
            self.event()
            self.update()
            self.draw()
            self.clock.tick(FPS)

    def event(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                quit()
            if GESTURE_MODE:
                if event.type == self.GET_GESTURE:
                    self.gesture = self.g.get_gesture()

    def update(self):
        self.player.update(self.gesture)
        self.player.fireballs.update()

    def draw(self):
        # Отрисовка интерфейса
        self.screen.blit(self.background, (0, 0))
        self.screen.blit(self.player.image, self.player.rect)

        self.screen.blit(self.enemy.image, self.enemy.rect)

        if self.player.charge_mode:
            self.screen.blit(self.player.charge_indicator, (self.player.rect.left + 120, self.player.rect.top))


        self.player.fireballs.draw(self.screen)

        self.screen.blit(text_render(self.gesture), (0, 0))

        pg.display.flip()


if __name__ == "__main__":
    Game()