-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
60 lines (54 loc) · 2.07 KB
/
game.py
File metadata and controls
60 lines (54 loc) · 2.07 KB
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
from collections import OrderedDict
from player import Player
import world
def play():
print("--------------")
print("Undead Escape!")
print("--------------")
world.parse_world_dsl()
player = Player()
while player.is_alive() and not player.victory:
room = world.tile_at(player.x, player.y)
print(room.intro_text())
room.modify_player(player)
if player.is_alive() and not player.victory:
choose_action(room, player)
elif not player.is_alive():
print("You failed to survive....")
def choose_action(room, player):
action = None
while not action:
available_actions = get_available_actions(room, player)
action_input = input("Action: ")
action = available_actions.get(action_input)
if action:
action()
else:
print("Invalid action!")
def get_available_actions(room, player):
actions = OrderedDict()
print("----------------")
print("Choose an action: \n")
if player.inventory:
action_adder(actions, "i", player.print_inventory, "Display inventory")
if isinstance(room, world.TraderTile):
action_adder(actions, "t", player.trade, "Trade")
if isinstance(room, world.EnemyTile) and room.enemy.is_alive():
action_adder(actions, "a", player.attack, "Attack")
else:
if world.tile_at(room.x, room.y - 1):
action_adder(actions, "n", player.move_north, "Go north")
if world.tile_at(room.x, room.y + 1):
action_adder(actions, "s", player.move_south, "Go south")
if world.tile_at(room.x + 1, room.y):
action_adder(actions, "e", player.move_east, "Go east")
if world.tile_at(room.x - 1, room.y):
action_adder(actions, "w", player.move_west, "Go west")
if player.hp < 100:
action_adder(actions, "h", player.heal, "Heal \n")
return actions
def action_adder(action_dict, hotkey, action, name):
action_dict[hotkey.lower()] = action
action_dict[hotkey.upper()] = action
print("{}: {}".format(hotkey, name))
play()