Bonsoir,
Comme le titre l'indique, j'ai un problème avec une boucle for qui ne fonctionne pas totalement.
Elle sert à gérer une collision entre un joueur et l'ensemble des obstacles du jeu (appelés o in obstacles).
Jusqu'ici tout va bien, mais quand le joueur rencontre ces obstacles, il n'y a que pour le premier que le joueur est bloqué, il traverse les autres.
Là ou c'est problématique, c'est que lorsque je ne met "sys.exit()" après la collision, la commande s’effectue correctement pour chaque obstacles.
Merci de votre aide. Voici les codes :
Version sys.exit():
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 def move(animat,c,background,obstacles): x = animat['x'] y = animat['y'] savex = animat['x'] savey = animat['y'] if c=='d': animat["direction"]= 'right' x = animat['x'] + 1 elif c=='q': animat["direction"]='left' x = animat['x'] -1 elif c=='s': animat["direction"]='up' y = animat['y'] + 1 elif c=='z': animat["direction"]='down' y = animat['y'] - 1 if Background.isValid(background,x+1,y+1): for o in obstacles: if Obstacle.testCollision(o,x+1,y+1)==False: animat['x']= savex animat['y']= savey else: animat['x']= x animat['y']= y
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 def move(animat,c,background,obstacles): x = animat['x'] y = animat['y'] savex = animat['x'] savey = animat['y'] if c=='d': animat["direction"]= 'right' x = animat['x'] + 1 elif c=='q': animat["direction"]='left' x = animat['x'] -1 elif c=='s': animat["direction"]='up' y = animat['y'] + 1 elif c=='z': animat["direction"]='down' y = animat['y'] - 1 if Background.isValid(background,x+1,y+1): for o in obstacles: if Obstacle.testCollision(o,x+1,y+1)==False: sys.exit() else: animat['x']= x animat['y']= y
Partager