-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab.py
309 lines (238 loc) · 9.89 KB
/
Lab.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
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
#-*- coding:utf-8 -*-
import os, sys, shutil, pygame
from pygame.locals import *
from random import randint
import main, utils, enemy
from constants import *
# Dimensions
W, H = 16*SQUARE+PANEL, 16*SQUARE
class Lab() :
def __init__(self, filename, new=True) :
if new :
shutil.copyfile("lab.txt", os.path.join(CONFIG, filename))
self.file = os.path.join(CONFIG, filename)
with open(os.path.join(CONFIG, filename), "r") as f :
self.level = int(f.readline())
self.money = int(f.readline())
self.lab = [ list(l) for l in f ]
# Define difficulty
self.time = 30000 + self.level*100
self.speed = 40 + self.level*2
# Add Schrodinger
print(dir, "truc")
self.schrodinger = enemy.Enemy(self.lab) #, dir)
# Create the window
self.disp = pygame.display.set_mode( (W, H) )
self.disp.fill(WHITE)
self.running = True
self.walking = False # True when Schrödinger walks
self.clock = pygame.time.Clock()
# Add elements and start main loop
self.main() # main square
self.panel() # side panel
self.selectmode() # events for select mode
self.loop()
def loop(self) :
while self.running :
# Check events
for evt in pygame.event.get() :
if evt.type == QUIT :
self.running = False
main.App()
elif evt.type == MOUSEMOTION :
self.onmousemove(evt.pos)
elif evt.type == MOUSEBUTTONUP :
self.onclick(evt.pos)
# Move Schrödinger
if self.walking :
pos = self.schrodinger.move()
prev = self.schrodinger.getprev()
if self.schrodinger.getdisturbed() : self.speed = 40 + self.level/10
if pos : # pos exists
x, y = pos
xprev, yprev = prev
coordx, coordy = xprev*SQUARE, yprev*SQUARE
# Animation between tiles
for i in xrange(1, SQUARE/3) :
coordx += 3*(x - xprev)
coordy += 3*(y - yprev)
self.main()
if x - xprev == 0 :
self.disp.blit(IMG_SCHRODINGER, (coordx, coordy-15))
elif x - xprev <= 0 :
self.disp.blit(IMG_SCHRODINGER_L, (coordx, coordy-15))
elif x - xprev >= 0 :
self.disp.blit(IMG_SCHRODINGER_R, (coordx, coordy-15))
pygame.display.update()
self.time -= self.clock.tick(self.speed)
else : # Cat is reached
if randint(0, 1) :
self.disp.blit(IMG_LOOSE_DEAD, (0,0))
pygame.display.update()
pygame.time.wait(5000)
os.remove(self.file)
self.running = False
main.App()
else :
self.disp.blit(IMG_LOOSE_ALIVE, (0,0))
pygame.display.update()
pygame.time.wait(7000)
self.walking = False
self.main()
self.schrodinger = enemy.Enemy(self.lab)
self.time = 30000 + self.level*100
unmoving = self.schrodinger.getunmoving()
if unmoving :
pygame.time.wait(1000*unmoving)
if self.time <= 0 :
self.level += 1
self.money += 2000 + self.level*500
self.time = 30000 + self.level*100
self.speed = 40 + self.level*2
self.save()
self.disp.blit(IMG_WIN, (0,0))
pygame.display.update()
pygame.time.wait(3000)
self.walking = False
self.main()
self.updatepanel()
self.schrodinger = enemy.Enemy(self.lab)
else :
self.clock.tick(40) # limit fps to 40
def panel(self) :
"""
Set up the side panel
"""
self.disp.blit(IMG_SIDEPANEL_BG, (16*SQUARE, 16, 450, 496))
self.btn_traps = []
x = 16*SQUARE+65
y = 20
for i in TRAPS :
self.disp.blit(i[0], (x,y))
name = utils.write(i[1], BLACK)
price = utils.write(str(i[3]), GRAY)
lines = utils.formattext(i[2], 35, BLACK, 15)
self.disp.blit(name, (x+40,y+2))
self.disp.blit(price, (x+275, y+2))
i = 20
for l in lines :
self.disp.blit(l, (x+40, y+i))
i += 15
self.btn_traps.append( (x, y, 330, i+5) )
y += 75
self.disp.blit(IMG_LEVEL, RECT_LEVEL)
self.disp.blit(IMG_MONEY, RECT_MONEY)
self.disp.blit(IMG_LAB_QUIT, BTN_LAB_QUIT)
self.disp.blit(IMG_LAB_START, BTN_LAB_START)
self.updatepanel()
def updatepanel(self) :
x = 16*SQUARE
self.disp.blit(IMG_LEVEL, RECT_LEVEL)
self.disp.blit(IMG_MONEY, RECT_MONEY)
level = utils.write("Level : " + str(self.level), WHITE, 13)
money = utils.write("Money : " + str(self.money), WHITE, 13)
self.disp.blit(level, (x+16, 0))
self.disp.blit(money, (x+233, 0))
pygame.display.update()
def selectmode(self) :
def onmousemove(pos) :
if not self.walking :
if utils.isinrect(pos, BTN_LAB_QUIT) :
self.disp.blit(IMG_LAB_QUIT_HOVER, BTN_LAB_QUIT)
elif utils.isinrect(pos, BTN_LAB_START) :
self.disp.blit(IMG_LAB_START_HOVER, BTN_LAB_START)
else :
self.disp.blit(IMG_LAB_QUIT, BTN_LAB_QUIT)
self.disp.blit(IMG_LAB_START, BTN_LAB_START)
i = 0
for t in self.btn_traps :
if utils.isinrect(pos, t) :
if self.money >= TRAPS[i][3] :
pygame.draw.rect(self.disp, BLACK, t, 2)
else :
pygame.draw.rect(self.disp, RED, t, 2)
else :
pygame.draw.rect(self.disp, WHITE, t, 2)
i += 1
pygame.display.update()
def onclick(pos) :
if not self.walking :
if utils.isinrect(pos, BTN_LAB_QUIT) :
# Save the game and back to main menu
self.save()
self.running = False
main.App()
elif utils.isinrect(pos, BTN_LAB_START) :
print("Starting Schrödinger !")
self.walking = True
i = 0
for t in self.btn_traps :
if utils.isinrect(pos, t) and self.money >= TRAPS[i][3] :
self.placemode(i)
i += 1
self.onmousemove = onmousemove
self.onclick = onclick
def save(self) :
with open(self.file, "w") as f :
f.write(str(self.level) + "\n")
f.write(str(self.money) + "\n")
for l in self.lab : f.write(''.join(l))
def main(self) :
"""
Set up elements for the main screen
"""
# Floor :
# self.disp.blit(IMG_FLOOR, (0,0))
pygame.draw.rect(self.disp, LIGHTGRAY, (0, 0, 512, 512))
# Other elements :
y = 0
for l in self.lab :
x = 0
for c in l :
if c == '<' : self.disp.blit(IMG_TABLE_L, (x,y))
elif c == '=' : self.disp.blit(IMG_TABLE_M, (x,y))
elif c == '>' : self.disp.blit(IMG_TABLE_R, (x,y))
elif c == 'c' : self.disp.blit(IMG_BOX, (x,y))
elif c == '4' : self.disp.blit(IMG_FIRE, (x,y))
elif c in ['.', ';', '?', '!', '*'] :
i = x/32
if i <= 0 or l[i-1] in [' ', '4'] :
self.disp.blit(IMG_TABLE_L, (x,y))
elif i >= 15 or l[i+1] in [' ', '4'] :
self.disp.blit(IMG_TABLE_R, (x,y))
else :
self.disp.blit(IMG_TABLE_M, (x,y))
if c == '.' : self.disp.blit(IMG_SPEEDOMETER, (x,y-10))
elif c == ';' : self.disp.blit(IMG_ERROR, (x,y-10))
elif c == '?' : self.disp.blit(IMG_INTERESTING, (x,y-10))
elif c == '!' : self.disp.blit(IMG_FAILED, (x,y-10))
elif c == '*' : self.disp.blit(IMG_STARTFIRE, (x,y-10))
x += 32
y += 32
pygame.display.update()
def placemode(self, i) :
img_trap = TRAPS[i][0]
trapname = TRAPS[i][1]
trapmoney = TRAPS[i][3]
trapchar = TRAPS[i][4]
pygame.draw.rect(self.disp, GREEN, self.btn_traps[i], 2)
pygame.display.update()
def onmousemove(pos) :
if utils.isinrect(pos, (0, 0, 16*SQUARE, 16*SQUARE)) :
self.main()
x, y = int(pos[0]/SQUARE), int(pos[1]/SQUARE)
if self.lab[y][x] in ["<", "=", ">"] :
self.disp.blit(img_trap, (x*SQUARE,y*SQUARE-10))
pygame.display.update()
def onclick(pos) :
x, y = int(pos[0]/SQUARE), int(pos[1]/SQUARE)
try :
if self.lab[y][x] in ["<", "=", ">"] :
self.money -= trapmoney
self.lab[y][x] = trapchar
except IndexError :
pass
self.updatepanel() # update money
self.selectmode()
self.onmousemove = onmousemove
self.onclick = onclick