view skaapsteker/sprites/enemies.py @ 632:0675f390653c

Initial port to Python 3 and Pygame 2.
author Simon Cross <hodgestar@gmail.com>
date Fri, 20 Jan 2023 20:01:06 +0100
parents b7f912705adb
children
line wrap: on
line source

from .base import Monster, PatrollingMonster
from pygame import transform


class RedOni(Monster):
    image_dir = 'sprites/oni red'

    attack_frame = 1
    attack_damage = 10
    frame_pause = 0.05  # Fast attacks

    facings = {
            'running' : (('left', None),
                ('right', lambda x: transform.flip(x, True, False))),
            'attacking' : (('left', None),
                ('right', lambda x: transform.flip(x, True, False))),
                }

    def setup(self, direction, **opts):
        super(RedOni, self).setup(**opts)
        self.facing = direction

    def start_attack(self, player):
        if self._animation != 'attacking':
            # Turn to face the player we're attacking
            if player.collide_rect.centerx > self.collide_rect.centerx:
                self.facing = 'right'
            else:
                self.facing = 'left'
        Monster.start_attack(self, player)


class FireballOni(RedOni):
    pass # TODO


class PatrollingOni(RedOni, PatrollingMonster):
    image_dir = 'sprites/oni blue'

class FlyingOni(PatrollingOni):
    image_dir = 'sprites/oni gold'

    gravitates = False

    patrol_velocity = (0, 200)

    def setup(self, **opts):
        super(PatrollingOni, self).setup(**opts)
        self.heading = 'up'

    def update(self):
        PatrollingOni.update(self)
        if self._animation == 'running':
            if self.heading == 'up':
                self.velocity = (0, -self.patrol_velocity[1])
            else:
                self.velocity = (0, self.patrol_velocity[1])

    def check_floors(self, floors):
        pass

    def collided(self, other):
        if not other.block or not other.floor:
            return
        if other.collide_rect.bottom < self.collide_rect.bottom:
            # We're colliding with something above us
            if self.heading == 'up':
                self.change_facing()
        else:
            if self.heading == 'down':
                self.change_facing()

    def change_facing(self):
        if self.heading == 'up':
            self.heading = 'down'
        else:
            self.heading = 'up'