From: Simon Cross Date: Sat, 5 Mar 2016 21:17:55 +0000 (+0200) Subject: Very hacky bullets. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=c1691479e387f6676f03dbf6e431345eea60dd65;p=koperkapel.git Very hacky bullets. --- diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index ea32bc1..53d370f 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -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) diff --git a/koperkapel/weapons.py b/koperkapel/weapons.py index ee83eb8..7dcb41e 100644 --- a/koperkapel/weapons.py +++ b/koperkapel/weapons.py @@ -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):