Exemple #1
0
class Shell(pygame.sprite.Sprite, Animator):  # общий класс для пуль всех типов
    def __init__(self, images, rect, name, speed):
        super().__init__()
        self.burstAnimator = Animator()
        self.flowAnimator = Animator()
        self.image = images[name]['default']
        self.burst_list = images[name]['burst']
        self.icon = images[name]['icon']
        self.speed = speed
        self.spawn = False

        x, y = rect.centerx, int(rect.top + self.icon.get_height() // 2)
        # хитбокс создается на основе картинки без эффектов
        self.icon_rect = self.icon.get_rect(center=(x, y))
        self.rect = self.image.get_rect(center=(x, y))
        self.hitted_rects = [self.rect.copy()]

        self.run_burst = False

    def draw(self, display):
        if self.run_burst:
            display.blit(self.burst_list[self.burstAnimator.getIteral],
                         self.rect)
        else:
            display.blit(self.image, self.rect)

    def update(self, now):
        self.rect.y -= self.speed

        if self.run_burst:
            self.burstAnimator.updateAnimation(now, len(self.burst_list), 40,
                                               self.kill)  # 40
            self.speed = 0
            self.rect = self.burst_list[self.burstAnimator.getIteral].get_rect(
                center=(self.rect.center))
        else:
            self.burstAnimator.change_time(now)

        self.icon_rect.center = self.rect.center
        self.hitted_rects[0] = self.icon_rect
Exemple #2
0
class Player(pygame.sprite.Sprite, HeartsGroup, Mana):
    def __init__(self, x, y, display_size):
        super().__init__()

        self.burstAnimator = Animator()
        self.flowAnimator = Animator()

        self.Health = HeartsGroup(display_size)
        self.Stamina = Mana(display_size)

        self.x, self.y = x, y
        self.display_size = display_size

        path = os.getcwd()
        self.skins = {
            'purple': {
                'left_move': [
                    pygame.image.load(path +
                                      r'\img\spaceship\purpleship\left' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'right_move': [
                    pygame.image.load(path +
                                      r'\img\spaceship\purpleship\right' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'default': [
                    pygame.image.load(path +
                                      r'\img\spaceship\purpleship\space' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'color':
                'purple'
            },
            'red': {
                'left_move': [
                    pygame.image.load(path + r'\img\spaceship\redship\left' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'right_move': [
                    pygame.image.load(path + r'\img\spaceship\redship\right' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'default': [
                    pygame.image.load(path + r'\img\spaceship\redship\space' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'color':
                'red'
            },
            'dark': {
                'left_move': [
                    pygame.image.load(path + r'\img\spaceship\dark\left' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'right_move': [
                    pygame.image.load(path + r'\img\spaceship\dark\right' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'default': [
                    pygame.image.load(path + r'\img\spaceship\dark\space' +
                                      str(i) + '.png') for i in range(1, 3)
                ],
                'color':
                'dark'
            }
        }

        self.images = self.skins['purple']
        self.acting_images = self.images['default']

        self.rect = pygame.Rect(self.x, self.y,
                                self.images['default'][0].get_width(),
                                self.images['default'][0].get_height())
        self.speed = {'accel': 1, 'finite': 8}
        self.DEFAULTXP = 10
        self.XP = self.DEFAULTXP

        self.hitted_rects = [
            pygame.Rect(self.rect.centerx - 10, self.rect.top + 15, 20,
                        self.rect.bottom - self.rect.top - 25),
            pygame.Rect(self.rect.left + 10, self.rect.top + 65,
                        self.rect.right - self.rect.left - 22, 45)
        ]
        self.last_strike = 0
        self.last_skinChanging = 0

    def update(self, now, tick=80):
        self.hitted_rects = [
            pygame.Rect(
                self.rect.centerx - self.rect.width * .2 // 2, self.rect.top,
                self.rect.width * 0.2,
                self.rect.bottom - self.rect.top - self.rect.height * 0.1),
            pygame.Rect(self.rect.left,
                        self.rect.top + self.rect.height * 0.36,
                        self.rect.right - self.rect.left,
                        self.rect.height * 0.25)
        ]

        self.flowAnimator.updateAnimation(now, len(self.acting_images), tick,
                                          self.flowAnimator.IteralToNull)

    def draw(self, display):
        display.blit(self.acting_images[self.flowAnimator.getIteral],
                     (self.rect.x, self.rect.y))

    def Strike(self, now, object, image, func):
        if object:
            if now - self.last_strike > object.cooldawn:
                if self.Stamina.toSpend(object.stm):
                    func(object(image, self.rect), ('all', 'player'))
                    self.last_strike = now

    def hit(self, damage):
        self.XP -= damage
        self.Health.hit(self.XP, damage)

        return self.XP

    def heal(self, amount):
        if self.XP == self.DEFAULTXP:
            pass

        elif self.XP + amount <= self.DEFAULTXP:
            self.XP += amount
            self.Health.heal(self.XP, amount)

        else:
            # аааааа дублирование кода
            amount = (self.XP + amount) - self.DEFAULTXP
            self.XP += amount
            super().heal(self.XP, amount)

    def changeSkinPack(self, now):
        if now - self.last_skinChanging > 250:
            a = list(self.skins.keys())
            index = a.index(self.images['color'])

            if index + 1 < len(a):
                pack_name = a[index + 1]
            else:
                pack_name = a[0]

            self.images = self.skins[pack_name]
            self.rect = pygame.Rect(self.rect.x, self.rect.y,
                                    self.images['default'][0].get_width(),
                                    self.images['default'][0].get_height())

            self.last_skinChanging = now