Repository navigation
Expand file tree
/
Copy pathdeplacements.py
More file actions
315 lines (287 loc) · 11 KB
/
Copy pathdeplacements.py
File metadata and controls
315 lines (287 loc) · 11 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
import pygame
from pygame.locals import *
import random
from classPerso import Monstre
import math
import classChest
from echanges import echange
import time
def mur(x,y,direction, map):
carte = map.grid
res = False
yPrec = y
xPrec = x
if direction=='haut':
ySuiv = (y - 1)
if yPrec != ySuiv and carte[yPrec][xPrec].walls[0]==True:
res = True
if direction=='droite':
xSuiv = (x + 1)
if xPrec != xSuiv and carte[yPrec][xPrec].walls[1]==True:
res = True
if direction=='bas':
ySuiv = (y + 1)
if yPrec != ySuiv and carte[yPrec][xPrec].walls[2]==True:
res = True
if direction=='gauche':
xSuiv = (x - 1)
if xPrec != xSuiv and carte[yPrec][xPrec].walls[3]==True:
res = True
return res
def ya_monstre(xPrec, yPrec, monstres, direction):
res = False
for m in monstres:
if direction == 'haut':
ySuiv = (yPrec - 1)
xSuiv = xPrec
if direction == 'droite':
xSuiv = (xPrec + 1)
ySuiv = yPrec
if direction == 'bas':
ySuiv = (yPrec + 1)
xSuiv = xPrec
if direction == 'gauche':
xSuiv = (xPrec - 1)
ySuiv = yPrec
if m.x == xSuiv and m.y == ySuiv:
res = True
break
return res
def attMonstre(x, y, direction, monstre, perso, map):
res = False
yPrec = y
xPrec = x
if monstre.vivant and perso.dansDonjon:
if direction=='haut':
ySuiv = (y - 1)
if yPrec != ySuiv and ySuiv == monstre.y and xPrec == monstre.x:
res = True
if direction=='droite':
xSuiv = (x + 1)
if xPrec != xSuiv and xSuiv == monstre.x and yPrec == monstre.y:
res = True
if direction=='bas':
ySuiv = (y + 1)
if yPrec != ySuiv and ySuiv == monstre.y and xPrec == monstre.x:
res = True
if direction=='gauche':
xSuiv = (x - 1)
if xPrec != xSuiv and xSuiv == monstre.x and yPrec == monstre.y:
res = True
if res == True:
if time.time() - perso.derniereAtt > 1.5/perso.vitesse :
perso.derniereAtt = time.time()
monstre.vie += - random.randint(perso.force-2, perso.force+2)
if monstre.vie <= 0:
monstre.vivant = False
res = False
perso.inventaire.pieces += 10
return res
def ya_coffre(x, y, direction, coffres, perso):
res = False
yPrec = y
xPrec = x
for coffre in coffres:
if direction == 'haut':
ySuiv = (y - 1)
if yPrec != ySuiv and ySuiv == coffre.y and xPrec == coffre.x:
res = True
if direction == 'droite':
xSuiv = (x + 1)
if xPrec != xSuiv and xSuiv == coffre.x and yPrec == coffre.y:
res = True
if direction == 'bas':
ySuiv = (y + 1)
if yPrec != ySuiv and ySuiv == coffre.y and xPrec == coffre.x:
res = True
if direction == 'gauche':
xSuiv = (x - 1)
if xPrec != xSuiv and xSuiv == coffre.x and yPrec == coffre.y:
res = True
if res:
echange(perso, coffre)
break
return res
def monstre_ya_coffre(x, y, direction, coffres):
res = False
yPrec = y
xPrec = x
for coffre in coffres:
if direction == 'haut':
ySuiv = (y - 1)
if yPrec != ySuiv and ySuiv == coffre.y and xPrec == coffre.x:
res = True
if direction == 'droite':
xSuiv = (x + 1)
if xPrec != xSuiv and xSuiv == coffre.x and yPrec == coffre.y:
res = True
if direction == 'bas':
ySuiv = (y + 1)
if yPrec != ySuiv and ySuiv == coffre.y and xPrec == coffre.x:
res = True
if direction == 'gauche':
xSuiv = (x - 1)
if xPrec != xSuiv and xSuiv == coffre.x and yPrec == coffre.y:
res = True
if res:
break
return res
def attPerso(x, y, direction, perso, monstre, map):
res = False
yPrec = y
xPrec = x
if perso.vivant and monstre.dansDonjon:
if direction=='haut':
ySuiv = (y - 1)
if yPrec != ySuiv and ySuiv == perso.y and xPrec == perso.x:
res = True
if direction=='droite':
xSuiv = (x + 1)
if xPrec != xSuiv and xSuiv == perso.x and yPrec == perso.y:
res = True
if direction=='bas':
ySuiv = (y + 1)
if yPrec != ySuiv and ySuiv == perso.y and xPrec == perso.x:
res = True
if direction=='gauche':
xSuiv = (x - 1)
if xPrec != xSuiv and xSuiv == perso.x and yPrec == perso.y:
res = True
if res == True:
attaque = random.randint(monstre.force - 2, monstre.force + 2)
if perso.protection > 0:
perso.protection -= attaque
perso.equipement[0].protection -= attaque
else:
perso.protection = 0
perso.vie -= attaque
if perso.vie <= 0:
perso.vivant = False
perso.vie = 0
res = False
return res
def deplacerPerso(perso, map, monstres, coffres, key):
res = False
x = perso.x
y = perso.y
pas_monstre = True
pas_coffre = True
if key == K_UP:
perso.dir = 0
if y > 0 and not mur(x, y, 'haut', map):
for monstre in monstres:
if attMonstre(x, y, 'haut', monstre, perso, map):
pas_monstre = False
if ya_coffre(x, y, 'haut', coffres, perso):
pas_coffre = False
if pas_monstre and pas_coffre:
perso.y -= 1
res = True
if key == K_DOWN:
perso.dir = 2
if y < map.w-1 and not mur(x, y, 'bas', map):
for monstre in monstres:
if attMonstre(x, y, 'bas', monstre, perso, map):
pas_monstre = False
if ya_coffre(x, y, 'bas', coffres, perso):
pas_coffre = False
if pas_monstre and pas_coffre:
perso.y += 1
res = True
if key == K_RIGHT:
perso.dir = 1
if x < map.h-1 and not mur(x, y, 'droite', map):
for monstre in monstres:
if attMonstre(x, y, 'droite', monstre, perso, map):
pas_monstre = False
if ya_coffre(x, y, 'droite', coffres, perso):
pas_coffre = False
if pas_monstre and pas_coffre:
perso.x += 1
res = True
if key == K_LEFT:
perso.dir = 3
if x > 0 and not mur(x, y, 'gauche', map):
for monstre in monstres:
if attMonstre(x, y, 'gauche', monstre, perso, map):
pas_monstre = False
if ya_coffre(x, y, 'gauche', coffres, perso):
pas_coffre = False
if pas_monstre and pas_coffre:
perso.x -= 1
res = True
return res
def deplacerMonstre(monstre, monstres, coffres, map, perso):
x = monstre.x
y = monstre.y
deplacementMonstre = [K_UP, K_DOWN, K_RIGHT, K_LEFT]
passage = False
passagePossible = []
distance = math.sqrt(math.pow(perso.x - x, 2) + math.pow(perso.y - y, 2))
peut_bouger = False
#Depalcement logique lorsque la distance du perso est inférieur ou égale à la vision du monstre
#si le monstre voit le joueur et qu'il est assez fort ou une chance sur deux
if distance <= monstre.vision and distance != 0 and (monstre.force >= perso.force or random.random() >= 0.5):
#rapprochement du monstre
if math.sqrt(math.pow(perso.x - x, 2)) > math.sqrt(math.pow(perso.y - y, 2)):
if perso.x > x: key = K_RIGHT
elif perso.x < x: key = K_LEFT
elif math.sqrt(math.pow(perso.x - x, 2)) < math.sqrt(math.pow(perso.y - y, 2)):
if perso.y > y: key = K_DOWN
elif perso.y < y: key = K_UP
else:
if random.randint(1,2) == 1:
if perso.x > x: key = K_RIGHT
elif perso.x < x: key = K_LEFT
else:
if perso.y > y: key = K_DOWN
elif perso.y < y: key = K_UP
#test mur et coffre
if key == K_UP:
peut_bouger = not mur(x, y, 'haut', map) and not monstre_ya_coffre(x, y, 'haut', coffres) and y > 0
if key == K_DOWN:
peut_bouger = not mur(x, y, 'bas', map) and not monstre_ya_coffre(x, y, 'bas', coffres) and y < map.w - 1
if key == K_LEFT:
peut_bouger = not mur(x, y, 'gauche', map) and not monstre_ya_coffre(x, y, 'gauche', coffres) and x > 0
if key == K_RIGHT:
peut_bouger = not mur(x, y, 'droite', map) and not monstre_ya_coffre(x, y, 'droite', coffres) and x < map.h - 1
#si il ne pas bouger alors deplacement autre part
if not peut_bouger:
for elt in deplacementMonstre:
if elt != monstre.dirRetour:
if elt == K_UP and not mur(x, y, 'haut', map) and not monstre_ya_coffre(x, y, 'haut', coffres) and y > 0:
passage = True
passagePossible.append(elt)
if elt == K_DOWN and not mur(x, y, 'bas', map) and not monstre_ya_coffre(x, y, 'bas', coffres) and y < map.w-1:
passage = True
passagePossible.append(elt)
if elt == K_LEFT and not mur(x, y, 'gauche', map) and not monstre_ya_coffre(x, y, 'gauche', coffres) and x > 0:
passage = True
passagePossible.append(elt)
if elt == K_RIGHT and not mur(x, y, 'droite', map) and not monstre_ya_coffre(x, y, 'droite', coffres) and x < map.h-1:
passage = True
passagePossible.append(elt)
if passage == False:
key = monstre.dirRetour
else:
key = random.choice(passagePossible)
if key == K_UP :
monstre.dir = 0
monstre.dirRetour = K_DOWN
if not attPerso(x, y, 'haut', perso, monstre, map) and not ya_monstre(x, y, monstres, 'haut'):
monstre.y -= 1
if key == K_DOWN :
monstre.dir = 2
monstre.dirRetour = K_UP
if not attPerso(x, y, 'bas', perso, monstre, map) and not ya_monstre(x, y, monstres, 'bas'):
monstre.y += 1
if key == K_LEFT :
monstre.dir = 3
monstre.dirRetour = K_RIGHT
if not attPerso(x, y, 'gauche', perso, monstre, map) and not ya_monstre(x, y, monstres, 'gauche'):
monstre.x -= 1
if key == K_RIGHT :
monstre.dir = 1
monstre.dirRetour = K_LEFT
if not attPerso(x, y, 'droite', perso, monstre, map) and not ya_monstre(x, y, monstres, 'droite'):
monstre.x += 1