forked from shreyaspapi/grid-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_game.py
More file actions
113 lines (88 loc) · 2.8 KB
/
grid_game.py
File metadata and controls
113 lines (88 loc) · 2.8 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
import random
CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
(0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
(0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
def get_locations():
return random.sample(CELLS, 3)
def move_player(player, move):
x, y = player
if move == "LEFT":
x -= 1
if move == "RIGHT":
x += 1
if move == "UP":
y -= 1
if move == "DOWN":
y += 1
return x, y
def get_moves(player):
moves = ["LEFT", "RIGHT", "UP", "DOWN"]
x, y = player
if x == 0:
moves.remove("LEFT")
if x == 4:
moves.remove("RIGHT")
if y == 0:
moves.remove("UP")
if y == 4:
moves.remove("DOWN")
return moves
def draw_map(player, monster, door):
print(" _"*5)
tile = "|{}"
for cell in CELLS:
x, y = cell
if x < 4:
line_end = ""
if cell == player:
output = tile.format("X")
elif cell == door:
output = tile.format("D")
#elif cell == monster:
#output = tile.format("M")
else:
output = tile.format("_")
else:
line_end = "\n"
if cell == player:
output = tile.format("X|")
elif cell == monster:
output = tile.format("M|")
elif cell == door:
output = tile.format("D|")
else:
output = tile.format("_|")
print(output, end=line_end)
def game_loop():
monster, door, player = get_locations()
playing = True
while playing:
draw_map(player, monster, door)
valid_moves = get_moves(player)
print("You're currently in room {}".format(player))
print("You can move {}".format(", ".join(valid_moves)))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
if move == 'QUIT':
print("\n ** See you next time! **\n")
break
if move in valid_moves:
player = move_player(player, move)
if player == monster:
print("\n ** Oh no! The monster got you! Better luck next time! **\n")
playing = False
if player == door:
print("\n ** You escaped! Congratulations! **\n")
playing = False
else:
input("\n ** Walls are hard! Don't run into them! **\n")
else:
if input("Play again? [Y/n] ").lower() != "n":
game_loop()
print("Welcome to the dungeon!")
print("You have 1 life with you Try your luck!!")
input("Press Enter to start!")
game_loop()