-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (96 loc) · 4.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from player import Player
import room
from npc import Alchemist, Wizard, Imp
from items import Glowing_Mushroom, Spellbook
# Create instance of player character
player = Player("Hero")
# Create instances of NPCs
alchemist = Alchemist("Alchemist", "An alchemist brewing a curious mixture in a cauldron, he seems overwhelmed.", "Quick! Pass me that Glowing Mushroom over there. I need to complete this batch of potion.", "glowing mushroom", "healing potion")
wizard = Wizard("Wizard", "A wizard dressed in long robes, he is searching the shelves for something.", "My spell backfired and I lost control of my summoned imp. It is locked in my quarters. I need to find my Spellbook to banish the fiend.", "spellbook", "magical sword")
imp = Imp("Imp", "A small fiend surrounded by an aura of fire.", "The imp speaks in a demonic tongue that you can't understand.")
# Assign NPCs to rooms
room.rooms["apothecary"]["npc"] = alchemist
room.rooms["library"]["npc"] = wizard
room.rooms["wizards_quarters"]["npc"] = imp
# Create instances room items
glowing_mushroom = Glowing_Mushroom("Glowing Mushroom", "A bioluminescent fungus that has a green glow. It has alchemical properties.")
spellbook = Spellbook("Spellbook", "A tome of arcane sigils and incantations.")
# Assign items to rooms
room.rooms["apothecary"]["items"] = [glowing_mushroom]
room.rooms["library"]["items"] = [spellbook]
# Create game loop to include each method
def game_loop():
# Print room description only when player enters a new room
last_room = None
while True:
if player.current_room != last_room:
print(f"{room.rooms[player.current_room]['description']}")
last_room = player.current_room
command = input("> ").lower().split() # Displays > and awaits case-insensitive user input
if len(command) == 0:
continue
if command[0] in ["move"]:
if len(command) > 1:
player.move(command[1])
else:
print("Specify direction to move.")
elif command[0] == "search":
player.search()
elif command[0] in ["pickup", "pick"]:
if len(command) > 1:
# Check if item to pick up is item name specified
if command[0] == "pick" and command[1] == "up":
if len(command) > 2:
item_name = " ".join(command[2:])
else:
print("Specify item to pick up.")
continue
else:
item_name = " ".join(command[1:])
item_name = item_name.lower()
player.pickup(item_name)
else:
print("Specify item to pick up.")
elif command[0] == "inspect":
npc = room.rooms[player.current_room].get("npc")
if npc:
npc.inspect()
else:
print("There is no one else here.")
elif command[0] == "speak":
npc = room.rooms[player.current_room].get("npc")
if npc:
npc.speak()
else:
print("There is no one to talk to here.")
elif command[0] == "trade":
npc = room.rooms[player.current_room].get("npc")
if npc:
# Check if NPC is able to trade
if hasattr(npc, 'trade'):
npc.trade(player)
else:
print(f"You can't trade with {npc.name}.")
else:
print("There is no one to trade with here.")
elif command[0] == "inventory":
print(f"Inventory: {', '.join(item.item_name for item in player.inventory) if player.inventory else 'Empty'}")
elif command[0] == "attack":
if len(command) > 1:
# Check if enemy to attack is enemy name specified
enemy_name = " ".join(command[1:])
player.attack(enemy_name)
else:
print("Specify an enemy to attack.")
elif command[0] == "quit":
confirm = input("End adventure? (yes/no): ")
if confirm == "yes":
print("Farewell.")
break
else:
print("Continue adventure.")
else:
print("Invalid command.")
# Run game
if __name__ == "__main__":
game_loop()