X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;ds=sidebyside;f=koperkapel%2Fweapons.py;h=7dcb41ed154250bcc009f0f5ce8fdf80261e2a56;hb=ec6ffa9c6a998de15e5f856756a6efb04c00418b;hp=da8c4c3712b6ccafeca80fed1c17bb929c9384ea;hpb=f24d1dbc09b54630ce9f8fd1d37facbada9bca10;p=koperkapel.git diff --git a/koperkapel/weapons.py b/koperkapel/weapons.py index da8c4c3..7dcb41e 100644 --- a/koperkapel/weapons.py +++ b/koperkapel/weapons.py @@ -1,49 +1,79 @@ """ Tools for dealing with weapons. """ -from pgzero.loaders import images +from pygame.surface import Surface +from pygame.draw import circle +from pgzero.loaders import images, sounds from .actors.animsurf import AnimatedSurfActor +from .util import safepath class Weapon: def __init__(self, name, damage, image_name=None, bullet_range=0, - can_tape=True, frames=("_1",)): + can_tape=True, frames=("_1",), sound=None): self.name = name self.image_name = image_name or name self.frames = frames self.damage = damage self.bullet_range = bullet_range self.can_tape = can_tape + self.sound = None + if sound: + self.sound = sounds.load(sound) + + def play_sound(self): + if self.sound: + self.sound.play() + + def assemble_bullet(self): + return BulletActor(self) WEAPONS = [ Weapon("spit", damage=1, bullet_range=2, can_tape=False, - image_name="blank", frames=("",)), + image_name="blank", frames=("",), sound="fire_spit"), Weapon("butter_knife", damage=2), Weapon("crowbar", damage=4), - Weapon("gun", damage=4, bullet_range=4), + Weapon("gun", damage=4, bullet_range=4, sound='gun_fire'), ] WEAPON_LOOKUP = {w.name: w for w in WEAPONS} +def weapon_by_name(weapon_name): + return WEAPON_LOOKUP[weapon_name] + + class WeaponActor(AnimatedSurfActor): def __init__(self, weapon, **kw): super().__init__(**kw) self.weapon = weapon +class BulletActor(AnimatedSurfActor): + def __init__(self, weapon, **kw): + radius = min(weapon.damage * 5, 32) + surf = Surface((radius * 2, radius * 2)) + surf.convert_alpha() + surf.fill((255, 255, 255, 0)) + circle(surf, (255, 0, 255, 32), (radius, radius), radius) + frames = [surf] + super().__init__(frames=frames, **kw) + self.weapon = weapon + + class WeaponFactory: def assemble_frame(self, suffix, weapon, tape): - surf = images.load("weapons/%s%s" % (weapon.image_name, suffix)) + surf = images.load(safepath("weapons/%s%s") + % (weapon.image_name, suffix)) frame = surf.copy() if tape: - tape_surf = images.load("weapons/tape") + tape_surf = images.load(safepath("weapons/tape")) frame.blit(tape_surf, (0, 0)) return frame def assemble(self, weapon_name, tape=False): - weapon = WEAPON_LOOKUP[weapon_name] + weapon = weapon_by_name(weapon_name) tape = tape and weapon.can_tape frames = [ self.assemble_frame(suffix, weapon, tape)