Very hacky bullets.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 21:17:55 +0000 (23:17 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 21:17:55 +0000 (23:17 +0200)
koperkapel/scenes/level.py
koperkapel/weapons.py

index ea32bc1eaa63107850cb9c3b82e79f22cc8a63e1..53d370f1d0f0aaeee9fe0e3e0a4d8d3a6db91b05 100644 (file)
@@ -31,6 +31,7 @@ class BaseLevelScene(Scene):
         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)
@@ -194,13 +195,19 @@ class GameLevelScene(BaseLevelScene):
             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
@@ -228,6 +235,10 @@ class GameLevelScene(BaseLevelScene):
         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)
index ee83eb816c6eb5c9295db74c05fb78199d506e56..7dcb41ed154250bcc009f0f5ce8fdf80261e2a56 100644 (file)
@@ -1,5 +1,7 @@
 """ 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
@@ -22,6 +24,9 @@ class Weapon:
         if self.sound:
             self.sound.play()
 
+    def assemble_bullet(self):
+        return BulletActor(self)
+
 
 WEAPONS = [
     Weapon("spit", damage=1, bullet_range=2, can_tape=False,
@@ -44,6 +49,18 @@ class WeaponActor(AnimatedSurfActor):
         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):