def main_loop(self): while not libtcod.console_is_window_closed(): self.render_all() should_exit = self.handle_keys() if should_exit: break
def run(self): while not libtcod.console_is_window_closed(): self.screen.display(self.con) for obj in self.map.objects: obj.clear(self.con) #handle keys and exit game if needed action = self.handle_keys() if action == 'exit': break if action is not None: pass
def is_closed(self): """ Tells if user closes window. Curses has no 'windows' interface for the user to close via some button, external to the mechanisms of the game itself (as in other UIs, where maybe an X button or Alt-F4 key combination allows to close the program), so it always returns False. Return: boolean telling if UI has been closed by the user. """ return libtcod.console_is_window_closed()
def _interpret_event(self, event): if libtcod.console_is_window_closed(): return Key.CLOSE_WINDOW elif event.vk in self.key_map: return self.key_map[event.vk] elif event.vk == libtcod.KEY_CHAR: if event.c == ord('c') and event.lctrl or event.rctrl: raise KeyboardInterrupt ch = chr(event.c) if event.lctrl or event.rctrl: ch = "^" + ch if event.lalt or event.ralt: ch = "!" + ch return ch else: return Key.NO_INPUT
def run (self): """ Main loop """ while not libtcod.console_is_window_closed() and not hasattr(self, 'stopped'): # Draw self.draw() # Handle keys turn = self.input() # If a turn has not been take, continue if not turn: continue # Think for object in self.map.objects: if hasattr(object, 'think'): object.think()
def mainLoop(self): """ Main loop of game. """ play = True # The game started while play: self.level.draw(self.console) # Draw level on self.console self.ui.draw(self.console) # Draw UI self.console.flush() # Flush console key = self.console.rkey() # Read one key # From the console if key.c == 0: # If no key was pressed for cell in self.level.redrawcells: # Then redraw animated self.level.drawCell(cell[0], cell[1], cell[2], cell[3], False) # cells else: move = self.console.keyparse(key) # Ask keyparser for move if move: # Player has moved... self.player.move(move) elif chr(key.c) == "x": # ...Or hasn't. # Build something self.level[self.player.x][self.player.y].setType(False, False) # Update cell self.level.update((self.player.x, self.player.y)) elif chr(key.c) == "z": # Or dig coords = self.console.askDirection() # Get direction if coords: # Keyparser returned coordinates (x, y) = coords (x, y) = (x + self.player.x, y + self.player.y) if self.level[x][y].isWall() and self.level[x][y].isDiggable(): # If you can dig that wall self.level[x][y].setType(True, True) # Dig it self.level.update((x, y)) # And update elif chr(key.c) == "m": # Show map for one _turn_ self.level.draw(self.console, forced=True) # Draw level self.console.flush() # Ignoring FOV. self.console.readKey() # And wait for keypress elif chr(key.c) == "q": # Quit play = False elif chr(key.c) == "S": self.save() elif chr(key.c) == "L": self.console.cprint((5, 5), "%d" % self.level.num) self.console.flush() self.console.readKey() elif chr(key.c) == ">": if self.level[self.player.x][self.player.y].retChar() == ">": if self.level.num + 1 >= len(self.dungeon.levels): self.dungeon.levels.append( Level((self.noise.getNoise, self.noise.getNoise1d), self.rng.rand, self.dungeon) ) self.level = self.dungeon.levels[self.player.level.num + 1] self.level.setPlayer(self.player) self.player.level = self.level for x in range(self.level.w + 1): for y in range(self.level.h + 1): if self.level[x][y].retChar() == "<": self.player.move((x, y), True, False) break elif chr(key.c) == "<": if self.level[self.player.x][self.player.y].retChar() == "<": if self.level.num != 0: self.level = self.dungeon.levels[self.player.level.num - 1] self.level.setPlayer(self.player) self.player.level = self.level for x in range(self.level.w + 1): for y in range(self.level.h + 1): if self.level[x][y].retChar() == ">": self.player.move((x, y), True, False) break for x in range(self.level.w): for y in range(self.level.h): if self.level[x][y].mob != None: self.level[x][y].mob.ai() # This line is useful for stress testing the game. # self.player.move((random.randint(-1,1),random.randint(-1,1))) # End game if window is closed play = not libtcod.console_is_window_closed() and play def load(): pass def save(): pass
def main(): # setup screen ltc.console_set_custom_font('lucida10x10_gs_tc.png', ltc.FONT_TYPE_GREYSCALE | ltc.FONT_LAYOUT_TCOD) ltc.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/ltc tutorial', fullscreen=False) ltc.sys_set_fps(LIMIT_FPS) con = ltc.console_new(LEVEL_WIDTH, LEVEL_HEIGHT) gui_panel = ltc.console_new(PANEL_WIDTH, PANEL_HEIGHT) # setup level leveldata, start_pos = leveltools.make_level(LEVEL_WIDTH, LEVEL_HEIGHT) fov_recompute = True # setup player leveldata.player = rlplayer.create_player(start_pos[0], start_pos[1]) leveldata.objects.insert(0, leveldata.player) rlmobs.populate_enemies(leveldata) rlitems.populate_items(leveldata) src.rlmsglog.m('Welcome to the dungeon!') # game state game_state = GS_PLAYING # MAIN LOOP while not ltc.console_is_window_closed(): # get user input mouse = ltc.Mouse() key = ltc.Key() ltc.sys_check_for_event(ltc.EVENT_KEY_PRESS | ltc.EVENT_MOUSE, key, mouse) if fov_recompute: leveldata.recompute_fov(TORCH_RADIUS) # draw level draw_level(con, leveldata) # noinspection PyTypeChecker draw_gui(gui_panel, con, leveldata, mouse) # display level ltc.console_blit(con, 0, 0, LEVEL_WIDTH, LEVEL_HEIGHT, 0, 0, 0) ltc.console_blit(gui_panel, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y) ltc.console_flush() clear_console(con) clear_console(gui_panel) # handle user input # noinspection PyTypeChecker player_taken_turn = handle_input(key, mouse, game_state, leveldata, con) if should_exit: break # all objects take their turn - player is first if game_state == GS_PLAYING and player_taken_turn: fov_recompute = True for o in leveldata.objects: o.take_turn(leveldata) if not leveldata.player.fighter.is_alive(): game_state = GS_DEAD
#Item ('silver revolver', x, y, 'r', libtcod.cyan, gun=Gun(15, 0.9)) # Contains a list of items on the player's current tile tile_items = list() # This is the first run of the game. firstRun = True turncount = 0 ## Changes the keyboard repeat delay. # libtcod.console_set_keyboard_repeat(0, 0) ############### ## Game loop ## ############### while not libtcod.console_is_window_closed (): if not firstRun: key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) #key = libtcod.console_wait_for_keypress(True) else: # If this is the first run of the game, then build a game world and place # the player on it. key = libtcod.console_check_for_keypress() # M.gameworld = [[Tile(floor=False) for y in range (M.MAP_HEIGHT)] # for x in range (M.MAP_WIDTH)] bsp = bspgen.Bsp(M.MAP_WIDTH,M.MAP_HEIGHT, M.gameworld) bsp.render_bsp() place_player() add_items() S.display_status() R.render()