Skip to content
Snippets Groups Projects
Select Git revision
  • 1462dc9a46e23f8f788efd15eaa1f2a63636b4ed
  • main default protected
2 results

MakeOptDvsRPlot.m

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    game.py 14.80 KiB
    #!/usr/bin/python3
    
    from map import rooms
    from player import *
    from items import *
    from gameparser import *
    from enemies import *
    import random
    
    def display_inventory():
        while True:
            print("Weapon equipped:", player.get_equipped_name("weapon"))
            print("Defense equipped:", player.get_equipped_name("defense"))
            print("Magic item equipped:", player.get_equipped_name("magic"))
            print("")
    
            if len(player.inventory) > 0:
                print("You have ", end = "")
                for x in range(len(player.inventory)-1):
                    print(player.inventory[x].name, end = ", ")
                print(player.inventory[len(player.inventory)-1].name, end = ".")
                print("\n")
    
            print("The commands you can use are:")
    
            for item in player.inventory:
                print("INSPECT", item.id.upper(), "to inspect", item.name)
    
            for category in player.equipped:
                item = player.equipped[category]
                if item == None: continue
                print("INSPECT", item.id.upper(), "to inspect", item.name)
    
            for item in player.inventory:
                if item.equippable == True:
                    print("EQUIP", item.id.upper(), "to equip", item.name)
            
            for category in player.equipped:
                item = player.equipped[category]
                if item == None: continue
                print("UNEQUIP", item.id.upper(), "to unequip", item.name)
    
            print("EXIT to exit the inventory sub-menu.")
    
            user_input = input(">")
            user_input = normalise_input(user_input)
            
            if user_input[0] == "inspect":
                if len(user_input) == 1:
                    print("You must specify something to inspect.")
                else:
                    player.inspect_item(user_input[1])
    
            if user_input[0] == "equip":
                if len(user_input) == 1:
                    print("You must specify something to equip.")
                else:
                    player.equip(user_input[1])
    
            if user_input[0] == "unequip":
                if len(user_input) == 1:
                    print("You must specify something to unequip.")
                else:
                    player.unequip(user_input[1])
    
            if user_input[0] == "exit":
                break
        
    def display_map():
        # display mini-map - uncleared rooms do not display names, just question marks
        print("You inspect your map, here is what the relevant part says.")
        print("Question marks represent rooms you haven't cleared yet.\n\n")
    
        start_point = player.current_room
        information = {
            "current": {
                "exists": True,
                "name": start_point.name,
            }
        }
    
        # gather information about rooms we need to print
        for direction in ["north", "east", "south", "west"]:
            if direction in start_point.exits:
                exists = True
            else:
                exists = False
    
            information[direction] = {
                "exists": exists,
            }
    
            if exists == True:
                room_directed_to_id = start_point.exits[direction]
                room = rooms[room_directed_to_id]
    
                if room.cleared == True:
                    information[direction]["name"] = room.name
                else:
                    information[direction]["name"] = "?????"
    
        # print the information
        north_name_offset = 0
        north_arrow_offset = len(information["current"]["name"]) // 2
        if information["west"]["exists"] == True and information["north"]["exists"] == True:
            north_name_offset = 8 + len(information["west"]["name"]) + len(information["current"]["name"]) // 2 - len(information["north"]["name"]) // 2
            north_arrow_offset = 8 + len(information["west"]["name"]) + len(information["current"]["name"]) // 2
    
        if information["north"]["exists"] == True:
            print(" " * north_name_offset + information["north"]["name"])
    
            print(" " * north_arrow_offset + "^")
            for i in range(2):
                print(" " * north_arrow_offset + "|")
    
        middle_print_line = ""
        if information["west"]["exists"] == True: 
            middle_print_line = middle_print_line + information["west"]["name"] + " <----- " 
    
        middle_print_line += information["current"]["name"]
    
        if information["east"]["exists"] == True:
            middle_print_line = middle_print_line + " -----> " + information["east"]["name"] 
    
        print(middle_print_line)
    
        south_name_offset = 0
        south_arrow_offset = len(information["current"]["name"]) // 2
        if information["west"]["exists"] == True and information["south"]["exists"] == True:
            south_name_offset = 8 + len(information["west"]["name"]) + len(information["current"]["name"]) // 2 - len(information["south"]["name"]) // 2
            south_arrow_offset = 8 + len(information["west"]["name"]) + len(information["current"]["name"]) // 2
    
        if information["south"]["exists"] == True:
            for i in range(2):
                print(" " * south_arrow_offset + "|")
            print(" " * south_arrow_offset + "v")
            
            print(" " * south_name_offset + information["south"]["name"])
    
        input("\n\n> Press any key to continue.")
    
    def display_help():
        pass
    
    def combat(enemy):
        print("You have encountered a level " + str(enemy.level), str(enemy.name) + "!")
    
        while True:
            print(enemy.name + " has " + str(enemy.hp) + " hp remaining.")
            print("")
            print("You have " + str(player.hp) + " hp remaining.\n")
            
            print("What would you like to do?")
            print("The commands you can use are:")
            print("ATTACK to perform a normal attack.")
            print("ATTACK STRONG to perform a strong attack with a chance to miss.")
            print("ATTACK QUICK to perform a quick attack.")
            print("INVENTORY to view your inventory, to switch weapons, inspect weapons, use items etc.")
            
            user_input = input(">")
            user_input = normalise_input(user_input)
    
            if user_input[0] == "inventory":
                display_inventory()
    
            elif user_input[0] == "attack":
                # normal attack
                if len(user_input) == 1:
                    print("You attacked using a normal attack!")
    
                    missed = player.check_attack_missed()
                    if missed == True:
                        print("Your attack missed. You did 0 damage!")
    
                    else:
                        attack_damage, critical = player.get_damage()
                        damage_to_do = enemy.apply_defense(attack_damage)
    
                        if critical == True:
                            print("Critical hit! Your attack did " + str(damage_to_do) + " damage!")
                        else:
                            print("Your attack did " + str(damage_to_do) + " damage!")
                        
                        enemy.take_damage(damage_to_do)
                        took_dura = player.take_attack_wear(2)
                        if took_dura: print("Your attack spent 2 durability!")
    
                # strong attack does more damage, but costs more durability, and has higher chance of missing
                elif len(user_input) > 1 and user_input[1] == "strong":
                    print("You attacked using a strong attack!")
                    
                    missed = player.check_attack_missed(1.25)
                    if missed == True:
                        print("Your attack missed. You did 0 damage!")
    
                    else:
                        attack_damage, critical = player.get_damage(1.25)
                        damage_to_do = enemy.apply_defense(attack_damage)
    
                        if critical == True:
                            print("Critical hit! Your attack did " + str(damage_to_do) + " damage!")
                        else:
                            print("Your attack did " + str(damage_to_do) + " damage!")
                        
                        enemy.take_damage(damage_to_do)
                        took_dura = player.take_attack_wear(3)
                        if took_dura: print("Your attack spent 3 durability!")
    
                # quick attack does a series of smaller attacks
                elif len(user_input) > 1 and user_input[1] == "quick":
                    print("You attacked using a series of quick attacks!")
                    for i in range(3):
                        missed = player.check_attack_missed(0.3)
                        if missed == True:
                            print("Your attack missed. You did 0 damage!")
    
                        else:
                            attack_damage, critical = player.get_damage(0.3)
                            damage_to_do = enemy.apply_defense(attack_damage)
    
                            if critical == True:
                                print("Critical hit! Your attack did " + str(damage_to_do) + " damage!")
                            else:
                                print("Your attack did " + str(damage_to_do) + " damage!")
                            
                            enemy.take_damage(damage_to_do)
                            took_dura = player.take_attack_wear(1)
                            if took_dura: print("Your attack spent 1 durability!")
    
                else:
                    print("Could not perform that type of attack.")
    
                # check if you killed enemy
                if enemy.alive == False:
                    print("You defeated the " + enemy.name + "!\n")
                    break
    
                # now calculate the enemies attack
                print("")
                print("The " + enemy.name + " will now attack.")
    
                enemy_attack_damage, critical = enemy.get_attack()
                damage_to_do = player.apply_defense(enemy_attack_damage)
    
                if critical == True:
                    print("Critical hit! The enemy did " + str(damage_to_do) + " damage!")
                else:
                    print("The enemy did " + str(damage_to_do) + " damage!")
    
                player.take_damage(damage_to_do)
    
                # check if you died
                if player.alive == False:
                    print("You died!\n")
                    break
            
            else:
                print("Could not recognise that command.")
    
    def puzzle():
        pass
    
    def chest():
        pass
    
    def boss_battle():
        pass
        
    def list_of_items(items):
        if len(items) > 0:
            print("'", end = "")
            for x in range(len(items)-1):
                print(items[x]["name"], end = ", ")
            print(items[len(items)-1]["name"], end = "'")
        else:
            return ''
        
    def print_room_items(room):
        if len(room.items) > 0:
            print("There is ", end = "")
            for x in range(len(room.items)-1):
                print(room.items[x].name, end = ", ")
            print(room.items[len(room.items)-1].name, end = " ")
            print("here.")
            print("")
    
    def print_inventory_items(items):
        if len(items) > 0:
            print("You have ", end = "")
            for x in range(len(items)-1):
                print(items[x].name, end = ", ")
            print(items[len(items)-1].name, end = ".")
            print("\n")
    
    def print_room(room):
        print("")
        print(room.name)
        print(room.description + "\n")
        print_room_items(room)
    
    def exit_leads_to(exits, direction):
        return rooms[exits[direction]].name
    
    def print_exit(direction, leads_to):
        print("GO " + direction.upper() + " to " + leads_to + ".")
    
    def print_menu(exits, room_items, inv_items):
        print("The commands you can use are:")
        
        # Iterate over available exits
        for direction in exits:
            # Print the exit name and where it leads to
            print_exit(direction, exit_leads_to(exits, direction))
    
        for item in room_items:
            print("TAKE", item.id.upper(), "to take a", item.name + ".")
    
        for item in inv_items:
            print("DROP", item.id.upper(), "to drop your", item.name + ".")
        
        print("INVENTORY to view your inventory")
        print("MAP to view the map")
        print("HELP for help information")
    
    def execute_go(direction):
        possible_exits = player.current_room.exits
        if direction in possible_exits:
            new_room_name = possible_exits[direction]
            new_room = rooms[new_room_name]
            print("You are moving to", new_room.name)
            player.current_room = new_room
        else:
            print("You cannot go there.")
    
    def execute_take(item_id):
        for item in player.current_room.items:
            if item.id == item_id:
                player.inventory.append(item)
                player.current_room.items.remove(item)
                return
    
        print("You cannot take that")
    
    def execute_drop(item_id):
        for item in player.inventory:
            if item.id == item_id:
                player.inventory.remove(item)
                player.current_room.items.append(item)
                return
    
        print("You cannot drop that")
    
    def execute_command(command):
        """This function takes a command (a list of words as returned by
        normalise_input) and, depending on the type of action (the first word of
        the command: "go", "take", or "drop"), executes either execute_go,
        execute_take, or execute_drop, supplying the second word as the argument.
    
        """
    
        if len(command) == 0:
            return
    
        if command[0] == "go":
            if len(command) > 1:
                execute_go(command[1])
            else:
                print("Go where?")
    
        elif command[0] == "take":
            if len(command) > 1:
                execute_take(command[1])
            else:
                print("Take what?")
    
        elif command[0] == "drop":
            if len(command) > 1:
                execute_drop(command[1])
            else:
                print("Drop what?")
    
        elif command[0] == "inventory" or command[0] == "inv":
            display_inventory()
    
        elif command[0] == "map":
            display_map()
    
        elif command[0] == "help":
            display_help()
        
        else:
            print("This makes no sense.")
    
    def menu(exits, room_items, inv_items):
        # Display menu
        print_menu(exits, room_items, inv_items)
    
        # Read player's input
        user_input = input("> ")
    
        # Normalise the input
        normalised_user_input = normalise_input(user_input)
    
        return normalised_user_input
    
    def move(exits, direction):
        # Next room to go to
        return rooms[exits[direction]]
    
    # This is the entry point of our program
    def main():
        print("You suddenly wake up in an ancient abandoned manner house. Every window boarded, every door to the real world locked...")
        print("The old house finds itself inhabited by a plethora of strange monsters, of which many seem violent. It is up to you...")
        print("to trek forward and find a way out with what little you have - avoiding death as if it were around every corner.")
        input("> Press enter to begin the game.")
    
        # Main game loop
        while True:
            # Display game status (room description, inventory etc.)
            print_room(player.current_room)
            # print_inventory_items(player.inventory) # No longer display inventory, as player can see it in inventory sub-menu.
            if player.current_room.cleared == False:
                # filler, for now
                enemy = Orc("orc_1", 1)
                combat(enemy)
    
            # Show the menu with possible actions and ask the player
            command = menu(player.current_room.exits, player.current_room.items, player.inventory)
    
            # Execute the player's command
            execute_command(command)
            
    if __name__ == "__main__":
        main()