1 """ Tools for dealing with weapons. """
3 from pygame.surface import Surface
4 from pygame.draw import circle
5 from pgzero.loaders import images, sounds
6 from .actors.animsurf import AnimatedSurfActor
7 from .util import safepath
11 def __init__(self, name, damage, image_name=None, bullet_range=0,
12 can_tape=True, frames=("_1",), sound=None):
14 self.image_name = image_name or name
17 self.bullet_range = bullet_range
18 self.can_tape = can_tape
21 self.sound = sounds.load(sound)
27 def assemble_bullet(self):
28 return BulletActor(self)
32 Weapon("spit", damage=1, bullet_range=2, can_tape=False,
33 image_name="blank", frames=("",), sound="fire_spit"),
34 Weapon("butter_knife", damage=2),
35 Weapon("crowbar", damage=4),
36 Weapon("gun", damage=4, bullet_range=4, sound='gun_fire'),
39 WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
42 def weapon_by_name(weapon_name):
43 return WEAPON_LOOKUP[weapon_name]
46 class WeaponActor(AnimatedSurfActor):
47 def __init__(self, weapon, **kw):
48 super().__init__(**kw)
52 class BulletActor(AnimatedSurfActor):
53 def __init__(self, weapon, **kw):
54 radius = min(weapon.damage * 5, 32)
55 surf = Surface((radius * 2, radius * 2))
57 surf.fill((255, 255, 255, 0))
58 circle(surf, (255, 0, 255, 32), (radius, radius), radius)
60 super().__init__(frames=frames, **kw)
66 def assemble_frame(self, suffix, weapon, tape):
67 surf = images.load(safepath("weapons/%s%s")
68 % (weapon.image_name, suffix))
71 tape_surf = images.load(safepath("weapons/tape"))
72 frame.blit(tape_surf, (0, 0))
75 def assemble(self, weapon_name, tape=False):
76 weapon = weapon_by_name(weapon_name)
77 tape = tape and weapon.can_tape
79 self.assemble_frame(suffix, weapon, tape)
80 for suffix in weapon.frames]
81 return WeaponActor(weapon, frames=frames)
84 default_weapons = WeaponFactory()