def end(self): super().end() if self.attacking.weapon and self.attacking.weapon.uses == 0: broken_screen(self.attacking) if self.defending.weapon and self.defending.weapon.uses == 0: broken_screen(self.defending) room.run_room(rooms.Fadeout(500)) self.attacking.team.play_music('map', True) self.attacking.played = True self.exp_or_die(self.attacking, self.defending) self.exp_or_die(self.defending, self.attacking) s.loaded_map.sprites.update() if self.defending.team.is_defeated(): s.winner = self.attacking.team elif self.attacking.team.is_defeated(): s.winner = self.defending.team print("#" * 12 + " " + _("Battle ends") + " " + "#" * 12 + "\r\n")
def begin(self): super().begin() if self.attacking.health <= 0: raise ValueError(f"{self.attacking} is dead!") if self.defending.health <= 0: raise ValueError(f"{self.defending} is dead!") if self.attacking.played: raise ValueError(f"{self.attacking} has already played!") self.attacking.prepare_battle() self.defending.prepare_battle() self.at, self.dt = self.attacking.number_of_attacks(self.defending) self.round = 1 print( f"\r\n{'#' * 12} {self.attacking.name} vs {self.defending.name} {'#' * 12}" ) att_str = _("%s is going to attack %d %s") print(att_str % (self.attacking.name, self.at, _("time") if self.at == 1 else _("times"))) print(att_str % (self.defending.name, self.dt, _("time") if self.dt == 1 else _("times"))) self.attacking.team.play_music('battle') room.run_room(rooms.Fadeout(2000, stop_mixer=False))
def pause_menu(self): menu_entries = [(_('Return to Game'), None), (_('Return to Main Menu'), self.reset), (_('Return to O.S.'), utils.return_to_os)] display.darken(200) menu = gui.Menu(menu_entries, f.MAIN, layout_gravity=gui.Gravity.CENTER, dismiss_callback=True, clear_screen=None) room.run_room(menu)
def attack(self, attacking=None, defending=None): if not defending: defending = self.curr_unit if not attacking: attacking = self.prev_unit assert (defending != attacking) # let the battle begin! room.run_room(rooms.BattleAnimation(attacking, defending)) self.reset_selection()
def test(): import resources import room import fonts as f import logging logging.basicConfig(level=logging.DEBUG) nine = NinePatch(resources.load_image('WindowBorder.png'), (70, 70), wait=False, padding=100, layout_gravity=gui.Gravity.FILL) nine.add_child(gui.Label("test", f.MAIN)) nine.add_child(gui.Clock(f.MAIN)) room.run_room(nine)
def test_battle_animation(): display.initialize() import gettext import logging import resources gettext.install('ice-emblem', resources.LOCALE_PATH) logging.basicConfig(level=80) s.load_map('resources/maps/default.tmx') unit1 = Unit(name='Soldier', health=30, level=1, experience=99, strength=5, skill=500, speed=1, luck=2, defence=1, resistance=1, movement=1, constitution=1, aid=1, affinity=None, wrank=[]) unit2 = Unit(name='Skeleton', health=31, level=1, experience=0, strength=6, skill=30, speed=10, luck=1, defence=1, resistance=1, movement=1, constitution=1, aid=1, affinity=None, wrank=[]) unit1.coord = (1, 1) unit2.coord = (1, 2) team1 = Team('Ones', (255, 0, 0), 0, [unit1], unit1, {}) team2 = Team('Twos', (0, 0, 255), 0, [unit2], unit2, {}) s.units_manager.units = [unit1, unit2] display.window.fill(c.GREY) while unit1.health > 0 and unit2.health > 0: room.run_room(BattleAnimation(unit1, unit2)) unit1, unit2 = unit2, unit1 pygame.quit()
def end_turn(self, *args): if self.team.is_turn_over(): super().end_turn() self.set_timeout(100, self.mark_done) else: modal = gui.Modal(_( "Are you sure you want to end your turn? There are still units that can move." ), f.SMALL, layout_gravity=gui.Gravity.CENTER, dismiss_callback=True, clear_screen=None) room.run_room(modal) if modal.answer: super().end_turn() self.set_timeout(100, self.mark_done)
def chosen(self, menu, choice): map_path = resources.map_path(choice) try: s.load_map(map_path) except: msg = _( "Error while loading map \"%s\"! Please report this issue.\n\n%s" ) % (map_path, traceback.format_exc()) logging.error(msg) dialog = gui.Dialog(msg, f.MONOSPACE, layout_gravity=gui.Gravity.FILL, padding=25) room.run_room(dialog) else: self.done = True
def broken_screen(unit): sounds.play('broke') room.run_room( gui.Label("%s is broken" % unit.weapon.name, f.SMALL, txt_color=c.RED))
def exp_or_die(unit1: Unit, unit2: Unit) -> None: if unit1.health > 0: unit1.gain_exp(unit2) room.run_room(ExperienceAnimation(unit1)) else: s.loaded_map.kill_unit(unit1)
def reset(self, *_): room.run_room(rooms.Fadeout(1000)) room.stop()
if __name__ == "__main__": import gui import logging import fonts as f logging.basicConfig(level=10) class TestLifeBar(gui.LinearLayout): def __init__(self): self.bar = LifeBar(points=11) self.label = gui.Label("{0}/{1}", f.SMALL) self.label.format(self.bar.value, self.bar.points) super().__init__(wait=True, children=[self.bar, self.label], orientation=gui.Orientation.HORIZONTAL) def loop(self, events, dt): for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.bar.value += 1 elif event.key == pygame.K_DOWN: self.bar.value -= 1 elif event.key == pygame.K_LEFT: if self.bar.points > 1: self.bar.points -= 1 elif event.key == pygame.K_RIGHT: self.bar.points += 1 self.label.format(self.bar.value, self.bar.points) room.run_room(TestLifeBar())
self.parent.cursor.point(*next(self.path)) self.parent.invalidate() except StopIteration: self.done = True class SelectAndWait(room.Room): """ Select a coordinate and wait 100ms. Should be a child of :class:`TileMap`. """ def __init__(self, coord: Coord): super().__init__() self.coord = coord def begin(self) -> None: super().begin() self.parent.select(self.coord) self.set_timeout(100, self.mark_done) if __name__ == '__main__': import gettext gettext.install('ice-emblem', resources.LOCALE_PATH) logging.basicConfig(level=0) room.run_room( TileMap('resources/maps/default.tmx', w=200, h=200, center=display.get_rect().center)) pygame.quit()