def main_menu(): """ Prints the main menu and starts a new game, loads a saved game or exits the game entirely. """ win.setscreencolors(None, "black", clear=True) win.write("Main Menu", x=41, y=10) win.write("A) New Game", x=30, y=12) win.write("B) Load Game", x=30, y=13) win.write("C) Exit", x=30, y=14) win.update() win.cursor = (1, 20) while True: ans = pyc.waitforkeypress() if ans is not None: ans = ans.upper() if ans == "A": name = "" while len(name) < 3: win.write("Please enter a name.", x=30, y=16) win.write(" ", x=30, y=17) win.cursor = (30, 17) name = win.input() name = name.title() new_game(name) elif ans == "B": try: load_game() except: win.write("No file to load.", x=30, y=16) continue elif ans == "C": pygame.quit() sys.exit() else: continue
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 = []