forked from yamax87/textAdventure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextGame_funcApproach_BasicWorking.py
More file actions
223 lines (134 loc) · 5.94 KB
/
textGame_funcApproach_BasicWorking.py
File metadata and controls
223 lines (134 loc) · 5.94 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Player moves from room to room
Sometimes encounters enemies
Sometimes encounters ppl who want to trade
Objective: Collect 10 coins
-player
-rooms
-other characters
-player inventory
***
3 rooms
1 contains exit
Player must find exit
1) Create room object
2) Create player object
3) Assign exit to a room
***
The following links might help:
https://inventwithpython.com/blog/2014/12/11/making-a-text-adventure-game-with-the-cmd-and-textwrap-python-modules/
(This one looks particularly useful:)
https://www.quora.com/I-am-very-new-to-Python-and-I-want-to-create-a-text-adventure-game-in-Python-How-can-I-do-this
3 possible approaches here:
1) Retain 'for' loop, but figure out way to manage situations where dict values that don't match playerMove (eg playerMove is 's' in room 3, but loop gets stuck on 'n', since 'n' != 's').
2) Replace 'for' loop with 'if' or switch statement, search dict_doors for matching key, access value (??seems to make sense??)
3) Remove dictionary, use variables only, use 2) to search through variables somehow
checkDoor = [value for key, value in dict_door.items() if playerMove in key.lower()] #returns a list of values for dict_door where the key matches playerMove.
#random room selection: Random number selected once, when the game starts.
"""
import random
import sys
def generateNewExit():
thisRoomIsExit = random.randint(2,3)
return thisRoomIsExit
def playerWins():
print('You\'re out. Yay. Presumably you found the massive hole in the floor?')
playAgain = input("Play Again? 'y' or 'n'")
if playAgain == 'y':
thisRoomIsExit = generateNewExit()
room1(thisRoomIsExit)
if playAgain == 'n':
print("Scared, huh? Obviously you hate puzzles with as many as 3 pieces. Let\'s see how you handle my fiendish two piece rubix cube...anyhow, see ya.")
sys.exit()
return exitLoc
############ ROOM DEFINITIONS BELOW ##########
def room1(exitLocation):
roomNumber = 1
if exitLocation == roomNumber: #checks if player has found exit
playerWins()
dict_doors = {
'n' : True,
's' : False,
'e' : False,
'w' : False,
}
playerMove = input('You are in Room 1. Which way do you want to go? Enter n,s,w or e.')
checkDoor = [value for key, value in dict_doors.items() if playerMove in key.lower()] #returns a list of values for dict_door where the key matches playerMove.
if len(checkDoor) >= 1:
checkDoor = checkDoor[0] #if the list assigned to checkDoor contains at least 1 entry, reassign checkDoor to a pure Boolean
print('You entered: ' + playerMove + '. The open status for this door is:' + str(checkDoor))
if checkDoor == True:
#print('The value is True.')
if playerMove == 'n':
print('You move into Room 2.')
room2(exitLocation)
elif checkDoor == False:
#print('The value is False.')
print('This door is locked.')
room1(exitLocation)
else: #if the player did not enter n,s,w or e. In which case checkDoor should contain an empty list.
#print('The value of this variable is neither True nor False.')
print('um, sense much? You need to enter n,s,w or e.')
room1(exitLocation)
def room2(exitLocation):
roomNum = 2
if exitLocation == roomNum: #checks if player has found exit
playerWins()
dict_doors = {
'n' : True,
's' : True,
'e' : False,
'w' : False,
}
playerMove = input('You are in Room 2. Which way do you want to go? Enter n,s,w or e.')
checkDoor = [value for key, value in dict_doors.items() if playerMove in key.lower()] #returns a list of values for dict_door where the key matches playerMove.
if len(checkDoor) >= 1:
checkDoor = checkDoor[0] #if the list assigned to checkDoor contains at least 1 entry, reassign checkDoor to a pure Boolean
print('You entered: ' + playerMove + '. The open status for this door is:' + str(checkDoor))
if checkDoor == True:
#print('The value is True.')
if playerMove == 'n':
print('You move into Room 3.')
room3(exitLocation)
if playerMove == 's':
print('You move into Room 1.')
room1(exitLocation)
elif checkDoor == False:
#print('The value is False.')
print('This door is locked.')
room2(exitLocation)
else: #if the player did not enter n,s,w or e. In which case checkDoor should contain an empty list.
#print('The value of this variable is neither True nor False.')
print('um, sense much? You need to enter n,s,w or e.')
room2(exitLocation)
def room3(exitLocation):
roomNum = 3
if exitLocation == roomNum: #checks if player has found exit
playerWins()
dict_doors = {
'n' : False,
's' : True,
'e' : False,
'w' : False,
}
playerMove = input('You are in Room 3. Which way do you want to go? Enter n,s,w or e.')
checkDoor = [value for key, value in dict_doors.items() if playerMove in key.lower()] #returns a list of values for dict_door where the key matches playerMove.
if len(checkDoor) >= 1:
checkDoor = checkDoor[0] #if the list assigned to checkDoor contains at least 1 entry, reassign checkDoor to a pure Boolean (the first entry in the list)
print('You entered: ' + playerMove + '. The open status for this door is:' + str(checkDoor))
if checkDoor == True:
#print('The value is True.')
if playerMove == 's':
print('You move into Room 2.')
room2(exitLoc)
elif checkDoor == False:
#print('The value is False.')
print('This door is locked.')
room3(exitLocation)
else: #if the player did not enter n,s,w or e. In which case checkDoor should contain an empty list.
#print('The value of this variable is neither True nor False.')
print('um, sense much? You need to enter n,s,w or e.')
room3(exitLocation)
##################
exitLocation = generateNewExit()
room1(exitLocation)