This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_game.py
641 lines (547 loc) · 21.9 KB
/
math_game.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import random
import time
with open('math_highscore.txt' , 'r+') as hiscore:
highscore = hiscore.read()
if highscore == '':
hiscore.write('No_one:0')
score = 0
def sleep(num):
time.sleep(num)
def load():
for i in range(101):
print(f"{i}% done", end = '\r', flush=True)
sleep(random.uniform(0, 0.04))
if i == 100:
sleep(1)
print("Complete!\n")
sleep(2)
def title(title):
print('-' * len(title))
print(title)
print('-' * len(title))
def type_effect(text):
for letter in text:
print(letter, end='', flush=True)
sleep(0.01)
def leave():
with open('math_highscore.txt' , 'r+') as hiscore:
highscore = hiscore.read()
key, value = highscore.split(':')
mydict = {}
mydict[key] = value
names = mydict.keys()
name = list(names)[0]
hscores = mydict.values()
hscore = list(hscores)[0]
int_hscore = int(hscore)
if score > int_hscore:
type_effect(f"NEW HIGH SCORE! Your score was {score} points!\n")
name = input("Enter your name: ")
if ':' in name:
while ':' in name:
type_effect("Invalid character used. Please don't use a colon(:).\n")
name = input("Enter your name: ")
elif len(name) < 3:
while len(name) < 3:
type_effect("Please make sure your name is greater than 3 characters.\n")
name = input("Enter your name: ")
elif len(name) > 20:
while len(name) > 20:
type_effect("Please make sure your name is lesser than 20 characters.\n")
name = input("Enter your name: ")
hiscore.truncate(0)
hiscore.seek(0)
result = f'{name}:{score}'
hiscore.write(f"{result}")
type_effect(f"Congratulations {name} for achieving the new high score!\n")
else:
type_effect(f"HIGH SCORE: {hscore} by {name}\n")
sleep(2)
type_effect("Ok, you are now leaving the game.")
sleep(1)
def menu():
global score
type_effect("\nMenu:\n\n")
with open('math_highscore.txt', 'r') as hi:
highscore = hi.read()
key, value = highscore.split(':')
mydict = {}
mydict[key] = value
names = mydict.keys()
name = list(names)[0]
hscores = mydict.values()
hscore = list(hscores)[0]
print(f"HIGH SCORE: {hscore} by {name}\n")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Indices")
print("6. INSTRUCTIONS")
print("7. CREDITS")
print("8. EXIT\n")
type_effect(f"SCORE: {score}\n\n")
def addition(num_1, num_2, num_3, num_4, p1, p2, l1, l2):
global score
while score >= 0:
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
type_effect(f"What is {num1} + {num2}?\n")
sleep(0.5)
start = time.time()
add_guess = input('>>> ')
end = time.time()
elapsed = end - start
try:
if add_guess == 'quit' or add_guess == 'exit' or add_guess == 'q' or add_guess == 'l':
break
if int(add_guess) == (num1 + num2):
type_effect(f'Correct! That took you {round(elapsed, 2)} seconds to answer!\n')
points = random.randint(p1, p2)
score += points
type_effect(f"You got {points} points!\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
else:
type_effect(f'Incorrect...{num1} + {num2} = {num1 + num2}. But you said it was {add_guess}.\n')
loss = random.randint(l1, l2)
score -= loss
type_effect(f"You lost {loss} points.\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
except ValueError:
type_effect("Invalid input. Try again!\n")
else:
type_effect('You lost!\n')
def subtraction(num_1, num_2, num_3, num_4, p1, p2, l1, l2):
global score
while score >= 0:
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
type_effect(f"What is {num1} - {num2}?\n")
sleep(0.5)
start = time.time()
sub_guess = input('>>> ')
end = time.time()
elapsed = end - start
try:
if sub_guess == 'quit' or sub_guess == 'exit' or sub_guess == 'q' or sub_guess == 'l':
break
if int(sub_guess) == (num1 - num2):
type_effect(f'Correct! That took you {round(elapsed, 2)} seconds to answer!\n')
points = random.randint(p1, p2)
score += points
type_effect(f"You got {points} points!\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
else:
type_effect(f'Incorrect...{num1} - {num2} = {num1 - num2}. But you said it was {sub_guess}.\n')
loss = random.randint(l1, l2)
score -= loss
type_effect(f"You lost {loss} points.\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
except ValueError:
type_effect("Invalid input. Try again!\n")
else:
type_effect('You lost!\n')
def multiplication(num_1, num_2, num_3, num_4, p1, p2, l1, l2):
global score
while score >= 0:
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
mul = ['times', '*']
type_effect(f"What is {num1} {random.choice(mul)} {num2}?\n")
sleep(0.5)
start = time.time()
mul_guess = input('>>> ')
end = time.time()
elapsed = end - start
try:
if mul_guess == 'quit' or mul_guess == 'exit' or mul_guess == 'q' or mul_guess == 'l':
break
if int(mul_guess) == (num1 * num2):
type_effect(f'Correct! That took you {round(elapsed, 2)} seconds to answer!\n')
points = random.randint(p1, p2)
score += points
type_effect(f"You got {points} points!\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
else:
type_effect(f'Incorrect...{num1} {random.choice(mul)} {num2} = {num1 * num2}. ')
type_effect(f'But you said it was {mul_guess}.\n')
loss = random.randint(l1, l2)
score -= loss
type_effect(f"You lost {loss} points.\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
except ValueError:
type_effect("Invalid input. Try again!\n")
else:
type_effect('You lost!\n')
sleep(2)
def division(num_1, num_2, num_3, num_4, p1, p2, l1, l2):
global score
while score >= 0:
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
while not(num1 % num2 == 0 and num1 != num2 and num1 > num2):
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
div = ['/']
type_effect(f"What is {num1} {random.choice(div)} {num2}?\n")
sleep(0.5)
start = time.time()
div_guess = input('>>> ')
end = time.time()
elapsed = end - start
try:
if div_guess == 'quit' or div_guess == 'exit' or div_guess == 'q' or div_guess == 'l':
break
if int(div_guess) == (num1 / num2):
type_effect(f'Correct! That took you {round(elapsed, 2)} seconds to answer!\n')
points = random.randint(p1, p2)
score += points
type_effect(f"You got {points} points!\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
else:
type_effect(f'Incorrect...{num1} {random.choice(div)} {num2} = {num1 / num2}.')
type_effect(f'But you said it was {div_guess}.\n')
loss = random.randint(l1, l2)
score -= loss
type_effect(f"You lost {loss} points.\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
except ValueError:
type_effect("Invalid input. Try again!\n")
else:
type_effect('You lost!\n')
sleep(2)
def indices(num_1, num_2, num_3, num_4, p1, p2, l1, l2):
global score
while score >= 0:
num1 = random.randint(num_1, num_2)
num2 = random.randint(num_3, num_4)
ind = ['to the power', '**']
type_effect(f"What is {num1} {random.choice(ind)} {num2}?\n")
sleep(0.5)
start = time.time()
ind_guess = input('>>> ')
end = time.time()
elapsed = end - start
try:
if ind_guess == 'quit' or ind_guess == 'exit' or ind_guess == 'q' or ind_guess == 'l':
break
if int(ind_guess) == (num1 ** num2):
type_effect(f'Correct! That took you {round(elapsed, 2)} seconds to answer!\n')
points = random.randint(p1, p2)
score += points
type_effect(f"You got {points} points!\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
else:
type_effect(f'Incorrect...{num1} {random.choice(ind)} {num2} = {num1 ** num2}.')
type_effect(f'But you said it was {ind_guess}.\n')
loss = random.randint(l1, l2)
score -= loss
type_effect(f"You lost {loss} points.\n")
type_effect(f"SCORE: {score}\n")
sleep(0.5)
except ValueError:
type_effect("Invalid input. Try again!\n")
else:
type_effect('You lost!\n')
sleep(2)
title(" Crazy Numbers! ")
load()
while score >= 0:
menu()
choice = input("Enter your choice: ")
while choice not in ['1', '2', '3', '4', '5', '6', '7', '8']:
type_effect("Invalid menu option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, or 8.\n")
choice = input("Please choose a valid option: ")
if choice == '8':
leave()
break
elif choice == '6':
title(" INSTRUCTIONS ")
type_effect("This is a simple game built with the intention to ")
type_effect("increase the problem-solving ability of an individual.\n")
type_effect("Here are the instructions: \n\n")
print(" 1. Choose the type of problem that you want to solve. For example, enter 1 if you want to do addition.")
print(" 2. Within that, choose the difficulty level that you want to attempt.")
print(" 3. If you ever want to leave, just enter 'quit' or 'exit'. It will return you back to the main menu.")
print(" Tip: You could just enter 'q' or 'l' which stands for quit and leave respectively.")
sleep(5)
input("Press 'enter' to return to the menu: ")
sleep(0.5)
elif choice == '7':
title(" CREDITS ")
type_effect("The main developer: Shashwat Bhandari\n\n")
print("Creator: Shashwat Bhandari")
print("Scripter: Shashwat Bhandari")
print("Tester: Shashwat Bhandari\n")
sleep(3)
input("Press 'enter' to return to the menu: ")
sleep(0.5)
elif choice == '1':
type_effect("OK! Choose the difficulty level:\n")
type_effect("Addition Levels available:\n\n")
print(" 1. Effortless")
print(" 2. Easy")
print(" 3. Medium")
print(" 4. Difficult")
print(" 5. Extreme")
print(" 6. Insane")
print(" 7. Crazy")
print(" 8. MENU")
print(" 9. QUIT\n")
type_effect(f" SCORE: {score}\n\n")
add_choice = input("Choose the level that you want to attempt: ")
while add_choice not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
type_effect("Invalid option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, 8, or 9.\n")
add_choice = input("Please choose a valid option: ")
if add_choice == '9':
leave()
break
elif add_choice == '8':
type_effect("OK! Now returning to the menu.\n")
sleep(1)
continue
elif add_choice == '1':
type_effect("OK! This will be a piece of cake!\n")
sleep(1)
addition(0, 10, 0, 25, 2, 8, 2, 6)
elif add_choice == '2':
type_effect("OK! This will be quite easy!\n")
sleep(1)
addition(10, 60, 25, 75, 8, 14, 6, 10)
elif add_choice == '3':
type_effect("OK! This will be quite hard!\n")
sleep(1)
addition(20, 135, 75, 150, 14, 20, 10, 14)
elif add_choice == '4':
type_effect("OK! This will be quite difficult!\n")
sleep(1)
addition(30, 485, 150, 500, 20, 26, 14, 18)
elif add_choice == '5':
type_effect("OK! This will be very difficult!\n")
sleep(1)
addition(40, 985, 500, 1000, 26, 32, 18, 22)
elif add_choice == '6':
type_effect("OK! This will be insanely difficult!\n")
sleep(1)
addition(50, 4985, 1000, 5000, 32, 38, 22, 26)
elif add_choice == '7':
type_effect("Good luck! You will need it...\n")
sleep(1)
addition(100, 14985, 5000, 15000, 38, 50, 26, 30)
elif choice == '2':
type_effect("OK! Choose the difficulty level:\n")
type_effect("Subtraction Levels available:\n\n")
print(" 1. Effortless")
print(" 2. Easy")
print(" 3. Medium")
print(" 4. Difficult")
print(" 5. Extreme")
print(" 6. Insane")
print(" 7. Crazy")
print(" 8. MENU")
print(" 9. QUIT\n")
type_effect(f" SCORE: {score}\n\n")
sub_choice = input("Choose the level that you want to attempt: ")
while sub_choice not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
type_effect("Invalid option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, 8, or 9.\n")
sub_choice = input("Please choose a valid option: ")
if sub_choice == '9':
leave()
break
elif sub_choice == '8':
type_effect("OK! Now returning to the menu.\n")
sleep(1)
continue
elif sub_choice == '1':
type_effect("OK! This will be a piece of cake!\n")
sleep(1)
subtraction(10, 25, 0, 10, 2, 8, 2, 6)
elif sub_choice == '2':
type_effect("OK! This will be quite easy!\n")
sleep(1)
subtraction(40, 75, 10, 40, 8, 14, 6, 10)
elif sub_choice == '3':
type_effect("OK! This will be quite hard!\n")
sleep(1)
subtraction(75, 150, 40, 75, 14, 20, 10, 14)
elif sub_choice == '4':
type_effect("OK! This will be quite difficult!\n")
sleep(1)
subtraction(150, 500, 75, 150, 20, 26, 14, 18)
elif sub_choice == '5':
type_effect("OK! This will be very difficult!\n")
sleep(1)
subtraction(500, 1000, 150, 750, 26, 32, 18, 22)
elif sub_choice == '6':
type_effect("OK! This will be insanely difficult!\n")
sleep(1)
subtraction(1000, 5000, 750, 3000, 32, 38, 22, 26)
elif sub_choice == '7':
type_effect("Good luck! You will need it...\n")
sleep(1)
subtraction( 5000, 15000, 3000, 10000, 38, 50, 26, 30)
elif choice == '3':
type_effect("OK! Choose the difficulty level:\n")
type_effect("Multiplication Levels available:\n\n")
print(" 1. Effortless")
print(" 2. Easy")
print(" 3. Medium")
print(" 4. Difficult")
print(" 5. Extreme")
print(" 6. Insane")
print(" 7. Crazy")
print(" 8. MENU")
print(" 9. QUIT\n")
type_effect(f" SCORE: {score}\n\n")
mul_choice = input("Choose the level that you want to attempt: ")
while mul_choice not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
type_effect("Invalid option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, 8, or 9.\n")
mul_choice = input("Please choose a valid option: ")
if mul_choice == '9':
leave()
break
elif mul_choice == '8':
type_effect("OK! Now returning to the menu.\n")
sleep(1)
continue
elif mul_choice == '1':
type_effect("OK! This will be a piece of cake!\n")
sleep(1)
multiplication(0, 5, 0, 5, 2, 8, 2, 6)
elif mul_choice == '2':
type_effect("OK! This will be quite easy!\n")
sleep(1)
multiplication(5, 10, 5, 10, 8, 14, 6, 10)
elif mul_choice == '3':
type_effect("OK! This will be quite hard!\n")
sleep(1)
multiplication(3, 15, 3, 15, 14, 20, 10, 14)
elif mul_choice == '4':
type_effect("OK! This will be quite difficult!\n")
sleep(1)
multiplication(5, 20, 5, 20, 20, 26, 14, 18)
elif mul_choice == '5':
type_effect("OK! This will be very difficult!\n")
sleep(1)
multiplication(7, 50, 7, 50, 26, 32, 18, 22)
elif mul_choice == '6':
type_effect("OK! This will be insanely difficult!\n")
sleep(1)
multiplication(9, 100, 9, 100, 32, 38, 22, 26)
elif mul_choice == '7':
type_effect("Good luck! You will need it...\n")
sleep(1)
multiplication( 11, 200, 11, 200, 38, 50, 26, 30)
elif choice == '4':
type_effect("OK! Choose the difficulty level:\n")
type_effect("Division Levels available:\n\n")
print(" 1. Effortless")
print(" 2. Easy")
print(" 3. Medium")
print(" 4. Difficult")
print(" 5. Extreme")
print(" 6. Insane")
print(" 7. Crazy")
print(" 8. MENU")
print(" 9. QUIT\n")
type_effect(f" SCORE: {score}\n\n")
div_choice = input("Choose the level that you want to attempt: ")
while div_choice not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
type_effect("Invalid option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, 8, or 9.\n")
div_choice = input("Please choose a valid option: ")
if div_choice == '9':
leave()
break
elif div_choice == '8':
type_effect("OK! Now returning to the menu.\n")
sleep(1)
continue
elif div_choice == '1':
type_effect("OK! This will be a piece of cake!\n")
sleep(1)
division(0, 50, 1, 50, 2, 8, 2, 6)
elif div_choice == '2':
type_effect("OK! This will be quite easy!\n")
sleep(1)
division(5, 100, 5, 100, 8, 14, 6, 10)
elif div_choice == '3':
type_effect("OK! This will be quite hard!\n")
sleep(1)
division(10, 200, 10, 200, 14, 20, 10, 14)
elif div_choice == '4':
type_effect("OK! This will be quite difficult!\n")
sleep(1)
division(20, 300, 20, 300, 20, 26, 14, 18)
elif div_choice == '5':
type_effect("OK! This will be very difficult!\n")
sleep(1)
division(30, 400, 30, 400, 26, 32, 18, 22)
elif div_choice == '6':
type_effect("OK! This will be insanely difficult!\n")
sleep(1)
division(40, 500, 40, 500, 32, 38, 22, 26)
elif div_choice == '7':
type_effect("Good luck! You will need it...\n")
sleep(1)
division( 50, 1000, 50, 1000, 38, 50, 26, 30)
elif choice == '5':
type_effect("OK! Choose the difficulty level:\n")
type_effect("Indices Levels available:\n\n")
print(" 1. Effortless")
print(" 2. Easy")
print(" 3. Medium")
print(" 4. Difficult")
print(" 5. Extreme")
print(" 6. Insane")
print(" 7. Crazy")
print(" 8. MENU")
print(" 9. QUIT\n")
type_effect(f" SCORE: {score}\n\n")
ind_choice = input("Choose the level that you want to attempt: ")
while ind_choice not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
type_effect("Invalid option!\nPlease choose from 1, 2, 3, 4, 5, 6, 7, 8, or 9.\n")
ind_choice = input("Please choose a valid option: ")
if ind_choice == '9':
leave()
break
elif ind_choice == '8':
type_effect("OK! Now returning to the menu.\n")
sleep(1)
continue
elif ind_choice == '1':
type_effect("OK! This will be a piece of cake!\n")
sleep(1)
indices(0, 5, 0, 3, 2, 8, 2, 6)
elif ind_choice == '2':
type_effect("OK! This will be quite easy!\n")
sleep(1)
indices(3, 10, 2, 3, 8, 14, 6, 10)
elif ind_choice == '3':
type_effect("OK! This will be quite hard!\n")
sleep(1)
indices(5, 15, 2, 3, 14, 20, 10, 14)
elif ind_choice == '4':
type_effect("OK! This will be quite difficult!\n")
sleep(1)
indices(10, 20, 2, 3, 20, 26, 14, 18)
elif ind_choice == '5':
type_effect("OK! This will be very difficult!\n")
sleep(1)
indices(5, 20, 3, 4, 26, 32, 18, 22)
elif ind_choice == '6':
type_effect("OK! This will be insanely difficult!\n")
sleep(1)
indices(2, 10, 3, 5, 32, 38, 22, 26)
elif ind_choice == '7':
type_effect("Good luck! You will need it...\n")
sleep(1)
indices( 10, 30, 3, 5, 38, 50, 26, 30)