-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojet_annee.py
1912 lines (1657 loc) · 81.7 KB
/
projet_annee.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 pygame.locals import *
import math
import random
from Variables_pour_importer import Objet, Types, Bouton
from Variables_pour_importer import horloge, arrondir, get_sign, trouve
class Fenetre():
def __init__(self):
self.open_menu = True
self.open_choose = False
self.open_play = False
self.open_maping = False
self.open_window = True
self.menu = Menu()
self.play = Play()
self.maping = Maping()
self.choose = Choose()
self.nb_joysticks = pygame.joystick.get_count()
for i in range(self.nb_joysticks):
pygame.joystick.Joystick(i).init()
"""
Classe qui permet de gerer la fenetre
"""
class Terrain:
def __init__(self):
self.numero = "5" # input("Quel terrain ?")
self.position_vert = self.position_rouge = self.position_bleu = ""
self.contenu = []
self.ouvrir_terrain()
# Creation des groupes
self.grp_missile = pygame.sprite.Group()
self.grp_bombe = pygame.sprite.Group()
self.grp_attak = pygame.sprite.Group()
self.grp_glace = pygame.sprite.Group()
self.grp_piege = pygame.sprite.Group()
self.grp_pers = pygame.sprite.Group()
self.grp_btn_bloc = pygame.sprite.Group()
self.grp_btn_maping = pygame.sprite.Group()
self.grp_btn_choose = pygame.sprite.Group()
self.grp_btn_play = pygame.sprite.Group()
self.grp_btn_menu = pygame.sprite.Group()
self.grp_btn_resultats = pygame.sprite.Group()
self.grp_btn_fen_quit = pygame.sprite.Group()
self.grp_bloc = pygame.sprite.Group()
self.grp_bloc_effet = pygame.sprite.Group()
self.grp_bloc_pieg = pygame.sprite.Group()
x_bloc = y_bloc = -30
for i in range(len(self.contenu)):
for j in range(len(self.contenu[i])):
self.ajout(self.contenu[i][j], x_bloc, y_bloc)
x_bloc += 30
y_bloc += 30
x_bloc = -30
self.nb_survivants = 3
# Fonction qui permet de rajouter un bloc
def ajout(self, case, coord_x, coord_y):
if case == "bloc":
self.grp_bloc.add(Objet(coord_x, coord_y, 30, 30, im.bloc, "bloc"))
elif case == "grnd":
self.grp_bloc.add(Objet(coord_x, coord_y, 30, 30, im.ground, "grnd"))
elif case == "escR":
self.grp_bloc.add(Objet(coord_x, coord_y, 15, 15, im.stair_right, "escR"))
self.grp_bloc.add(Objet(coord_x, coord_y + 15, 30, 15, im.slab_down, "escR"))
elif case == "escL":
self.grp_bloc.add(Objet(coord_x + 15, coord_y, 15, 15, im.stair_left, "escL"))
self.grp_bloc.add(Objet(coord_x, coord_y + 15, 30, 15, im.slab_down, "escL"))
elif case == "dalU":
self.grp_bloc.add(Objet(coord_x, coord_y, 30, 15, im.slab_up, "dalU"))
elif case == "dalD":
self.grp_bloc.add(Objet(coord_x, coord_y + 15, 30, 15, im.slab_down, "dalD"))
elif case == "slim":
self.grp_bloc.add(Objet(coord_x, coord_y, 30, 30, im.trampo, "slim"))
elif case == "jump":
self.grp_bloc_effet.add(Objet(coord_x, coord_y, 30, 30, im.jumper, "jump"))
elif case == "axel":
self.grp_bloc_effet.add(Objet(coord_x, coord_y, 30, 30, im.accelerator, "axel"))
elif case == "pieg":
self.grp_bloc_pieg.add(Objet(coord_x, coord_y + 22, 30, 4, im.poseur_piege_haut, "pieg"))
self.grp_bloc.add(Objet(coord_x, coord_y + 26, 30, 4, im.poseur_piege_bas, "pieg"))
# Fonction qui permet de sauvegarder les modifications apportees au terrain
def sauvegarder(self):
# On vide le terrain
i, j = len(self.contenu) - 1, len(self.contenu[0])
self.contenu = []
for i in range(i):
self.contenu.append(["vide"] * j)
# On le reremplit
for groupe in (self.grp_bloc, self.grp_bloc_effet, self.grp_bloc_pieg):
for bloc in groupe:
self.contenu[int(bloc.rect.top / 30) + 1][int(bloc.rect.left / 30) + 1] = bloc.numero
self.contenu.append([str(self.position_vert[0]), str(self.position_vert[1]), str(self.position_rouge[0]),
str(self.position_rouge[1]), str(self.position_bleu[0]), str(self.position_bleu[1])])
# On le met dans le format du fichier texte
for i in range(len(self.contenu)):
self.contenu[i] = "'".join(self.contenu[i])
self.contenu = "\n".join(self.contenu)
# On le met dans le fichier texte
fichier_text = open("terrains/terrain" + self.numero + ".txt", "w")
fichier_text.write(self.contenu)
fichier_text.close()
# On remet self.contenu dans la bonne forme
self.ouvrir_terrain()
def ouvrir_terrain(self):
fichier_text = open("terrains/terrain" + self.numero + ".txt", "r")
self.contenu = fichier_text.read()
fichier_text.close()
self.contenu = self.contenu.split("\n")
for i in range(len(self.contenu)):
self.contenu[i] = self.contenu[i].split("'")
self.position_vert = [int(self.contenu[-1][0]), int(self.contenu[-1][1])]
self.position_rouge = [int(self.contenu[-1][2]), int(self.contenu[-1][3])]
self.position_bleu = [int(self.contenu[-1][4]), int(self.contenu[-1][5])]
# Fonction qui prends un screenshot
def screenshot(self, name):
sub = ecran.subsurface(pygame.Rect(0, 0, (len(self.contenu[0]) - 2) * 30, (len(self.contenu) - 3) * 30))
if name != "screenshots/screenshot":
pygame.image.save(sub, name + ".png")
else:
conteur = 0
while conteur > -1:
conteur += 1
try:
a = pygame.image.load(name + str(conteur) + ".png")
except:
pygame.image.save(sub, name + str(conteur) + ".png")
conteur = -1
"""
Classe qui cree l'objet terrain
"""
class Missile(pygame.sprite.Sprite):
def __init__(self, centerx, centery, lanceur):
pygame.sprite.Sprite.__init__(self)
self.image = [im.missile.subsurface(index, 0, 24, 24) for index in range(0, 96, 24)]
self.lanceur = lanceur
self.rect = pygame.Rect(0, 0, 24, 24)
self.rect.center = (arrondir(centerx) + 15, arrondir(centery) + 15)
self.vie = 6
self.index_img_explosion = 0
self.degel = 0
self.x = int(self.rect.centerx / 30)
self.y = int(self.rect.centery / 30)
self.dx = self.dy = 0
self.act_listes = self.anc_listes = self.new_listes = self.chemin = []
self.search_cible()
self.index_img = int(self.dx / 2 + 0.5) + 2 if self.dx else int(self.dy / 2 + 0.5)
def move(self):
if not self.vie > 0:
self.index_img_explosion += 1
if not self.index_img_explosion < 24.5:
ter.grp_missile.remove(self)
else:
if self.degel:
self.degel -= 1
else:
if self.cible.mort or not self.cible:
self.search_cible()
if not self.cible:
self.vie = 0
if self.vie > 0:
if self.besoin_tourner(self.rect.centerx, self.rect.centery):
self.recherche_deplacement_vers_cible()
self.rect.centerx += self.dx * 5
self.rect.centery += self.dy * 5
for groupe in (ter.grp_pers, ter.grp_missile):
for objet in groupe:
if self.rect.colliderect(
objet.rect) and objet != self.lanceur and objet != self and objet.vie > 0:
objet.vie -= 15
self.vie = 0
if groupe == ter.grp_pers:
objet.temps_recharg_anim_degat = 20
for bloc in ter.grp_bloc:
if self.rect.colliderect(bloc.rect):
self.vie = 0
self.index_img = int(self.dx / 2 + 0.5) + 2 if self.dx else int(self.dy / 2 + 0.5)
def recherche_deplacement_vers_cible(self):
self.x = int(self.rect.centerx / 30)
self.y = int(self.rect.centery / 30)
but = (int(self.cible.rect.centerx / 30), int(self.cible.rect.centery / 30))
self.act_listes = [[(self.x, self.y)]]
self.anc_listes = []
self.chemin = []
self.new_listes = []
if but == (self.x, self.y):
self.chemin = [(self.x, self.y), (self.x, self.y)]
while self.chemin == []:
for liste1 in self.act_listes:
deplacements = []
for i in (1, 0), (0, 1), (-1, 0), (0, -1):
if self.deplacement_possible((liste1[-1][0] + i[0], liste1[-1][1] + i[1])):
deplacements.append(i)
deplacements_diagonale = []
for i in deplacements:
for j in deplacements:
if math.sqrt((i[0] + j[0]) ** 2) == 1:
if self.deplacement_possible((liste1[-1][0] + i[0] + j[0], liste1[-1][1] + i[1] + j[1])):
ajout = True
for vecteur in deplacements_diagonale:
if (i[0] + j[0], i[1] + j[1]) == vecteur:
ajout = False
if ajout: deplacements_diagonale.append((i[0] + j[0], i[1] + j[1]))
for i in deplacements_diagonale:
deplacements.append(i)
for i in deplacements:
point = ((liste1[-1][0] + i[0], liste1[-1][1] + i[1]))
temp = []
for point2 in liste1:
temp.append(point2)
temp.append(point)
if point == but:
self.chemin = temp
self.new_listes.append(temp)
self.anc_listes = []
for i in self.act_listes:
self.anc_listes.append(i)
self.act_listes = []
for i in self.new_listes:
self.act_listes.append(i)
self.new_listes = []
if self.act_listes == []:
self.chemin = [(self.x, self.y), (self.x, self.y)]
self.vie = 0
# 2 a copier
self.dx = self.chemin[1][0] - self.x
self.dy = self.chemin[1][1] - self.y
# 3 a copier
def deplacement_possible(self, point):
ajout = False
case = ter.contenu[point[1] + 1][point[0] + 1]
if case == "vide" or case == "axel" or case == "jump":
ajout = True
for listes in self.act_listes, self.anc_listes:
for liste in listes:
if liste[-1] == point:
ajout = False
return ajout
def search_cible(self):
list = []
for pers in ter.grp_pers:
if pers != self.lanceur and pers.vie > 0:
self.cible = pers
self.recherche_deplacement_vers_cible()
if self.vie:
list.append((pers, len(self.chemin)))
if list:
if len(list) == 2:
self.cible = list[0][0] if list[0][1] < list[1][1] else list[1][0]
else:
self.cible = list[0][0]
ter.grp_missile.add(self)
self.vie = 6
def besoin_tourner(self, x, y):
return True if x == int(x / 30) * 30 + 15 and y == int(y / 30) * 30 + 15 else False
"""
Classe qui definit le missiles a tete chercheuse envoyes par les Sprinters
"""
class Bombe(pygame.sprite.Sprite):
def __init__(self, centre_x, centre_y, lanceur, direction):
pygame.sprite.Sprite.__init__(self)
self.coord_lancement = [centre_x, centre_y]
self.rect = pygame.Rect(centre_x - 10, centre_y - 10, 20, 20)
self.image = im.bombe
self.lanceur = lanceur
self.index_img = self.count = self.index_img_explosion = 0
self.count_traj = -1
self.direction = direction
self.vie = 1
self.degel = 0
ter.grp_bombe.add(self)
def move(self):
if self.vie <= 0:
self.index_img_explosion += 0.3
if not self.index_img_explosion < 8:
ter.grp_bombe.remove(self)
elif self.degel:
self.degel -= 1
else:
self.count_traj += 1
self.rect.centery = self.coord_lancement[1] - (-self.count_traj ** 2 + 80 * self.count_traj) / 60
self.rect.centerx += self.direction * 8
explode = False
for groupe in (ter.grp_pers, ter.grp_missile, ter.grp_bombe):
for objet in groupe:
if self.rect.colliderect(objet.rect) and objet != self.lanceur and objet != self and objet.vie:
explode = True
break
for bloc in ter.grp_bloc:
if self.rect.colliderect(bloc.rect):
explode = True
if explode:
self.explosion()
def explosion(self):
self.vie = 0
for groupe in (ter.grp_pers, ter.grp_attak, ter.grp_missile, ter.grp_bombe):
for objet in groupe:
distance = math.sqrt(
(objet.rect.centerx - self.rect.centerx) ** 2 + (objet.rect.centery - self.rect.centery) ** 2)
if distance < 210 and objet != self.lanceur and objet != self and objet.vie:
if groupe == ter.grp_bombe:
objet.explosion()
objet.vie -= (-distance * 40) / 210 + 40
if groupe == ter.grp_pers:
objet.temps_recharg_anim_degat = 20
"""
Classe qui definit les bombes envoyees par les Fighters
"""
class Glace(pygame.sprite.Sprite):
def __init__(self, centerx, centery, lanceur):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(centerx - 192, centery - 192, 384, 384)
self.index_img = 0
self.lanceur = lanceur
for groupe in (ter.grp_pers, ter.grp_attak, ter.grp_missile, ter.grp_bombe):
for objet in groupe:
if objet != self.lanceur:
distance = math.sqrt(
(self.rect.centerx - objet.rect.centerx) ** 2 + (self.rect.centery - objet.rect.centery) ** 2)
if distance < 192 and objet.vie > 0 and objet.degel == 0:
objet.degel = 300
ter.grp_glace.add(self)
"""
Classe qui definit les rayons de congelation envoyes par les Tanks
"""
class Piege(pygame.sprite.Sprite):
def __init__(self, x, y, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = pygame.Rect(x, y, 30, 48)
self.vie = 1
self.index_img = 0
for grp in ter.grp_bloc, ter.grp_piege:
for objet in grp:
if self.rect.colliderect(objet.rect):
self.vie = 0
if self.vie:
ter.grp_piege.add(self)
"""
Classe qui definit les cristaux que laissent les personnages derriere eux
"""
class Attak(pygame.sprite.Sprite):
def __init__(self, centre_x, centre_y, deplacement_x, deplacement_y, image, lanceur, explosion, couleur):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = pygame.Rect(centre_x - 10, centre_y - 10, 20, 20)
self.deplacement_x = deplacement_x
self.deplacement_y = deplacement_y
self.x, self.y = centre_x, centre_y
self.lanceur = lanceur
self.couleur = couleur
self.explosion = explosion
self.vie = 1
self.index_img_explosion = 0
self.index_img = self.count = 0
self.degel = 0
ter.grp_attak.add(self)
def move(self, test=None):
if self.vie <= 0:
self.index_img_explosion += 0.3
if not self.index_img_explosion < 8:
ter.grp_attak.remove(self)
elif self.degel:
self.degel -= 1
else:
self.count += 0.1
if self.count > 4:
self.count = 0
self.index_img = int(self.count)
self.x += self.deplacement_x * 7
self.y += self.deplacement_y * 7
self.rect.centerx = self.x
self.rect.centery = self.y
for groupe in (ter.grp_attak, ter.grp_missile):
for objet in groupe:
if self.rect.colliderect(
objet.rect) and objet != self.lanceur and objet != self and objet.vie > 0 and test == None:
self.vie = 0
objet.vie -= 3
self.rect.left += self.deplacement_x
self.rect.top += self.deplacement_y
for pers in ter.grp_pers:
if self.rect.colliderect(pers.rect) and pers != self.lanceur and pers.vie > 0:
self.vie = 0
if test == "test":
self.vie = -1
else:
pers.vie -= 3
pers.tps_recharg_anim_degat = 10
if pers.faiblesse == self.couleur:
pers.vie -= 7
for bloc in ter.grp_bloc:
if self.rect.colliderect(bloc.rect):
self.vie = 0
"""
Classe qui definit les petites attaques que chacun possede
"""
# COPIER LES ATTAQUES ICI
class IA(pygame.sprite.Sprite):
def __init__(self, position, type, sprinter, fighter, tank, image_attak, image_explosion, faiblesse, couleur):
pygame.sprite.Sprite.__init__(self)
self.sprinter = sprinter
self.fighter = fighter
self.tank = tank
self.gros = [pygame.image.load("Images/Perso/Gros/" + couleur + " sprinter.png"),
pygame.image.load("Images/Perso/Gros/" + couleur + " fighter.png"),
pygame.image.load("Images/Perso/Gros/" + couleur + " tank.png")]
self.type = type
if self.type == 0:
self.image = self.sprinter
self.rect = pygame.Rect(position[0], position[1], 40, 52)
self.vitesse = 6
self.vie = 45
self.anim_degat = im.anim_degat_sprinter
self.anim_ralenti = im.anim_ralenti_sprinter
self.anim_poison = im.anim_poison_sprinter
elif self.type == 1:
self.image = self.fighter
self.rect = pygame.Rect(position[0], position[1], 60, 48)
self.vitesse = 2.5
self.vie = 60
self.tps_recharg_saut = 0
self.anim_degat = im.anim_degat_fighter
self.anim_ralenti = im.anim_ralenti_fighter
self.anim_poison = im.anim_poison_fighter
elif self.type == 2:
self.image = self.tank
self.rect = pygame.Rect(position[0], position[1], 56, 60)
self.vitesse = 2
self.vie = 100
self.anim_degat = im.anim_degat_tank
self.anim_ralenti = im.anim_ralenti_tank
self.anim_poison = im.anim_poison_tank
self.tps_recharg_changement_type = 0
self.left, self.top = position[0], position[1]
self.pos_init = position
self.couleur = couleur
self.faiblesse = faiblesse
self.dy = self.dx = self.tps_en_air = self.y_max = self.effet_poison = 0
self.state = "STANDING"
self.acceleration = self.effet_gel = 1
self.force_saut = 0
self.full_vie = self.vie
self.tps_recharg_anim_degat = 0
self.pers_valide = False
self.fantome = False
self.effet_rapide = 1
self.resistance = 0
self.anim_bouclier = 0
self.sauter = 0
self.deplacer = 0
self.tps_recharg_choix_type = 30
self.cible = False
self.index_img = 0
self.image = dict([(direction, [self.image.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.direction = -1
self.anim_degat = dict([(direction,
[self.anim_degat.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_ralenti = dict([(direction,
[self.anim_ralenti.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_poison = dict([(direction,
[self.anim_poison.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.image_attak = [image_attak.subsurface(index, 0, 20, 20) for index in range(0, 80, 20)]
self.image_explosion = image_explosion
self.tirer_attak = self.tps_recharg_attak = 0
self.tps_recharg_attak_spe = 0
self.degel = 0
self.mort = False
self.animation = True
self.indestructible = False
def reinit(self):
self.vie = self.full_vie
self.rect.topleft = self.pos_init
self.left, self.top = self.pos_init[0], self.pos_init[1]
self.dy = self.dx = self.tps_en_air = self.y_max = self.effet_poison = self.force_saut = self.tps_recharg_anim_degat = self.tirer_attak = self.tps_recharg_attak = self.tirer_piege = self.tps_recharg_piege = self.degel = self.sauter = self.deplacer = 0
self.state = "STANDING"
self.acceleration = self.effet_gel = self.effet_rapide = 1
self.direction = -1
self.mort = self.cible = self.pers_valide = self.indestructible = False
self.animation = True
self.tps_recharg_choix_type = 30
def changer_type(self):
if self.type == 0:
self.image = self.sprinter
self.rect = pygame.Rect(self.rect.left, self.rect.top, 40, 52)
self.vitesse = 6
self.vie = 45
self.anim_degat = im.anim_degat_sprinter
self.anim_ralenti = im.anim_ralenti_sprinter
self.anim_poison = im.anim_poison_sprinter
elif self.type == 1:
self.image = self.fighter
self.rect = pygame.Rect(self.rect.left, self.rect.top, 60, 48)
self.vitesse = 2.5
self.vie = 60
self.tps_recharg_saut = 0
self.anim_degat = im.anim_degat_fighter
self.anim_ralenti = im.anim_ralenti_fighter
self.anim_poison = im.anim_poison_fighter
elif self.type == 2:
self.image = self.tank
self.rect = pygame.Rect(self.rect.left, self.rect.top, 56, 60)
self.vitesse = 2
self.vie = 100
self.anim_degat = im.anim_degat_tank
self.anim_ralenti = im.anim_ralenti_tank
self.anim_poison = im.anim_poison_tank
self.full_vie = self.vie
self.image = dict([(direction, [self.image.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_degat = dict([(direction,
[self.anim_degat.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_ralenti = dict([(direction,
[self.anim_ralenti.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_poison = dict([(direction,
[self.anim_poison.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.pers_valide = False
self.tps_recharg_changement_type = 30
def move(self):
if self.vie <= 0 and self.indestructible == False:
if self.animation:
if self.mort == False:
self.mort = True
self.tps_en_air = 0
self.force_saut = 40
ter.nb_survivants -= 1
self.jump()
self.top += self.dy
self.rect.top = self.top
if self.top > len(ter.contenu) * 30:
self.animation = False
elif self.degel:
self.degel -= 1
else:
# Effets blocs
for bloc in ter.grp_bloc_effet:
if self.rect.colliderect(bloc) and bloc.numero == "axel" and not self.fantome:
self.acceleration = 2
break
else:
self.acceleration = 1
if self.rect.colliderect(bloc) and bloc.numero == "jump" and not self.fantome:
self.force_saut = 100
self.tps_en_air = 0
break
# Rencontre piege
for piege in ter.grp_piege:
if self.rect.colliderect(piege.rect) and piege.vie > 0:
piege.vie = 0
if piege.image == im.piege_vert_bonus:
self.effet_rapide = 2.3
elif piege.image == im.piege_vert_malus:
self.effet_poison = 800
elif piege.image == im.piege_rouge_bonus:
self.vie += 10
if self.vie > self.full_vie:
self.vie = self.full_vie
elif piege.image == im.piege_rouge_malus:
self.vie -= 5
self.tps_recharg_anim_degat = 20
elif piege.image == im.piege_bleu_bonus:
self.indestructible = self.vie
self.resistance = 1000
elif piege.image == im.piege_bleu_malus:
self.effet_gel = 3.3
if self.effet_rapide > 1:
self.effet_rapide -= 0.001
if not self.effet_rapide > 2:
self.effet_rapide = 1
if self.effet_gel > 1:
self.effet_gel -= 0.001
if not self.effet_gel > 3:
self.effet_gel = 1
if self.effet_poison:
if self.effet_poison % 100 == 0:
self.vie -= 1
self.tps_recharg_anim_degat = 10
self.effet_poison -= 1
if self.resistance:
self.resistance -= 1
if self.resistance == 0:
self.indestructible = 0
if self.anim_bouclier:
self.anim_bouclier -= 1
if not self.test_collision() or self.fantome:
self.IA_choisir_cible()
# Check deplacement vertical sprinter
if self.type == 0 and self.state == "STANDING" and self.sauter:
self.force_saut = 80
# Check deplacement vertical fighter
elif self.type == 1:
if self.sauter and self.tps_recharg_saut == 0:
self.tps_en_air = 0
self.force_saut = 65
self.tps_recharg_saut = 40
if self.tps_recharg_saut:
self.tps_recharg_saut -= 1
# Check deplacement vertical tank
elif self.type == 2 and self.sauter:
self.force_saut += 3
# Deplacement vertical
if self.force_saut or self.state == "JUMPING":
self.jump()
self.top += self.dy
self.rect.top = self.top
self.collision(0, self.dy)
# Deplacement horizontal + Animation
if self.deplacer:
self.dx = self.deplacer * self.vitesse * self.acceleration * self.effet_rapide / int(self.effet_gel)
self.left += self.dx
self.rect.left = self.left
self.direction = self.deplacer
temp = self.rect.copy()
self.collision(self.dx, 0)
if self.rect.contains(temp):
self.index_img += 0.5 / (self.effet_gel * 2)
if not self.index_img < 4:
self.index_img = 0
else:
self.index_img = 0
else:
self.index_img = 0
else:
self.index_img = 0
# Lancement attaque
self.IA_tirer_attak()
# Lancement attaque speciale
if False: # §§§§§§§ A CHANGER §§§§§§§
if self.type == 0:
if self.tps_recharg_attak_spe == 0:
Missile(self.rect.centerx, self.rect.centery, self)
self.tps_recharg_attak_spe = 14 # 250
if self.type == 2:
if self.tps_recharg_attak_spe == 0:
Glace(self.rect.centerx, self.rect.centery, self)
self.tps_recharg_attak_spe = 450
# Changement etat special
if self.type == 1:
if False:
self.fantome = True
else:
self.fantome = False
if False: # §§§§§§§ A CHANGER §§§§§§§
if self.tps_recharg_attak_spe == 0:
Bombe(self.rect.centerx, self.rect.centery, self, self.direction)
self.tps_recharg_attak_spe = 300
if self.tps_recharg_attak_spe != 0:
self.tps_recharg_attak_spe -= 1
# Detection etat
self.statement()
if self.state == "STANDING" and self.dy > 0: # Si on vient de tomber, pas si on monte sur un bloc en montant
self.dy = self.tps_en_air = self.force_saut = self.y_max = 0
if self.tps_recharg_anim_degat:
self.tps_recharg_anim_degat -= 1
if self.indestructible != False:
if self.vie != self.indestructible and self.resistance:
self.anim_bouclier = 7
self.vie = self.indestructible
def IA_sauter_sprinter(self):
if self.state == "STANDING":
None
def collision(self, dx, dy):
for bloc in ter.grp_bloc:
if self.rect.colliderect(bloc.rect) and not (self.fantome and bloc.numero != "grnd"):
if dx and self.state == "STANDING":
ecart = self.rect.bottom - bloc.rect.top
if ecart < 16:
self.rect.bottom -= ecart
self.top = self.rect.top
if self.test_collision():
self.rect.bottom += ecart
self.top = self.rect.top
self.bloquage(dx, dy, bloc)
else:
self.bloquage(dx, dy, bloc)
else:
self.bloquage(dx, dy, bloc)
def test_collision(self):
for bloc in ter.grp_bloc:
if self.rect.colliderect(bloc.rect):
if not (self.fantome and bloc.numero == "grnd"):
return True;
break
def bloquage(self, dx, dy, mur):
if dx > 0:
self.rect.right = mur.rect.left
self.left = self.rect.left
elif dx < 0:
self.rect.left = mur.rect.right
self.left = self.rect.left
if dy > 0:
if mur.numero == "slim" and self.state == "JUMPING":
self.force_saut = dy * 7 - 8
self.tps_en_air = 0
self.top = self.rect.top
self.y_max = 0
self.rect.bottom = mur.rect.top
self.top = self.rect.top
elif dy < 0:
self.rect.top = mur.rect.bottom
self.top = self.rect.top
self.tps_en_air = 0
self.force_saut = 0
def jump(self):
self.dy = -10 * self.tps_en_air ** 2 + self.tps_en_air * self.force_saut
self.tps_en_air += 0.125
self.dy -= -10 * self.tps_en_air ** 2 + self.tps_en_air * self.force_saut
# Creation variable pour calculer la hauteur du saut
if self.dy > 0 and not self.y_max:
self.y_max = self.rect.bottom
def statement(self):
self.state = "JUMPING"
list = []
temp = self.rect.copy()
temp.top += 1
for bloc in ter.grp_bloc:
if temp.colliderect(bloc.rect) and not (bloc.numero == "slim" and self.dy > 1 and self.sauter) and not (
self.fantome and bloc.numero != "grnd"):
self.state = "STANDING"
def IA_choisir_cible(self):
list1 = []
# list2 = []
for pers in ter.grp_pers:
if pers != self and pers.vie > 0:
list1.append(pers.rect.center)
# list2.append(pers.type)
if len(list1) == 0:
self.cible = False
elif len(list1) == 1:
self.cible = list1[0]
else:
self.cible = list1[0] if math.sqrt(
(list1[0][0] - self.rect.centerx) ** 2 + (list1[0][1] - self.rect.centery) ** 2) < math.sqrt(
(list1[1][0] - self.rect.centerx) ** 2 + (list1[1][1] - self.rect.centery) ** 2) else list1[1]
def IA_tirer_attak(self):
if self.tps_recharg_attak:
self.tps_recharg_attak -= 1
else:
for pers in ter.grp_pers:
if pers != self and pers.mort == False and self.tps_recharg_attak == 0:
dis_x = pers.rect.centerx - self.rect.centerx
dis_y = pers.rect.centery - self.rect.centery
hyp = math.sqrt(dis_x ** 2 + dis_y ** 2)
dx = dis_x / hyp
dy = dis_y / hyp
case_arrivee = (int(pers.rect.centerx / 30), int(pers.rect.centery / 30))
x, y = self.rect.center
while True:
x += dx
y += dy
if ter.contenu[int(y / 30) + 1][int(x / 30) + 1] != "vide":
tir = False
break
elif (int(x / 30), int(y / 30)) == case_arrivee:
tir = True
break
if tir:
dis_x += random.randint(-15, 15)
dis_y += random.randint(-15, 15)
hyp = math.sqrt(dis_x ** 2 + dis_y ** 2)
dx = dis_x / hyp
dy = dis_y / hyp
Attak(self.rect.centerx, self.rect.centery, dx, dy, self.image_attak, self,
self.image_explosion, self.couleur)
self.tps_recharg_attak = 30
"""
Classe qui definit les ia
"""
class Personnage(pygame.sprite.Sprite):
def __init__(self, position, type, sprinter, fighter, tank, image_attak, image_explosion, numero_manette, faiblesse,
couleur):
pygame.sprite.Sprite.__init__(self)
self.sprinter = sprinter
self.fighter = fighter
self.tank = tank
self.gros = [pygame.image.load("Images/Perso/Gros/" + couleur + " sprinter.png"),
pygame.image.load("Images/Perso/Gros/" + couleur + " fighter.png"),
pygame.image.load("Images/Perso/Gros/" + couleur + " tank.png")]
self.type = type
if self.type == 0:
self.image = self.sprinter
self.rect = pygame.Rect(position[0], position[1], 40, 52)
self.vitesse = 6
self.vie = 45
self.anim_degat = im.anim_degat_sprinter
self.anim_ralenti = im.anim_ralenti_sprinter
self.anim_poison = im.anim_poison_sprinter
elif self.type == 1:
self.image = self.fighter
self.rect = pygame.Rect(position[0], position[1], 60, 48)
self.vitesse = 2.5
self.vie = 60
self.tps_recharg_saut = 0
self.anim_degat = im.anim_degat_fighter
self.anim_ralenti = im.anim_ralenti_fighter
self.anim_poison = im.anim_poison_fighter
elif self.type == 2:
self.image = self.tank
self.rect = pygame.Rect(position[0], position[1], 56, 60)
self.vitesse = 2
self.vie = 100
self.anim_degat = im.anim_degat_tank
self.anim_ralenti = im.anim_ralenti_tank
self.anim_poison = im.anim_poison_tank
self.tps_recharg_changement_type = 0
self.left, self.top = position[0], position[1]
self.pos_init = position
self.couleur = couleur
self.faiblesse = faiblesse
self.dy = self.dx = self.tps_en_air = self.y_max = self.effet_poison = 0
self.state = "STANDING"
self.joystick = pygame.joystick.Joystick(numero_manette)
self.acceleration = self.effet_gel = 1
self.force_saut = 0
self.full_vie = self.vie
self.tps_recharg_anim_degat = 0
self.pers_valide = False
self.fantome = False
self.effet_rapide = 1
self.resistance = 0
self.anim_bouclier = 0
self.index_img = 0
self.image = dict([(direction, [self.image.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.direction = -1
self.anim_degat = dict([(direction,
[self.anim_degat.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_ralenti = dict([(direction,
[self.anim_ralenti.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.anim_poison = dict([(direction,
[self.anim_poison.subsurface(x, y, self.rect.width, self.rect.height) for x in
range(0, self.rect.width * 4, self.rect.width)]) for direction, y in
zip((-1, 1), range(0, self.rect.height * 2, self.rect.height))])
self.image_attak = [image_attak.subsurface(index, 0, 20, 20) for index in range(0, 80, 20)]
self.image_explosion = image_explosion