-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
1274 lines (1017 loc) · 42.9 KB
/
Copy pathmain.py
File metadata and controls
1274 lines (1017 loc) · 42.9 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
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
#!/usr/bin/env python3
#
# @Programmer Dominik Sysojew - Osinski
# @Graphics Joseph Illingworth
# Space Invaders
# V 1.2.1
#
# See changelog.txt for list of changes made in this version
#
import tkinter
import random
import math
#
# Try to import windows sounds, and define functions for music playback
#
try:
import winsound
def AudioPlay(path):
winsound.PlaySound(path, winsound.SND_FILENAME | winsound.SND_ASYNC | winsound.SND_NODEFAULT)
def AudioStop():
winsound.PlaySound(None, winsound.SND_PURGE)
except:
def AudioPlay(path):
pass
def AudioStop():
pass
lastx = 0
lasty = 0
window = tkinter.Tk()
window.title("Space Invaders")
window.overrideredirect(True)
window.geometry(str(window.winfo_screenwidth())+"x"+str(window.winfo_screenheight()))
window.config(bg="black")
window.config(cursor="none")
#
# This class defines 'static' weapon presets, they should not be changed during runtime
# Additional weapons may be added.
# Structure for the weapons
#
# 'image' : tkinter.PhotoImage - This stores the image which is drawn on screen if the game is used. This field is required, even if no missiles would be fired
# 'level' : list [] - This list contains lists which define how the bullets should be shot, example
# [[(10, 0, -10, 0, 10, 1)]] - this would shoot one bullet at level 1.
# Each tuple () defines a singular bullet. List containing tuples is defining set of bullets at specific level (identified by index).
# The weapon level is counted from 0, therefore weapon at "level 1" as shown in GUI, will be accessed as index 0.
# The values are storred as following:
# (Offset_y, Offset_x, velocity_y, velocity_x, damage, piercing)
# Offset_y - defines what is the Y offset on the screen from the player
# Offset_x - defines what is the X offset on the screen from the player
# velocity_y - defines what is the velocity of the missile in Y direction
# velocity_x - defines what is the velocity of the missile in X direction
# damage - defines how much damage the weapon deals at hit
# piercing - defines how much times the missile can hit a obsticle before discarding
# 'bpl' : list of integers [] - This list defines how many missiles may be there on screen at a given time.
# 'cooldown' : list of integers [] - This defines how many frames the player must wait before next missile can be shot, this can be changed at different levels
# 'soundfile' : string - This defines which sound file should be played once weapon is fired
#
class weapon:
NONE = {
"image":tkinter.PhotoImage(file="data/images/upgrades/laser.gif"),
"level":[[]],
"bpl":[0],
"cooldown":[0],
'soundfile':''
}
LASER = {
"image":tkinter.PhotoImage(file="data/images/weapons/laser.gif"),
"level":[
[
(10, 0, -10, 0, 10, 1)
],
[
(10, 0, -15, 0, 10, 1)
],
[
(10, 0, -10, 0, 10, 1),
(10, 0, -9, 1, 10, 1),
(10, 0, -9, -1, 10, 1)
],
[
(10, 0, -15, 0, 10, 1),
(10, 0, -14, 1, 10, 1),
(10, 0, -14, -1, 10, 1)
],
[
(10, 0, -10, 0, 10, 1),
(10, 0, -9, 1, 10, 1),
(10, 0, -9, -1, 10, 1),
(10, 0, -8, 2, 10, 1),
(10, 0, -8, -2, 10, 1)
],
[
(10, 0, -15, 0, 10, 1),
(10, 0, -14, 1, 10, 1),
(10, 0, -14, -1, 10, 1),
(10, 0, -13, 2, 10, 1),
(10, 0, -13, -2, 10, 1)
]
],
"bpl":[5, 7, 15, 21, 25, 35],
"cooldown":[5, 5, 5, 5, 5, 5],
'soundfile':'data/sounds/laser.wav'
}
PLAZMA = {
"image":tkinter.PhotoImage(file="data/images/weapons/plazma.gif"),
"level":[
[
(10, 0, -10, 0, 50, 1)
],
[
(10, 0, -15, 0, 50, 1)
],
[
(10, 0, -10, 0, 50, 1),
(15, 15, -10, 0, 50, 1),
(15, -15, -10, 0, 50, 1)
],
[
(10, 0, -15, 0, 50, 1),
(15, 15, -15, 0, 50, 1),
(15, -15, -15, 0, 50, 1)
]
],
"bpl":[3, 5, 15, 25],
"cooldown":[5, 5, 5, 5],
'soundfile':'data/sounds/plazma_v3.wav'
}
HULLBREAKER = {
"image":tkinter.PhotoImage(file="data/images/weapons/hullbreaker.gif"),
"level":[
[
(10, 0, -10, 0, 10, 2)
],
[
(10, 0, -10, 0, 20, 3)
],
[
(10, 0, -13, 0, 20, 3)
],
[
(10, 0, -13, 0, 30, 4)
]
],
"bpl":[5, 5, 5, 5],
"cooldown":[3, 3, 3, 3],
'soundfile':'data/sounds/hullbreaker.wav'
}
DEVASTATOR = {
"image":tkinter.PhotoImage(file="data/images/weapons/devastator.gif"),
"level":[
[
(-0,0,-2000,0,10,1),
(-25,0,-2000,0,10,1),
(-50,0,-2000,0,10,1),
(-75,0,-2000,0,10,1),
(-100,0,-2000,0,10,1),
(-125,0,-2000,0,10,1),
(-150,0,-2000,0,10,1),
(-175,0,-2000,0,10,1),
(-200,0,-2000,0,10,1),
(-225,0,-2000,0,10,1),
(-250,0,-2000,0,10,1),
(-275,0,-2000,0,10,1),
(-300,0,-2000,0,10,1),
(-325,0,-2000,0,10,1),
(-350,0,-2000,0,10,1),
(-375,0,-2000,0,10,1),
(-400,0,-2000,0,10,1),
(-425,0,-2000,0,10,1),
(-450,0,-2000,0,10,1),
(-475,0,-2000,0,10,1),
(-500,0,-2000,0,10,1),
(-525,0,-2000,0,10,1),
(-550,0,-2000,0,10,1),
(-575,0,-2000,0,10,1),
(-600,0,-2000,0,10,1),
(-625,0,-2000,0,10,1),
(-650,0,-2000,0,10,1),
(-675,0,-2000,0,10,1),
(-700,0,-2000,0,10,1),
(-725,0,-2000,0,10,1),
(-750,0,-2000,0,10,1),
(-775,0,-2000,0,10,1),
(-800,0,-2000,0,10,1),
(-825,0,-2000,0,10,1),
(-850,0,-2000,0,10,1),
(-875,0,-2000,0,10,1),
(-900,0,-2000,0,10,1),
(-925,0,-2000,0,10,1),
(-950,0,-2000,0,10,1),
(-975,0,-2000,0,10,1),
(-1000,0,-2000,0,10,1),
(-1025,0,-2000,0,10,1),
(-1050,0,-2000,0,10,1),
(-1075,0,-2000,0,10,1)
],
[
(-0,0,-2000,0,10,1),
(-25,0,-2000,0,10,1),
(-50,0,-2000,0,10,1),
(-75,0,-2000,0,10,1),
(-100,0,-2000,0,10,1),
(-125,0,-2000,0,10,1),
(-150,0,-2000,0,10,1),
(-175,0,-2000,0,10,1),
(-200,0,-2000,0,10,1),
(-225,0,-2000,0,10,1),
(-250,0,-2000,0,10,1),
(-275,0,-2000,0,10,1),
(-300,0,-2000,0,10,1),
(-325,0,-2000,0,10,1),
(-350,0,-2000,0,10,1),
(-375,0,-2000,0,10,1),
(-400,0,-2000,0,10,1),
(-425,0,-2000,0,10,1),
(-450,0,-2000,0,10,1),
(-475,0,-2000,0,10,1),
(-500,0,-2000,0,10,1),
(-525,0,-2000,0,10,1),
(-550,0,-2000,0,10,1),
(-575,0,-2000,0,10,1),
(-600,0,-2000,0,10,1),
(-625,0,-2000,0,10,1),
(-650,0,-2000,0,10,1),
(-675,0,-2000,0,10,1),
(-700,0,-2000,0,10,1),
(-725,0,-2000,0,10,1),
(-750,0,-2000,0,10,1),
(-775,0,-2000,0,10,1),
(-800,0,-2000,0,10,1),
(-825,0,-2000,0,10,1),
(-850,0,-2000,0,10,1),
(-875,0,-2000,0,10,1),
(-900,0,-2000,0,10,1),
(-925,0,-2000,0,10,1),
(-950,0,-2000,0,10,1),
(-975,0,-2000,0,10,1),
(-1000,0,-2000,0,10,1),
(-1025,0,-2000,0,10,1),
(-1050,0,-2000,0,10,1),
(-1075,0,-2000,0,10,1)
],
[
(-0,0,-2000,0,10,1),
(-25,0,-2000,0,10,1),
(-50,0,-2000,0,10,1),
(-75,0,-2000,0,10,1),
(-100,0,-2000,0,10,1),
(-125,0,-2000,0,10,1),
(-150,0,-2000,0,10,1),
(-175,0,-2000,0,10,1),
(-200,0,-2000,0,10,1),
(-225,0,-2000,0,10,1),
(-250,0,-2000,0,10,1),
(-275,0,-2000,0,10,1),
(-300,0,-2000,0,10,1),
(-325,0,-2000,0,10,1),
(-350,0,-2000,0,10,1),
(-375,0,-2000,0,10,1),
(-400,0,-2000,0,10,1),
(-425,0,-2000,0,10,1),
(-450,0,-2000,0,10,1),
(-475,0,-2000,0,10,1),
(-500,0,-2000,0,10,1),
(-525,0,-2000,0,10,1),
(-550,0,-2000,0,10,1),
(-575,0,-2000,0,10,1),
(-600,0,-2000,0,10,1),
(-625,0,-2000,0,10,1),
(-650,0,-2000,0,10,1),
(-675,0,-2000,0,10,1),
(-700,0,-2000,0,10,1),
(-725,0,-2000,0,10,1),
(-750,0,-2000,0,10,1),
(-775,0,-2000,0,10,1),
(-800,0,-2000,0,10,1),
(-825,0,-2000,0,10,1),
(-850,0,-2000,0,10,1),
(-875,0,-2000,0,10,1),
(-900,0,-2000,0,10,1),
(-925,0,-2000,0,10,1),
(-950,0,-2000,0,10,1),
(-975,0,-2000,0,10,1),
(-1000,0,-2000,0,10,1),
(-1025,0,-2000,0,10,1),
(-1050,0,-2000,0,10,1),
(-1075,0,-2000,0,10,1)
]
],
"bpl":[44, 44, 44],
"cooldown":[20, 15, 10],
'soundfile':'data/sounds/fire.wav'
}
class entity():
def __init__(self, loc_x, loc_y, imagefile=None, image=None, vel_x=2, vel_y=0, health=10):
if imagefile == None and image == None:
return
if imagefile == None:
self.image = image
if image == None:
self.image = tkinter.PhotoImage(file=imagefile)
self.loc_x = loc_x
self.loc_y = loc_y
self.imgID = canvas.create_image((self.loc_x, self.loc_y), image=self.image)
self.vel_x = vel_x
self.vel_y = vel_y
self.health = health
def changeImage(self, imagefile=None, image=None):
if imagefile == None and image == None:
return False
if imagefile == None:
self.image = image
if image == None:
self.image = tkinter.PhotoImage(file=imagefile)
canvas.delete(self.imgID)
self.imgID = canvas.create_image((self.loc_x, self.loc_y), image=self.image)
return True
def addLocation(self, x, y):
if self.loc_x + x <= 25 and x < 0:
return False
if self.loc_x + x >= window.winfo_screenwidth()-25 and x > 0:
return False
if self.loc_y + y <= 25 and y < 0:
return False
if self.loc_y + y >= window.winfo_screenheight()-25 and y > 0:
return False
self.loc_x += x
self.loc_y += y
canvas.move(self.imgID, x, y)
return True
def autoLocate(self):
if not self.addLocation(self.vel_x, 0):
self.vel_x = -self.vel_x
self.addLocation(self.vel_x, 0)
if not self.addLocation(0, self.vel_y):
self.vel_y = -self.vel_y
self.addLocation(0, self.vel_y)
def preremove(self):
canvas.delete(self.imgID)
class MenuEntity(entity):
def __init__(self, loc_x, loc_y, action, imagefile=None, image=None, vel_x=0, vel_y=0, health=1):
super().__init__(loc_x, loc_y, imagefile, image, vel_x, vel_y, health)
self.action = action
def preremove(self):
canvas.delete(self.imgID)
self.action()
def justRemove(self):
canvas.delete(self.imgID)
class Player:
def __init__(self, loc_x, loc_y, imagefile=None, image:tkinter.PhotoImage=None, health=3, invincible=False, weaponType=weapon.LASER, weaponLevel = 0):
if imagefile == None and image == None:
print("WARNING: trying to create player object without any image or imagefile")
return
if imagefile == None:
self.image = image
if image == None:
self.image = tkinter.PhotoImage(file=imagefile)
self.loc_x = loc_x
self.loc_y = loc_y
self.health = health
self.invincible = invincible
self.weaponType = weaponType
self.weaponLevel = weaponLevel
self.weaponCooldown = 0
self.imgID = canvas.create_image((self.loc_x, self.loc_y), image=self.image)
self.updateMoveLeft = False
self.updateMoveUp = False
self.updateMoveDown = False
self.updateMoveRight = False
self.updateShoot = False
self.speedLeft = 0
self.speedUp = 0
self.speedDown = 0
self.speedRight = 0
self.score = 0
self.playerShots = []
def changeImage(self, imagefile=None, image:tkinter.PhotoImage=None):
if imagefile == None and image == None:
return False
if imagefile == None:
self.image = image
if image == None:
self.image = tkinter.PhotoImage(file=imagefile)
canvas.delete(self.imgID)
self.imgID = canvas.create_image((self.loc_x, self.loc_y), image=self.image)
return True
def addLocation(self, x, y):
if self.loc_x + x <= 25 and x < 0:
return False
if self.loc_x + x >= window.winfo_screenwidth()-25 and x > 0:
return False
if self.loc_y + y <= 25 and y < 0:
return False
if self.loc_y + y >= window.winfo_screenheight()-25 and y > 0:
return False
self.loc_x += x
self.loc_y += y
canvas.move(self.imgID, x, y)
return True
def update(self):
x = 0
y = 0
if self.updateMoveLeft:
if self.speedLeft < 15:
self.speedLeft += 1
if self.loc_x - self.speedLeft > 25:
self.loc_x -= self.speedLeft
x -= self.speedLeft
if self.updateMoveUp:
if self.speedUp < 15:
self.speedUp += 1
if self.loc_y - self.speedUp > 25:
self.loc_y -= self.speedUp
y -= self.speedUp
if self.updateMoveDown:
if self.speedDown < 15:
self.speedDown += 1
if self.loc_y + self.speedDown < window.winfo_screenheight()-25:
self.loc_y += self.speedDown
y += self.speedDown
if self.updateMoveRight:
if self.speedRight < 15:
self.speedRight += 1
if self.loc_x + self.speedRight < window.winfo_screenwidth()-25:
self.loc_x += self.speedRight
x += self.speedRight
if self.updateShoot:
if len(self.playerShots) < self.weaponType["bpl"][self.weaponLevel]:
if self.weaponCooldown == 0:
self.weaponCooldown = self.weaponType["cooldown"][self.weaponLevel]
AudioPlay(self.weaponType['soundfile'])
for z in self.weaponType["level"][self.weaponLevel]:
self.playerShots.append(shot(self.loc_x + z[1], self.loc_y + z[0], image=self.weaponType["image"], vel_y=z[2], vel_x=z[3], damage=z[4], piercing=z[5]))
if self.weaponCooldown != 0:
self.weaponCooldown -= 1
canvas.move(self.imgID, x, y)
def preremove(self):
canvas.delete(self.imgID)
class shot(entity):
def __init__(self, loc_x, loc_y, imagefile=None, image=None, vel_x=0, vel_y=-10, damage=10, piercing=1):
super(shot, self).__init__(loc_x, loc_y, imagefile, image, vel_x, vel_y)
self.piercing = piercing
self.damage = damage
def update(self):
self.loc_x += self.vel_x
self.loc_y += self.vel_y
canvas.move(self.imgID, self.vel_x, self.vel_y)
class upgrade(entity):
def __init__(self, loc_x, loc_y, type, imagefile=None, image=None, vel_x=0, vel_y=5):
super(upgrade, self).__init__(loc_x, loc_y, imagefile, image, vel_x, vel_y)
self.type = type
def update(self):
self.loc_x += self.vel_x
self.loc_y += self.vel_y
canvas.move(self.imgID, self.vel_x, self.vel_y)
def controllerDown(event):
if event.char == event.keysym:
if event.char.lower() == "w":
playerOne.updateMoveUp = True
if event.char.lower() == "a":
playerOne.updateMoveLeft = True
if event.char.lower() == "s":
playerOne.updateMoveDown = True
if event.char.lower() == "d":
playerOne.updateMoveRight = True
if event.char.lower() == "i" and playerTwo != None:
playerTwo.updateMoveUp = True
if event.char.lower() == "j" and playerTwo != None:
playerTwo.updateMoveLeft = True
if event.char.lower() == "k" and playerTwo != None:
playerTwo.updateMoveDown = True
if event.char.lower() == "l" and playerTwo != None:
playerTwo.updateMoveRight = True
def controllerUp(event):
if event.char == event.keysym:
if event.char.lower() == "w":
playerOne.updateMoveUp = False
playerOne.speedUp = 0
if event.char.lower() == "a":
playerOne.updateMoveLeft = False
playerOne.speedLeft = 0
if event.char.lower() == "s":
playerOne.updateMoveDown = False
playerOne.speedDown = 0
if event.char.lower() == "d":
playerOne.updateMoveRight = False
playerOne.speedRight = 0
if event.char.lower() == "i" and playerTwo != None:
playerTwo.updateMoveUp = False
playerTwo.speedUp = 0
if event.char.lower() == "j" and playerTwo != None:
playerTwo.updateMoveLeft = False
playerTwo.speedLeft = 0
if event.char.lower() == "k" and playerTwo != None:
playerTwo.updateMoveDown = False
playerTwo.speedDown = 0
if event.char.lower() == "l" and playerTwo != None:
playerTwo.updateMoveRight = False
playerTwo.speedRight = 0
def controllerMouse(event):
global playerOne, lastx, lasty
if(event.x == playerOne.loc_x and event.y == playerOne.loc_y):
lastx = event.x
lasty = event.y
return
#move player
playerOne.addLocation(event.x-lastx, 0)
playerOne.addLocation(0, event.y-lasty)
lastx = event.x
lasty = event.y
def update():
global bg1pos, bg2pos, score, scoreID1, scoreID2, wave, waveID, healthID1, healthID2, playerOne, playerTwo
scbu1 = int(playerOne.score/10000)
scbu2 = 0
if playerTwo != None:
scbu2 = int(playerTwo.score/10000)
# This line works outside virtualbox, but doesn't work inside
# This line should be commented out for running inside virtualbox
window.event_generate("<Motion>", warp=True, x=playerOne.loc_x, y=playerOne.loc_y) #<==========================================================================================================
#Check whatever the background is still on the screen
#If the background is below the screen teleport it above the screen
if bg1pos >= backgroundImage.height():
bg1pos = -backgroundImage.height()
canvas.move(bg1, 0, -backgroundImage.height()*2)
if bg2pos >= backgroundImage1.height():
bg2pos = -backgroundImage1.height()
canvas.move(bg2, 0, -backgroundImage1.height()*2)
#move the background down by 1 pixel to give the feeling of movement
canvas.move(bg1, 0, 1)
canvas.move(bg2, 0, 1)
bg1pos += 1
bg2pos += 1
#Update the scoring on the bottom left side of screen
canvas.delete(scoreID1)
if playerOne.health > 0:
scoreID1 = canvas.create_text((10, 50), text="Weapon level: " + str(playerOne.weaponLevel + 1) + "\nScore: "+str(playerOne.score), fill="white", anchor=tkinter.NW, font=("System 20 bold"))
else:
scoreID1 = canvas.create_text((10, 50), text="GAME OVER\nScore: " + str(playerOne.score), fill="white", anchor=tkinter.NW, font=("System 20 bold"))
if playerTwo != None:
canvas.delete(scoreID2)
if playerTwo.health > 0:
scoreID2 = canvas.create_text((window.winfo_screenwidth()-10, 50), text="Weapon level: " + str(playerTwo.weaponLevel + 1) + "\nScore: "+str(playerTwo.score), fill="white", anchor=tkinter.NE, font=("System 20 bold"), justify=tkinter.RIGHT)
else:
scoreID2 = canvas.create_text((window.winfo_screenwidth()-10, 50), text="GAME OVER\nScore: " + str(playerTwo.score), fill="white", anchor=tkinter.NE, font=("System 20 bold"), justify=tkinter.RIGHT)
canvas.delete(waveID)
waveID = canvas.create_text((10, window.winfo_screenheight()-40), text="Wave: " + str(wave), fill="white", anchor=tkinter.NW, font=("System 20 bold"))
canvas.delete(healthID1)
if playerOne.health == 1:
healthID1 = canvas.create_image((10, 10), image=life1, anchor=tkinter.NW)
elif playerOne.health == 2:
healthID1 = canvas.create_image((10, 10), image=life2, anchor=tkinter.NW)
elif playerOne.health == 3:
healthID1 = canvas.create_image((10, 10), image=life3, anchor=tkinter.NW)
elif playerOne.health == 4:
healthID1 = canvas.create_image((10, 10), image=life4, anchor=tkinter.NW)
elif playerOne.health == 5:
healthID1 = canvas.create_image((10, 10), image=life5, anchor=tkinter.NW)
elif playerOne.health == 6:
healthID1 = canvas.create_image((10, 10), image=life6, anchor=tkinter.NW)
if playerTwo != None:
canvas.delete(healthID2)
if playerTwo.health == 1:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life1, anchor=tkinter.NE)
elif playerTwo.health == 2:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life2, anchor=tkinter.NE)
elif playerTwo.health == 3:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life3, anchor=tkinter.NE)
elif playerTwo.health == 4:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life4, anchor=tkinter.NE)
elif playerTwo.health == 5:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life5, anchor=tkinter.NE)
elif playerTwo.health == 6:
healthID2 = canvas.create_image((window.winfo_screenwidth()-10, 10), image=life6, anchor=tkinter.NE)
todelPlayerOneShots = []
todelPlayerTwoShots = []
todelenemyshots = []
todelenemies = []
todelupgrades = []
for x in enemies:
for y in playerOne.playerShots:
#calculate enemy > shot collisions
if x.health <=0:
continue
gapX = x.loc_x - y.loc_x
gapY = x.loc_y - y.loc_y
gap = math.sqrt(gapX**2+gapY**2)
if gap <= 25:
if y.piercing == 1:
y.preremove()
todelPlayerOneShots.append(y)
else:
y.piercing -= 1
#random reward
x.health -= y.damage
if x.health <= 0:
x.preremove()
if wave != 0:
rand = random.randint(1,100)
playerOne.score += 10
if (rand == 42 or rand == 7):
randw = random.randint(1,100)
if randw < 50:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.LASER, image=laserUpgradeImage))
elif randw < 75:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.PLAZMA, image=plazmaUpgradeImage))
elif randw < 99:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.HULLBREAKER, image=hullbreakerUpgradeImage))
elif randw == 100:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.DEVASTATOR, image=devastatorUpgradeImage))
todelenemies.append(x)
if playerTwo != None:
for y in playerTwo.playerShots:
#calculate enemy > shot collisions
if x.health <=0:
continue
gapX = x.loc_x - y.loc_x
gapY = x.loc_y - y.loc_y
gap = math.sqrt(gapX**2+gapY**2)
if gap <= 25:
if y.piercing == 1:
y.preremove()
todelPlayerTwoShots.append(y)
else:
y.piercing -= 1
#random reward
rand = random.randint(1,100)
x.health -= y.damage
if x.health <= 0:
x.preremove()
playerTwo.score += 10
if (rand == 42 or rand == 7):
randw = random.randint(1,100)
if randw < 50:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.LASER, image=laserUpgradeImage))
elif randw < 75:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.PLAZMA, image=plazmaUpgradeImage))
elif randw < 99:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.HULLBREAKER, image=hullbreakerUpgradeImage))
elif randw == 100:
upgrades.append(upgrade(x.loc_x, x.loc_y, weapon.DEVASTATOR, image=devastatorUpgradeImage))
todelenemies.append(x)
#calculate enemy > player collisions
gapX1 = x.loc_x - playerOne.loc_x
gapY1 = x.loc_y - playerOne.loc_y
gap1 = math.sqrt(gapX1**2+gapY1**2)
if playerTwo != None:
gapX2 = x.loc_x - playerTwo.loc_x
gapY2 = x.loc_y - playerTwo.loc_y
gap2 = math.sqrt(gapX2**2+gapY2**2)
if gap1 <= 25 and not playerOne.invincible:
x.preremove()
todelenemies.append(x)
playerOne.health -= 1
if playerOne.health > 0:
playerOne.changeImage(imagefile="data/images/player1-inv.gif")
window.after(3000, lambda: removeInvinsibility(playerOne, "data/images/player1.gif"))
playerOne.addLocation(-playerOne.loc_x+window.winfo_screenwidth()/2-25, -playerOne.loc_y+window.winfo_screenheight()-(window.winfo_screenheight()/8))
playerOne.weaponLevel = max(0, playerOne.weaponLevel-2)
playerOne.invincible = True
if playerOne.health == 0:
#gameover
playerOne.weaponType = weapon.NONE
playerOne.preremove()
#canvas.create_text((window.winfo_screenwidth()/2, window.winfo_screenheight()/2), text="Game Over\nYou have scored "+str(score)+" points", fill="white", font=("System 24 bold"))
if playerTwo != None and gap2 <= 25 and not playerTwo.invincible:
x.preremove()
todelenemies.append(x)
playerTwo.health -= 1
if playerTwo.health > 0:
playerTwo.changeImage(imagefile="data/images/player2-inv.gif")
window.after(3000, lambda: removeInvinsibility(playerTwo, "data/images/player2.gif"))
playerTwo.addLocation(-playerTwo.loc_x+window.winfo_screenwidth()/2-25, -playerTwo.loc_y+window.winfo_screenheight()-(window.winfo_screenheight()/8))
playerTwo.weaponLevel = max(0, playerTwo.weaponLevel-2)
playerTwo.invincible = True
if playerTwo.health == 0:
#gameover
playerTwo.weaponType = weapon.NONE
playerTwo.preremove()
#canvas.create_text((window.winfo_screenwidth()/2, window.winfo_screenheight()/2), text="Game Over\nYou have scored "+str(score)+" points", fill="white", font=("System 24 bold"))
#move the enemy
x.autoLocate()
#randomly shoot the player
if ((random.randint(1,1000) <= 3) or (random.randint(1,1000) <= 7 and bullethellMode)) and wave != 0:
enemyshots.append(shot(x.loc_x, x.loc_y, image=eggWeaponImage, vel_y=random.randint(5,15)))
if bullethellMode:
enemyshots.append(shot(x.loc_x, x.loc_y, image=eggWeaponImage, vel_y=random.randint(5,15), vel_x=random.randint(1,5)))
enemyshots.append(shot(x.loc_x, x.loc_y, image=eggWeaponImage, vel_y=random.randint(5,15), vel_x=-random.randint(1,5)))
#calculate enemyshots > player collisions
for x in enemyshots:
gapX1 = x.loc_x - playerOne.loc_x
gapY1 = x.loc_y - playerOne.loc_y
gap1 = math.sqrt(gapX1**2+gapY1**2)
if playerTwo != None:
gapX2 = x.loc_x - playerTwo.loc_x
gapY2 = x.loc_y - playerTwo.loc_y
gap2 = math.sqrt(gapX2**2+gapY2**2)
if gap1 <= 25 and not playerOne.invincible:
x.preremove()
todelenemyshots.append(x)
playerOne.health -= 1
if playerOne.health > 0:
playerOne.changeImage(imagefile="data/images/player1-inv.gif")
window.after(3000, lambda: removeInvinsibility(playerOne, "data/images/player1.gif"))
playerOne.addLocation(-playerOne.loc_x+window.winfo_screenwidth()/2-25, -playerOne.loc_y+window.winfo_screenheight()-(window.winfo_screenheight()/8))
playerOne.weaponLevel = max(0, playerOne.weaponLevel-2)
playerOne.invincible = True
if playerOne.health == 0:
#gameover
playerOne.weaponType = weapon.NONE
playerOne.preremove()
#canvas.create_text((window.winfo_screenwidth()/2, window.winfo_screenheight()/2), text="Game Over\nYou have scored "+str(score)+" points", fill="white", font=("System 24 bold"))
if playerTwo != None and gap2 <= 25 and not playerTwo.invincible:
x.preremove()
todelenemyshots.append(x)
playerTwo.health -= 1
if playerTwo.health > 0:
playerTwo.changeImage(imagefile="data/images/player2-inv.gif")
window.after(3000, lambda: removeInvinsibility(playerTwo, "data/images/player2.gif"))
playerTwo.addLocation(-playerTwo.loc_x+window.winfo_screenwidth()/2-25, -playerTwo.loc_y+window.winfo_screenheight()-(window.winfo_screenheight()/8))
playerTwo.weaponLevel = max(0, playerTwo.weaponLevel-2)
playerTwo.invincible = True
if playerTwo.health == 0:
#gameover
playerTwo.weaponType = weapon.NONE
playerTwo.preremove()
#canvas.create_text((window.winfo_screenwidth()/2, window.winfo_screenheight()/2), text="Game Over\nYou have scored "+str(score)+" points", fill="white", font=("System 24 bold"))
x.update()
if x.loc_x >= window.winfo_screenwidth() or x.loc_y >= window.winfo_screenheight()+15 or x.loc_x < 0:
if (x.loc_x >= window.winfo_screenwidth() or x.loc_x <= 0) and bullethellMode:
x.vel_x = -x.vel_x
else:
x.preremove()
todelenemyshots.append(x)
#calculate upgrades > player collisions
for x in upgrades:
gapX1 = x.loc_x - playerOne.loc_x
gapY1 = x.loc_y - playerOne.loc_y
gap1 = math.sqrt(gapX1**2+gapY1**2)
if playerTwo != None:
gapX2 = x.loc_x - playerTwo.loc_x
gapY2 = x.loc_y - playerTwo.loc_y
gap2 = math.sqrt(gapX2**2+gapY2**2)
if gap1 <= 25 and playerOne.health > 0:
x.preremove()
todelupgrades.append(x)
playerOne.score += 100
if playerOne.weaponType == x.type and playerOne.weaponLevel < len(playerOne.weaponType["level"])-1:
playerOne.weaponLevel += 1
if playerOne.weaponType != x.type:
playerOne.weaponType = x.type
playerOne.weaponLevel = 0
if playerTwo != None and gap2 <= 25 and playerTwo.health > 0:
x.preremove()
todelupgrades.append(x)
playerTwo.score += 100
if playerTwo.weaponType == x.type and playerTwo.weaponLevel < len(playerTwo.weaponType["level"])-1:
playerTwo.weaponLevel += 1
if playerTwo.weaponType != x.type:
playerTwo.weaponType = x.type
playerTwo.weaponLevel = 0
x.update()
if x.loc_x >= window.winfo_screenwidth() or x.loc_y >= window.winfo_screenheight()+15:
x.preremove()
todelupgrades.append(x)
for x in playerOne.playerShots:
#move players shots
x.update()
if x.loc_x <= 0 or x.loc_y <= 0 or x.loc_x > window.winfo_screenwidth():
#if the shot is outside the screen delete it
x.preremove()
todelPlayerOneShots.append(x)
if playerTwo != None:
for x in playerTwo.playerShots:
#move players shots
x.update()
if x.loc_x <= 0 or x.loc_y <= 0 or x.loc_x > window.winfo_screenwidth():
#if the shot is outside the screen delete it
x.preremove()
todelPlayerTwoShots.append(x)
for x in todelenemies:
try:
enemies.remove(x)
except:
pass #if the enemy was already deleted ignore exception
for x in todelPlayerOneShots:
try:
playerOne.playerShots.remove(x)
except:
pass
if playerTwo != None:
for x in todelPlayerTwoShots:
try:
playerTwo.playerShots.remove(x)
except:
pass
for x in todelenemyshots:
try:
enemyshots.remove(x)
except:
pass
for x in todelupgrades:
try:
upgrades.remove(x)
except:
pass
if len(enemies) == 0 and len(todelenemies) != 0:
prespawnNextWave()