Exemplo n.º 1
0
class Decoration(pygame.sprite.Sprite):
    def __init__(self, images, coords, group, offset, block_size):
        super(Decoration, self).__init__()

        self.images = images
        self.image = self.images[0]
        self.animationController = Animation(len(self.images), self)

        self.rect = pygame.Rect((offset[0] + coords[0] * block_size[0],
                                 offset[1] + coords[1] * block_size[1]),
                                (block_size[0], block_size[1]))

        group.add(self)

    def update(self):
        if self.animationController.animCount > 0:
            self.animationController.animateRepeat(self.images, 1.4)
Exemplo n.º 2
0
class Player(pygame.sprite.Sprite):
    def __init__(
            self, screen, images, ends
    ):  # ends: idle, walk_down, walk_right, walk_left,,walk_up, spell
        super(Player, self).__init__()
        self.images = images
        self.ends = ends
        self.currentState = ends[0]

        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.rect.center = (20 * 23, 22 * 23 - 1)

        self.spellCasting = False
        self.currentSpell = None
        self.animationController = Animation(self.currentState[1], self)

        self.defaultHP = 100
        self.lives = 100

        self.defaultMana = 100
        self.mana = 100

        self.spellAnims = {
            "cutSpell": (5, 1.8),
            "lightingSpell": (6, 1),
            "explosionSpell": (6, 1)
        }

        self.timerStart = 0

    def startTimer(self):
        self.timerStart = pygame.time.get_ticks()

    def timerUpdate(self):
        if not self.mana == self.defaultMana:
            seconds = (pygame.time.get_ticks() -
                       self.timerStart) / 1000  #calculate how many seconds
            if seconds > 4:
                self.mana += 1

    @property
    def currentAnimation(self):
        return self.ends.index(self.currentState)

    def dealDamage(self, damage):
        self.lives -= damage

    def changeAnimation(self, id):
        self.currentState = self.ends[id]
        self.animationController.animCount = self.currentState[
            1] - self.currentState[0]
        self.animationController.currentAnimFrame = 0
        self.animationController.currentGameFrame = 0
        self.image = self.images[self.currentState[0]:self.currentState[1]][0]
        center = self.rect.center
        self.rect = pygame.Rect((self.rect.x, self.rect.y),
                                self.image.get_size())
        self.rect.center = center

    def castingSpell(self, spellName, spell):
        self.spellCasting = spellName

        if self.currentSpell:
            self.currentSpell.activated = True

        self.currentSpell = spell
        self.mana -= self.currentSpell.manaConsuptions
        self.startTimer()
        self.changeAnimation(self.spellAnims[spellName][0])

    def freeze(self):
        pass

    def update(self):
        if not self.spellCasting:
            self.animationController.animateRepeat(
                self.images[self.currentState[0]:self.currentState[1]], 2)
        else:
            if self.animationController.animateOnce(
                    self.images[self.currentState[0]:self.currentState[1]],
                    self.spellAnims[self.spellCasting][1]):
                self.spellCasting = ''
                self.currentSpell.activated = True
                self.changeAnimation(0)

        self.timerUpdate()
Exemplo n.º 3
0
class SnowBoss(pygame.sprite.Sprite):
    def __init__(self, pos, images, snowflakeImage, lives, player, attackdist,
                 timerController):
        super(SnowBoss, self).__init__()
        self.images = images
        self.image = images[0]
        self.rect = pygame.Rect(pos, self.image.get_size())

        self.lives = lives
        self.defaultHP = lives

        self.player = player
        self.atackdis = attackdist
        self.snowflakeImage = snowflakeImage
        self.timerController = timerController

        self.noticed = False
        self.snowflakes = pygame.sprite.Group()
        self.speed = 2.5
        self.currentlyAttack = False
        self.approaching = True
        self.animationController = Animation(2, self)

    def dealDamage(self, damage):
        self.lives -= damage
        if self.lives <= 0:
            self.kill()

    @property
    def xDistance(self):
        return self.player.rect.centerx - self.rect.centerx

    @property
    def yDistance(self):
        return self.player.rect.centery - self.rect.centery

    def approach(self, untilDist=60):
        sign = lambda x: x and (1, -1)[x < 0]

        direction = (sign(self.xDistance) * self.speed,
                     sign(self.yDistance) * self.speed)
        distance = math.sqrt((self.xDistance)**2 + (self.yDistance)**2)

        if distance <= untilDist:
            if not self.currentlyAttack:
                self.timerController.createTimer(1, self.iceFreeze, False)
                self.currentlyAttack = True
            return True

        self.rect.move_ip(direction)
        return False

    def checkPlayer(self):
        distance = math.sqrt((self.xDistance)**2 + (self.yDistance)**2)
        if distance <= self.atackdis:
            self.noticed = True
            self.snowBall()

    def snowBall(self):
        sign = lambda x: x and (1, -1)[x < 0]
        direction = (sign(self.xDistance), sign(self.yDistance))
        snowflake = Snowflake(self.rect.center, self.snowflakeImage,
                              self.player, direction, 400, 15, self.snowflakes)
        self.currentlyAttack = False

    def snowRing(self):
        diresctions = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1),
                       (0, -1), (1, -1)]
        for direction in diresctions:
            snowFlake = Snowflake(self.rect.center, self.snowflakeImage,
                                  self.player, direction, 300, 5,
                                  self.snowflakes)
        self.currentlyAttack = False

    def iceFreeze(self):
        radius = pygame.Rect(self.rect.center, (75, 75))
        if radius.colliderect(self.player.rect):
            self.player.freeze()
            self.timerController.createTimer(1, self.snowBall, False)
        else:
            self.timerController.createTimer(1, self.snowRing, False)

    def aproachVar(self):
        self.approaching = True

    def update(self):
        if not self.noticed:
            self.checkPlayer()
        else:
            if self.approaching:
                if self.approach():
                    self.approaching = False
                    self.timerController.createTimer(3, self.aproachVar, False)

        self.snowflakes.update()

        self.timerController.updateTimers()

        self.animationController.animateRepeat(self.images, 2)