-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
438 lines (381 loc) · 18.1 KB
/
app.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#-*-coding:Utf-8 -*
# TheSecretTower
# Copyright (C) 2011 Pierre SURPLY
#
# This file is part of TheSecretTower.
#
# TheSecretTower is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TheSecretTower is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TheSecretTower. If not, see <http://www.gnu.org/licenses/>.
# Auteur : Pierre Surply
import os
import random
import math
import copy
import threading
# Pygame
import pygame
from pygame.locals import *
import element
import char
from jeu import *
import menu
from map import *
from editeur import *
import client
# Constante
import const
class App:
""" Classe définissant l'application The Secret Tower"""
def __init__(self):
"""Initialisation de l'application"""
# Initialisation de pygame
pygame.init()
# création de la fenetre
self.size = (800, 600)
self.fenetre = pygame.display.set_mode(self.size, pygame.DOUBLEBUF)
pygame.display.set_icon(pygame.image.load("img/perso_small.png").convert_alpha())
pygame.display.set_caption(const.fenetre_titre)
pygame.mouse.set_visible(False)
# font
self.font = pygame.font.Font("img/font.ttf", 20)
self.font_petit = pygame.font.Font("img/font.ttf", 15)
const.sprite_bloc = pygame.image.load("img/bloc.png").convert_alpha()
const.sprite_lave = pygame.image.load("img/lave.png").convert_alpha()
const.sprite_torch = pygame.image.load("img/torch.png").convert_alpha()
const.sprite_perso = pygame.image.load("img/perso.png").convert_alpha()
const.sprite_arm = pygame.image.load("img/arm_perso.png").convert_alpha()
const.sprite_item = pygame.image.load("img/item.png").convert_alpha()
const.sprite_mobs = pygame.image.load("img/mobs.png").convert_alpha()
const.sprite_degats = pygame.image.load("img/degats.png").convert_alpha()
const.sprite_fire = pygame.image.load("img/fire.png").convert_alpha()
const.vide = pygame.image.load("img/vide.png").convert_alpha()
const.click = pygame.mixer.Sound("sound/click.wav")
const.step = (pygame.mixer.Sound("sound/step1.wav"), pygame.mixer.Sound("sound/step2.wav"))
const.door = pygame.mixer.Sound("sound/door.wav")
const.wood = (pygame.mixer.Sound("sound/wood1.wav"), pygame.mixer.Sound("sound/wood2.wav"))
const.stone = (pygame.mixer.Sound("sound/stone1.wav"), pygame.mixer.Sound("sound/stone2.wav"), pygame.mixer.Sound("sound/stone3.wav"))
const.dirt = pygame.mixer.Sound("sound/dirt.wav")
const.hurt = (pygame.mixer.Sound("sound/hurt1.wav"), pygame.mixer.Sound("sound/hurt2.wav"))
const.sword = (pygame.mixer.Sound("sound/sword1.wav"), pygame.mixer.Sound("sound/sword2.wav"))
const.zombie = (pygame.mixer.Sound("sound/zombie1.wav"), pygame.mixer.Sound("sound/zombie2.wav"), pygame.mixer.Sound("sound/zombie3.wav"))
const.goblin = (pygame.mixer.Sound("sound/goblin1.wav"), pygame.mixer.Sound("sound/goblin2.wav"))
const.keeper = (pygame.mixer.Sound("sound/keeper1.wav"), pygame.mixer.Sound("sound/keeper2.wav"), pygame.mixer.Sound("sound/keeper3.wav"))
const.fire = pygame.mixer.Sound("sound/fire.wav")
const.fireworks = (pygame.mixer.Sound("sound/fireworks1.wav"),pygame.mixer.Sound("sound/fireworks2.wav"))
pygame.mixer.fadeout(300)
self.perso = Perso()
self.partie = []
self.coef = 2
self.pos_screen = (0,0)
const.chatbox = char.Chatbox()
def main(self):
cmd = 1
while cmd:
cmd = menu(self, "Main Menu", ["Play", "Edit Level", "Quit"])
if cmd == 2:
self.perso.reset()
self.perso.set_org_color()
self.partie = ["Gen", 0]
self.perso.map = 0
cmd = menu(self, "Edit Level", ["New level", "Load level"])
if cmd == 1:
cmd = editeur(self, [], "Unnamed")
elif cmd == 2:
nom = ask(self, "Edit Level] Level's name :")
cmd = editeur(self, open_map("map/custom/"+nom), nom)
elif cmd == 1:
if not os.path.isdir("data/perso/"):
os.mkdir("data/perso/")
characters = os.listdir("data/perso")[:5]
characters = ["[ "+i+" ]" for i in characters]
while len(characters) < 5:
characters.append("--Empty--")
characters.append("Cancel")
cmd = menu(self, "Choose a Character", characters)
if cmd > len(os.listdir("data/perso/")) and cmd != 6:
self.perso.set_org_color()
self.perso.nom = ask(self, "Enter a name for the Character")
while cmd != 6:
cmd = menu(self, "Choose "+self.perso.nom+"'s colors", ["Shirt", "Pants", "Hair", "Skin", "Other", "Done"], self.perso)
if cmd != 6:
menu_color(self, cmd-1, self.perso)
self.perso.save()
elif cmd < 6:
self.perso.load(os.listdir("data/perso")[cmd-1])
cmd = menu(self, "Play", ["Single Player", "Multi Player", "Delete Character", "Edit Character", "Cancel"], self.perso)
if cmd == 1:
cmd = menu(self, "Single Player", ["Entering the Tower !", "Play Custom Level", "Cancel"], self.perso)
if cmd == 1:
if not os.path.isdir("data/save"):
os.mkdir("data/save/")
saves = os.listdir("data/save")[:5]
saves = ["[ "+i+" ]" for i in saves]
while len(saves) < 5:
saves.append("--Empty--")
saves.append("Cancel")
cmd = menu(self, "Choose a Game", saves)
if cmd < 6:
self.perso.reset()
if cmd > len(os.listdir("data/save/")) and cmd != 6:
self.partie = []
self.partie = self.nouvelle_partie(ask(self, "New Game] Game's name : "))
else:
self.partie = self.charger_partie(os.listdir("data/save")[cmd-1])
self.perso.map = self.partie[1]
self.perso.id_porte = self.partie[2]
cmd = 1
while open_map("save/"+self.partie[0]+"/map"+str(self.partie[1])) != [] and cmd == 1:
cmd = jeu(self, open_map("save/"+self.partie[0]+"/map"+str(self.partie[1])), self.perso)
self.partie[1] = self.perso.map
if self.partie[1] < 0:
if open_map("save/"+self.partie[0]+"/map"+str(self.partie[1])) == []:
save_map("save/"+self.partie[0]+"/map"+str(self.partie[1]), self.gen_map(self.partie[1]))
elif cmd == 2:
self.perso.reset()
self.partie = ["Gen", 0]
self.perso.map = 0
cmd = jeu(self, open_map("map/custom/" + ask(self, "Load Level] Level's name :")), self.perso)
elif cmd == 2:
cmd = self.multiplayer()
elif cmd == 3:
cmd = menu(self, "Are you sure ?", ["Yes, delete "+self.perso.nom+" definitely !", "No, I want to keep "+self.perso.nom], self.perso)
if cmd == 1:
os.remove("data/perso/"+self.perso.nom)
elif cmd == 4:
old_name = self.perso.nom
while cmd != 3:
cmd = menu(self, "Edit Character", ["Rename", "Change colors", "Done"], self.perso)
if cmd == 1:
self.perso.nom = ask(self, "Enter a name")
elif cmd == 2:
while cmd != 6:
cmd = menu(self, "Choose "+self.perso.nom+"'s colors", ["Shirt", "Pants", "Hair", "Skin", "Other", "Done"], self.perso)
if cmd != 6:
menu_color(self, cmd-1, self.perso)
os.remove("data/perso/"+old_name)
self.perso.save()
const.runned = False
pygame.quit()
def multiplayer(self):
self.perso.reset()
self.partie = ["Multi", 0]
self.perso.map = 0
const.runned = True
const.input = []
const.output == ""
const.host = ask(self, "Enter the IP of the server :").lower()
thread=threading.Thread(target=client.connect)
const.input.append("co_perso;"+self.perso.get_char(";"))
const.input_udp = "set_adr_udp;"+self.perso.nom
thread.start()
tps_connect = 0
cmd = 1
while const.output == "":
tps_connect +=1
if const.output != "Connected":
cmd = menu(self, const.output, ["Ok"])
const.output = ""
else:
const.output = ""
const.input.append("get_inv")
const.input.append("get_mobs")
while const.output == "":
tps_connect +=1
self.perso.inv.empty()
self.perso.inv.load(const.output)
const.output = ""
const.input.append("get_welcome")
while not cmd in [5,0]:
const.input.append("set_map;"+str(self.partie[1]))
const.input.append("get_map;"+str(self.partie[1]))
const.input.append("get_last_event_map;"+str(self.partie[1]))
const.input.append("get_last_event")
while const.map == []:
tps_connect +=1
cmd = jeu(self, const.map, self.perso)
const.map = []
self.partie[1] = self.perso.map
const.runned = False
const.mobs = True
return cmd
def blit(self, element, coef=1):
"""Ajoute Element à l'écran"""
if isinstance(element, Perso):
if not element.sens:
self.fenetre.blit(pygame.transform.scale(element.image, (int(element.image.get_width()*coef),int(element.image.get_height()*coef))), (element.x-(element.image.get_width()-50), element.y))
else:
self.fenetre.blit(pygame.transform.scale(element.image, (int(element.image.get_width()*coef),int(element.image.get_height()*coef))), (element.x,element.y))
elif isinstance(element, Element) and coef == 1:
self.fenetre.blit(element.image, (element.x,element.y))
elif isinstance(element, Element):
self.fenetre.blit(pygame.transform.scale(element.image, (int(element.image.get_width()*coef),int(element.image.get_height()*coef))), (element.x,element.y))
def scale(self, coef):
x = self.perso.x*coef-((800-50*coef)/2)
y = self.perso.y*coef-((600-50*coef)/2)
if x < 0:
x=0
if x+800 >800*coef:
x = 800*coef-800
if y < 0:
y = 0
if y+600 >600*coef:
y = 600*coef-600
# Mouvement camera
x_dep = 0
y_dep = 0
pas = 30
if self.pos_screen[0]+pas < x:
x_dep = self.pos_screen[0]+pas
elif self.pos_screen[0]-pas > x:
x_dep = self.pos_screen[0]-pas
else:
x_dep = x
if self.pos_screen[1]+pas < y:
y_dep = self.pos_screen[1]+pas
elif self.pos_screen[1]-pas > y:
y_dep = self.pos_screen[1]-pas
else:
y_dep = y
self.pos_screen = (x_dep,y_dep)
self.fenetre.blit(pygame.transform.scale(self.fenetre, (800*coef, 600*coef)), (0,0), (self.pos_screen[0],self.pos_screen[1], self.size[0],self.size[1]))
def flip(self):
"""Rafraichissement"""
if self.size[0] != 800 or self.size[1] != 600:
new_size = (self.size[0]*(float(self.size[0])/800.0),self.size[1]*(float(self.size[1])/600.0))
print new_size, self.size
self.fenetre.blit(pygame.transform.scale(self.fenetre, new_size), (0,0))
pygame.display.flip()
def save_screen(self):
return copy.copy(self.fenetre)
def nouvelle_partie(self, nom):
partie = [nom, 0, 0]
self.perso.inv.empty()
self.perso.inv.add(Item(1,1))
# Copie map std
i = 0
while True:
if not os.path.isdir("data/save/"):
os.mkdir("data/save/")
if not os.path.isdir("data/save/"+nom+"/"):
os.mkdir("data/save/"+nom+"/")
break
elif menu(self, "Warning : Erase existing game ?", ["Yes", "No"]) == 1:
for entry in os.listdir("data/save/"+nom+"/"):
os.remove("data/save/"+nom+"/"+entry)
os.rmdir("data/save/"+nom+"/")
os.mkdir("data/save/"+nom+"/")
break
else:
nom = ask(self, "New Game] Game's name : ")
while open_map("map/std/map"+str(i)) != []:
save_map("save/"+nom+"/map"+str(i),open_map("map/std/map"+str(i)))
i = i+1
# Génération sous-sol
save_map("save/"+nom+"/map-1", self.gen_map(-1))
# Fichier global
file = open("data/save/"+nom+"/"+nom, "w")
file.write("map=0\nid=0\n")
file.close()
cine(self, 1)
return partie
def charger_partie(self, nom):
partie = []
partie.append(nom)
try:
file = open("data/save/"+nom+"/"+nom)
tampon = file.read()
elements = tampon.split("\n")
i = 1
for element in elements:
if element != "":
prop = element.split("=")
if prop[0] == "map" and i == 1:
partie.append(int(prop[1]))
i = i+1
elif prop[0] == "id" and i == 2:
partie.append(int(prop[1]))
self.perso.id_porte = int(prop[1])
i = i +1
elif prop[0] == "inv":
self.perso.inv.empty()
self.perso.inv.load(prop[1])
file.close()
except IOError:
print (path + " : Partie introuvable !")
return partie
def save_partie(self):
file = open("data/save/"+self.partie[0]+"/"+self.partie[0], "w")
tampon = "map="+str(self.partie[1])+"\n"
tampon = tampon +"id="+str(self.perso.id_porte)+"\n"
tampon += "inv="+self.perso.inv.save()+"\n"
file.write(tampon)
file.close()
def set_size(self,new_size):
self.size = new_size
self.fenetre = pygame.display.set_mode(self.size, pygame.DOUBLEBUF | pygame.RESIZABLE)
def gen_map(self, level):
map = []
bloc = Porte(0, 1, int(math.fabs(level-1)), 0)
xent= random.randint(0, 15)
yent=1
bloc.move_el(xent*50,yent*50)
map.append(bloc)
bloc = Porte(0, 0,int(math.fabs(level-2)), 0)
xsor= random.randint(0, 15)
ysor=10
bloc.move_el(xsor*50,ysor*50)
map.append(bloc)
xd = random.randint(0, 16)
yd = random.randint(0, 12)
size = random.randint(2,5)
for x in range(16):
bloc = Bloc(5)
bloc.move_el(x*50, 0)
map.append(bloc)
bloc = Bloc(5)
bloc.move_el(x*50, 550)
map.append(bloc)
for x in range(16):
for y in range(10):
if (math.fabs(xent-x) > 2 or math.fabs(y+2-yent) > 1) and (math.fabs(xsor-x) > 2 or math.fabs(y-ysor) > 1):
if int(10+(level/10)) < 4:
rand_max = 4
else:
rand_max = int(10+(level/10))
if (random.randint(0, rand_max)) < 2:
rand = random.randint(0,200)
if rand < 20:
bloc = Coal(14)
elif rand < 40 and level >= -10:
bloc =Tin(20)
elif rand < 60 and level >= -10:
bloc = Copper(15)
elif rand < 70 and level < -10:
bloc = Iron(16)
elif rand < 75 and level < -20:
bloc = Gold(18)
elif rand < 80 and level < -30:
bloc = Diamond(19)
elif rand < 90 and level < -20:
bloc = Titanium(17)
elif rand < 95 and level < -30:
bloc = Uranium(21)
else:
bloc = Stone(1)
bloc.move_el(x*50, y*50+50)
map.append(bloc)
elif math.fabs(xd-x)+math.fabs(yd-y) > size:
bloc = Terre(7)
bloc.move_el(x*50, y*50+50)
map.append(bloc)
return map