-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpythonShineFinal.py
459 lines (407 loc) · 17.5 KB
/
pythonShineFinal.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# PythonShine
# Created by Evan Chisholm and Alex Land
import random
import turtle as t
# VARIABLES
startGame = "" # Indicates if game should begin
godMode = "" # Determines if player should choose their own position/health etc
fillColor = "tan" # Default fill color of the biomes (For turtles)
playerPos = -1 # Indicates position of the player
playerName = "" # Holds player name
playerHealth = "" # Indicates current health of the player
playerSword = 0 # Indicates level of player's sword
playerDiamonds = 0 # Indicates number of player's diamonds
biomeData = [] # Holds information about all of the biomes (to be parsed)
biomeDiamonds = [] # Holds number of diamonds in each biome
biomeSwords = [] # Holds level of sword in each biome
biomeEnemies = [] # Holds level of enemy sword in each
biomeList = [] # Holds a list of all currently active biomes (minus catastrophe biomes)
localList = [] # Contains all info from the text file
pythonShine = "" # Indicates position of PythonShine
maximumTurns = "" # Indicates maximum number of turns to play
turnNumber = 0 # Indicates current turn number
roundOn = False # Indicates whether the game should loop for another turn, or restart
gameOn = True # Indicates whether the game should end completely
catastrophe = "" # Indicates whether the player wants catastrophes to occur or not
catastrophePos = "" # Indicates the position of a catastrophe
catastropheNumber = 0 # Number of catastrophes occured (for positioning turtles)
# FUNCTIONS
def gameStart(): # This function initializes the game, and defines godMode (allows
global startGame # user to choose their own biomes etc)
counter = 0
while startGame != "y":
startGame = raw_input("Would you like to play a game (y/n)? ")
if startGame != "y":
print "Please enter y to play."
counter += 1
if counter >= 5:
print "Think you're funny? You're not."
global godMode
while godMode == "":
godModeInput = raw_input("Would you like to manually choose biomes (y/n)? ")
if godModeInput == "y" or godModeInput == "yes":
godMode = True
elif godModeInput == "n" or godModeInput == "no":
godMode = False
else:
print "Please enter y for yes or n for no."
def enterInfo(): # Gets player to enter information like health, name and max turns
global playerHealth
while playerHealth == "":
if godMode == True:
while playerHealth.isdigit() == False or int(playerHealth) < 10 or int(playerHealth) > 50:
playerHealth = raw_input("What is your initial health? (Pick a number between 10 and 50) ")
playerHealth = int(playerHealth)
if godMode == False:
playerHealth = 10* (random.randint(1,5))
global playerName
while playerName == "":
playerName = raw_input("What is the name of your character? ")
global maximumTurns
while maximumTurns.isdigit() == False or int(maximumTurns) <= 0:
maximumTurns = raw_input("How many turns do you want to play (maximum)? ")
maximumTurns = int(maximumTurns)
global catastrophe
while catastrophe == "":
catastropheInput = raw_input("Would you like to allow catastrophes (y/n)? ")
if catastropheInput == "y":
catastrophe = True
elif catastropheInput == "n":
catastrophe = False
else:
print "Please enter y for yes or n for no."
global playerPos
playerPos = 0
global roundOn
roundOn = True
def turnGenerator(): # Generates the "dice rolling" every turn, or allows player to choose
global playerPos
global turnNumber
if turnNumber >= 0:
if godMode == True:
playerPos = raw_input("Which biome would you like to go to? ")
while str(playerPos).isdigit() == False or (int(playerPos) <= 0 or int(playerPos) > (len(biomeList))):
playerPos = raw_input("Which biome would you like to go to (Pick a number between 1 and 7)? ")
playerPos = int(playerPos)
if godMode == False:
playerPosInput = raw_input("Are you ready for the next turn? (y/n) ")
if playerPosInput == "y":
playerPos = ((playerPos + random.randint(1,6)) % (len(biomeList)))
elif playerPosInput != "y":
print "Tricked you, there was never a choice."
playerPos = ((playerPos + random.randint(1,6)) % (len(biomeList)))
turnNumber += 1
print "Starting turn number", turnNumber
print playerName, "has travelled to position", playerPos
def biomeDraw(fillColor): # Turtle function to draw an individual biome
t.fillcolor(fillColor)
t.begin_fill()
t.forward(50)
t.left(90)
t.forward(100)
t.left(90)
t.forward(50)
t.left(90)
t.forward(100)
t.left(90)
t.end_fill()
t.forward(70)
def playerDraw(): # Turtle function to draw the player
t.fillcolor("red")
t.begin_fill()
t.circle(10)
t.end_fill()
def drawBoard(): # Turtle function to draw the entire board
global catastropheNumber
t.penup()
t.forward(-300)
t.pendown()
for i in range(len(biomeList)):
if i == pythonShine and godMode == True:
biomeDraw("green")
else:
biomeDraw("tan")
t.home()
def drawPlayer(): # Turtle function to position the turtle, so the player can be drawn correctly
global playerPos
t.penup()
t.forward(-275)
t.right(90)
t.forward(50)
t.left(90)
if playerPos >= 0:
t.forward((playerPos) * 70)
playerDraw()
def readFile(the_file): # Reads the text file in the same directory as this script
'''
CODE PROVIDED TO INCORPORATE
<file-name including extension .txt>(String) --> List of strings
Assumptions:
1) the_file is in the same directory as this program
2) the_file contains one biome data per line
3) after each biome in the file there is a return (
so that the next biome is in the next line), and also
there is (one single) return after the last biome
in the_file
to call or invoke this function:
listStrings = read_string_list_from_file(<file-name.txt in quotes>)
'''
fileRef = open(the_file,"r") # opening file to be read
global localList
localList=[]
for line in fileRef:
string = line[0:len(line)-1] # eliminates trailing '\n'
# of each line
localList.append(string) # adds string to list
fileRef.close()
#........
#print "\n JUST TO TRACE, the local list of strings is:\n"
#for element in localList:
# print element
#print
#........
return localList
def biomeDataParser(localList): # Parses text file, and seperates in to lists of each item
global biomeData # such as Diamonds, Swords and Enemies
global biomeDiamonds
global biomeSwords
global biomeEnemies
global biomeList
for i in range(len(localList)):
biomeData.append(localList[i].split("-"))
for j in range(len(biomeData)):
for k in range(1):
biomeDiamonds.append(int(biomeData[j][k]))
biomeSwords.append(int(biomeData[j][k+1]))
biomeEnemies.append(int(biomeData[j][k+2]))
biomeList.append(j)
def biomeTable(): # Draws the table indicating information for each biome
print "The current board state is as follows:"
print "Biome Diamd Sword Enemy"
for i in range(len(biomeList)):
if godMode == True:
if pythonShine == i:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i], " <=== PythonShine"
elif playerPos == i and turnNumber >= 0:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i], " <===", playerName
elif playerPos == i and turnNumber < 0:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i]
elif i == pythonShine and pythonShine == playerPos:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i], " <=== PythonShine", " <===", playerName
else:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i]
if godMode == False:
if pythonShine == i:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i]
elif playerPos == i and turnNumber >= 0:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i], " <===", playerName
elif playerPos == i and turnNumber < 0:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i]
elif i == pythonShine and pythonShine == playerPos:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i], " <=== PythonShine", " <===", playerName
else:
print biomeList[i] , " " * (5-(len(str(i)))), biomeDiamonds[i], " " * (5-(len(str(biomeDiamonds[i])))), \
biomeSwords[i], " " * (5-(len(str(biomeSwords[i])))), biomeEnemies[i]
def pythonShineGenerator(): # Generates the PythonShine position
global pythonShine
if godMode == True:
while pythonShine.isdigit() == False or int(pythonShine) < 1 or int(pythonShine) > (len(biomeList)):
pythonShine = raw_input("Where do you want to place PythonShine? ")
pythonShine = int(pythonShine)
elif godMode == False:
pythonShine = random.randint(1, 7)
def combatCalculator(): # Calculates sword pickups, and combat scenarios
global playerSword # (adds or subtracts player health)
global playerHealth
if biomeSwords[playerPos] > playerSword:
tempSword = playerSword
playerSword = biomeSwords[playerPos]
biomeSwords[playerPos] = tempSword
print playerName, "has picked up a level", playerSword, "sword!"
if playerSword > biomeEnemies[playerPos]:
healthGain = random.randint(1, playerHealth)
playerHealth += healthGain
print playerName, "won the fight and gained", healthGain, "health!"
if biomeEnemies[playerPos] > playerSword:
if playerHealth > 1:
healthLost = random.randint(1, playerHealth)
if playerHealth == 1:
healthLost = random.randint(0,1)
playerHealth -= healthLost
print playerName, "lost the fight and lost", healthLost, "health."
if playerSword == biomeEnemies[playerPos]:
healthLost = random.randint(1, (playerHealth / 2))
playerHealth -= healthLost
print playerName, "tied the fight, but lost", healthLost, "health."
def diamondCalculator(): # Calculates number of diamonds picked up in each biome
global playerDiamonds
if playerPos == pythonShine:
playerDiamonds = 9999
else:
tempDiamonds = (biomeDiamonds[playerPos])/3
playerDiamonds += tempDiamonds
biomeDiamonds[playerPos] -= tempDiamonds
print playerName, "has collected", tempDiamonds, "diamonds!"
def playerInfo(): # Displays information about the player's current stats,
global playerSword # and prompts player to play again if dead
global gameOn
global roundOn
gameOnInput = ""
if roundOn == True:
print playerName + " currently has:"
print playerHealth, "health."
print playerDiamonds, "diamonds."
if playerSword == 0:
print "No sword."
else:
"A level", playerSword, "sword."
if playerPos == -1:
print "And is about to spawn."
else:
print "And is in position", playerPos
print "=========================================================="
elif roundOn == False:
print "Game Over!"
if playerHealth <= 0:
print playerName + " has died."
elif playerPos == pythonShine:
print playerName + " has won! " + playerName + " has found PythonShine!"
elif turnNumber == maximumTurns:
print "The maximum number of turns has been reached."
print playerName + " ended the round with:"
print playerHealth, "health."
print playerDiamonds, "diamonds."
if playerSword == 0:
print "No sword."
else:
"A level", playerSword, "sword."
print "=========================================================="
if gameOn == True and roundOn == False:
while gameOnInput == "" and gameOn == True:
gameOnInput = raw_input("Would you like to play another game? (y/n) ")
if gameOnInput == "y":
gameOn = True
roundOn = True
elif gameOnInput == "n":
gameOn = False
endGameCalculations()
else:
print "Please input y for yes or n for no."
gameOnInput = ""
def gameCheck(): # Checks if the game should continue, or end (if player is dead,
global roundOn # max turns reached, or player is in PythonShine)
if playerPos == pythonShine:
roundOn = False
if playerHealth <= 0:
roundOn = False
if turnNumber == maximumTurns:
roundOn = False
def roundReset(): # Resets the variables to their default for each round
global playerDiamonds
global catastrophe
global catastropheNumber
global pythonShine
global maximumTurns
global playerHealth
global turnNumber
global biomeData
global biomeDiamonds
global biomeSwords
global biomeEnemies
global biomeList
playerDiamonds = 0
catastrophe = ""
catastropheNumber = 0
pythonShine = ""
maximumTurns = ""
playerHealth = ""
turnNumber = 0
biomeData = []
biomeDiamonds = []
biomeSwords = []
biomeEnemies = []
biomeList = []
def endGameCalculations():
base2List = []
base2Str = ""
for i in range(len(biomeDiamonds)):
if biomeDiamonds[i]%2 ==0:
base2List.append(0)
else:
base2List.append(1)
print "The base2 number created from the diamonds in the biomes is,", base2List
base10Conversion = 0
j = len(base2List)-1
for i in range(len(base2List)):
if base2List[i] == 0:
base10Conversion += 0
elif base2List[i] == 1:
if i == len(base2List):
base10Conversion += 1
else:
base10Conversion += 2**j
j -= 1
print "And so, lo and behold, your final score is....", base10Conversion
def catastropheGenerator(): # Generates position of catastrophes, and removes the biome
global catastrophePos
global catastropheNumber
global pythonShine
catastrophePos = random.randint(1,5*(len(biomeList)))
if catastrophePos < len(biomeList):
print "A catastrophe has occured in biome", catastrophePos
print "Biome", catastrophePos, "has been destroyed!"
biomeList.remove(catastrophePos)
catastropheNumber += 1
if playerPos == catastrophePos:
playerHealth = 0
print playerName, "has been caught in the catastrophe!"
if pythonShine == catastrophePos:
pythonShine = -1
else:
pythonShine -= 1
for j in range(len(biomeList)):
if j >= catastrophePos:
biomeList[j] -= 1
# EXECUTION TOP LEVEL
# WHILE LOOP
while gameOn == True:
gameStart()
readFile("biomesData1.txt")
biomeDataParser(localList)
pythonShineGenerator()
print
biomeTable()
print
enterInfo()
while roundOn == True:
t.speed(0)
print
biomeTable()
print
playerInfo()
drawBoard()
drawPlayer()
print
turnGenerator()
diamondCalculator()
combatCalculator()
print
gameCheck()
if catastrophe == True:
catastropheGenerator()
gameCheck()
t.clearscreen()
print
biomeTable()
print
playerInfo()
roundReset()