Bonjour, dans mon jeu j'essaie depuis plusieurs jours de tester un ennemi à l'écran, sauf que quand j'essaie de le faire apparaître... Python crashe sans même renvoyer d'erreur, est-ce que quelqu'un peut m'aider ?
Le fichier de l'ennemi :
Le fichier de la classe du jeu :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import pygame import random from enemyIMG import * from enemySND import * from gear import EnemyBoomerang class Enemy(pygame.sprite.Sprite): def __init__(self, image=pygame.Surface((64, 64)), tileX=0, tileY=0, enemyName=None): super().__init__() self.tileX=tileX self.tileY=tileY self.enemyName=enemyName self.image=image self.rect=self.image.get_rect() self.rect.x=self.tileX*64 self.rect.y=self.tileY*64 self.health=0 self.max_health=0 self.attack=0 self.velocity=0 self.direction="SOUTH" self.action="idle" self.mask=pygame.mask.from_surface(self.image) class ArmMimic(Enemy): def __init__(self, game, tileX, tileY): pygame.sprite.Sprite.__init__(self) self.enemyName="armMimic" self.game=game self.tileX=tileX self.tileY=tileY self.health=12 self.max_health=12 self.attack=4 self.image=armMimicS1 self.direction="SOUTH" self.velocity=14 self.frame=1 self.isDamaged=False Enemy().__init__(image=self.image, tileX=self.tileX, tileY=self.tileY, enemyName=self.enemyName) def reMove(self): self.remove(self.game.enemies) def damageKnockback(self): pass def damage(self, amount): self.isDamaged=True hit.play() self.health-=amount if self.health<=0: self.health=0 while pygame.mixer.get_busy(): if not pygame.mixer.get_busy(): break else: pass die.play() self.reMove() def move_right(self): if not self.game.check_colliding(self, self.game.player_char): self.action="move" self.direction="EAST" self.rect.x+=self.velocity def move_left(self): if not self.game.check_colliding(self, self.game.player_char): self.action="move" self.direction="WEST" self.rect.x-=self.velocity def move_up(self): if not self.game.check_colliding(self, self.game.player_char): self.action="move" self.direction="NORTH" self.rect.y-=self.velocity def move_down(self): if not self.game.check_colliding(self, self.game.player_char): self.action="move" self.direction="SOUTH" self.rect.y+=self.velocity def setImage(self): if self.action=="idle": if self.direction=="SOUTH": self.image=armMimicS1 elif self.direction=="NORTH": self.image=armMimicN1 elif self.direction=="WEST": self.image=armMimicW1 elif self.direction=="EAST": self.image=armMimicE1 self.frame=1 elif self.action=="move": if self.frame==1: if self.direction=="SOUTH": self.image=armMimicS1 elif self.direction=="NORTH": self.image=armMimicN1 elif self.direction=="WEST": self.image=armMimicW1 elif self.direction=="EAST": self.image=armMimicE1 self.frame=2 elif self.frame==2: if self.direction=="SOUTH": self.image=armMimicS2 elif self.direction=="NORTH": self.image=armMimicN2 elif self.direction=="WEST": self.image=armMimicW2 elif self.direction=="EAST": self.image=armMimicE2 self.frame=1 self.mask=pygame.mask.from_surface(self.image) class Moblin(Enemy): def __init__(self, game, tileX, tileY, colour="red", param=None): pygame.sprite.Sprite.__init__(self) self.game=game self.colour=colour self.param=param self.image=redMoblinS1 self.direction="SOUTH" self.tileX=tileX self.tileY=tileY self.max_health=8 self.health=8 self.attack=2 self.action="idle" self.actions=[] self.chosenA="" self.hasBoomerang=True if self.colour=="red": self.max_health=8 self.health=8 self.attack=2 self.image=redMoblinS1 if self.param=="boomerang": self.actions=["move_right", "move_left", "move_up", "move_down", "spawnBoomerang"] #On y rajoutera l'action du lancer de boomerang plus tard. elif self.param=="arrow": pass else: self.actions=["move_right", "move_left", "move_up", "move_down"] elif self.colour=="blue": pass elif self.colour=="golden": pass else: pass self.rect=self.image.get_rect() self.velocity=14 self.frame=1 Enemy.__init__(self, image=self.image, tileX=self.tileX, tileY=self.tileY, enemyName="moblin") def reMove(self): self.remove(self.game.enemies) def damage(self, amount): hit.play() self.health-=amount if self.health<=0: self.health=0 while pygame.mixer.get_busy(): if not pygame.mixer.get_busy(): break else: pass die.play() self.reMove() def move_right(self): self.action="move" self.direction="EAST" self.rect.x+=self.velocity def move_left(self): self.action="move" self.direction="WEST" self.rect.x-=self.velocity def move_up(self): self.action="move" self.direction="NORTH" self.rect.y-=self.velocity def move_down(self): self.action="move" self.direction="SOUTH" self.rect.y+=self.velocity def spawnBoomerang(self): #Modifier plus tard self.game.gear.add(EnemyBoomerang(self)) self.hasBoomerang=False while not self.hasBoomerang: pass def setComportment(self): self.chosenA=random.choice(self.actions) exec(f"self.{self.chosenA}()") def setImage(self): #Modifier plus tard if self.action=="idle": self.frame=1 if self.direction=="SOUTH": if self.colour=="red": self.image=redMoblinS1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="NORTH": if self.colour=="red": self.image=redMoblinN1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="EAST": if self.colour=="red": self.image=redMoblinE1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="WEST": if self.colour=="red": self.image=redMoblinW1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.action=="move": if self.frame==2: if self.direction=="SOUTH": if self.colour=="red": self.image=redMoblinS2 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="NORTH": if self.colour=="red": self.image=redMoblinN2 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="EAST": if self.colour=="red": self.image=redMoblinE2 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="WEST": if self.colour=="red": self.image=redMoblinW2 elif self.colour=="blue": pass elif self.colour=="golden": pass self.frame=1 elif self.frame==1: if self.direction=="SOUTH": if self.colour=="red": self.image=redMoblinS1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="NORTH": if self.colour=="red": self.image=redMoblinN1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="EAST": if self.colour=="red": self.image=redMoblinE1 elif self.colour=="blue": pass elif self.colour=="golden": pass elif self.direction=="WEST": if self.colour=="red": self.image=redMoblinW1 elif self.colour=="blue": pass elif self.colour=="golden": pass self.frame=2 self.mask=pygame.mask.from_surface(self.image)
Sachant que l'erreur survient quand je mets la fonction spawnEnemy quelque part dans la fonction __init__ de la classe Game, quelqu'un pourrait m'aider ? Merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import pygame from player import * from gear import * from enemy import * from eventList import * from hud import * from tilesetLoader import * from saveDataLoader import * class Game: def __init__(self): self.fm=FileManager() self.fm.openFile() self.name=self.fm.name self.world=self.fm.world self.screenCoordsCol=self.fm.screenCoordsCol self.screenCoordsRow=self.fm.screenCoordsRow self.max_health=self.fm.max_health self.rupees=self.fm.rupees self.current_health=self.fm.current_health self.sword_level=self.fm.sword_level self.shield_level=self.fm.shield_level self.gotSword=self.fm.gotSword self.gotShield=self.fm.gotShield self.companion=self.fm.companion self.assignedItems=self.fm.assignedItems self.inventoryContent=self.fm.inventoryContent #Apparition du joueur self.link=PlayerChar(self) self.player_char=pygame.sprite.GroupSingle(self.link) self.shade=Shadow(self, self.link) self.hud=HUD(self, self.link.current_max_health, self.link.current_health) self.tile_loader=TileLoader(self) self.gear=pygame.sprite.Group() self.items=pygame.sprite.Group() #Ce dictionnaire permet de définir toutes les touches utilisables en jeu. #Par exemple, K_RIGHT correspond à la flèche de droite. self.enemies=pygame.sprite.Group() self.pressed={ "K_RIGHT":False, "K_LEFT":False, "K_UP":False, "K_DOWN":False, "K_y":False, "K_g":False } def check_colliding(self, sprite, group): return pygame.sprite.spritecollide(sprite, group, False, pygame.sprite.collide_mask) def initMap(self, screenCoords, world): pass def initSideScrollPart(self, dungeon, warp): pass def spawnEnemy(self): self.enemies.add(Moblin(self, 1, 2, "red", "boomerang")) def checkXCollision(self, sprite1, group_or_sprite2): if type(group_or_sprite2)==pygame.sprite.Sprite or issubclass(pygame.sprite.Sprite, type(group_or_sprite2)): sprite2=group_or_sprite2 sprite2G=pygame.sprite.Group(sprite2) try: assert bool(self.check_colliding(sprite1, sprite2G)) for sprite in self.check_colliding(sprite1, sprite2G): if sprite1.rect.x<sprite.rect.x+sprite.image.get_width()//2: return False elif sprite1.rect.x>sprite.rect.x+sprite.image.get_width()//2: return True except AssertionError: return None elif type(group_or_sprite2)==pygame.sprite.Group or type(group_or_sprite2)==list: group=group_or_sprite2 try: #On affirme que la liste des collisions entre sprite1 et le groupe donné contient au moins un élément. #Si elle est vide, la fonction renvoie None. #Si collision il y a, la fonction renvoie False si elle a eu lieu à gauche et True si elle a eu lieu à droite. assert bool(self.check_colliding(sprite1, group)) for sprite2 in self.check_colliding(sprite1, group): if sprite1.rect.x<sprite2.rect.x+sprite2.image.get_width()//2: return False elif sprite1.rect.x>sprite2.rect.x+sprite2.image.get_width()//2: return True except AssertionError: return None def checkYCollision(self, sprite1, group): try: #Même commentaire que pour checkXCollision, sauf que False est ici utilisé pour le haut et True pour le bas. assert bool(self.check_colliding(sprite1, group)) for sprite2 in self.check_colliding(sprite1, group): if sprite1.rect.y<sprite2.rect.y+sprite2.image.get_height()//2: return False elif sprite1.rect.y>sprite2.rect.y+sprite2.image.get_height()//2: return True except AssertionError: return None
Partager