· 9 years ago · Mar 28, 2017, 09:52 AM
1#Another Text Adventure
2#Author: Josh Scott
3#Version 1.0.1
4
5
6#imports time
7import time
8
9#allows for random functions
10import random
11
12#allows SQL actions
13import sqlite3
14
15#this is the library used for claering the brackets from the leaderboard.
16import itertools
17
18MapData="wwwwwwwwwwwwwwwwwwwwwwffffffffpsvswwwwwwwfftffffffppsssswwwwwffffffffpppwwwwwwwwwffffffpppppwtppbbswwfffffppppppwppwwsswwffffpppppppwwwwsspwwppppppppbppwppppppwwvpppppwwbwwwppppmpwwwwwwwwwpbpppppppmmwwpppppppppppppppmmmwwpppppppvpppppppmmmwwmmppppppppppppppppwwmmmmmmmmmpppvmmpppwwmmmmmmmmmpmmmmmmmpwwmmmdmmmmppmmmmmmmmwwwmmmmmmmpppmmmmmmmwwwmmmmmmppppppmmmmwwwwwmmmmpppppppptwwwwwwwwwwwwwwwwwwwwwwww"
19
20LoadArray = ["","","","","","","","","","","","","","","","","",""]
21
22#access of the files made global
23Userfile = sqlite3.connect("Users.db")
24USER = Userfile.cursor()
25
26Itemfile = sqlite3.connect("Items.db")
27ITEM = Itemfile.cursor()
28
29Monsterfile = sqlite3.connect("Monsters.db")
30MONSTER = Monsterfile.cursor()
31
32print("")
33print(" █████╗ ███╗ ██╗ ██████╗ ████████╗██╗ ██╗███████╗██████╗ ")
34print(" ██╔â•â•██╗████╗ ██║██╔â•â•â•██╗╚â•â•██╔â•â•â•██║ ██║██╔â•â•â•â•â•██╔â•â•██╗ ")
35print(" ███████║██╔██╗ ██║██║ ██║ ██║ ███████║█████╗ ██████╔╠")
36print(" ██╔â•â•██║██║╚██╗██║██║ ██║ ██║ ██╔â•â•██║██╔â•â•╠██╔â•â•██╗ ")
37print(" ██║ ██║██║ ╚████║╚██████╔╠██║ ██║ ██║███████╗██║ ██║ ")
38print(" ╚â•╠╚â•â•╚â•╠╚â•â•â•╠╚â•â•â•â•â•╠╚â•╠╚â•╠╚â•â•╚â•â•â•â•â•â•â•╚â•╠╚â•â• ")
39time.sleep(.5)
40print(" ")
41print(" ████████╗███████╗██╗ ██╗████████╗ ")
42print(" ╚â•â•██╔â•â•â•██╔â•â•â•â•â•╚██╗██╔â•╚â•â•██╔â•â•â• ")
43print(" ██║ █████╗ ╚███╔╠██║ ")
44print(" ██║ ██╔â•â•╠██╔██╗ ██║ ")
45print(" ██║ ███████╗██╔╠██╗ ██║ ")
46print(" ╚â•╠╚â•â•â•â•â•â•â•╚â•╠╚â•╠╚â•â• ")
47time.sleep(.5)
48print(" ")
49print(" █████╗ ██████╗ ██╗ ██╗███████╗███╗ ██╗████████╗██╗ ██╗██████╗ ███████╗")
50print(" ██╔â•â•██╗██╔â•â•██╗██║ ██║██╔â•â•â•â•â•████╗ ██║╚â•â•██╔â•â•â•██║ ██║██╔â•â•██╗██╔â•â•â•â•â•")
51print(" ███████║██║ ██║██║ ██║█████╗ ██╔██╗ ██║ ██║ ██║ ██║██████╔â•█████╗ ")
52print(" ██╔â•â•██║██║ ██║╚██╗ ██╔â•██╔â•â•╠██║╚██╗██║ ██║ ██║ ██║██╔â•â•██╗██╔â•â•â• ")
53print(" ██║ ██║██████╔╠╚████╔╠███████╗██║ ╚████║ ██║ ╚██████╔â•██║ ██║███████╗")
54print(" ╚â•╠╚â•â•╚â•â•â•â•â•╠╚â•â•â•╠╚â•â•â•â•â•â•â•╚â•╠╚â•â•â•╠╚â•╠╚â•â•â•â•â•╠╚â•╠╚â•â•╚â•â•â•â•â•â•â•")
55time.sleep(1)
56
57#main menu
58def Main():
59 PlayerOption = "4"
60 while PlayerOption not in["1","2","3","9"]:
61 print("\n\nMain Menu")
62 print("")
63 print("1. Load Game")
64 print("2. New Game")
65 print("3. Leader Board")
66 print("9. Quit")
67 print("")
68 PlayerOption = input("> ")
69 print("")
70
71 if PlayerOption == ("1"):
72 Loadgame(Userfile, USER,LoadArray)
73 elif PlayerOption == ("2"):
74 Newgame(Userfile, USER, LoadArray)
75 elif PlayerOption == ("3"):
76 Leaderboard(Userfile,USER,LoadArray)
77 elif PlayerOption == ("9"):
78 exit()
79
80#loading an already saved game
81def Loadgame(Userfile, USER, LoadArray):
82 MapData="wwwwwwwwwwwwwwwwwwwwwwffffffffpssswwwwwwwfftffffffppsssswwwwwffffffffpppwwwwwwwwwffffffpppppwtppbbswwfffffppppppwppwwsswwffffpppppppwwwwsspwwppppppppbppwppppppwwvpppppwwbwwwppppmpwwwwwwwwwpbpppppppmmwwpppppppppppppppmmmwwpppppppppppppppmmmwwmmppppppppppppppppwwmmmmmmmmmpppvmmpppwwmmmmmmmmmpmmmmmmmpwwmmmdmmmmppmmmmmmmmwwwmmmmmmmpppmmmmmmmwwwmmmmmmppppppmmmmwwwwwmmmmpppppppptwwwwwwwwwwwwwwwwwwwwwwww"
83 UserName = ""
84
85 while UserName == "":
86 UserName = input("What is your name?\n> ")
87
88 print("Ah yes and what was our password?")
89 UserPass = ""
90
91 while UserPass == "":
92 UserPass = input("> ")
93
94 NoExist = True
95 while NoExist == True:
96 try:
97 #checking if the Username is already in use
98 #if the name cannot be found the program will break and run into the 'except' section
99 USER.execute("SELECT u.ID, u.Name, u.Pass, u.lvl, u.Type, u.HP, u.X, u.Y, u.Money, u.Helm, u.Arm, u.Weapon, u.Item1, u.Item2, u.Item3, u.Item4, u.Item5,u.Map FROM Users u WHERE u.Name = ?", (UserName,))
100 Load = USER.fetchone()
101 Place = 0
102
103 for x in Load:
104 LoadArray[Place] = x
105 Place += 1
106
107 ID = LoadArray[0]
108 UserName = LoadArray[1]
109 Password = LoadArray[2]
110 Level = LoadArray[3]
111 PlayerType = LoadArray[4]
112 Health = LoadArray[5]
113 XCoord = LoadArray[6]
114 YCoord = LoadArray[7]
115 Gold = LoadArray[8]
116 Helm = LoadArray[9]
117 Arm = LoadArray[10]
118 Weapon = LoadArray[11]
119 Item1 = LoadArray[12]
120 Item2 = LoadArray[13]
121 Item3 = LoadArray[14]
122 Item4 = LoadArray[15]
123 Item5 = LoadArray[16]
124 MapData = LoadArray[17]
125
126 Count = 0
127
128 for Row in range(20):
129 MapRow = []
130 for Column in range(20):
131 MapRow.append(MapData[Count])
132 Count+=1
133 Map.append(MapRow)
134 #at this point username exists.
135
136
137 if UserPass == Password:
138 NoExist = False
139 PlayGame(UserName,Password,Level,PlayerType,Health,XCoord,YCoord,Gold,Helm,Arm,Weapon,Item1,Item2,Item3,Item4,Item5,Map)
140 else:
141 print("I know of nobody by that name friend.")
142 print("What is your real name?")
143 UserName = input("> ")
144 UserPass = input("And your password?\n> ")
145
146
147 except:
148 print("I know of nobody by that name friend.")
149 print("What is your real name?")
150 UserName = input("> ")
151 UserPass = input("And your password?\n> ")
152
153
154
155#creating a new save file
156def Newgame(Userfile, USER, LoadArray):
157 print("What is your name traveller?")
158 UserName = ""
159 while UserName == "":
160 UserName = input("> ")
161
162 AlreadyUsed = True
163 while AlreadyUsed == True:
164 try:
165 #checking if the Username is already in use
166 #if the name cannot be found the program will break and run into the 'except' section
167 USER.execute("SELECT u.ID, u.Name, u.Pass, u.lvl, u.Type, u.HP, u.X, u.Y, u.Money, u.Helm, u.Arm, u.Weapon, u.Item1, u.Item2, u.Item3, u.Item4, u.Item5,u.Map FROM Users u WHERE u.Name = ?", (UserName,))
168 Load = USER.fetchone()
169 Place = 0
170
171 for x in Load:
172 LoadArray[Place] = x
173 Place += 1
174
175 ID = LoadArray[0]
176 UserName = LoadArray[1]
177 Password = LoadArray[2]
178 Level = LoadArray[3]
179 PlayerType = LoadArray[4]
180 Health = LoadArray[5]
181 XCoord = LoadArray[6]
182 YCoord = LoadArray[7]
183 Gold = LoadArray[8]
184 Helm = LoadArray[9]
185 Arm = LoadArray[10]
186 Weapon = LoadArray[11]
187 Item1 = LoadArray[12]
188 Item2 = LoadArray[13]
189 Item3 = LoadArray[14]
190 Item4 = LoadArray[15]
191 Item5 = LoadArray[16]
192 MapData = LoadArray[17]
193
194 Count = 0
195 print("I already know someone by that name.")
196 UserName = input("What is your name traveller?\n> ")
197
198 except:
199
200
201 print("Greetings",UserName,"It has been many moons since a HERO of your calibre has been seen in these lands. Remind me, which school do you follow?\n\t1. The Fighter\n\t2. The Ranger\n\t3. The Wizard")
202 PlayerType = int(input("> "))
203 while PlayerType not in (1,2,3):
204 print("invalid Choice")
205
206 PlayerType = int(input("> "))
207
208 print("Ahh, good. I hope you can help the people of this land. A TERRIBLE creature; The Dragon Vex. Controls and torments these lands. To defeat him you must first collect the ancient relics. A Helm, A chest piece and the great weapon of the hero to defeat him. Will you do this for us? [Y/N]")
209 PlayerOption = input("> ")
210 if PlayerOption in ["y", "Y", "Yes", "YES", "yes"]:
211 print("Good")
212 else:
213
214 while PlayerOption not in ["y", "Y", "Yes", "YES", "yes"]:
215
216 x = random.randint(1,3)
217
218 if x == 1:
219 print("Please?")
220 elif x == 2:
221 print("Pretty Please?")
222 elif x == 3:
223 print("But you're the Hero!")
224 else:
225 print("loop Error")
226 PlayerOption = input("> ")
227
228 print("Now, for when we next meet we should have a password to make sure you are who you say you are.")
229 print("PASSWORD:")
230 Password1 = input("> ")
231 print("I’m a smidge deaf in that ear. Once more?")
232 Password2 = input("> ")
233
234 while Password1 != Password2:
235 print("Those two weren't the same. \nTry again")
236 Password1 = input("> ")
237 print("One more time")
238 Password2 = input("> ")
239
240 Password = Password1
241 Health = 10
242 Level = 1
243 Gold = 5
244 Weapon = 0
245 XCoord = 1
246 YCoord = 8
247
248 #creating the table for the players if on a new system
249 NewTable = """
250 CREATE TABLE IF NOT EXISTS Users(
251 ID INTEGER PRIMARY KEY,
252 Name VARCHAR (20),
253 Pass VARCHAR(20),
254 Lvl INTEGER,
255 Type INTEGER,
256 HP INTEGER,
257 X INTEGER,
258 Y INTEGER,
259 Money INTEGER,
260 Helm INTEGER,
261 Arm INTEGER,
262 Weapon INTEGER,
263 Item1 INTEGER,
264 Item2 INTEGER,
265 Item3 INTEGER,
266 Item4 INTEGER,
267 Item5 INTEGER,
268 Map VARCHAR (400),
269 FOREIGN KEY(Helm)REFERENCES Items(Id),
270 FOREIGN KEY(Arm)REFERENCES Items(Id),
271 FOREIGN KEY(Weapon)REFERENCES Items(Id),
272 FOREIGN KEY(Item1)REFERENCES Items(Id),
273 FOREIGN KEY(Item2)REFERENCES Items(Id),
274 FOREIGN KEY(Item3)REFERENCES Items(Id),
275 FOREIGN KEY(Item4)REFERENCES Items(Id),
276 FOREIGN KEY(Item5)REFERENCES Items(Id));"""
277
278 USER.execute(NewTable)
279
280 #save function with placeholder data
281
282 insert= """
283 INSERT INTO Users(Name,Pass,Lvl,Type,HP,X,Y,Money,Helm,Arm,Weapon,Map,Item1,Item2,Item3,Item4,Item5)
284 VALUES("PLACEHOLDER","PLACEHOLDER",1,0,10,1,8,50,17,19,0,"wwwwwwwwwwwwwwwwwwwwwwffffffffpssswwwwwwwfftffffffppsssswwwwwffffffffpppwwwwwwwwwffffffpppppwtppbbswwfffffppppppwppwwsswwffffpppppppwwwwsspwwppppppppbppwppppppwwvpppppwwbwwwppppmpwwwwwwwwwpbpppppppmmwwpppppppppppppppmmmwwpppppppppppppppmmmwwmmppppppppppppppppwwmmmmmmmmmpppvmmpppwwmmmmmmmmmpmmmmmmmpwwmmmdmmmmppmmmmmmmmwwwmmmmmmmpppmmmmmmmwwwmmmmmmppppppmmmmwwwwwmmmmpppppppptwwwwwwwwwwwwwwwwwwwwwwww",0,0,0,0,0)"""
285
286 USER.execute(insert)
287
288 USER.execute ("SELECT * FROM Users ")
289
290 #saving over the placeholders
291
292 USER.execute("UPDATE Users SET Name = ? WHERE Name = 'PLACEHOLDER' ",(UserName,))
293
294 USER.execute("UPDATE Users SET Pass = ? WHERE Pass = 'PLACEHOLDER' ",(Password,))
295
296 USER.execute("UPDATE Users SET Type = ? WHERE Type = 0 ",(PlayerType,))
297
298 USER.execute("UPDATE Users SET Weapon = ? WHERE Weapon = 0 ",(PlayerType,))
299 Userfile.commit()
300 AlreadyUsed = False
301
302
303
304def Leaderboard(Userfile,USER,LoadArray):
305 #print leader board after sorting
306
307
308 ###############################################################################################
309 ###############################***GRADE A SORTING ALGORITHM***#################################
310 ###############################################################################################
311
312
313
314 Place = 0
315
316 Scorelist = []
317 Namelist = []
318 Score = ""
319 Name = ""
320 USER.execute("SELECT u.ID FROM Users u")
321
322 #loading data from table into separate arrays until end of list
323
324 Length = len(USER.fetchall())
325 Length = int(Length)
326
327 USER.execute('SELECT * FROM Users')
328 #Fetches the data in the specified rows
329
330 Userfile.row_factory = lambda USER, row: row[0]
331 Scorelist = USER.execute('SELECT Money FROM Users').fetchall()
332 Namelist = USER.execute('SELECT Name FROM Users').fetchall()
333
334 Scorelist
335 Namelist
336
337 #Bubblesort function
338 #Grade A Algorithm
339
340 for passnum in range(abs(Length)-1):
341 i = 0
342
343 for i in range(abs(Length)-1):
344
345 if Scorelist[i]<Scorelist[i+1]:
346 tempScore = Scorelist[i]
347 tempName = Namelist[i]
348 Scorelist[i] = Scorelist[i+1]
349 Namelist[i] = Namelist[i+1]
350 Scorelist[i+1] = tempScore
351 Namelist[i+1] = tempName
352
353 passnum += 1
354 #Bubblesort complete
355
356 #removes brackets from the named lists
357 Names = list(itertools.chain.from_iterable(Namelist))
358 Scores = list(itertools.chain.from_iterable(Scorelist))
359
360 x=0
361 while x<abs(Length):
362 print("\t",(x+1),".",Names[x]," \t ",Scores[x])
363 x+=1
364 Main()
365
366
367
368#main game section
369def PlayGame(UserName,Password,Level,PlayerType,Health,XCoord,YCoord,Gold,Helm,Arm,Weapon,Item1,Item2,Item3,Item4,Item5,Map):
370 Map = []
371 Count = 0
372
373
374 Itemfile = sqlite3.connect("Items.db")
375 ITEM = Itemfile.cursor()
376
377 for Row in range(20):
378 MapRow = []
379 for Column in range(20):
380 MapRow.append(MapData[Count])
381 Count+=1
382 Map.append(MapRow)
383
384 print("Welcome to the game,",UserName,". To travel enter in a direction type: N = North, E = East, S = South, W = West) Find The Relics!")
385
386 while Running:
387
388 #every 5 turns the user's health will increase by 1 until it is at it's maximum.
389
390 if Health < 10+(Level*2):
391 Health += 1
392
393 #checks the map tile to determine what landscape the user is on.
394 if Map[YCoord][XCoord] == "p":
395 print("The plains span across the land before you")
396 elif Map[YCoord][XCoord] == "f":
397 print("A thick forest looms over you")
398 elif Map[YCoord][XCoord] == "v":
399 print("You enter a small villiage")
400 Leaving = False
401 while not Leaving:
402 PlayerOption = input("Do you wish to visit:\n\t1.The Inn\n\t2.The Shop\n\t9.Leave\n> ")
403 if PlayerOption == "1":
404 NewGold = Inn(Gold)
405 if NewGold < Gold:
406 Health = 10 + (Level*2)
407 Gold = NewGold
408 elif PlayerOption == "2":
409 #ensures that the data from the shop isn't incorrect
410 #then inputs it into correct variable
411 BoughtItem = Shop(Weapon,Arm,Helm,Gold,Item1,Item2,Item3,Item4,Item5,Health,Level)
412 if BoughtItem == 50:
413 print("That isn't an option")
414 elif BoughtItem <13:
415 Gold -= 50
416 Weapon = BoughtItem
417 Level += 1
418 print("You have a new weapon")
419 elif BoughtItem == 20:
420 Armour = BoughtItem
421 Gold-=75
422 Level += 1
423 print("You addorn your new armour")
424 elif BoughtItem == 18:
425 Helm=BoughtItem
426 Gold -=25
427 Level += 1
428 print("You place the new helmet on your head; you feel protected.")
429 elif BoughtItem == 16:
430 if Item1 != 16:
431 Item1 = 16
432 Gold -=25
433 elif Item2 != 16:
434 Item2 = 16
435 Gold -=25
436 elif Item3 !=16:
437 Item3 = 16
438 Gold -=25
439 elif Item4 != 16:
440 Item4 = 16
441 Gold -=25
442 elif Item5 != 16:
443 Item5 = 16
444 Gold -=25
445 else:
446 print("ShopError")
447
448 elif PlayerOption =="9":
449 Leaving = True
450 elif Map[YCoord][XCoord] == "m":
451 print("You are stood on a great mountain")
452 elif Map[YCoord][XCoord] == "b":
453 print("This bridge can take you across the water")
454 elif Map[YCoord][XCoord] == "s":
455 print("Course sand sinks into your boots as you find yourself on a beach")
456 elif Map[YCoord][XCoord] == "d":
457 #this is the end of the game. if all items have been found then the final text will play.
458 if Weapon in range(12,16):
459 print("")
460 if Arm == 22:
461 if Helm == 21:
462 print("'You cannot best me, mortal.' Bellows the mighty dragon, \n'Your attempt at defeating me shall be your undoing!' \nAs the beast rears it's head to exhale a crackling fireball, \nit notices the light of your glowing tunic. \n'It cannot be!' he exclaims as he looses his fiery breath, 'you shall die!' \n\nAs you walk from the burning wreckage of the door that lay behind you, \nVex admits defeat. With the weapon of the ancient in hand the beast is \nstruck and reduced to a smoldering \npile of ash.\n\nThe land is saved. Thanks to you.")
463 x = int(input("\nOne last thing hero...\n\nWhat will you do now?\n\n\t1.Live the rest of my days in peace\n\t2.Go on to find a new adventure\n\t3.Now that the dragon is gone I am free to pilliage and plunder the land as its new ruler\n\n> "))
464 if x != 3:
465 print("That is most noble. For a second I thought that you were going to take over as the new protagonist.")
466 else:
467 print("After years of ruling with an iron fist the gold you stole from the dragon corrupts you and turns you into a large dragon. An adventurer comes, wearing your old armour, and kills you where you stand. You have become Vex.")
468 else:
469 print("You do not have the Holy Helmet")
470 else:
471 print("You do not have the Mighty Breastplate or the Holy Helmet")
472 else:
473 print("You are not ready to face the Dragon Vex")
474 elif Map[YCoord][XCoord] == "t":
475 print("You found a treasure chest.")
476
477 #tells the user what treasure they have found and then changes the treasure tile to plains.
478 #this stops the user from repetetively activating the treasure tile to cheat.
479 if Weapon != 12 + PlayerType:
480 Weapon = 12 + PlayerType
481 if PlayerType == 1:
482 print(UserName,"! You have found the Ancient Sword")
483 elif PlayerType ==2:
484 print(UserName,"! You have found the Ancient Bow")
485 elif PlayerType ==3:
486 print(UserName,"! You have found the Ancient Wand")
487
488 elif Arm != 22:
489 Arm = 22
490
491 print(UserName,"! You have found the Breastplate of Buttkicking")
492
493 elif Helm != 21:
494 Helm = 21
495
496 print(UserName,"! You have found the Holy Helmet")
497 Map[YCoord][XCoord] = "p"
498 else:
499 print("invalid")
500
501 #users interaction with the game is logged and the corresponding action takes place.
502
503 PlayerOption = input ("(n,e,s,w,save,inventory)\n> ")
504 while PlayerOption in ["Save", "save", "SAVE", "sAVE","inventory","INVENTORY","Inventory","iNVENTORY"]:
505 if PlayerOption in ["Save", "save", "SAVE", "sAVE"]:
506 Save(Userfile,Itemfile,UserName,Password,Level,PlayerType,Health,XCoord,YCoord,Gold,Helm,Arm,Weapon,Item1,Item2,Item3,Item4,Item5,Map)
507 PlayerOption = input ("(n,e,s,w)\n> ")
508 elif PlayerOption in ["inventory","INVENTORY","Inventory","iNVENTORY"]:
509 if Item1 == 0:
510 A = "Empty"
511 else:
512 A = "Health Potion"
513 if Item2 == 0:
514 B = "Empty"
515 else:
516 B = "Health Potion"
517 if Item3 == 0:
518 C = "Empty"
519 else:
520 C = "Health Potion"
521 if Item4 == 0:
522 D = "Empty"
523 else:
524 D = "Health Potion"
525 if Item5 == 0:
526 E = "Empty"
527 else:
528 E = "Health Potion"
529
530 Weapon = int(Weapon)
531 Arm = int(Arm)
532 Helm = int(Helm)
533 #load inventory.
534 while True:
535 try:
536 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Weapon,))
537
538 Place = 0
539 Gear = ITEM.fetchone()
540 ItemData = ["","","","",""]
541
542 for G in Gear:
543 ItemData[Place] = G
544 Place += 1
545
546 WeaponName = ItemData[1]
547 break
548 except:
549 print("Weapon Error Bug!")
550
551 while True:
552 try:
553 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Arm,))
554
555 Place = 0
556 Gear = ITEM.fetchone()
557 ItemData = ["","","","",""]
558
559 for G in Gear:
560 ItemData[Place] = G
561 Place += 1
562
563 ArmName = ItemData[1]
564 break
565 except:
566 print("Armour Bug!")
567
568 while True:
569 try:
570 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Helm,))
571
572 Place = 0
573 Gear = ITEM.fetchone()
574 ItemData = ["","","","",""]
575
576 for G in Gear:
577 ItemData[Place] = G
578 Place += 1
579
580 HelmName = ItemData[1]
581 break
582 except:
583 print("Helmet Bug")
584
585 print("\n\nCurrent Inventory:\n##################\n\t1.",A,"\n\t2.",B,"\n\t3.",C,"\n\t4.",D,"\n\t5.",E,"\n\nHealth:",Health,"Max Health:",(10+(Level*2)),"\nGold:",Gold,"\n\nHelmet:",HelmName,"\nArmour:",ArmName,"\nWeapon:",WeaponName)
586 PlayerOption = input ("(n,e,s,w)\n> ")
587
588 if (PlayerOption in ["n","N"]) and (Map[YCoord-1][XCoord]!= "w"):
589 YCoord -= 1
590 print("You travelled North")
591 elif (PlayerOption in ["E","e"]) and (Map[YCoord][XCoord+1]!= "w"):
592 XCoord += 1
593 print("You travelled East")
594 elif (PlayerOption in ["s","S"]) and (Map[YCoord+1][XCoord]!="w"):
595 YCoord += 1
596 print("You travelled South")
597 elif (PlayerOption in ["W","w"]) and (Map[YCoord][XCoord-1]!="w"):
598 XCoord -= 1
599 print("You travelled West")
600 else:
601 print("Invalid")
602
603
604
605 battle = random.randint(1,20)
606 if battle == 1:
607 Health = Battle(UserName,Health,Weapon,Arm,Helm,Item1,Item2,Item3,Item4,Item5,Level,Gold)
608 if Health <= 0:
609 print("You Died!")
610 Main()
611 else:
612 Loot = random.randint(5,20)
613 print("You found",Loot,"Gold on the body")
614 Gold += Loot
615
616
617
618
619
620def Save(Userfile,Itemfile,UserName,Password,Level,PlayerType,Health,XCoord,YCoord,Gold,Helm,Arm,Weapon,Item1,Item2,Item3,Item4,Item5,Map):
621 #opens items from inventory into Items variables for storing.
622
623 MapData=""
624
625 Count = 0
626
627 #sets the data from Map lists into a single string
628 for y in range(20):
629 MapRow = []
630 for x in range(20):
631 MapData += Map[y][x]
632
633 USER.execute("UPDATE Users SET Lvl = ?, Money = ?, HP = ?, X = ?, Y = ?, Arm = ?, Helm = ?, Weapon = ?, Item1 = ?, Item2 = ?, Item3 = ?, Item4 = ?, Item5 = ?, Map = ? WHERE Name = ? ",(Level,Gold,Health,XCoord,YCoord,Arm,Helm,Weapon,Item1,Item2,Item3,Item4,Item5,MapData,UserName,))
634 Userfile.commit()
635
636 #requests continuation
637 print("sucesfsful save")
638 print("Continue?")
639 PlayerOption = input("> ")
640 if PlayerOption in ["no","n","N","NO","No","nO"]:
641 Main()
642
643
644
645def Inn(Gold):
646 #user entered the Inn
647 print("Welcome to the Inn Child")
648 print("Do you wish to stay the night for 5 Gold?")
649 print("\nGold:",Gold)
650 InnOption = input("> ")
651 while InnOption not in ["y","Y","Yes","yES","YES","yes","n","N","No","nO","NO","no"]:
652 if InnOption in ["y","Y","Yes","yES","YES","yes"]:
653 if Gold < 5:
654 print("You don't appear to be able to afford the room.")
655 else:
656 Gold -= 5
657 print("Have a nice night!")
658 else:
659 print("What was that?")
660 innOption = ("> ")
661
662 return Gold
663
664
665def Shop(Weapon,Arm,Helm,Gold,Item1,Item2,Item3,Item4,Item5,Health,Level):
666
667 #opens the items that are better and all potions
668 print("Welcome to the shop")
669
670
671 Itemfile = sqlite3.connect("Items.db")
672 ITEM = Itemfile.cursor()
673
674 full = False
675 #checks if the Players inventory is full
676 if Item1 != 0 and Item2 != 0 and Item3 != 0 and Item4 != 0 and Item5 != 0:
677 full = True
678
679 #maximum buying weapons are 4th Tier so Max is 9+3
680 if Weapon < 10:
681 SaleWeapon = Weapon + 3
682 else:
683 SaleWeapon = 50
684 if Arm == 19:
685 SaleArm = 20
686 else:
687 SaleArm = 50
688 if Helm == 17:
689 SaleHelm = 18
690 else:
691 SaleHelm = 50
692 SalePotion = 16
693
694 Place = 0
695 #seeing what the next weapon or armour above the current is.
696 if SaleWeapon != 50:
697 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(SaleWeapon,))
698 WeaponSaleList = ITEM.fetchone()
699 for x in WeaponSaleList:
700 if Place == 1:
701 WeaponName = x
702 Place += 1
703 Place = 0
704
705 if SaleArm != 50:
706 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(SaleArm,))
707 ArmourSaleList = ITEM.fetchone()
708 for x in ArmourSaleList:
709 if Place ==1:
710 ArmourName = x
711 Place += 1
712 Place = 0
713
714 if SaleHelm != 50:
715 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(SaleHelm,))
716 HelmetSaleList = ITEM.fetchone()
717 for x in HelmetSaleList:
718 if Place == 1:
719 HelmetName = x
720 Place += 1
721
722
723 #prints list where if there is not weapon or armour above the current it will print that it is out of stock.
724 if SaleWeapon != 50:
725 print("\n1.",WeaponName,"-50 Gold")
726 else:
727 print("1. Out of Stock")
728 if SaleArm != 50:
729 print("2.",ArmourName,"-75 Gold")
730 else:
731 print("2.Out of Stock")
732 if SaleHelm != 50:
733 print("3.",HelmetName,"-25 Gold")
734 else:
735 print("3. Out of Stock")
736 print("4. Healing Potion -25 Gold")
737 print("9. Exit Shop")
738 print("\nYou own",Gold,"Gold")
739
740 if Item1 == 0:
741 A = "Empty"
742 else:
743 A = "Health Potion"
744 if Item2 == 0:
745 B = "Empty"
746 else:
747 B = "Health Potion"
748 if Item3 == 0:
749 C = "Empty"
750 else:
751 C = "Health Potion"
752 if Item4 == 0:
753 D = "Empty"
754 else:
755 D = "Health Potion"
756 if Item5 == 0:
757 E = "Empty"
758 else:
759 E = "Health Potion"
760
761 Weapon = int(Weapon)
762 Arm = int(Arm)
763 Helm = int(Helm)
764
765 while True:
766 try:
767 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Weapon,))
768
769 Place = 0
770 Gear = ITEM.fetchone()
771 ItemData = ["","","","",""]
772
773 for G in Gear:
774 ItemData[Place] = G
775 Place += 1
776
777 WeaponName = ItemData[1]
778 break
779 except:
780 print("Weapon Error Bug!")
781
782 while True:
783 try:
784 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Arm,))
785
786 Place = 0
787 Gear = ITEM.fetchone()
788 ItemData = ["","","","",""]
789
790 for G in Gear:
791 ItemData[Place] = G
792 Place += 1
793
794 ArmName = ItemData[1]
795 break
796 except:
797 print("Armour Bug!")
798
799 while True:
800 try:
801 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Helm,))
802
803 Place = 0
804 Gear = ITEM.fetchone()
805 ItemData = ["","","","",""]
806
807 for G in Gear:
808 ItemData[Place] = G
809 Place += 1
810 HelmName = ItemData[1]
811 break
812 except:
813 print("Helmet Bug")
814
815 print("\n\nCurrent Inventory:\n##################\n\t1.",A,"\n\t2.",B,"\n\t3.",C,"\n\t4.",D,"\n\t5.",E,"\n\nHealth:",Health,"Max Health:",(10+(Level*2)),"\nGold:",Gold,"\n\nHelmet:",HelmName,"\nArmour:",ArmName,"\nWeapon:",WeaponName)
816
817
818 PlayerOption = input("> ")
819 while PlayerOption not in ["1","2","3","4","9"]:
820 print("What was that?")
821 PlayerOption = input("> ")
822
823 if PlayerOption == "1" and SaleWeapon != 50 and Gold>=50:
824 Gold -= 50
825 SoldItem = SaleWeapon
826 elif PlayerOption == "2" and SaleArm != 50 and Gold >= 75:
827 Gold -= 75
828 SoldItem = SaleArm
829 elif PlayerOption == "3" and SaleHelm != 50 and Gold >=25:
830 Gold -= 25
831 SoldItem = SaleHelm
832 elif PlayerOption == "4" and full == False and Gold >= 25:
833 Gold -= 25
834 SoldItem = 16
835 elif PlayerOption == "9":
836 print()
837 SoldItem = 50
838 else:
839 SoldItem = 50
840
841 return SoldItem
842
843#battle function
844def Battle(UserName,Health,Weapon,Arm,Helm,Item1,Item2,Item3,Item4,Item5,Level,Gold):
845 #Random addition on every attack
846
847
848 ###############################################################################################
849 ###############################***GRADE A SORTING ALGORITHM***#################################
850 ###############################################################################################
851
852 #THE USER FILE IS COMPARED WITH THE MONSTER FILE TO SEE WHETHER STATS ARE HIGHER OR LOWER TO
853 #DETERMINE WHETHER AN ATTACK HITS AND HOW MUCH HEALTH THE ENEMY HAS.
854
855
856 Monsterfile = sqlite3.connect("Monsters.db")
857 MONSTER = Monsterfile.cursor()
858
859 Itemfile = sqlite3.connect("Items.db")
860 ITEM = Itemfile.cursor()
861
862 inventory= [Item1,Item2,Item3,Item4,Item5]
863 Weapon = int(Weapon)
864 #data from the player file is used to gain further data about their gear and/or stats
865 #data about monsters is also gained.
866 while True:
867 try:
868 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Weapon,))
869
870 Place = 0
871 Gear = ITEM.fetchone()
872 ItemData = ["","","","",""]
873
874 for G in Gear:
875 ItemData[Place] = G
876 Place += 1
877
878 Buff = ItemData[2]
879 break
880 except:
881 print("Weapon Error Bug!")
882 print("Error Error\n\nline640 ish")
883 while True:
884 try:
885 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Arm,))
886
887 Place = 0
888 Gear = ITEM.fetchone()
889 ItemData = ["","","","",""]
890
891 for G in Gear:
892 ItemData[Place] = G
893 Place += 1
894
895 AC = 0
896 AC += ItemData[2]
897 break
898 except:
899 print("Armour Bug!")
900
901 while True:
902 try:
903 ITEM.execute("SELECT * FROM Items WHERE Id = ?",(Helm,))
904
905 Place = 0
906 Gear = ITEM.fetchone()
907 ItemData = ["","","","",""]
908
909 for G in Gear:
910 ItemData[Place] = G
911 Place += 1
912
913 AC += ItemData[2]
914 break
915 except:
916 print("Helmet Bug")
917
918 LoadMonster=["","","",""]
919
920 M = random.randint(1,10)
921 while True:
922 try:
923 MONSTER.execute("SELECT * FROM Monsters WHERE Id = ?",(M,))
924
925 Load = MONSTER.fetchone()
926 Place = 0
927
928 for x in Load:
929 LoadMonster[Place] = x
930 Place += 1
931
932 MonsterName = LoadMonster[1]
933 MonsterHealth = LoadMonster[2]
934 MonsterAtk = LoadMonster[3]
935 break
936 except:
937 print("Monster Bug!")
938
939 #begin battle sequence
940 Intro = random.randint(1,3)
941
942 if Intro ==1:
943 print(UserName," stop! A ",MonsterName," is aproaching!")
944 elif Intro ==2:
945 print("That ",MonsterName," looks like it is about to charge... Prepare to fight!")
946 elif Intro ==3:
947 print("It's an ambush! Kill the ",MonsterName,"!")
948
949 print("[In a Battle type the action you wish to perform (attack, blast, shoot etc.) or the number of the item you wish to use from your inventory]")
950 print("What will you do?")
951 while Health > 0 and MonsterHealth > 0:
952 moved = False
953 print("\nATK",Buff)
954 print("DEF",AC)
955 print("\nHP:",Health)
956 while not moved:
957 PlayerOption = input("\n> ")
958 Addition = random.randint(-2,+2)
959 chance = random.randint(1,20)
960 if (PlayerOption in ["attack","hit","smack","shoot","fire","kill","zap","loose","throw","chuck","blast","slice","cut","stab","finish","hug"]) and (8 < chance < 20):
961 MonsterHealth -= (Buff + Addition)
962 moved = True
963 #calculates damage output
964 print("\nYou did",(Buff + Addition),"damage to the",MonsterName)
965 elif PlayerOption in ["1","2","3","4","5"]:
966 #seeing if user has potions to use
967 if inventory[int(PlayerOption-1)] == 0:
968 print("You have no Potions in that slot")
969 else:
970 Health += 5
971 if Health > 10+(Level*2):
972 Health = 10 +(Level*2)
973 inventory[int(PlayerOption-1)] = 0
974
975 moved = True
976 elif (PlayerOption in ["attack","hit","smack","shoot","fire","kill","zap","loose","throw","chuck","blast","slice","cut","stab","finish","hug"]) and(chance==20):
977 #critical hit
978 MonsterHealth -=(Buff*2 + Addition)
979 moved = True
980 print("\nCritical Hit!")
981 print("You did",(Buff + Addition),"damage to the",MonsterName)
982 elif (PlayerOption in ["attack","hit","smack","shoot","fire","kill","zap","loose","throw","chuck","blast","slice","cut","stab","finish","hug"]):
983 #miss
984 print("You missed!")
985 moved = True
986 else:
987 print("That's not an action you can make.")
988
989 Addition = random.randint(-5,+1)
990 chance = random.randint(1,20)
991 Damage = MonsterAtk + Addition
992 #monster attack
993 if Damage<0:
994 Damage=0
995 if AC < chance and MonsterHealth > 0:
996 Health -= Damage
997 print("The",MonsterName,"did",Damage,"damage to you!")
998 else:
999 print("The",MonsterName,"missed!")
1000
1001
1002
1003 if Health <= 0:
1004 if inventory[0] == 16:
1005 Health = 10 +(Level*2)
1006 print("I just gave Healthyou a potion becuase otherwise you would've died.")
1007 inventory[0] = 0
1008 elif inventory[1] == 16:
1009 Health = 10 +(Level*2)
1010 print("I just gave you a potion becuase otherwise you would've died.")
1011 inventory[1] = 0
1012 elif inventory[2] == 16:
1013 Health = 10 +(Level*2)
1014 print("I just gave you a potion becuase otherwise you would've died.")
1015 inventory[2] = 0
1016 elif inventory[3] == 16:
1017 Health = 10 +(Level*2)
1018 print("I just gave you a potion becuase otherwise you would've died.")
1019 inventory[3] = 0
1020 elif inventory[4] == 16:
1021 Health = 10 +(Level*2)
1022 print("I just gave you a potion becuase otherwise you would've died.")
1023 inventory[4] = 0
1024 else:
1025 print("You died, do you wish to continue?(y/n)")
1026 PlayerOption = input("> ")
1027 if PlayerOption in ["y","Y","Yes","YES","yes","yES"]:
1028 Loadgame(Userfile, USER, LoadArray)
1029
1030
1031 return Health
1032
1033Map = []
1034Count = 0
1035
1036for Row in range(20):
1037 MapRow = []
1038 for Column in range(20):
1039 MapRow.append(MapData[Count])
1040 Count+=1
1041 Map.append(MapRow)
1042
1043Running = True
1044while Running:
1045 if Main() == 1 :
1046 Loadgame()
1047 elif Main() == 2 :
1048 Newgame()
1049 PlayGame()
1050 elif Main() ==3:
1051 Leadboard()
1052 elif Main() == 9:
1053 exit