def __init__(self, window, stage): self.window = window self.stage = stage self.sprites = ( retro.Sprite.from_path(asset("arrow_left.png")), retro.Sprite.from_path(asset("arrow_right.png")), ) self.sprites[0].rect.midleft = self.window.rect().midleft self.sprites[1].rect.midright = self.window.rect().midright
def __init__(self, window): self.window = window self.stage = retro.Stage(asset("map.png")) self.stage.camera = self.window.rect() self.stage.camera.move_ip(350, 170) self.player = Player(self.window, self.stage) self.spawner = Spawner(self.stage)
class Explosion(retro.Sprite): IMG = retro.Image(asset("bang.png")) def __init__(self, center): retro.Sprite.__init__(self, self.IMG) self.rect.center = center self.ticker = retro.Ticker(end=120) def update(self): if self.ticker.finished: self.kill()
class Crosshair(retro.Sprite): IMG = retro.Image(asset("crosshair.png")) def __init__(self, window): self.window = window retro.Sprite.__init__(self, self.IMG) self.rect.center = self.window.rect().center self.speed = 5 def scroll_vec(self): p = self.rect.center offset = 20 w = self.window.rect().w h = self.window.rect().h return retro.Directions( up=(0 <= p[1] <= offset), down=(h - offset <= p[1] <= h), left=(0 <= p[0] <= offset), right=(w - offset <= p[0] <= w), ).vec def move(self, stage): scroll_vec = self.scroll_vec() # move crosshair key_hold = self.window.events.key_hold move_vec = retro.Directions( up=key_hold(retro.K_UP), down=key_hold(retro.K_DOWN), left=key_hold(retro.K_LEFT), right=key_hold(retro.K_RIGHT), ).vec for i, _ in enumerate(move_vec): move_vec[i] -= scroll_vec[i] move_vec = numpy.clip(move_vec, -1, 1) self.rect.move_ip( move_vec[0] * self.speed, move_vec[1] * self.speed, ) # move camera stage.camera.move_ip( scroll_vec[0] * self.speed, scroll_vec[1] * self.speed, ) stage.camera.clamp_ip(stage.image.rect())
class Ammunitions(retro.Sprite): IMG = retro.Image(asset("bullet.png")) IMG.scale(0.5) def __init__(self, window): self.window = window retro.Sprite.__init__(self, self.IMG) self.rect.bottomleft = self.window.rect().bottomleft self.count = 12 def draw(self, dest): for i in range(self.count): self.rect.left = i * self.rect.width retro.Sprite.draw(self, dest)
class Kidnaper(Enemy): KIDNAPER_IMG = retro.Image(asset("bandit_kidnaper.png"), ) def __init__(self): Enemy.__init__(self, self.KIDNAPER_IMG) def kill(self, p): kidnaper = self.rect.copy() kidnaper.width //= 2 victim = self.rect.copy() victim.width //= 2 victim.x += victim.width victim_killed = (self.alive and victim.collidepoint(p)) kidnaper_killed = (self.alive and kidnaper.collidepoint(p)) if victim_killed or kidnaper_killed: self.alive = False return (kidnaper_killed - victim_killed)
class Hide(retro.Sprite): IMG = retro.Image(asset("hide.png")) def __init__(self, window): self.window = window retro.Sprite.__init__(self, self.IMG) self.rect.center = self.window.rect().center self.hidden = False def update(self): self.hidden = self.window.events.key_hold(retro.K_LSHIFT) def draw(self, dest): if self.hidden: retro.Sprite.draw(self, dest)
class Runner(Enemy): RUNNER_IMG = retro.Image(asset("bandit_street3.png"), ) def __init__(self, stage): Enemy.__init__(self, self.RUNNER_IMG) self.stage = stage self.dx = -2 def move(self): self.rect.x += self.dx if not self.stage.image.rect().contains(self.rect): self.dx *= -1 self.image.flip(x=True, y=False) self.rect.clamp_ip(self.stage.image.rect()) def update(self, target): Enemy.update(self, target) self.move()