-
Notifications
You must be signed in to change notification settings - Fork 1
/
Warzone2.py
1335 lines (1152 loc) · 52.2 KB
/
Warzone2.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
from Network import network
pygame.init()
net = network()
# Tuple declaration for different colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (200, 0, 0)
light_red = (255, 0, 0)
light_grey = (220, 220, 220)
green = (0, 155, 0)
light_green = (0, 255, 0)
# different colours for health bar
gren = (0, 200, 0)
yelow = (100, 100, 0)
rde = (200, 0, 0)
yellow = (200, 200, 0)
light_yellow = (255, 255, 0)
blue = (32, 139, 185)
light_blue = (0, 0, 255)
# variable for pause
pause = False
# for player 1
player_death = 0
player_kills = 0
# for player 2
enemy_death = 0
enemy_kills = 0
font = pygame.font.SysFont('comicsans', 40, True)
time_left = int(300)
# Window Size
display_width = 1280
display_height = 640
gameDisplay = pygame.display.set_mode((display_width, display_height))
# Background
background_clouds = pygame.image.load("images\Misc_img\Background.png")
# Title bar
pygame.display.set_caption("WAR ZONE")
icon = pygame.image.load("images\Misc_img\log.png")
pygame.display.set_icon(icon)
img = pygame.image.load('images\Misc_img\guiii.png')
img1 = pygame.image.load('images\Misc_img\game1.png')
timer_button = pygame.image.load("images\Misc_img\Timer_button.png")
player_1 = pygame.image.load("images\Misc_img\idle1.png")
bullet_right = pygame.image.load(r"images\Misc_img\bulletright.png")
bullet_right = pygame.transform.smoothscale(bullet_right, (12, 12))
bullet_left = pygame.transform.flip(bullet_right, True, False)
walkright = [pygame.image.load('images\Run_img\Run (1).png'),
pygame.image.load('images\Run_img\Run (2).png'),
pygame.image.load('images\Run_img\Run (3).png'),
pygame.image.load('images\Run_img\Run (4).png'),
pygame.image.load('images\Run_img\Run (5).png'),
pygame.image.load('images\Run_img\Run (6).png'),
pygame.image.load('images\Run_img\Run (7).png'),
pygame.image.load('images\Run_img\Run (8).png')] # Right Walking Animations for id==0
walkright1 = [pygame.image.load('images\Run_img\Run (1).png'),
pygame.image.load('images\Run_img\Run (2).png'),
pygame.image.load('images\Run_img\Run (3).png'),
pygame.image.load('images\Run_img\Run (4).png'),
pygame.image.load('images\Run_img\Run (5).png'),
pygame.image.load('images\Run_img\Run (6).png'),
pygame.image.load('images\Run_img\Run (7).png'),
pygame.image.load('images\Run_img\Run (8).png')] # Right Walking Animations for id==1
walkleft = [pygame.image.load(r'images\Run_left_img\runss1.png'),
pygame.image.load(r'images\Run_left_img\runss2.png'),
pygame.image.load(r'images\Run_left_img\runss3.png'),
pygame.image.load(r'images\Run_left_img\runss4.png'),
pygame.image.load(r'images\Run_left_img\runss5.png'),
pygame.image.load(r'images\Run_left_img\runss6.png'),
pygame.image.load(r'images\Run_left_img\runss7.png'),
pygame.image.load(r'images\Run_left_img\runss8.png')] # Left Walking Animations for id==0
walkleft1 = [pygame.image.load(r'images\Run_left_img\runss1.png'),
pygame.image.load(r'images\Run_left_img\runss2.png'),
pygame.image.load(r'images\Run_left_img\runss3.png'),
pygame.image.load(r'images\Run_left_img\runss4.png'),
pygame.image.load(r'images\Run_left_img\runss5.png'),
pygame.image.load(r'images\Run_left_img\runss6.png'),
pygame.image.load(r'images\Run_left_img\runss7.png'),
pygame.image.load(r'images\Run_left_img\runss8.png')] # Left Walking Animations for id==1
deadright = [pygame.image.load('images\Dead_img\Dead (1).png'),
pygame.image.load('images\Dead_img\Dead (2).png'),
pygame.image.load('images\Dead_img\Dead (3).png'),
pygame.image.load('images\Dead_img\Dead (4).png'), # right death animations
pygame.image.load('images\Dead_img\Dead (5).png'),
pygame.image.load('images\Dead_img\Dead (6).png'),
pygame.image.load('images\Dead_img\Dead (7).png'),
pygame.image.load('images\Dead_img\Dead (8).png')
]
deadleft = [pygame.image.load('images\Dead_left_img\deadss1.png'),
pygame.image.load('images\Dead_left_img\deadss2.png'),
pygame.image.load('images\Dead_left_img\deadss3.png'),
pygame.image.load('images\Dead_left_img\deadss4.png'),
pygame.image.load('images\Dead_left_img\deadss5.png'),
pygame.image.load('images\Dead_left_img\deadss6.png'), # left death animations
pygame.image.load('images\Dead_left_img\deadss7.png'),
pygame.image.load('images\Dead_left_img\deadss8.png')
]
# variables fo animation id==0
walk = 0
death = 0
left = False
right = False
jump = False
standr = True
standl = False
pygame.display.update()
# fps declared
FPS = 32
clock = pygame.time.Clock()
# different font sizes
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
chatStr = ""
printchat = ""
printchatcheck = ""
chat_word = ""
FPScount = 0
time_str = ""
prev = ""
start_tick = 0
timer_count = 0
timer_flag = 0
# initial values for variables
player_health = 100
enemy_health = 100
playerX = 32
playerY = 400
opponent_X = 1248
opponent_Y = 400
opponent_X_old = 1248
opponent_X_change = 0
opponent_Y_old = 400
opponent_Y_change = 0
# size is 24X24
player_bullet_x = 1285 # player_bullet_x is X coordinate of players bullet
player_bullet_y = 645 # player_bullet_y is Y coordinate of player's bullet
enemy_bullet_x = 1285
enemy_bullet_y = 645
enemy_bullet_old = 1285
enemy_bullet_change = 0
# status (h-hit,d-drown,n-none of the above
player_status = 'n'
enemy_status = 'n'
air = False # player in air or not
# initial for id==1
if net.id == '1':
playerX = 1248
playerY = 400
opponent_X = 32
opponent_Y = 400
# variables for animation id==1
walk1 = 0
left1 = False
right1 = False
jump1 = False
standr1 = False
standl1 = True
def message_to_screen(msg, color, y_displace=0, size="small"): # function for displaying some msg on screen
textsurf, textRect = text_objects(msg, color, size)
textRect.center = (640, 200 + y_displace)
gameDisplay.blit(textsurf, textRect)
# to clear the text printed on the screen after 5 seconds
"""
FUNCTION DESCRIPTION:
Updates the screen at every instant so that the game keeps on working without removing the necessary data from the screen
"""
def chat_screen_update(): # function for updating screen
gameDisplay.fill(white)
gameDisplay.blit(background_clouds, [0, 0])
button("Chat", 1180, 11, 90, 40, yellow, light_yellow)
button("PAUSE", 1180, 55, 90, 40, red, light_red, action="paused")
gameDisplay.blit(timer_button, [580, 10])
text = smallfont.render(chat_word + printchat, True, black)
gameDisplay.blit(text, [20, 60])
text_to_button(time_str, black, 623, 24, 30, 30)
health_bars(player_health, enemy_health) # draw health bars
player_draw(playerX, playerY, player_1) # draw player
player_draw(opponent_X, opponent_Y, player_1, True) # draw enemy
if enemy_bullet_change > 0: # bullet image according to direction
gameDisplay.blit(bullet_right, (enemy_bullet_x, enemy_bullet_y))
else:
gameDisplay.blit(bullet_left, (enemy_bullet_x, enemy_bullet_y))
# bande bhi yahan update honge taaki purana text overwrite ho jaaye
"""
FUNCTION DESCRIPTION:
Creates text objects according to size specified
"""
def text_objects(msg, color, size="small"): # for creating text objects
# the below statement added just to prevent error local variable used before reference
textSurface = smallfont.render(msg, True, color)
if size == "small":
textSurface = smallfont.render(msg, True, color)
elif size == "medium":
textSurface = medfont.render(msg, True, color)
elif size == "large":
textSurface = largefont.render(msg, True, color)
return textSurface, textSurface.get_rect()
"""
FUNCTION DESCRIPTION:
Adds Text to buttons
"""
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size="small"): # adding text to buttons
textSurf, textRect = text_objects(msg, color, size)
textRect.center = ((buttonx + (buttonwidth // 2)), (buttony + (buttonheight // 2)))
gameDisplay.blit(textSurf, textRect)
"""
FUNCTION DESCRIPTION:
Rules and regulations along with controls.
"""
def helps(): # Controls/Help Screen
helps = True
while helps:
gameDisplay.fill(green)
message_to_screen("HELP", red, -100, "large")
message_to_screen("SHOOT AND KILL THE ENEMY ", black, 0, "small")
message_to_screen("PRESS SPACE BAR TO SHOOT", black, 60, "small")
message_to_screen("HEALTH BARS CONSTANTLY KEEPS A CHECK ON YOUR HEALTH AS WELL AS YOUR ENEMY'S HEALTH", black,
120, "small")
message_to_screen("SCORE ON TOP LEFT CORNER SIGNIFIES RATIO OF PLAYER 1 KILLS TO PLAYER 2 KILLS", black, 180,
"small")
message_to_screen("PRESS CHAT BUTTON TO SEND A MESSAGE ", black, 240, "small")
message_to_screen("PRESS PAUSE BUTTON TO PAUSE THE SCREEN", black, 280, "small")
cur = pygame.mouse.get_pos() # it returns tuple of position of mouse on screen
click = pygame.mouse.get_pressed() # it returns a tuple of which mouse button is pressed whether left ceter or right for eg (1,0,0) means left is pressed
if 130 + 150 > cur[0] > 130 and 535 + 50 > cur[1] > 535:
# to lighten the button when mouse is over it
pygame.draw.rect(gameDisplay, light_red, (130, 535, 150, 50))
if click[0] == 1: # that is on left click
game_intro()
else:
pygame.draw.rect(gameDisplay, red, (130, 535, 150, 50))
if 885 + 150 > cur[0] > 885 and 535 + 50 > cur[1] > 535:
pygame.draw.rect(gameDisplay, light_yellow, (885, 535, 150, 50))
if click[0] == 1:
gameLoop()
else:
pygame.draw.rect(gameDisplay, yellow, (885, 535, 150, 50))
# to put text in the button
text_to_button("BACK", black, 130, 535, 150, 50)
text_to_button("PLAY", black, 885, 535, 150, 50)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(15)
"""
FUNCTION DESCRIPTION:
Creates Buttons.
"""
def button(text, x, y, width, height, inactive_color, active_color, action=None): # for adding functionality to button
global pause
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# click has tuple containing 3 elements. first one left click,second one mouse scroll, third one right click
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action is not None:
if action == "quit":
pygame.quit()
quit()
if action == "chat":
chat_box()
if action == "paused":
pause = True
paused()
if action == "unpause":
unpause()
else:
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
text_to_button(text, black, x, y, width, height)
"""
FUNCTION DESCRIPTION:
This function is called whenever chat button is pressed and displays the text as u type.
"""
def chat_box(): # to create chat box
chat_screen_update()
pygame.display.update()
global chat_word, printchat
printchat = ""
current_string = []
output = ""
writing = True
while writing:
chat_word = "Chat:"
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
current_string = current_string[0:-1]
elif event.key == pygame.K_RETURN:
writing = False
break
elif event.key == pygame.K_MINUS:
current_string.append("_")
elif event.key <= 127:
current_string.append(chr(event.key))
output = "".join(current_string)
text = smallfont.render(chat_word + output, True, black)
timer(start_tick)
chat_screen_update()
gameDisplay.blit(text, [20, 60])
pygame.display.update()
# Send Network Stuff- yahan opposition player ki position update karni padegi
global chatStr
chatStr = output
"""
FUNCTION DESCRIPTION:
Enables the chat to be displayed for 5 seconds.
"""
def chatting(): # to chat
global chatStr, printchat, printchatcheck, FPScount, chat_word
reply = send_data(chatStr)
if reply != printchatcheck:
printchat = reply
FPScount = 0
if FPScount <= 151: FPScount += 1
if len(printchat) > 0 and FPScount == 1:
chat_word = "Chat:"
text = smallfont.render(chat_word + printchat, True, black)
printchatcheck = printchat
gameDisplay.blit(text, [20, 60])
pygame.display.update()
# pygame.time.wait(5000)
# chatStr = ""
# printchat = ""
# # print("DONE")
# chat_screen_update()
"""
FUNCTION DESCRIPTION:
Sends data to server via network file.
"""
def send_data(output): # to send/receive data/// output is chat string
"""
Send position to server
return: None
"""
global playerX, playerY, opponent_X, opponent_Y, player_bullet_x, player_bullet_y, enemy_bullet_x, enemy_bullet_y, timer_count, player_status, enemy_status
# print(enemy_bullet_x,enemy_bullet_y)
data = str(net.id) + ":" + output + '?' + str(playerX) + ',' + str(playerY) + ',' + str(
player_bullet_x) + ',' + str(player_bullet_y) + ',' + str(player_status)
reply = net.send(data)
arr = reply.split('?')
timer_count = int(arr[3])
print(reply)
if net.id == "0":
opponent_X = int(arr[2][2:].split(',')[0])
opponent_Y = int(arr[2][2:].split(',')[1])
enemy_bullet_x = int(arr[2][2:].split(',')[2])
enemy_bullet_y = int(arr[2][2:].split(',')[3])
enemy_status = (arr[2][2:].split(',')[4])
playerX = int(arr[1][2:].split(',')[0])
playerY = int(arr[1][2:].split(',')[1])
player_bullet_x = int(arr[1][2:].split(',')[2])
player_bullet_y = int(arr[1][2:].split(',')[3])
player_status = (arr[1][2:].split(',')[4])
else:
opponent_X = int(arr[1][2:].split(',')[0])
opponent_Y = int(arr[1][2:].split(',')[1])
enemy_bullet_x = int(arr[1][2:].split(',')[2])
enemy_bullet_y = int(arr[1][2:].split(',')[3])
enemy_status = (arr[1][2:].split(',')[4])
playerX = int(arr[2][2:].split(',')[0])
playerY = int(arr[2][2:].split(',')[1])
player_bullet_x = int(arr[2][2:].split(',')[2])
player_bullet_y = int(arr[2][2:].split(',')[3])
player_status = (arr[2][2:].split(',')[4])
# for direction of enemy and its bullet
global opponent_Y_old, opponent_Y_change, opponent_X_old, opponent_X_change, enemy_bullet_change, enemy_bullet_old, chat_word
opponent_X_change = opponent_X - opponent_X_old
opponent_Y_change = opponent_Y - opponent_Y_old
opponent_X_old = opponent_X
opponent_Y_old = opponent_Y
enemy_bullet_change = enemy_bullet_x - enemy_bullet_old
enemy_bullet_old = enemy_bullet_x
reply = arr[0]
return reply[2:]
"""
FUNCTION DESCRIPTION:
to unpause the game
"""
def unpause(): # to undo pause
global pause
pause = False
"""
FUNCTION DESCRIPTION:
to pause the game
"""
def paused(): # pause screen
textsurf, textrect = text_objects("PAUSED", red, "large")
textrect.center = (630, 200)
gameDisplay.blit(textsurf, textrect)
timeadd = 0
global start_tick
while pause:
# timeadd += 1
# if timeadd % 15 == 0:
# start_tick += 1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# gameDisplay.fill(white)
# message_to_screen("PAUSED",red,-100,"large")
button("CONTINUE", 300, 372, 150, 50, red, light_red, action="unpause")
button("QUIT", 842, 372, 150, 50, blue, light_blue, action="quit")
pygame.display.update()
clock.tick(15)
else:
chat_screen_update()
"""
FUNCTION DESCRIPTION:
Displays Game Over Screen.
"""
def game_over(): # game over screen
global player_death
global player_kills
global enemy_death
global enemy_kills
global timer_count
pygame.mixer.music.stop()
# pygame.mixer.music.unload()
pygame.mixer.music.load("Music\GameOver.wav")
pygame.mixer.music.play(-1)
global chatStr, printchat, printchatcheck, FPScount, chat_word
chatStr = ""
printchat = ""
printchatcheck = ""
FPScount = 0
chat_word = ""
game_over = True
start_time = pygame.time.get_ticks()
flag = 1
while game_over:
time_left = 4 - (pygame.time.get_ticks() - start_time) / 1000
if time_left > 0 and flag == 1:
send_data("")
if timer_count == 0:
flag = 0
gameDisplay.fill(white)
gameDisplay.blit(img1, [0, 0])
if int(player_death) < int(enemy_death):
textsurf, textrect = text_objects("YOU WON", red, "large")
textrect.center = (957, 177)
gameDisplay.blit(textsurf, textrect)
textsurf1, textrect1 = text_objects("PLAYER 1 :" + " + " + str(player_kills) + " - " + str(player_death),
black, "small")
textrect1.center = (957, 277)
gameDisplay.blit(textsurf1, textrect1)
textsurf2, textrect2 = text_objects("PLAYER 2 :" + " + " + str(enemy_kills) + " - " + str(enemy_death),
black, "small")
textrect2.center = (957, 327)
gameDisplay.blit(textsurf2, textrect2)
elif int(player_death) > int(enemy_death):
textsurf, textrect = text_objects("YOU LOSE", red, "large")
textrect.center = (957, 177)
gameDisplay.blit(textsurf, textrect)
textsurf1, textrect1 = text_objects("PLAYER 1 :" + " + " + str(player_kills) + " - " + str(player_death),
black, "small")
textrect1.center = (957, 277)
gameDisplay.blit(textsurf1, textrect1)
textsurf2, textrect2 = text_objects("PLAYER 2 :" + " + " + str(enemy_kills) + " - " + str(enemy_death),
black, "small")
textrect2.center = (957, 327)
gameDisplay.blit(textsurf2, textrect2)
else:
textsurf, textrect = text_objects("TIE", red, "large")
textrect.center = (957, 177)
gameDisplay.blit(textsurf, textrect)
textsurf1, textrect1 = text_objects("PLAYER 1 :" + " + " + str(player_kills) + " - " + str(player_death),
black, "small")
textrect1.center = (957, 277)
gameDisplay.blit(textsurf1, textrect1)
textsurf2, textrect2 = text_objects("PLAYER 2 :" + " + " + str(enemy_kills) + " - " + str(enemy_death),
black, "small")
textrect2.center = (957, 327)
gameDisplay.blit(textsurf2, textrect2)
cur = pygame.mouse.get_pos() # it returns tuple of position of mouse on screen
click = pygame.mouse.get_pressed() # it returns a tuple of which mouse button is pressed whether left ceter or right for eg (1,0,0) means left is pressed
if 770 + 170 > cur[0] > 770 and 560 + 50 > cur[1] > 560:
# to lighten the button when mouse is over it
pygame.draw.rect(gameDisplay, light_red, (770, 560, 170, 50))
if click[0] == 1: # that is on left click
enemy_kills = 0
enemy_death = 0
player_death = 0
player_kills = 0
gameLoop()
else:
pygame.draw.rect(gameDisplay, red, (770, 560, 170, 50))
if 1010 + 170 > cur[0] > 1010 and 560 + 50 > cur[1] > 560:
pygame.draw.rect(gameDisplay, light_yellow, (1010, 560, 170, 50))
if click[0] == 1:
pygame.quit()
# to quit pygame
quit()
else:
pygame.draw.rect(gameDisplay, yellow, (1010, 560, 170, 50))
# to put text in the button
text_to_button("PLAY AGAIN", black, 770, 560, 170, 50)
text_to_button("QUIT", black, 1010, 560, 170, 50)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(10)
"""
FUNCTION DESCRIPTION:
Displays Game Intro Screen.
"""
def game_intro(): # game intro screen
intro = True
pygame.mixer.music.load("Music\GameStart.wav")
pygame.mixer.music.play(-1)
while intro:
gameDisplay.fill(white)
gameDisplay.blit(img, [0, 0])
cur = pygame.mouse.get_pos() # it returns tuple of position of mouse on screen
click = pygame.mouse.get_pressed() # it returns a tuple of which mouse button is pressed whether left ceter or right for eg (1,0,0) means left is pressed
if 842 + 150 > cur[0] > 842 and 291 + 50 > cur[1] > 291:
# to lighten the button when mouse is over it
pygame.draw.rect(gameDisplay, light_red, (842, 291, 150, 50))
if click[0] == 1: # that is on left click
gameLoop()
else:
pygame.draw.rect(gameDisplay, red, (842, 291, 150, 50))
if 842 + 150 > cur[0] > 842 and 372 + 50 > cur[1] > 372:
pygame.draw.rect(gameDisplay, light_yellow, (842, 372, 150, 50))
if click[0] == 1:
helps()
else:
pygame.draw.rect(gameDisplay, yellow, (842, 372, 150, 50))
if 842 + 150 > cur[0] > 842 and 456 + 50 > cur[1] > 456:
pygame.draw.rect(gameDisplay, light_blue, (842, 456, 150, 50))
if click[0] == 1:
pygame.quit()
# to quit pygame
quit()
else:
pygame.draw.rect(gameDisplay, blue, (842, 456, 150, 50))
# to put text in the button
text_to_button("PLAY", black, 842, 291, 150, 50)
text_to_button("HELP", black, 842, 372, 150, 50)
text_to_button("QUIT", black, 842, 456, 150, 50)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(10)
"""
FUNCTION DESCRIPTION:
Enables chat to be displayed while playing and not stopping the game.
"""
def chatWithPlay(): # to chat while playing
global chatStr, printchat, printchatcheck, FPScount, chat_word
if FPScount == 150:
chatStr = ""
printchat = ""
chat_word = ""
# print("DONE")
chat_screen_update()
"""
FUNCTION DESCRIPTION:
Game Timer.
"""
def timer(start_tick): # timer control
global time_left, timer_count
if timer_count < 2:
if time_left > 300:
textsurf, textrect = text_objects("WAITING FOR OTHER PLAYERS TO JOIN......", red, "small")
textrect.center = (630, 150)
gameDisplay.blit(textsurf, textrect)
clock.tick(300)
pygame.display.update()
# print(time_left)
time_left = 301 - (pygame.time.get_ticks() - start_tick) / 1000
min, sec = divmod(time_left, 60)
if int(min) == int(0) and int(sec) == int(0):
# print("helllllo")
game_over()
if sec < 10:
sec = '0' + str(int(sec))
else:
sec = str(int(sec))
global time_str, prev
time_str = "0" + str(int(min)) + ":" + sec
if time_str != prev:
gameDisplay.blit(timer_button, [580, 10])
text_to_button(time_str, black, 623, 24, 30, 30, 'medium')
# chat_screen_update()
prev = time_str
"""
FUNCTION DESCRIPTION:
movement of the player with id=0 and id=1 respectively
"""
def player_draw(player_x, player_y, image, mirror=False): # to draw players and display scrores
# pygame.draw.rect(gameDisplay, red, (player_x, player_y + 32, 32, 32))
# pygame.draw.rect(gameDisplay, black, (player_x, player_y, 32, 16))
global walk1, left1, right1, standr1, standl1, walk, left, right, standr, standl
player_x -= 16
if net.id == '1':
mirror = not mirror
if mirror:
image = pygame.transform.flip(image, True, False)
if net.id == '1': # display score
text = font.render('Score ' + str(player_death) + " : " + str(player_kills), 1, (0, 0, 0))
else:
text = font.render('Score ' + str(player_kills) + " : " + str(player_death), 1, (0, 0, 0))
gameDisplay.blit(text, (10, 20))
if net.id != '1':
if not mirror:
if walk + 1 >= 32:
walk = 0
if left:
gameDisplay.blit(walkleft[walk // 4], [player_x, player_y])
walk += 1
left = False
standl = True
standr = False
elif right:
gameDisplay.blit(walkright[walk // 4], [player_x, player_y])
walk += 1
right = False
standr = True
standl = False
elif standr:
gameDisplay.blit(image, [player_x, player_y])
elif standl:
# image = pygame.transform.flip(image, True, False)
gameDisplay.blit(image, [player_x, player_y])
else:
if walk1 + 1 >= 32:
walk1 = 0
elif opponent_X_change > 0:
gameDisplay.blit(walkright1[walk1 // 4], [player_x, player_y])
walk1 += 1
right1 = False
standl1 = False
standr1 = True
elif opponent_X_change < 0:
gameDisplay.blit(walkleft1[walk1 // 4], [player_x, player_y])
walk1 += 1
left1 = False
standl1 = True
standr1 = False
elif standr1:
gameDisplay.blit(image, [player_x, player_y])
elif standl1:
# image = pygame.transform.flip(image, True, False)
gameDisplay.blit(image, [player_x, player_y])
else:
if mirror:
if walk1 + 1 >= 32:
walk1 = 0
if left1:
gameDisplay.blit(walkleft1[walk1 // 4], [player_x, player_y])
walk1 += 1
left1 = False
standl1 = True
standr1 = False
elif right1:
gameDisplay.blit(walkright1[walk1 // 4], [player_x, player_y])
walk1 += 1
right1 = False
standr1 = True
standl1 = False
elif standr1:
gameDisplay.blit(image, [player_x, player_y])
elif standl1:
# image = pygame.transform.flip(image, True, False)
gameDisplay.blit(image, [player_x, player_y])
else:
if walk + 1 >= 32:
walk = 0
if opponent_X_change < 0:
gameDisplay.blit(walkleft[walk // 4], [player_x, player_y])
walk += 1
left = False
standl = True
standr = False
elif opponent_X_change > 0:
gameDisplay.blit(walkright[walk // 4], [player_x, player_y])
walk += 1
right = False
standr = True
standl = False
elif standr:
gameDisplay.blit(image, [player_x, player_y])
elif standl:
# image = pygame.transform.flip(image, True, False)
gameDisplay.blit(image, [player_x, player_y])
# gameDisplay.blit(image, [player_x - 16, player_y])
# chat_screen_update()
"""
FUNCTION DESCRIPTION:
To check whether player/bullet is trying to overlap with an obstacle
"""
def obstacle_check(player_x, player_y, change_x, change_y, air_stay, direction, obstacle_x, obstacle_y, width, height,
player_width=28, player_height=60): # to check an obstacle
if obstacle_x <= player_x + change_x <= obstacle_x + width or obstacle_x <= player_x + player_width + change_x <= obstacle_x + width:
if obstacle_y <= player_y + change_y <= obstacle_y + height or obstacle_y <= player_y + player_height + change_y <= obstacle_y + height or player_y <= obstacle_y < player_y + player_height or player_y <= obstacle_y + height <= player_y + player_height:
global air
if player_width == 24 and player_height == 24: # for fire
air_stay = True
return change_x, change_y, air_stay, direction
if direction["right"] and direction["up"] == 0 and direction["down"] == 0:
change_x = obstacle_x - player_x - player_width
elif direction["left"] and direction["up"] == 0 and direction["down"] == 0:
change_x -= obstacle_x + width - player_x
elif direction["up"] == 0 and direction["down"] == 0:
if abs(obstacle_x - player_x - player_width) < abs(obstacle_x + width - player_x):
change_x = obstacle_x - player_x - player_width
else:
change_x = obstacle_x + width - player_x
if direction["up"]:
if obstacle_y < player_y + change_y < obstacle_y + height:
change_y = + 8
direction["up"] = 0
direction["down"] = 1
air_stay = 32 - air_stay - 2
if air_stay != 0:
change_x = 0
elif direction["down"]:
if obstacle_y < player_y + player_height + change_y < obstacle_y + height or obstacle_y < player_y + change_y < obstacle_y + height:
change_y = obstacle_y - player_y - player_height
if change_y < 0:
change_y = +16
direction["up"] = 0
direction["down"] = 0
air_stay = 0
air = False
if air_stay != 0:
change_x = 0
# if width == player_width and height == player_height and obstacle_x == opponent_X and obstacle_y == opponent_Y:
# change_y = 0
# if net.id=='1':
# change_x=4
# else:
# change_x=-4
return change_x, change_y, air_stay, direction
"""
FUNCTION DESCRIPTION:
Calls obstacle_check() for all obstacles/opponent
"""
def obstacles(playerX, playerY, x_change, y_change, air_stay_count, direction, player_width=28,
player_height=60): # to check all obstacles
# if player_height != 24 and player_height != player_width:
# x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change,
# air_stay_count,
# direction, opponent_X, opponent_Y, 28, 60,
# player_width,
# player_height) # OPPONENT
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 128, 352, 32, 32, player_width,
player_height) # plank 2
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 480, 512, 32, player_width,
player_height) # ground 1
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 512, 480, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 544, 448, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 576, 416, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 608, 384, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 224, 288, 192, 32, player_width,
player_height) # plank 3
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 256, 320, 128, 64, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 224, 96, 64, player_width,
player_height) # plank 1
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 192, 128, 32, player_width,
player_height) # plank 1
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 288, 64, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 0, 320, 32, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 544, 224, 128, 96, player_width,
player_height) # plank 4
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 800, 256, 128, 96, player_width,
player_height) # plank 5
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 640, 480, 192, 32, player_width,
player_height) # ground 2
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 672, 512, 128, 64, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 704, 576, 64, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 960, 480, 320, 32, player_width,
player_height) # ground 3
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 992, 512, 288, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1024, 544, 256, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1056, 576, 224, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1088, 608, 192, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1120, 352, 32, 32, player_width,
player_height) # plank 7
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1056, 256, 32, 32, player_width,
player_height) # plank 6
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1152, 192, 128, 32, player_width,
player_height) # plank 8
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1184, 224, 96, 64, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1216, 288, 64, 32, player_width,
player_height)
x_change, y_change, air_stay_count, direction = obstacle_check(playerX, playerY, x_change, y_change, air_stay_count,
direction, 1248, 320, 32, 32, player_width,
player_height)
return x_change, y_change, air_stay_count, direction
"""
FUNCTION DESCRIPTION:
to update the health meters showing indication of health with different colours
"""
def health_bars(player_health, enemy_health): # to display health bars
if player_health > 75:
player_health_color = gren
elif player_health > 50:
player_health_color = yelow
else:
player_health_color = rde
if enemy_health > 75:
enemy_health_color = gren
elif enemy_health > 50: