self._overlay = self._surfaces['floor'].copy()
self._doors = self.actors.add_layer("doors", level=9)
self._keypads = self.actors.add_layer("keypads", level=8)
+ self._bullets = self.actors.add_layer("bullets", level=10)
# These are already Actors
for door in self._level.doors:
self._doors.add(door)
self._level.remove_enemy(enemy)
self._enemies.remove(enemy)
+ def _fire_bullet(self, bullet, pos, dp):
+ if len(self._bullets) >= 10:
+ return
+ bullet.game_pos = pos
+ self._bullets.add(bullet)
+
@defer_to_update
def _fire_weapon(self, world):
weapon = weapon_by_name(world.weapons.current)
weapon.play_sound()
if weapon.bullet_range > 0:
- # ranged, fire bullet
- print("Pew.")
+ self._fire_bullet(
+ weapon.assemble_bullet(), self._player_pos, self._angle_dp)
else:
# melee
pos, dp = self._player_pos, self._angle_dp
for enemy in self._enemies:
enemy.pos = self.calc_offset(
enemy.game_pos[0] * TILE_SIZE, enemy.game_pos[1] * TILE_SIZE)
+ for bullet in self._bullets:
+ bullet.pos = self.calc_offset(
+ bullet.game_pos[0] * TILE_SIZE + (TILE_SIZE // 2),
+ bullet.game_pos[0] * TILE_SIZE + (TILE_SIZE // 2))
more = self._check_held_keys(dt)
if more:
events.extend(more)
""" Tools for dealing with weapons. """
+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
if self.sound:
self.sound.play()
+ def assemble_bullet(self):
+ return BulletActor(self)
+
WEAPONS = [
Weapon("spit", damage=1, bullet_range=2, can_tape=False,
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):