-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathia.py
107 lines (79 loc) · 4.31 KB
/
ia.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
import random
def ia(rect, fruit, joueur):
#Coordonnées des murs
a, b = (rect.x + rect.w), (rect.y + rect.h)
#Collision Mur
collision_haut = (joueur.rect.y - 20 < rect.y) and joueur.direction == 2
collision_bas = (joueur.rect.y + 40 > b) and joueur.direction == 3
collision_droite = (joueur.rect.x + 40 > a) and joueur.direction == 0
collision_gauche = (joueur.rect.x - 20 < rect.x) and joueur.direction == 1
#Collision corps
collision_corps_haut = [joueur.rect.x, joueur.rect.y - 20] in joueur.corps and joueur.direction == 2
collision_corps_bas = [joueur.rect.x, joueur.rect.y + 20] in joueur.corps and joueur.direction == 3
collision_corps_droite = [joueur.rect.x + 20, joueur.rect.y] in joueur.corps and joueur.direction == 0
collision_corps_gauche= [joueur.rect.x - 20, joueur.rect.y] in joueur.corps and joueur.direction == 1
#Gestion collision
if collision_droite or collision_gauche or collision_corps_droite or collision_corps_gauche:
if fruit.rect.y > (joueur.rect.y) and obstruction(joueur, fruit, 3):
joueur.direction = 3
else:
joueur.direction = 2
if collision_haut or collision_bas or collision_corps_haut or collision_corps_bas:
if fruit.rect.x > (joueur.rect.x) and obstruction(joueur, fruit, 0):
joueur.direction = 0
else:
joueur.direction = 1
#Se deplace vers le fruit si la voie est libre
if fruit.rect.x < (joueur.rect.x-19) and joueur.direction != 0 and obstruction(joueur, fruit, 1):
if [joueur.rect.x - 20, joueur.rect.y] not in joueur.corps:
joueur.direction = 1
elif fruit.rect.x > (joueur.rect.x+19) and joueur.direction != 1 and obstruction(joueur, fruit, 0):
if [joueur.rect.x + 20, joueur.rect.y] not in joueur.corps:
joueur.direction = 0
if fruit.rect.y > (joueur.rect.y+19) and joueur.direction != 2 and obstruction(joueur, fruit, 3):
if [joueur.rect.x, joueur.rect.y + 20] not in joueur.corps:
joueur.direction = 3
elif fruit.rect.y < (joueur.rect.y-19) and joueur.direction != 3 and obstruction(joueur, fruit, 2):
if [joueur.rect.x, joueur.rect.y - 20] not in joueur.corps:
joueur.direction = 2
#Retourne False si le corps du serpent fait obstruction
def obstruction(joueur, fruit, direction):
for bloc in joueur.corps:
#Fruit aligné avec la tete du serpent
fruit_vertical = (joueur.rect.x-20 < fruit.rect.x < joueur.rect.x+20)
fruit_horizontal = (joueur.rect.y-20 < fruit.rect.y < joueur.rect.y+20)
#Bloc du corps aligné avec la tete du serpent
corps_vertical = (joueur.rect.x == bloc[0])
corps_horizontal = (joueur.rect.y == bloc[1])
#Corps entre serpent et fruit
fruit_obs_haut = corps_vertical and (fruit.rect.y <= bloc[1] <= joueur.rect.y)
fruit_obs_bas = corps_vertical and (fruit.rect.y >= bloc[1] >= joueur.rect.y)
fruit_obs_droite = corps_horizontal and (fruit.rect.x >= bloc[0] >= joueur.rect.x)
fruit_obs_gauche = corps_horizontal and (fruit.rect.x <= bloc[0] <= joueur.rect.x)
#bloc du corps en serpent et mur
corps_obs_haut = corps_vertical and (bloc[1] < joueur.rect.y)
corps_obs_bas = corps_vertical and (bloc[1] > joueur.rect.y)
corps_obs_droite = corps_horizontal and (bloc[0] > joueur.rect.x)
corps_obs_gauche = corps_horizontal and (bloc[0] < joueur.rect.x)
match direction:
case 0:
if corps_obs_droite and (not fruit_obs_droite) and (not fruit_horizontal):
return False
elif fruit_obs_droite:
return False
case 1:
if corps_obs_gauche and (not fruit_obs_gauche) and (not fruit_horizontal):
return False
elif fruit_obs_gauche:
return False
case 2:
if corps_obs_haut and (not fruit_obs_haut) and (not fruit_vertical):
return False
elif fruit_obs_haut:
return False
case 3:
if corps_obs_bas and (not fruit_obs_bas) and (not fruit_vertical):
return False
elif fruit_obs_bas:
return False
return True