-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
421 lines (346 loc) · 11.1 KB
/
main.py
File metadata and controls
421 lines (346 loc) · 11.1 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
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
import msvcrt
import os, sys, inspect
import time
import pickle
import minewalker
import cursor
import pickle
cursor.hide()
# Simple class that stores ANSI Escape characters for specific colours
class color:
BOLD = '\u001b[1m'
UNDERLINE = '\u001b[4m'
SELECT = '\033[47m'
BASE = '\033[0m'
RED = '\033[91m'
YELLOW = ''
BLACK = '\033[30m'
GREEN = '\033[92m'
#Main menu UI object
class UI(object):
def __init__(self):
self.keylog = []
self.menulist = ["Play","Instructions","Leaderboard","Settings","Credits","Exit"]
self.functionlist = ["Play","Instructions","Leaderboard","Settings","Credits","Exit"]
self.currentSelection = 0
self.mygame = minewalker.Game()
self.highscores = {0:"Null",1:"Null",2:"Null",3:"Null",4:"Null",5:"Null"}
self.highscoreNames = []
self.highscoreList = []
self.load_leaderboard()
self.reorganise_hs()
def load_leaderboard(self):
try:
with open("highscores.dat","rb") as save:
# Highscore dictionary
self.highscores = pickle.load(save)
except:
pass
def reorganise_hs(self):
for score, name in sorted(self.highscores.items()):
self.highscoreNames.append(name)
self.highscoreList.append(score)
self.highscoreList.reverse()
self.highscoreNames.reverse()
def check_highscore(self, score):
# print(self.highscoreList)
if score > self.highscoreList[3]:
return True
else:
return False
def save_highscore(self):
with open("highscores.dat","wb") as sav:
pickle.dump(self.highscores,sav)
# Functions for each of the menu items
def cls(self, type = 0):
os.system("cls")
def wait(self):
while True:
if msvcrt.kbhit():
msvcrt.getch()
return 0
def save_to_disk(self,characterList):
with open('settings.dat','wb') as save:
pickle.dump(characterList, save)
def load_settings(self):
try:
with open('settings.dat','rb') as save:
characterList = pickle.load(save)
self.mygame.playerChar = characterList[0]
self.mygame.mineChar = characterList[1]
self.mygame.pathChar = characterList[2]
self.mygame.boxChar = characterList[3]
except:
pass
# Main play function
def play(self):
self.load_settings()
self.cls()
word = "Difficulty:"
width= os.get_terminal_size().columns
half = width/2
for listIndex, option in enumerate(word):
blankchars = half - len(word)/2
chars = ""
for x in range(int(blankchars)):
chars += " "
print(chars + color.UNDERLINE + word + color.BASE)
modes = ["Beginner", "Easy", "Normal", "Moderate", "Hard", "Extreme", "Ultra Madness"]
self.print_list(modes,cls=False)
if self.move_list(modes,cls=False) == 0:
self.mygame.length = 10
self.mygame.nMines = 15
self.mygame.displayInterval = 2
self.mygame.powerups = 100
if self.currentSelection == 0:
self.mygame.powerups = 30
self.mygame.displayInterval = 2.5
self.mygame.length = 10
self.mygame.nMines = 15
elif self.currentSelection == 1:
self.mygame.displayInterval = 2.0
self.mygame.powerups = 25
self.mygame.length = 12
self.mygame.nMines = 17
elif self.currentSelection == 2:
self.mygame.powerups = 20
self.mygame.displayInterval = 1.5
self.mygame.length = 14
self.mygame.nMines = 20
elif self.currentSelection == 3:
self.mygame.length = 16
self.mygame.powerups = 15
self.mygame.displayInterval = 1.2
self.mygame.nMines = 25
elif self.currentSelection == 4:
self.mygame.length = 18
self.mygame.powerups = 10
self.mygame.displayInterval = 1.0
self.mygame.nMines = 30
elif self.currentSelection == 5:
self.mygame.length = 20
self.mygame.powerups = 8
self.mygame.displayInterval = 0.8
self.mygame.nMines = 40
elif self.currentSelection == 6:
self.mygame.length = 22
self.mygame.powerups = 5
self.mygame.displayInterval = 0.5
self.mygame.nMines = 60
else:
pass
self.mygame.run()
score = self.mygame.score
if self.check_highscore(score) == True:
print("You got a High Score!")
name = str(input("Enter your Name: "))
self.highscores[score] = name
self.reorganise_hs()
print("\nWould you like to Play Again?")
option_list = ["Yes", "No"]
print("\n")
self.currentSelection = 0
self.print_list(option_list,cls=False)
if self.move_list(option_list,cls=False) == 0:
if self.currentSelection == 0:
self.cls()
self.play()
return 0
else:
self.cls()
return 0
# Instructions function
def instructions(self):
# self.cls()
instructions = """Welcome to MineWalker!\n\n{0}Objective:{1}
\nReach the last square of the minefield without setting off any mines.\n
\n{2}How to Play:{3}
\nThe position of each mine will be shown to you for a small amount of time depending on your level of difficulty.
\nRemember where the mines are and navigate the minefield using W A S D or the ARROW KEYS to get to the cell at the intersection of the last row and column.\n
\n{4}Special Abilities:{5}
\nQ --> Scan for mines in immediate squares around you
""".format(color.UNDERLINE, color.BASE, color.UNDERLINE, color.BASE, color.UNDERLINE, color.BASE)
print(instructions)
print("\n\n[ Press ANY KEY to Return to Main Menu ]")
self.wait()
return 0
# Leaderboard function
def leaderboard(self):
self.reorganise_hs()
self.highscoreNames=self.highscoreNames[:4]
self.highscoreList=self.highscoreList[:4]
for iterator in range(len(self.highscoreNames)):
print(str(self.highscoreNames[iterator]) + ": " + str(self.highscoreList[iterator]))
print("\n\n[ Press ANY KEY to Return to Main Menu ]")
self.wait()
return 0
# Settings function
def settings(self):
self.currentSelection = 0
playerChar = self.mygame.playerChar
mineChar = self.mygame.mineChar
pathChar = self.mygame.pathChar
boxChar = self.mygame.boxChar
menulist = [f"Choose your player character: {playerChar}", f"Choose your mine character: {mineChar}", f"Choose your path character: {pathChar}",
f"Choose your box character: {boxChar}", "Save Changes", "Reset to Defaults", "Return to Main Menu"]
self.print_list(menulist)
# select = True
characterList = [playerChar, mineChar, pathChar, boxChar]
gameCharList = [self.mygame.playerChar, self.mygame.mineChar, self.mygame.pathChar, self.mygame.boxChar]
runSettings = True
def reset(characterList):
# print(selIndex)
# print("rests")
playerChar = "O"
mineChar = "#"
pathChar = "."
boxChar = "_"
characterList = [playerChar, mineChar, pathChar, boxChar]
# print(characterList)
self.mygame.playerChar = characterList[0]
self.mygame.mineChar = characterList[1]
self.mygame.pathChar = characterList[2]
self.mygame.boxChar = characterList[3]
return characterList
while runSettings:
select = False
if self.move_list(menulist) == 0:
self.print_list(menulist)
selIndex = self.currentSelection
# Return
if selIndex == len(menulist)-1:
runSettings = False
return 0
# Reset to Default
elif selIndex == len(menulist)-2:
characterList = reset(characterList)
# Save Changes
elif selIndex == len(menulist)-3:
for eachChar in characterList:
if characterList.count(eachChar)>1:
print("\nPlease Enter separate, valid characters!")
time.sleep(1.3)
characterList = reset(characterList)
break
self.mygame.playerChar = characterList[0]
self.mygame.mineChar = characterList[1]
self.mygame.pathChar = characterList[2]
self.mygame.boxChar = characterList[3]
self.save_to_disk(characterList)
print("\n Changes Saved!")
time.sleep(1)
else:
select = True
print("Please type your chosen symbol.")
while select == True:
if msvcrt.kbhit():
char = self.capture_input()[0]
menulist[selIndex] = menulist[selIndex][:-1] + str(char)
print(selIndex)
characterList[selIndex] = char
select = False
for i in range(4):
# print('yes')
# time.sleep(0.2)
# print(menulist)
# print(characterList)
menulist[i] = menulist[i][:-1] + characterList[i]
self.print_list(menulist)
# Credits function
def credits(self):
credits = """CREDITS:\n
\nAdrien Plisson: Inspiration for restrict function\nPraveen Gollakota: Inspiration for Dispatcher method\nRealPython: Basic Python usage and Syntax\nGeeksForGeeks: Class members and Terminal size usage\n\n[ Press ANY KEY to Return to Main Menu ]
\n© Copyright Muse_HD 2021 """
print(credits)
self.wait()
return 0
# Game exit
def exit(self):
self.save_highscore()
sys.exit("Thank you for playing my game!")
# Method for printing list
def print_list(self, menulist, cls = True):
listIndex = 0
# clear = '\033[{0}A'.format(len(menulist)+1)
width= os.get_terminal_size().columns
half = width/2
# Clear screen vs moving cursor
if cls:
self.cls()
else:
print()
print('\033[{0}A'.format(len(menulist)+2))
# Add padding to center menu items
for listIndex, option in enumerate(menulist):
blankchars = half - len(option)/2
chars = ""
for x in range(int(blankchars)):
chars += " "
# Highlight selected menu lists
if listIndex == self.currentSelection:
print((chars + color.SELECT + color.BLACK + option + color.BASE))
else:
print((chars + option))
# Gets keystrokes (and arrows) and returns a single string
def capture_input(self):
key_stroke = msvcrt.getch()
if ord(key_stroke) == 224:
key = ord(msvcrt.getch())
if key == 80:
key_stroke = b's'
elif key == 72:
key_stroke = b'w'
elif key == 75:
key_stroke = b'a'
elif key == 77:
key_stroke = b'd'
else:
pass
try:
key_stroke = (str(key_stroke, 'utf-8'))
except UnicodeDecodeError:
pass
self.keylog.append(key_stroke)
return key_stroke
# Function that moves the current selection through the menu list
def move_list(self, menulist, cls = True):
keylist = {"w":-1, "s":+1}
scroll = True
while scroll == True:
if msvcrt.kbhit():
key_stroke = self.capture_input()
if key_stroke == "\r" or key_stroke == " ":
if cls == True:
self.cls()
scroll = False
return 0
elif key_stroke in keylist:
direction = (keylist[key_stroke])
# Copy instance variable to a local var and add the direction
currentSelection = self.currentSelection
currentSelection += direction
# Conditions to prevent menu list from wrapping around
if currentSelection > len(menulist)-1:
currentSelection -= 1
elif currentSelection < 0:
currentSelection += 1
self.currentSelection = currentSelection
self.print_list(menulist,cls)
# # If Enter or Spacebar is pressed, the currently selected function from the menu list is called using eval
def main_menu(self):
mainlist = ["Play","Instructions","Leaderboard","Settings","Credits","Exit"]
# Contains a list of all menu items with lowercase letters to match the specific functions created for them.
dispatch = ["self." + menuitem.lower() for menuitem in mainlist]
if self.move_list(mainlist) == 0:
if eval(dispatch[self.currentSelection])() == 0:
self.currentSelection = 0
self.print_list(mainlist)
return 0
def update(self):
self.capture_input()
# Main Control
menu = UI()
menu.print_list(menu.menulist)
while True:
if msvcrt.kbhit():
menu.main_menu()