-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
1289 lines (1090 loc) · 43.1 KB
/
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
import sys
import os
import random
import mysql.connector as sqltor
# Centers the Pygame window on the user's screen
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100,100)
# Initializes the Pygame module
pygame.init()
########################################################################################################################
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
########################################################################################################################
'''
This script is very long, so I've included a table of contents so it will be easier for people to navigate:
1. Common variables - Line 46
2. Importing foreign assets - Line 62
3. Classes used in the game - Line 82
4. Functions related to MySQL connectivity - Line 294
5. General utility functions - Line 527
5. Functions related to controlling the snakes - Line 575
6. Functions that are used in the flow of the game - Line 662
7. Functions related to singleplayer modes of the game - Line 822
8. Functions related to multiplayer modes of the game - Line 1007
9. Introduction functions of the game - Line 1215
10. This is where the game starts - Line 1291
'''
########################################################################################################################
# Setting up all common variables in the game
FONT = pygame.font.Font(resource_path("8-bit.ttf"), 20)
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
width = 1200
height = 600
rows = 48
columns = 24
Blue = (55,105,148)
Yellow = (255,195,49)
Black = (0,0,0)
White = (255,255,255)
########################################################################################################################
# Setting up all the assets to be used in the game
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Python: The Game")
gameIcon = pygame.image.load(resource_path("Pictures\\Python_logo_icon.png"))
pygame.display.set_icon(gameIcon)
logo = pygame.image.load(resource_path("Pictures\\Python-icon.png"))
logo = pygame.transform.scale(logo, (200, 200))
singleplayer_easy_instructions = pygame.image.load(resource_path("Pictures\\Singleplayer_easy_instructions.PNG"))
singleplayer_hard_instructions = pygame.image.load(resource_path("Pictures\\Singleplayer_hard_instructions.PNG"))
multiplayer_coop_instructions = pygame.image.load(resource_path("Pictures\\Multiplayer_coop_instructions.PNG"))
multiplayer_computer_instructions = pygame.image.load(resource_path("Pictures\\Multiplayer_computer_instructions.PNG"))
click_sound = pygame.mixer.Sound(resource_path("Music\\click.wav"))
eat_sound = pygame.mixer.Sound(resource_path("Music\\eat.ogg"))
hit_sound = pygame.mixer.Sound(resource_path("Music\\hit.wav"))
gamestart_sound = pygame.mixer.Sound(resource_path("Music\\game_start.ogg"))
gameover_sound = pygame.mixer.Sound(resource_path("Music\\game_over.ogg"))
########################################################################################################################
# Classes used in the game
class cube(object):
def __init__(self, start,color,dirnx=1, dirny=0):
self.pos = start
self.dirnx = dirnx
self.dirny = dirny
self.color = color
def move(self, dirnx, dirny):
self.dirnx = dirnx
self.dirny = dirny
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
def draw(self, surface, eyes=False):
dis = width // rows
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(surface, self.color, (i * dis + 1, j * dis + 1, dis, dis))
if eyes:
centre = dis // 2
radius = 3
circleMiddle = (i * dis + centre - radius, j * dis + 8)
circleMiddle2 = (i * dis + dis - radius * 2, j * dis + 8)
pygame.draw.circle(surface, Black, circleMiddle, radius)
pygame.draw.circle(surface, Black, circleMiddle2, radius)
class snake(object):
body = []
turns = {}
def __init__(self, color, pos, left_key, right_key, up_key, down_key):
self.color = color
self.head = cube(pos, color)
self.body = []
self.turns = {}
self.body.append(self.head)
self.dirnx = 0
self.dirny = 1
self.left_key = left_key
self.right_key = right_key
self.up_key = up_key
self.down_key = down_key
def move(self):
for i, c in enumerate(self.body):
p = c.pos[:]
if p in self.turns:
turn = self.turns[p]
c.move(turn[0], turn[1])
if i == len(self.body) - 1:
self.turns.pop(p)
else:
if c.dirnx == -1 and c.pos[0] <= 0:
c.pos = (rows - 1, c.pos[1])
elif c.dirnx == 1 and c.pos[0] >= rows - 1:
c.pos = (0, c.pos[1])
elif c.dirny == 1 and c.pos[1] >= columns - 1:
c.pos = (c.pos[0], 0)
elif c.dirny == -1 and c.pos[1] <= 0:
c.pos = (c.pos[0], columns - 1)
else:
c.move(c.dirnx, c.dirny)
def addCube(self):
tail = self.body[-1]
dx, dy = tail.dirnx, tail.dirny
if dx == 1 and dy == 0:
self.body.append(cube((tail.pos[0] - 1, tail.pos[1]), self.color))
elif dx == -1 and dy == 0:
self.body.append(cube((tail.pos[0] + 1, tail.pos[1]), self.color))
elif dx == 0 and dy == 1:
self.body.append(cube((tail.pos[0], tail.pos[1] - 1), self.color))
elif dx == 0 and dy == -1:
self.body.append(cube((tail.pos[0], tail.pos[1] + 1), self.color))
self.body[-1].dirnx = dx
self.body[-1].dirny = dy
def draw(self, surface):
for i, c in enumerate(self.body):
if i == 0:
c.draw(surface, True)
else:
c.draw(surface)
# A special class only used by Hard Mode to kill the snake when it reaches the edges of the game screen
class snakey(object):
global sh
body = []
turns = {}
def __init__(self, color, pos, left_key, right_key, up_key, down_key):
self.color = color
self.head = cube(pos, color)
self.body = []
self.turns = {}
self.body.append(self.head)
self.dirnx = 0
self.dirny = 1
self.left_key = left_key
self.right_key = right_key
self.up_key = up_key
self.down_key = down_key
def move(self):
for i, c in enumerate(self.body):
p = c.pos[:]
if p in self.turns:
turn = self.turns[p]
c.move(turn[0], turn[1])
if i == len(self.body) - 1:
self.turns.pop(p)
else:
if c.dirnx == -1 and c.pos[0] <= 0:
hit_sound.play()
pygame.time.delay(1000)
die(sh, "hard", singleplayer_hard)
break
elif c.dirnx == 1 and c.pos[0] >= rows - 1:
hit_sound.play()
pygame.time.delay(1000)
die(sh, "hard", singleplayer_hard)
break
elif c.dirny == 1 and c.pos[1] >= columns - 1:
hit_sound.play()
pygame.time.delay(1000)
die(sh, "hard", singleplayer_hard)
break
elif c.dirny == -1 and c.pos[1] <= 0:
hit_sound.play()
pygame.time.delay(1000)
die(sh, "hard", singleplayer_hard)
break
else:
c.move(c.dirnx, c.dirny)
def addCube(self):
tail = self.body[-1]
dx, dy = tail.dirnx, tail.dirny
if dx == 1 and dy == 0:
self.body.append(cube((tail.pos[0] - 1, tail.pos[1]), self.color))
elif dx == -1 and dy == 0:
self.body.append(cube((tail.pos[0] + 1, tail.pos[1]), self.color))
elif dx == 0 and dy == 1:
self.body.append(cube((tail.pos[0], tail.pos[1] - 1), self.color))
elif dx == 0 and dy == -1:
self.body.append(cube((tail.pos[0], tail.pos[1] + 1), self.color))
self.body[-1].dirnx = dx
self.body[-1].dirny = dy
def draw(self, surface):
for i, c in enumerate(self.body):
if i == 0:
c.draw(surface, True)
else:
c.draw(surface)
#A class to handle the test input boxes used in the game
class InputBox:
def __init__(self, x, y, w, h,text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.active = False
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, self.color)
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
########################################################################################################################
# All the functions dealing with MySQL connectivity
def details(main,modey,scorey = 0):
global host, user, passwd, name, mode, pts
clock = pygame.time.Clock()
mode = modey
pts = scorey
host = InputBox(150, 135, 140, 32)
user = InputBox(150, 185, 140, 32)
passwd = InputBox(550, 185, 140, 32)
name= InputBox(150, 385, 140, 32)
list = [host,user,passwd,name]
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
for box in list:
box.handle_event(event)
box.update()
win.fill(White)
words("Enter your MySQL details so your account can be accessed", 30, Black, width//2, 50)
words("Host:", 20, Black, 100, 150)
words("User:", 20, Black, 100, 200)
words("Passwd:", 20, Black, 500, 200)
words("Enter the name under which your score should be saved to the scoreboard", 25, Black, width//2, 300)
words("Name:", 20, Black, 100, 400)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 500 < mouse[0] < 700 and 500 < mouse[1] < 550:
drawButton(Blue, (500, 500, 200, 50), "Submit", White, 20)
if click[0] == 1:
click_sound.play()
done = not done
pygame.time.delay(150)
submit(main)
else:
drawButton(Yellow, (500, 500, 200, 50), "Submit", Black, 20)
for box in list:
box.draw(win)
pygame.display.update()
clock.tick(30)
def submit(main):
sql(mode,name.text,pts,counting_string)
game_over("You ate " +str(pts)+ " apples before you lost!", main)
def sql(mode,name,score,time):
mycon = sqltor.connect(host = host.text,user= user.text,passwd= passwd.text)
cursor = mycon.cursor()
cursor.execute("create database if not exists snake")
cursor.execute("use snake")
cursor.execute("create table if not exists "+mode+" (name varchar(20) not null, score integer not null, time char(5) not null)")
cursor.execute("select * from "+mode)
data = cursor.fetchall()
found = False
for row in data:
if row[0] == name:
if row [1] < score:
found = True
cursor.execute("update "+mode+" set score = {}, time = '{}' where name = '{}' ".format(score,time,name))
break
else:
break
if not found:
cursor.execute("insert into "+mode+" values ('{}',{},'{}')".format(name,score,time))
mycon.commit()
mycon.close()
def scoreboard():
win.fill(White)
flag = True
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 500 < mouse[0] < 700 and 270 < mouse[1] < 320:
drawButton(Blue,(500, 270, 200, 50), "Easy Mode", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
pygame.time.delay(150)
access("easy")
else:
drawButton(Yellow, (500, 270, 200, 50), "Easy Mode", Black, 20)
if 500 < mouse[0] < 700 and 330 < mouse[1] < 380:
drawButton(Blue,(500, 330, 200, 50), "Hard Mode", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
pygame.time.delay(150)
access("hard")
else:
drawButton(Yellow,(500, 330, 200, 50), "Hard Mode", Black, 20)
pygame.display.update()
def access(mode):
global host,user,passwd
clock = pygame.time.Clock()
host = InputBox(150, 135, 140, 32)
user = InputBox(150, 185, 140, 32)
passwd = InputBox(550, 185, 140, 32)
list = [host, user, passwd]
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
for box in list:
box.handle_event(event)
box.update()
win.fill(White)
words("Enter your MySQL details so your account can be accessed", 30, Black, width // 2, 50)
words("Host:", 20, Black, 100, 150)
words("User:", 20, Black, 100, 200)
words("Passwd:", 20, Black, 500, 200)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 500 < mouse[0] < 700 and 500 < mouse[1] < 550:
drawButton(Blue, (500, 500, 200, 50), "Submit", White, 20)
if click[0] == 1:
click_sound.play()
done = not done
pygame.time.delay(150)
access_submit(mode)
else:
drawButton(Yellow, (500, 500, 200, 50), "Submit", Black, 20)
for box in list:
box.draw(win)
pygame.display.update()
clock.tick(30)
def access_submit(mode):
mycon = sqltor.connect(host=host.text, user=user.text, passwd=passwd.text)
cursor = mycon.cursor()
cursor.execute("create database if not exists snake")
cursor.execute("use snake")
cursor.execute("create table if not exists " + mode + " (name varchar(20) not null, score integer not null, time char(5) not null)")
cursor.execute("select * from "+mode)
data = cursor.fetchall()
mycon.close()
scoreboard_display(data,mode)
def scoreboard_display(data,mode):
scoreio = True
while scoreio:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
words("('Name',Score,'Time Taken')",15,(255,0,0),width//2-20,50)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 1000 < mouse[0] < 1120 and 40 < mouse[1] < 65:
drawButton(Blue, (1000, 40, 120, 25), "Delete all scores", White, 10)
if click[0] == 1:
click_sound.play()
scoreio = not scoreio
pygame.time.delay(150)
delete_scores(mode)
else:
drawButton(Yellow, (1000, 40, 120, 25), "Delete all scores", Black, 10)
if 1000 < mouse[0] < 1120 and 70 < mouse[1] < 95:
drawButton(Blue, (1000, 70, 120, 25), "Back to Main Menu", White, 10)
if click[0] == 1:
click_sound.play()
scoreio = not scoreio
pygame.time.delay(150)
main_menu()
else:
drawButton(Yellow, (1000, 70, 120, 25), "Back to Main Menu", Black, 10)
x = 30
y = 100
count = 0
for row in data:
count+=1
words(str(count), 15, (255,0,0),x,y)
words(str(row),15,Black,x+135,y)
y += 50
if y == height:
x += 350
y = 100
pygame.display.update()
def delete_scores(mode):
mycon = sqltor.connect(host=host.text, user=user.text, passwd=passwd.text)
cursor = mycon.cursor()
cursor.execute("use snake")
cursor.execute("drop table "+mode)
mycon.close()
main_menu()
########################################################################################################################
# General utility functions used throughout the game
def words(text,size,color,x_location,y_location):
font = resource_path("8-bit.ttf")
type = pygame.font.Font(font, size)
words = type.render(text,True,color)
wordsRect = words.get_rect()
wordsRect.center = (x_location,y_location)
win.blit(words, wordsRect)
def score(size,x,y,snake,color = Black):
score_keeper = "Score: "+str(len(snake.body)-1)
words(score_keeper,size,color,x,y)
def timer(size,x,y,start_time):
global counting_string
counting_time = pygame.time.get_ticks() - start_time
# change milliseconds into minutes, seconds, milliseconds
counting_minutes = str(counting_time // 60000).zfill(2)
counting_seconds = str((counting_time % 60000) // 1000).zfill(2)
counting_string = "%s:%s" % (counting_minutes, counting_seconds)
words(counting_string,size,Black,x,y)
def drawGrid():
sizebtnr = height//columns
sizebtnc = width//rows
x = 0
for i in range(columns+23):
x = x + sizebtnr
y = 25
for j in range(rows-26):
y = y + sizebtnc
pygame.draw.circle(win, (150, 150, 150), (x,y), 3)
def drawButton(color,position,text,text_color,size):
pygame.draw.rect(win, color, position)
words(text, size, text_color, position[0] + (position[2] // 2), position[1] + (position[3] // 2))
def close():
pygame.quit()
sys.exit()
########################################################################################################################
# All the functions related to controlling the snakes
def movement(snake, keys):
if keys[snake.left_key]:
if snake.dirnx == 1 and snake.dirny == 0:
pass
else:
snake.dirnx = -1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif keys[snake.right_key]:
if snake.dirnx == -1 and snake.dirny == 0:
pass
else:
snake.dirnx = 1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif keys[snake.up_key]:
if snake.dirnx == 0 and snake.dirny == 1 and len(snake.body) > 1:
pass
else:
snake.dirnx = 0
snake.dirny = -1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif keys[snake.down_key]:
if snake.dirnx == 0 and snake.dirny == -1:
pass
else:
snake.dirnx = 0
snake.dirny = 1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
# Edward the Algorithm. This is a rudimentary algorithm that guides the snake head to the target fruit.
def moveAI(snake,snack):
head = snake.body[0]
if snake.dirnx == 1:
if snack.pos[1] > head.pos[1]:
snake.dirnx = 0
snake.dirny = 1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif snack.pos[1] < head.pos[1]:
snake.dirnx = 0
snake.dirny = -1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
else:
pass
elif snake.dirnx == -1:
if snack.pos[1] > head.pos[1]:
snake.dirnx = 0
snake.dirny = 1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif snack.pos[1] < head.pos[1]:
snake.dirnx = 0
snake.dirny = -1
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
else:
pass
elif snake.dirny == 1:
if snack.pos[0] > head.pos[0]:
snake.dirnx = 1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif snack.pos[0] < head.pos[0]:
snake.dirnx = -1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
else:
pass
elif snake.dirny == -1:
if snack.pos[0] > head.pos[0]:
snake.dirnx = 1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
elif snack.pos[0] < head.pos[0]:
snake.dirnx = -1
snake.dirny = 0
snake.turns[snake.head.pos[:]] = [snake.dirnx, snake.dirny]
else:
pass
########################################################################################################################
# General functions related to the flow of the game
def game_over(end_text,play_again):
over = True
while over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
words("GAME OVER",50,Black,width//2,(height//2)-20)
words(end_text,30,Black,width//2,(height//2)+30)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 610 < mouse[0] < 810 and 400 < mouse[1] < 450:
drawButton(Blue, (610, 400, 200, 50), "Main Menu", White, 20)
if click[0] == 1:
click_sound.play()
over = not over
pygame.time.delay(150)
pygame.mixer.music.load(resource_path("Music\\Menu music.mp3"))
pygame.mixer.music.set_volume(0.70)
pygame.mixer.music.play(-1)
main_menu()
else:
drawButton(Yellow, (610, 400, 200, 50), "Main Menu", Black, 20)
if 400 < mouse[0] < 600 and 400 < mouse[1] < 450:
drawButton(Blue, (400, 400, 200, 50), "Play again", White, 20)
if click[0] == 1:
click_sound.play()
over = not over
play_again()
else:
drawButton(Yellow, (400, 400, 200, 50), "Play again", Black, 20)
pygame.display.update()
def randomSnack(item):
while True:
x = random.randrange(1, rows - 1)
y = random.randrange(2, columns - 1)
if len(list(filter(lambda z: z.pos == (x, y), item))) > 0:
continue
else:
break
return (x,y)
def die(snake,mode,main):
end = True
size = 25
xw = 90
yh = 20
pygame.mixer.music.fadeout(100)
gameover_sound.play()
score(size, xw, yh,snake)
while end:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
while size < 83 or xw < (width // 2) or yh < (height // 2):
win.fill(White)
if size < 83:
size += 1
if xw < (width // 2):
xw += 1
if yh < (height// 2):
yh += 1
score(size, xw, yh,snake)
pygame.display.update()
if size == 83:
pygame.time.delay(1000)
details(main,mode,len(snake.body)-1)
pygame.display.update
# die2 is a the die function used by multiplayer modes
def die2(s,ss,main):
end = True
size = 25
x1 = 90
y1 = 20
x2 = 1100
y2 = 20
pygame.mixer.music.fadeout(100)
gameover_sound.play()
score(size, x1, y1, s,Blue)
score(size,x2,y2,ss, Yellow)
while end:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
while size < 60 or x1 < (width // 2) or y1 < ((height // 2)-30) or x2 > (width // 2) or y2 < ((height // 2)+30):
win.fill(White)
if size < 60:
size += 1
if x1 < (width // 2):
x1 += 1
if y1 < ((height // 2)-30):
y1 += 1
if x2 > (width // 2):
x2 -= 1
if y2 < ((height // 2)+30):
y2 += 1
score(size, x1, y1, s, Blue)
score(size, x2, y2, ss, Yellow)
pygame.display.update()
if size == 60:
pygame.time.delay(1000)
if len(s.body)> len(ss.body):
game_over("Blue won by " + str(len(s.body) - len(ss.body)) + " point(s)!", main)
elif len(s.body) < len(ss.body):
game_over("Yellow won by " + str(len(ss.body) - len(s.body)) + " point(s)!", main)
else:
game_over("It's a tie!", main)
pygame.display.update
def redrawWindow(snake,snack,start_time):
win.fill(White)
snake.draw(win)
drawGrid()
snack.draw(win)
score(20,90,20, snake)
timer(20,1100,20,start_time)
pygame.display.update()
#redrawWindow2 is only used by the multiplayer modes
def redrawWindow2(s,ss,snack,start_time):
win.fill(White)
s.draw(win)
ss.draw(win)
drawGrid()
snack.draw(win)
score(20,90,20,s,Blue)
score(20,1100,20,ss, Yellow)
timer(20,590,20, start_time)
pygame.display.update()
########################################################################################################################
# All the game functions regarding the Singleplayer modes
def singleplayer_easy():
pygame.time.delay(2000)
pygame.mixer.music.load(resource_path("Music\\Game music.mp3"))
pygame.mixer.music.set_volume(0.70)
pygame.mixer.music.play(-1)
s = snake(Blue, (10, 10), pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN)
snack = cube(randomSnack(s.body), color=(255, 0, 0))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
running = True
while running:
pygame.time.delay(75)
clock.tick(60)
s.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
keys = pygame.key.get_pressed()
for key in keys:
movement(s, keys)
if s.body[0].pos == snack.pos:
eat_sound.play()
s.addCube()
snack = cube(randomSnack(s.body), color=(255, 0, 0))
for x in range(len(s.body)):
if s.body[x].pos in list(map(lambda z: z.pos, s.body[x + 1:])):
hit_sound.play()
pygame.time.delay(1000)
die(s, "easy", singleplayer_easy)
break
redrawWindow(s, snack, start_time)
def singleplayer_hard():
global sh
pygame.time.delay(2000)
pygame.mixer.music.load(resource_path("Music\\Game music.mp3"))
pygame.mixer.music.set_volume(0.70)
pygame.mixer.music.play(-1)
sh = snakey(Blue, (10, 10), pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN)
snack = cube(randomSnack(sh.body), color=(255, 0, 0))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
running = True
while running:
t = 81
for i in sh.body:
t -= 1
pygame.time.delay(t)
clock.tick(60)
sh.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
keys = pygame.key.get_pressed()
for key in keys:
movement(sh, keys)
if sh.body[0].pos == snack.pos:
eat_sound.play()
sh.addCube()
snack = cube(randomSnack(sh.body), color=(255, 0, 0))
for x in range(len(sh.body)):
if sh.body[x].pos in list(map(lambda z: z.pos, sh.body[x + 1:])):
hit_sound.play()
pygame.time.delay(1000)
die(sh, "hard", singleplayer_hard)
break
redrawWindow(sh, snack, start_time)
def singleplayer_e_instructions():
win.fill(White)
pygame.mixer.music.fadeout(3000)
flag = True
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.blit(singleplayer_easy_instructions,(120,80))
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 900 < mouse[0] < 1100 and 525 < mouse[1] < 575:
drawButton(Blue, (900, 525, 200, 50), "Proceed", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
else:
drawButton(Yellow, (900, 525, 200, 50), "Proceed", Black, 20)
pygame.display.update()
def singleplayer_h_instructions():
win.fill(White)
pygame.mixer.music.fadeout(3000)
flag = True
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.blit(singleplayer_hard_instructions,(120,80))
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 900 < mouse[0] < 1100 and 525 < mouse[1] < 575:
drawButton(Blue, (900, 525, 200, 50), "Proceed", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
else:
drawButton(Yellow, (900, 525, 200, 50), "Proceed", Black, 20)
pygame.display.update()
def singleplayer():
win.fill(White)
flag = True
while flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
win.fill(White)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 500 < mouse[0] < 700 and 270 < mouse[1] < 320:
drawButton(Blue, (500, 270, 200, 50), "Easy Mode", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
pygame.time.delay(150)
singleplayer_e_instructions()
gamestart_sound.play()
singleplayer_easy()
else:
drawButton(Yellow, (500, 270, 200, 50), "Easy Mode", Black, 20)
if 500 < mouse[0] < 700 and 330 < mouse[1] < 380:
drawButton(Blue, (500, 330, 200, 50), "Hard Mode", White, 20)
if click[0] == 1:
click_sound.play()
flag = not flag
pygame.time.delay(150)
singleplayer_h_instructions()
gamestart_sound.play()
singleplayer_hard()
else:
drawButton(Yellow, (500, 330, 200, 50), "Hard Mode", Black, 20)
pygame.display.update()