-
Notifications
You must be signed in to change notification settings - Fork 0
/
dungeongame.py
150 lines (116 loc) · 3.61 KB
/
dungeongame.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Dungeon Game
Explore a dungeon, avoid the monster, and find a hidden door and escape.
Created March 2018
Updated April 2018
Author: George Ballard
"""
import os
import random
GAME_DIMENSIONS = (5, 5)
player = {'location': None, 'path': []}
def clear():
"""Clear the screen"""
os.system('cls' if os.name == 'nt' else 'clear')
def build_cells(width, height):
"""Create and return a 'width' x 'height' grid of two-tuples
>>> cells = build_cells(2,2)
>>> len(cells)
4
"""
cells = []
for y in range(height):
for x in range(width):
cells.append((x, y))
return cells
def get_locations(cells):
"""Randomly pick starting locations for the monster, the door,
and the player.
>>> cells = build_cells(2,2)
>>> m, d, p = get_locations(cells)
>>> m != d and d !=p
True
>>> d in cells
True
"""
monster = random.choice(cells)
door = random.choice(cells)
player = random.choice(cells)
if monster == door or monster == player or door == player:
monster, door, player = get_locations(cells)
return monster, door, player
def get_moves(player):
"""Based on the player's position, return the list
of acceptable moves.
>>> GAME_DIMENSIONS = (2,2)
>>> get_moves((0,2))
['RIGHT', 'UP', 'DOWN']
"""
x, y = player
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if x == 0:
moves.remove('LEFT')
if x == GAME_DIMENSIONS[0] - 1:
moves.remove('RIGHT')
if y == 0:
moves.remove('UP')
if y == GAME_DIMENSIONS[1] - 1:
moves.remove('DOWN')
return moves
def move_player(player, move):
x, y = player['location']
player['path'].append((x, y))
if move == 'LEFT':
x -= 1
elif move == 'UP':
y -= 1
elif move == 'RIGHT':
x += 1
elif move == 'DOWN':
y += 1
return x, y
def draw_map():
print(' _'*GAME_DIMENSIONS[0])
row_end = GAME_DIMENSIONS[0]
tile = '|{}'
for index, cell in enumerate(cells):
if index % row_end < row_end - 1:
if cell == player['location']:
print(tile.format('X'), end='')
elif cell in player['path']:
print(tile.format('.'), end='')
else:
print(tile.format('_'), end='')
else:
if cell == player['location']:
print(tile.format('X|'))
elif cell in player['path']:
print(tile.format('.|'))
else:
print(tile.format('_|'))
def play():
cells = build_cells(*GAME_DIMENSIONS)
monster, door, player['location'] = get_locations(cells)
while True:
clear()
print("WELCOME TO THE DUNGEON!")
moves = get_moves(player['location'])
draw_map(cells)
print("\nYou're currently in room {}".format(player['location']))
print("\nYou can move {}".format(', '.join(moves)))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
if move in ['QUIT', 'Q']:
break
if move not in moves:
print("\n** Walls are hard! Stop running into them! **\n")
continue
player['location'] = move_player(player, move)
if player['location'] == door:
print("\n** You escaped! **\n")
break
elif player['location'] == monster:
print("\n** You got eaten! **\n")
break
if __name__ == '__main__':
play()