def loop(self): """Main ui loop.""" go = False # program movement while 1: x = None if not go: c = field.scr.getch() field.status(c) if c in self.keymap: field.status("c is in keymap") self.commands[self.keymap[c]]() # g<dir> elif c == 103: go = 99 c = field.scr.getch() continue # <num><dir> elif 48 <= c <= 57: go = c-48 field.status(go) c = field.scr.getch() if 48 <= c <= 57: go += int('%d%d' % (go, c-48)) c = field.scr.getch() continue # <dir> if str(c) in conf.directions: x,y = self.get_coords(c) #field.status('%d,%d' % (x,y)) if x < 1 or x > conf.xmax: go = False continue elif y < 1 or y > conf.ymax: go = False continue self.location = x,y if self.mode == 'wall': items = field[self.location] put_wall = True for item in items: if item.kind == 'wall': put_wall = False break if put_wall: field.set(self.location, Item('wall')) if go: go -= 1 field.full_display() lx, ly = self.location field.status('%d,%d' % (lx,ly)) field.msg() if x: field.scr.move(y-1,x-1) field.scr.refresh()
def up(self): """Go up the stairs.""" if not field.contains(self.loc, "up"): field.text.append("Need a staircase to ascend.") return cur = levels.current levels.list[cur] = field.fld, level.down, level.up cur -= 1 field.fld, level.down, level.up = levels.list[cur] field.full_display()
def up(self): """Go up the stairs.""" if not field.contains(self.loc, "up"): field.text.append("Need a staircase to ascend.") return cur = levels.current levels.list[cur] = field.fld, level.down, level.up cur -= 1 field.fld, level.down, level.up = levels.list[cur] field.full_display(self)
def init_level(self): field.load_map("empty") level.populate() # make hero's party level.hero = Being('party', field.random(), level.last_index) level.last_index += 1 level.hero.place() level.hoplite = Being("hoplite", Loc(1, 1), level.last_index) level.last_index += 1 level.fencer = Being("fencer", Loc(1, 2), level.last_index) level.last_index += 1 level.mage = Being("mage", Loc(1, 3), level.last_index) level.last_index += 1 field.full_display([level.hero])
def enter_tactical(self, target): """Enter tactical mode.""" if self.team == "monsters": level.attacking_monster = level.monsters.index(self) am_kind = self.kind if target.team == "monsters": log("attack() - target: %s %s" % (target.kind, target)) log("attack() - level.monsters: %s" % str(level.monsters)) level.attacking_monster = level.monsters.index(target) am_kind = target.kind log("level.attacking_monster: %s" % level.attacking_monster) level.save_monsters = deepcopy(level.monsters) log("level.save_monsters: %s" % level.save_monsters) cur = levels.current levels.list[cur] = field.fld, level.down, level.up field.load_map("local") if conf.xmax<10: field.blank() conf.mode = "tactical" level.populate(am_kind) while True: loc = field.random() if not field.next_to(loc, "wall"): break level.hoplite.place(loc) # very rarely a hero may be placed next to a wall, for hero in [level.fencer, level.mage]: for x in range(100): max_dist = random.randint(1,8) rloc = field.random() if (field.distance(loc, rloc) <= max_dist) and not field.next_to(rloc, "wall"): break hero.place(rloc) field.full_display()
def enter_tactical(self, target): """Enter tactical mode.""" if self.team == "monsters": level.attacking_monster = level.monsters.index(self) am_kind = self.kind if target.team == "monsters": # log("attack() - target: %s %s" % (target.kind, target)) # log("attack() - level.monsters: %s" % str(level.monsters)) level.attacking_monster = level.monsters.index(target) am_kind = target.kind # log("level.attacking_monster: %s" % level.attacking_monster) level.save_monsters = deepcopy(level.monsters) # log("level.save_monsters: %s" % level.save_monsters) cur = levels.current levels.list[cur] = field.fld, level.down, level.up field.load_map("local") if conf.xmax<10: field.blank() conf.mode = "tactical" level.populate(am_kind) while True: loc = field.random() if not field.next_to(loc, "wall"): break level.hoplite.place(loc) # very rarely a hero may be placed next to a wall, for hero in [level.fencer, level.mage]: for x in range(100): max_dist = random.randint(1,8) rloc = field.random() if (field.distance(loc, rloc) <= max_dist) and not field.next_to(rloc, "wall"): break hero.place(rloc) field.full_display(self)
def down(self): """ Go down the stairs * save old level, check if lower level exists, load it if it does, * otherwise create it, change current level num """ if not field.contains(self.loc, "down"): field.text.append("Need a staircase to descend.") return cur = levels.current levels.list[cur] = field.fld cur += 1 levels.current = cur if levels.list[cur]: field.fld, down, up = levels.list[cur] else: field.blank() for ltype, lnumbers in conf.level_types.items(): if cur in lnumbers: field.load_map( ltype.replace('_', ' ') ) break level.populate() self.place(level.up) field.full_display()
def down(self): """ Go down the stairs * save old level, check if lower level exists, load it if it does, * otherwise create it, change current level num """ if not field.contains(self.loc, "down"): field.text.append("Need a staircase to descend.") return cur = levels.current levels.list[cur] = field.fld cur += 1 levels.current = cur if levels.list[cur]: field.fld, down, up = levels.list[cur] else: field.blank() for ltype, lnumbers in conf.level_types.items(): if cur in lnumbers: field.load_map( ltype.replace('_', ' ') ) break level.populate() self.place(level.up) field.full_display(self)
def main(self): """ Main loop. Create hero's team; move them; create new levels when old ends. """ level_num = 1 field.load_map("empty") level.populate() # make hero's party level.hero = Being('party', field.random(), level.last_index) level.last_index += 1 level.hero.place() level.hoplite = Being("hoplite", Loc(1,1), level.last_index) level.last_index += 1 level.fencer = Being("fencer", Loc(1,2), level.last_index) level.last_index += 1 level.mage = Being("mage", Loc(1,3), level.last_index) level.last_index += 1 field.full_display() # game loop while True: time.sleep(0.01) monster = level.hero.find_closest_monster() if conf.mode == "strategical": party = [level.hero] elif conf.mode == "tactical": party = [level.hoplite, level.fencer, level.mage] level.hoplite.heal() level.fencer.heal() level.mage.heal() all_near_wall = True for cur_being in party: if not cur_being.alive: continue if not level.monsters and conf.mode == "tactical" and not test: break if cur_being.program: log("- - cur being (%s) has a program.." % cur_being.kind) times, direction = cur_being.program ok = cur_being.move(direction) # program may have been reseted in being.move() if cur_being.program: log("- - cur being STILL has a program..") times -= 1 if (not ok) or (times < 1): # bumped into something or end of program cur_being.program = None log("- - resetting program because bumped into something?..") else: cur_being.program = times, direction else: health_bar() # log("- - cur being (%s) has NO program..\n" % cur_being.kind) loc = cur_being.loc # log("kind: %s, x: %d y: %d;\n" % (cur_being.kind, x, y)) field.display() field.scr.move(loc.y-1, loc.x-1) c = field.scr.getch() if c in keymap.keys(): exec(commands[keymap[c]] + '()') elif 49 <= c <= 57: # e.g. '5l' moves 5 times to the left cur_being.move_program(c-48) move_res = cur_being.move(c) if type(move_res) == type(cur_being): # live monster monster = move_res # cur_being.attack(monster) if conf.mode == "strategical" and random.random() < ask_chance and not monster.asked: monster.ask(cur_being) else: cur_being.attack(monster) # check if all party is near a wall in tactical mode: if conf.mode == "tactical": all_near_wall = True party = [level.hoplite, level.fencer, level.mage] for hero in party: log("checking hero: %s" % (hero.kind)) if not hero.next_to("wall"): log("hero: %s is NOT close to a wall." % (hero.kind)) all_near_wall = False break else: log("hero: %s IS close to a wall." % (hero.kind)) hero.advance() if all_near_wall or conf.auto_combat: break field.msg() log("loop: all_near_wall: %s" % all_near_wall) if ((not level.monsters or all_near_wall or conf.auto_combat) and conf.mode == "tactical" and not test): we_won = False if not level.monsters or conf.auto_combat: we_won = True cur = levels.current field.fld, level.down, level.up = levels.list[cur] conf.mode = "strategical" level.monsters = level.save_monsters # killed the monster team.. level.hero.program = None attacker = level.monsters[level.attacking_monster] if we_won: attacker.die() else: # we need to try to move away from the enemy (running away!!) dir = field.get_rev_dir(level.hero.location, attacker.location) level.hero.move(dir) conf.auto_combat = False field.full_display() else: move(level_num) field.display() field.msg()
def main(self): """ Main loop. Create hero's team; move them; create new levels when old ends. """ level_num = 1 self.init_level() # game loop while True: time.sleep(0.01) monster = level.hero.find_closest_monster() if conf.mode == "strategical": party = [level.hero] elif conf.mode == "tactical": party = [level.hoplite, level.fencer, level.mage] level.hoplite.heal() level.fencer.heal() level.mage.heal() all_near_wall = True for cur_being in party: if not cur_being.alive: continue if not level.monsters and conf.mode == "tactical" and not test: break if cur_being.program: cur_being.handle_program() else: self.manual_move(cur_being) # check if all party is near a wall in tactical mode: if conf.mode == "tactical": all_near_wall = self.tactical_wall_check() if all_near_wall or conf.auto_combat: break field.msg() log("loop: all_near_wall: %s" % all_near_wall) if (not level.monsters or all_near_wall or conf.auto_combat) and conf.mode == "tactical" and not test: we_won = not level.monsters or conf.auto_combat cur = levels.current field.fld, level.down, level.up = levels.list[cur] conf.mode = "strategical" level.monsters = level.save_monsters # killed the monster team.. level.hero.program = None attacker = level.monsters[level.attacking_monster] if we_won: attacker.die() else: # we need to try to move away from the enemy (running away!!) dir = field.get_rev_dir(level.hero.location, attacker.location) level.hero.move(dir) conf.auto_combat = False field.full_display(level.party) else: self.move(level_num) field.full_display(level.hero) field.msg()