def new_game(name): """ Creates and populates a new map, including the player, and begins the game. """ # initializes the first floor of the dungeon globals.map = dungeon.assign_map(MAP_WIDTH, MAP_HEIGHT, MAX_FEATURES, ROOM_CHANCE, DOOR_CHANCE, globals.map) # initializes the player object (mobile) globals.player = objs.Mobile("@", "burnsand", 0, 0, name, description="You") globals.player.slots = { "head": None, "neck": None, "shoulders": None, "hands": None, "wield": None, "offhand": None, "chest": None, "waist": None, "legs": None, "feet": None, } # initializes list of mobiles and adds the player (will be mobiles later) globals.mobiles = [] globals.mobiles.append(globals.player) globals.things = [] populate_map() utils.message("Welcome to Steam Rogue! Good luck traversing the pits!", "seagull") play_game()
def load_game(): """ Loads pertinent data from a saved game. """ file = shelve.open("savegame.txt", "r") globals.map = file["map"] globals.mobiles = file["mobiles"] globals.things = file["things"] globals.player = globals.mobiles[file["player_index"]] globals.game_msgs = file["game_msgs"] file.close() utils.message("Welcome back!", "white") play_game()
def play_game(): """ Main game function. Controls everything else. """ while True: globals.player.calculate_fov() render_messages() render_window() for mobile in globals.mobiles: mobile.check_for_items() if mobile is not globals.player: mobile.take_turn() else: turn_taken = False while turn_taken == False: turn_taken = handle_keys() if globals.player.dead == True: utils.message("Game over! Try Again!") render_messages() pyc.waitforkeypress() pyc.waitforkeypress() main_menu() break render_messages()
def game_menu(title, choices): """ Presents a list of items and returns a choice. """ char = "a" cy = 10 options = {} win.fill(" ", fgcolor="black", region=(30, 5, SCREEN_WIDTH - 60, SCREEN_HEIGHT - 10)) win.write(title, x=35, y=7) if len(choices) < 26: for choice in choices: options.update({char: choice}) win.write(char + ": " + choice.name, 35, cy) cy = cy + 1 char = chr(ord(char) + 1) else: # TODO handle lists bigger than 26 items. utils.message("Too many items. Will be fixed later.") return "None" win.write("*: Exit", 35, cy) cy = cy + 1 win.update() pyc.waitforkeypress() while True: ans = pyc.waitforkeypress() if ans is not None: ans = ans.lower() if ans in options: render_window() win.update() return options[ans] elif ans == "*": render_window() win.update() return "None" else: win.write("That is not a viable option.", 35, cy) win.update()
def handle_keys(): """ Waits for a key to be pressed and handles it. Returns False if an action has not been taken, True if it has. This will be replaced by a time system later. """ while True: for event in pygame.event.get(): # exits the game if need be if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() # handles keyboard input elif event.type == KEYDOWN: keys = pygame.key.get_pressed() if keys[K_KP8] or keys[K_UP]: return globals.player.move_or_interact(0, -1) if keys[K_KP9]: return globals.player.move_or_interact(1, -1) if keys[K_KP6] or keys[K_RIGHT]: return globals.player.move_or_interact(1, 0) if keys[K_KP3]: return globals.player.move_or_interact(1, 1) if keys[K_DOWN] or keys[K_KP2]: return globals.player.move_or_interact(0, 1) if keys[K_KP1]: return globals.player.move_or_interact(-1, 1) if keys[K_LEFT] or keys[K_KP4]: return globals.player.move_or_interact(-1, 0) if keys[K_KP7]: return globals.player.move_or_interact(-1, -1) if keys[K_KP5]: return globals.player.pass_turn() if (keys[K_LSHIFT] or keys[K_RSHIFT]) and keys[K_s]: save_game() utils.message("Game saved... press any key to" + " return to the main menu.") render_window() pyc.waitforkeypress() pyc.waitforkeypress() main_menu() # If "<" and on an up staircase if ((keys[K_LSHIFT] or keys[K_RSHIFT]) and keys[K_COMMA]) and globals.map[globals.player.x][ globals.player.y ].char == "<": utils.message("Hit 'Esc' if you want to exit the game.") return False # If ">" and on a down staircase if ((keys[K_LSHIFT] or keys[K_RSHIFT]) and keys[K_PERIOD]) and globals.map[globals.player.x][ globals.player.y ].char == ">": utils.message("Other floors are currently " + " under construction") return False # If "," or "g" attempt to pick up an item if keys[K_COMMA] or keys[K_g]: if len(globals.player.items_on_tile) == 0: utils.message("There's nothing there.") return False elif len(globals.player.items_on_tile) > 1: my_choice = game_menu("Choose an item to pick up.", globals.player.items_on_tile) if my_choice == "None": utils.message("Well fine then.") else: return globals.player.pick_up_item(my_choice) else: for thing in globals.things: if thing.x == globals.player.x and thing.y == globals.player.y: item = thing return globals.player.pick_up_item(item) # if "d", attempt to drop an item if keys[K_d]: if len(globals.player.inventory) == 0: utils.message("You have no items to drop.") return False else: my_choice = game_menu("Choose an item to drop.", globals.player.inventory) if my_choice == "None": utils.message("Well fine then.") else: return globals.player.drop_item(my_choice) # If "i", open inventory screen if keys[K_i]: if len(globals.player.inventory) == 0: utils.message("Inventory is Empty.") return False else: my_choice = game_menu("Inventory", globals.player.inventory) if my_choice == "None": utils.message("Okay.") else: utils.message(my_choice.description) utils.message(my_choice.name) return False # If "u", attempt to use an item if keys[K_u]: if len(globals.player.inventory) == 0: utils.message("You don't have any items to use!") return False else: my_choice = game_menu("Inventory", globals.player.inventory) if my_choice == "None": utils.message("Okay.") return False else: globals.player.use_item(my_choice) return True # If "w", attempt to wield a weapon if keys[K_w]: if len(globals.player.inventory) == 0: utils.message("You don't have any items to equip!") else: my_choice = game_menu("Choose an item to equip.", globals.player.inventory) if my_choice == "None": utils.message("Okay.") return False else: return globals.player.wield_item(my_choice) # If "e", attempt to equip some armor if keys[K_e]: if len(globals.player.inventory) == 0: utils.message("You don't have any items to equip!") else: my_choice = game_menu("Choose an item to equip.", globals.player.inventory) if my_choice == "None": utils.message("Okay.") return False else: return globals.player.equip_item(my_choice) pygame.event.clear() keys = []