-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jeu.py
271 lines (206 loc) · 7.38 KB
/
Jeu.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
import pygame as pg
import random as rd
import sys
import os
path = 'K:\profil\Bureau\labyrinthe-master2\labyrinthe-master'
#path = os.path.dirname(os.path.abspath(__file__))
os.chdir(path)
import labyrinthe # as labyrinthe
import myLib
### COLOR
BLACK=[0,0,0]
WHITE=[255,255,255]
RED=[255,0,0]
GREEN=[0,255,0]
slate_gray=[112,138,144]
dark_slate_blue=[72,61,139]
pg.init()
### SCREEN
Largeur=800
Hauteur=600
SIZE=[Largeur, Hauteur]
screen=pg.display.set_mode(SIZE)
pg.display.set_caption("Lab")
background = pg.image.load('back.png').convert()
end = pg.image.load('img/endScreen.png')
### LABYRINTHE
sa = rd.randint(100, 300)
print(sa)
laby = labyrinthe.CLaby()
laby.initRandom()
laby.gen(sa) #On génère un labyrinthe qui contient sa salles.
screen.blit(background, (0,0))
### BOUSSOLE
compass = pg.image.load('img/compass.png')
def boussole(dir) :
screen.blit(pg.transform.rotate(compass, 90*(-dir%4)), (0,0))
return
boussole(0)
### INDICES
indice = False
arrow = pg.image.load('img/direction.png')
def fleche(plCoor, plDir) : #Affiche une flèche qui indique la direction à suivre
hintDir = myLib.absDirToRel(dir, laby.dirToEnd(plCoor))
screen.blit(pg.transform.rotate(arrow, 90*((hintDir-1)%4)), (720,0))
###MUSIC
bgm = 'background.mp3'
pg.mixer.init()
pg.mixer.music.load(bgm)
### TEXTURES
dir = 0
x=0
y=0
coor=[x,y]
welcome1 = pg.image.load('img\welcomeScreen_1.png')
screen.blit(welcome1, (0,0))
pg.display.flip()
class ImageNotFoundError(Exception): pass
class CImage():
def __init__(self, type, posx, posy, orientation):
self.type = type
self.posx = posx
self.posy = posy
self.orientation = orientation
self.dist = abs(posx*10)+abs(posy)+0.1
if orientation == 0:
self.dist += 0.1
if orientation == 2:
self.dist -= 0.1
if type=='chest':
self.dist -= 0.1
if type in ("floor","pavement"):
self.dist += 1000
try: #Permet d'éviter une erreure due au fait de charger des images non-existantes
self.image = pg.image.load('img/tiles/%s%0+2d%+02d_%d.png'%(self.type, self.posx, self.posy, self.orientation))
except pg.error:
if os.path.isfile('img/tiles/%s%0+2d%+02d_%d.empty'%(self.type, self.posx, self.posy, self.orientation)):
self.image = None
else:
raise ImageNotFoundError
def render(self): #Affiche une image si elle existe
if self.image:
screen.blit(self.image, (0,0))
images = set()
def load(): #Charge la totalité des images au lancement du programme
loaded = {}
pg.mixer.music.play()
for step, obj in enumerate(['floor', 'pavement', 'wall', 'tunnel', 'chest']):
for i in range (4):
for j in range (-10, 10):
for k in range (4):
try: #Permet d'éviter une erreure due au fait de charger des images non-existantes
loaded[obj, i, j, k] = CImage(obj, i, j, k%4)
except ImageNotFoundError:
pass
screen.blit(pg.image.load('img\welcomeScreen_%d.png'%(step+1)), (0,0))
pg.display.flip()
return loaded
loaded = load()
def construction (aRoom, direction, i, j, images, loaded): #Selectionne les images à placer en fonction de la position du joueur dans le labyrinthe
isDoors = aRoom.doors
screen.blit(background, (0,0))
if aRoom.BigRoomId == 0:
im = loaded["floor", i, j, (direction)%4]
else:
im = loaded["pavement", i, j, (direction)%4]
images.add(im)
if aRoom.isEnd:
images.add(loaded["chest", i, j, (direction)%4])
for d in range (4):
if isDoors[d] == 0 :
im = loaded["wall", i, j, (d-direction)%4]
else:
im = loaded["tunnel", i, j, (d-direction)%4]
images.add(im)
construction(laby.laby[x,y], dir,0 ,0, images, loaded)
images = sorted(images, key=lambda x: x.dist, reverse=True)
for im in images:
im.render()
boussole(dir)
def voisins(aRoom, userDir, i, j, images, disti, distj): #Détermine les objets à afficher en fonction de leur visibilité
if disti >= 3:
return
if distj > disti+1:
return
r = aRoom.doors[userDir%4]
if r != 0:
construction (r, dir%4, i+1, j, images, loaded)
voisins(r, userDir, i+1, j, images, disti+1, distj)
r = aRoom.doors[(userDir+1)%4]
if r != 0:
construction (r, dir%4, i, j+1, images, loaded)
voisins(r, userDir, i, j+1, images, disti, distj+1)
r = aRoom.doors[(userDir+3)%4]
if r != 0:
construction (r, dir%4, i, j-1, images, loaded)
voisins(r, userDir, i, j-1, images, disti, distj+1)
def affichage(userRoom, userDir) :
images = set()
construction (userRoom, userDir, 0, 0, images, loaded)
voisins(userRoom, userDir, 0, 0, images, 0, 0)
images = sorted(images, key=lambda x: x.dist, reverse=True)
for im in images :
im.render()
boussole(dir)
pg.display.flip()
VECT = [(1,0),(0,1),(-1,0),(0,-1)]
affichage(laby.laby[x,y], dir)
pg.display.flip()
### BOUCLE INFINIE
jeuEnCours = True
while jeuEnCours:
for event in pg.event.get() :
if event.type==pg.QUIT:
pg.quit()
elif event.type == pg.KEYDOWN:
indice = False
if event.key == 273: #haut
if laby.laby[x,y].doors[dir%4] != 0:
dx, dy = VECT[dir%4]
x += dx
y += dy
elif event.key == 274: #bas
if laby.laby[x,y].doors[(dir+2)%4] != 0:
dx, dy = VECT[dir%4]
x -= dx
y -= dy
elif event.key == 275: #droite
dir-=1
elif event.key == 276: #gauche
dir+=1
elif event.key == 27: #échap
pg.quit()
elif event.key == 104: #h
indice = True
elif event.key == 114: #r
while laby.laby[x,y].isEnd != 1 :
autoDir = myLib.absDirToRel(dir, laby.dirToEnd((x,y)))
if autoDir == 1 :
dx, dy = VECT[dir%4]
x += dx
y += dy
elif autoDir == 2 :
dir+=1
elif autoDir == 0 :
dir-=1
elif autoDir == 3 :
dx, dy = VECT[dir%4]
x -= dx
y -= dy
affichage(laby.laby[x,y], dir)
pg.display.flip()
pg.time.delay(500)
else:
print (event.key) ## afficher la touche que si elle n'est pas traitée avant
affichage(laby.laby[x,y], dir)
if indice:
fleche((x,y), dir)
pg.display.flip()
if laby.laby[x,y].isEnd:
pg.time.delay(500)
screen.blit(end, (0,0))
pg.display.flip()
pg.time.delay(5000)
jeuEnCours = False
break
pg.quit()