-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdungeon_game.py
More file actions
122 lines (100 loc) · 4.03 KB
/
dungeon_game.py
File metadata and controls
122 lines (100 loc) · 4.03 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
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
117
118
119
120
121
122
import random
MATRIX_SIZE = 5
chosen_cells = dict()
def initialize_locations():
monster = get_random_unique_location("MONSTER", MATRIX_SIZE, MATRIX_SIZE)
door = get_random_unique_location("DOOR", MATRIX_SIZE, MATRIX_SIZE)
start = get_random_unique_location("PLAYER", MATRIX_SIZE, MATRIX_SIZE)
# return monster, door, start
return monster, door, start
def get_random_unique_location(label, x, y):
candidate_x = random.randint(0, x - 1)
candidate_y = random.randint(0, y - 1)
# First entry. Proceed.
if len(chosen_cells.keys()) == 0:
chosen_cells[label] = (candidate_x, candidate_y)
else:
locations = []
for key in chosen_cells.keys():
locations.append(chosen_cells[key])
for location in locations:
while (location[0] == candidate_x) and (location[1] == candidate_y):
candidate_x = random.randint(0, x - 1)
candidate_y = random.randint(0, y - 1)
chosen_cells[label] = (candidate_x, candidate_y)
def move_player(location, move):
current_location_x = location[0]
current_location_y = location[1]
if move == 'L':
chosen_cells['PLAYER'] = (current_location_x, current_location_y - 1)
if move == 'R':
chosen_cells['PLAYER'] = (current_location_x, current_location_y + 1)
if move == 'U':
chosen_cells['PLAYER'] = (current_location_x - 1, current_location_y)
if move == 'D':
chosen_cells['PLAYER'] = (current_location_x + 1, current_location_y)
if move == 'S':
chosen_cells['PLAYER'] = (current_location_x, current_location_y)
def move_monster(location, move):
current_location_x = location[0]
current_location_y = location[1]
if move == 'L':
chosen_cells['MONSTER'] = (current_location_x, current_location_y - 1)
if move == 'R':
chosen_cells['MONSTER'] = (current_location_x, current_location_y + 1)
if move == 'U':
chosen_cells['MONSTER'] = (current_location_x - 1, current_location_y)
if move == 'D':
chosen_cells['MONSTER'] = (current_location_x + 1, current_location_y)
if move == 'S':
chosen_cells['MONSTER'] = (current_location_x, current_location_y)
def get_moves(location):
MOVES = ['L', 'R', 'U', 'D', 'S']
if location[1] == 0:
MOVES.remove('L')
if location[0] == 0:
MOVES.remove('U')
if location[1] == MATRIX_SIZE - 1:
MOVES.remove('R')
if location[0] == MATRIX_SIZE - 1:
MOVES.remove('D')
return MOVES
def print_matrix():
player_location = chosen_cells['PLAYER']
door_location = chosen_cells['DOOR']
monster_location = chosen_cells['MONSTER']
MATRIX = [[0 for x in range(MATRIX_SIZE)] for x in range(MATRIX_SIZE)]
MATRIX[player_location[0]][player_location[1]] = 1
MATRIX[door_location[0]][door_location[1]] = 2
MATRIX[monster_location[0]][monster_location[1]] = 3
for x in list(range(MATRIX_SIZE)):
for y in list(range(MATRIX_SIZE)):
print("{} ".format(MATRIX[x][y]), end="")
print("\n", end="")
# Start out by assigning the locations.
initialize_locations()
print(chosen_cells)
print("Welcome to the dungeon!")
while True:
print("You're currently in room {}".format(chosen_cells['PLAYER']))
print_matrix()
print("You can move {}".format(get_moves(chosen_cells['PLAYER'])))
print("Enter Q to quit.")
move = input("> ")
move = move.upper()
if move == 'Q':
break
if move in get_moves(chosen_cells['PLAYER']):
move_player(chosen_cells['PLAYER'], move)
monster_moves = get_moves(chosen_cells['MONSTER'])
monster_move = monster_moves[random.randint(0, len(monster_moves) - 1)]
move_monster(chosen_cells['MONSTER'], monster_move)
else:
print("Sorry, I don't undestand {}".format(move))
continue
if chosen_cells['PLAYER'] == chosen_cells['DOOR']:
print("You've escaped!")
break
if chosen_cells['PLAYER'] == chosen_cells['MONSTER']:
print("You were eaten by a monster! O noes!")
break