def run(): console.init() key = tcod.Key() mouse = tcod.Mouse() player = Player(int(variables.screen_width / 2), int(variables.screen_height / 2)) game_map = BoundedMap(80, 50) while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) console.update(player, game_map) action = input.handle_keys(key) move = action.get('move') quit_game = action.get('exit') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if quit_game: return True if key.vk == tcod.KEY_ESCAPE: return True
def play_game(): """ Main Loop """ player_action = None #Main Loop while not libtcod.console_is_window_closed(): render.render_all() libtcod.console_flush() map = gvar.game.player.currentmap() for object in map.objects: object.clear() if object.fighter is not None and libtcod.map_is_in_fov( gvar.foh_map, object.x, object.y) and not gvar.game.ticks.contains(object): object.fighter.join_battle() #handle death state if gvar.game.game_state == 'dead': player_action = input.handle_keys() if player_action == 'exit': save.save_game() break else: continue else: actor = gvar.game.ticks.getWithPriority() if actor[1] == gvar.game.player: #handle keys and exit game if needed player_action = input.handle_keys() if player_action == 'exit': save.save_game() main_menu() break gvar.game.ticks.put(actor[1], int(player_action + actor[0])) else: #let mobs take their turn if gvar.game.game_state == 'playing': if actor[1].ai: speed = actor[1].ai.take_turn() gvar.game.ticks.put(actor[1], int(speed + actor[0]))
def play_game(): """ Main Loop """ player_action = None #Main Loop while not libtcod.console_is_window_closed(): render.render_all() libtcod.console_flush() map = gvar.game.player.currentmap() for object in map.objects: object.clear() if object.fighter is not None and libtcod.map_is_in_fov(gvar.foh_map, object.x, object.y) and not gvar.game.ticks.contains(object): object.fighter.join_battle() #handle death state if gvar.game.game_state == 'dead': player_action = input.handle_keys() if player_action == 'exit': save.save_game() break else: continue else: actor = gvar.game.ticks.getWithPriority() if actor[1] == gvar.game.player: #handle keys and exit game if needed player_action = input.handle_keys() if player_action == 'exit': save.save_game() main_menu() break gvar.game.ticks.put(actor[1], int(player_action + actor[0])) else: #let mobs take their turn if gvar.game.game_state == 'playing': if actor[1].ai: speed = actor[1].ai.take_turn() gvar.game.ticks.put(actor[1], int(speed + actor[0]))
def play_game(game): while not libtcod.console_is_window_closed(): render.render_all(game) if game.state == 'pathing': if game.player.mover.takepath() == 'empty': game.state = 'playing' game.map_movement = True player_action = input.handle_keys(game) # If 'tookturn' is added, fov_recompute should be true. if player_action == "exit": break
def play_game(game): while not libtcod.console_is_window_closed(): render.render_all(game) #In the case of pathing, follow the path or switch back to normal playing when done. if game.state == 'pathing': if game.player.mover.takepath(game) == 'empty': game.state = 'playing' game.map_movement = True if game.map_movement: pass # Here we can check to see if player movement has occurred and see if an event should come up. player_action = input.handle_keys(game) # If 'tookturn' is added, fov_recompute should be true. if player_action == "exit": break
def main(): screen_width = 80 screen_height = 50 player_x = int(screen_width / 2) player_y = int(screen_height / 2) libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_put_char(0, player_x, player_y, '@', libtcod.BKGND_NONE) libtcod.console_flush() action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move player_x += dx player_y += dy if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = tcod.Key() mouse = tcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state) fov_recompute = False tcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', tcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): cursor = Entity(37, 30, 1, "+", C.EN_CURSOR, libtcod.white, "") cursor.visible = False player = Entity(37, 30, 1, "O", C.EN_HUMAN, libtcod.green, "Hrac") player.fighter.set_props(attack=10, defense=1, maxhp=100) npc = Entity(30, 30, 1, "X", C.EN_HUMAN, libtcod.white, "NPC") npc.fighter.set_props(attack=10, defense=1, maxhp=50) box = Entity(15, 15, 1, 254, C.EN_MOVABLE, libtcod.white, "Box") pot = Entity(30, 32, 1, '+', C.EN_ITEM, libtcod.white, "Healing Potion S") pot.item.set_props(hp=20, price=10, type=C.ITEM_CONSUM) #pot.item.add_to_inventory(player.inventory) sword = Entity(25, 30, 1, '?', C.EN_ITEM, libtcod.white, "Weak sword") sword.item.set_props(attack=20, price=10, type=C.ITEM_WEAP) sword2 = Entity(25, 30, 1, '?', C.EN_ITEM, libtcod.white, "Super sword") sword2.item.set_props(attack=30, price=10, type=C.ITEM_WEAP) armor = Entity(20, 30, 1, '?', C.EN_ITEM, libtcod.white, "Super Armor") armor.item.set_props(maxhp=20, defense=5, price=10, type=C.ITEM_ARMOR, requires={ 'lvl': 1, 'eyes': 2 }) # na 1. miste hrac, na -1. miste kurzor gv.entities = [player, box, pot, npc, sword, sword2, armor, cursor] event = EventChangeColor(box, libtcod.red, [1, 1, 1]) event2 = EventChangeChar(player, "V", [1, 1, 0.5, 1, 0.5]) gv.events = [event, event2] libtcod.console_set_custom_font( '../fonts/Cheepicus_15x15.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(gv.screen_width, gv.screen_height, 'libtcod tutorial revised', False, libtcod.RENDERER_OPENGL2, "F", True) c_map = libtcod.console.Console(gv.screen_width, gv.screen_height) c_inv = libtcod.console.Console(22, 22) c_info = libtcod.console.Console(22, 32) c_gmst = libtcod.console.Console(22, 3) cons = {'map': c_map, 'inv': c_inv, 'info': c_info, 'gmst': c_gmst} key = libtcod.Key() mouse = libtcod.Mouse() casovac = threading.Thread(target=thread_timer, daemon=True) casovac.start() while not libtcod.console_is_window_closed(): handle_events(gv.events, gv.timer) libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(cons, gv.entities, gv.game_map, gv.colors) libtcod.console_flush() clear_all(c_map, gv.entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') grab = action.get('grab') swap = action.get('swap') inv = action.get('inv') if grab: if gv.GAME_STATE == C.GS_PLAY: for i in gv.entities: if (i != player) and (i.type == C.EN_ITEM): if i.x == player.x and i.y == player.y: i.item.collect(player.inventory) elif gv.GAME_STATE == C.GS_INVENTORY: player.inventory.items[player.inventory.y].use_item( player, player) if swap: if gv.GAME_STATE == C.GS_PLAY: gv.GAME_STATE = C.GS_PAUSE cursor.visible = True elif gv.GAME_STATE == C.GS_PAUSE: gv.GAME_STATE = C.GS_PLAY cursor.visible = False cursor.x = player.x cursor.y = player.y cursor.h = player.h if inv: if gv.GAME_STATE == C.GS_INVENTORY: gv.GAME_STATE = C.GS_PLAY gv.redraw_map = True elif gv.GAME_STATE == C.GS_PLAY: gv.GAME_STATE = C.GS_INVENTORY if move: if gv.GAME_STATE == C.GS_PLAY: dx, dy, dh = move if player.move(dx, dy, dh): cursor.move(dx, dy, dh) #if dh != 0: #gv.redraw_map = True elif gv.GAME_STATE == C.GS_INVENTORY: dx, dy, dh = move player.inventory.move(dx, dy) elif gv.GAME_STATE == C.GS_PAUSE: dx, dy, dh = move cursor.move(dx, dy, dh) if exit: if gv.GAME_STATE != C.GS_PLAY: gv.GAME_STATE = C.GS_PLAY gv.redraw_map = True else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())