-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPydust.py
398 lines (277 loc) · 11.6 KB
/
Pydust.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
import pygame
from pygame.locals import *
import time
import random
import os
import operator
from Ressources.Classes.Elements import *
from tkinter import *
pygame.init()
class Cell :
"""Classe de type cellule avec les fonctions usuelles
NECESSITE PYGAME
"""
def __init__(self, x, y) :
(largeur, hauteur) = (5,5)################################################
self.pos = (x,y)
self.stat = False #Statut de la cellule (Vivante ou morte)
self.zone = pygame.Rect((x,y),(largeur,hauteur)) #Permet de connaitre la zone de la cellule (position et taille)
self.surf = pygame.Surface(self.zone.size) # Objet affichable de la cellule
self.aff = self.surf , self.zone
self.check = False # Marqueur de 'check' de la cellule (permet de savoir si l'algorithme à déjà calculé l'état de la cellu
self.element = None
def fill(self,i,j) :
"""Actualise la couleur de la cellule"""
NOIR = (0,0,0)
if not self.stat :
self.surf.fill(NOIR)
else :
self.surf.fill(self.element.color)
def chkup(self) :
'''Change le statut 'check' de la cellule'''
if self.check :
self.check = False
else :
self.check = True
class Grille :
'''Classe du damier, permet le calcul de la génération suivante'''
def __init__(self,grille) :
self.gr = grille
self.alv = []
self.chktmp = []
self.past_alv = []
def afill(self) :
for k in self.alv :
self.gr[k.posi][k.posj].fill(k.posi,k.posj)
def next(self) :
'''Passe à la génération suivante'''
global tour, Num_gen
self.past_alv = list(self.alv)
for k in self.past_alv :
i,j = k.posi,k.posj
if (not self.gr[i][j].check) and (k.group != 'Solid') :
k.dep()
for k in self.past_alv : # on uncheck toutes les cellules
i,j = k.posi,k.posj
self.gr[i][j].check = False
if i == 0 or i == (len(self.gr)-1) or j == 0 or j == (len(self.gr[0])-1) :
k.kill()
for k in self.alv : # on uncheck toutes les cellules
i,j = k.posi,k.posj
self.gr[i][j].check = False
if i == 0 or i == (len(self.gr)-1) or j == 0 or j == (len(self.gr[0])-1) :
k.kill()
Num_gen=1
def alea(self):
i=1
def save(self):
i=1
def charger (self):
i=1
def Affichage_plateau() :
global Plateau
global fenetre
Plateau.afill()
for k in Plateau.alv :
i,j = k.posi,k.posj
fenetre.blit(Plateau.gr[i][j].aff[0],Plateau.gr[i][j].aff[1])
#Cara damier
pos_x, pos_y =55,205
scale = 5
nb_i = 78
nb_j = 240
#Damier
tabl = []
for i in range(nb_i) :
soutab = []
for j in range(nb_j) :#######
soutab.append(Cell(scale*j + pos_x, scale*i + pos_y))##################################
tabl.append(soutab)
Plateau = Grille(tabl)
for i in range(len(Plateau.gr)) :
for j in range(len(Plateau.gr[0])) :
if i == 1 or i == len(Plateau.gr)-2 or j == 1 or j == len(Plateau.gr[0])-2 :
Plateau.gr[i][j].element = Marble(i,j,Plateau)
#-----------------------------------Affichage-assignation des variables-----------------------------------------
fenetre = pygame.display.set_mode((1340,675))
RED = (255,0,0) #Définitions de couleurs usuelles
GREEN = (0,255,0)
GRIS = (150,150,150)
NOIR = (0,0,0)
PALE = (50,50,50)
autoplay = 0
Affichage = True #Variable si on affiche ou non le plateau
appuyeG = 0
appuyeD = 0
tour=0 #Variable pour tuer les cellules dans le mode non tore
Num_gen=1 #Variable désignant le numéro de la génération
Num_ite=1 #Nombre d'iteration manuelle
font=pygame.font.Font(None, 24) #On définit la police et la taille d'écriture
pygame.display.flip()
c_element = Marble
ind_elem = 0
entite = 0
mouse_pos = (0,0)
pushA = 0
slowmo = False
radius = 1
clock = pygame.time.Clock()
#-----------------------------------------Définition des boutons----------------------------------------------------
#BOUTON CHANGER ELEM
Changeelem = font.render("Changer d'élement",1,(255,255,255)) #J'arrive à court de synonymes ...
zone_clic_E = Changeelem.get_rect(topleft=(53, 180))
#ENTITE
message_entite = font.render("Nombre entités : ", 1,(255,255,255))
#---------------------------------------------------------------------------------------------------------------------
#FENETRE EVOLUTION DU JEU
zone_ext = pygame.Rect((50,200),((nb_j)*scale+10,(nb_i)*scale+10)) #Zone du rect ext
zone_int = pygame.Rect((55,205),((nb_j)*scale,(nb_i)*scale))
rect_ext = pygame.Surface(zone_ext.size) #Definition du rect ext
rect_int = pygame.Surface(zone_int.size)
rect_ext.fill(GRIS) # couleur du rect ext
rect_int.fill(PALE)
#############################
def Cursorpos(p) :
(y,x) = p
return (int((x-pos_y)/scale),int((y-pos_x)/scale))
#Boucle programme
continuer = True
while continuer:
entite = font.render(str(len(Plateau.alv)), 1, (255,255,255))
Matiere = font.render(c_element(3.1415,3.1415,None).name, 1, (255,255,255))
if appuyeG :
(i,j) = Cursorpos(mouse_pos)
if zone_int.collidepoint(mouse_pos) :
for ri in range(radius) :
for rj in range(radius) :
if i+ri > len(Plateau.gr) or i+ri < 0 :
Is = 0
else :
Is = ri
if j+rj > len(Plateau.gr[1]) or j+rj < 0 :
Js =0
else :
Js = rj
if not Plateau.gr[i+Is][j+Js].stat:
Plateau.gr[i+Is][j+Js].element = c_element(i+Is,j+Js,Plateau)
if appuyeD :
(i,j) = Cursorpos(mouse_pos)
if zone_int.collidepoint(mouse_pos) :
for ri in range(radius) :
for rj in range(radius) :
if i+ri > len(Plateau.gr) or i+ri < 0 :
Is = 0
else :
Is = ri
if j+rj > len(Plateau.gr[1]) or j+rj < 0 :
Js =0
else :
Js = rj
if Plateau.gr[i+Is][j+Js].stat:
Plateau.gr[i+Is][j+Js].element.kill()
if slowmo :
if s < 1 :
s += 0.005
time.sleep(s)
for event in pygame.event.get():
if event.type == pygame.QUIT: #Fermer le programme
continuer = False
if event.type == MOUSEMOTION :
mouse_pos = event.pos
if appuyeG :
(i,j) = Cursorpos(mouse_pos)
if zone_int.collidepoint(mouse_pos) :
for ri in range(radius) :
for rj in range(radius) :
if i+ri > len(Plateau.gr) or i+ri < 0 :
Is = 0
else :
Is = ri
if j+rj > len(Plateau.gr[1]) or j+rj < 0 :
Js =0
else :
Js = rj
if not Plateau.gr[i+Is][j+Js].stat:
Plateau.gr[i+Is][j+Js].element = c_element(i+Is,j+Js,Plateau)
if appuyeD :
(i,j) = Cursorpos(mouse_pos)
if zone_int.collidepoint(mouse_pos) :
for ri in range(radius) :
for rj in range(radius) :
if i+ri > len(Plateau.gr) or i+ri < 0 :
Is = 0
else :
Is = ri
if j+rj > len(Plateau.gr[1]) or j+rj < 0 :
Js =0
else :
Js = rj
if Plateau.gr[i+Is][j+Js].stat:
Plateau.gr[i+Is][j+Js].element.kill()
if event.type == MOUSEBUTTONDOWN :
if event.button == 3 :
appuyeD = True
if event.button == 1 :
appuyeG = True
if event.button == 2 :
(i,j) = Cursorpos(mouse_pos)
c = Plateau.gr[i][j]
print("<----------Debug-----------> \nCell : ",(i,j))
print("Stat : ",c.stat)
print("Elem : ",c.element)
print("Check : ",c.check)
print("In alv : ",c.element in Plateau.alv)
if c.element != None and c.element.group != "Solid" :
print( "Near : ", c.element.near)
if event.type == MOUSEBUTTONUP : # relachement bouton
if event.button == 3 :
appuyeD = False
if event.button == 1 : #clic gauche
appuyeG = False
if zone_clic_E.collidepoint(event.pos) :
menu = Tk()
var_choix = StringVar()
liste = Listbox(menu)
liste.pack()
for e in Elemlist :
liste.insert(END,e(3.1415,3.1415,Plateau).name)
def clic(evt):
global ind_elem
ind_elem = liste.curselection()[0]
menu.destroy()
liste.bind('<ButtonRelease-1>',clic)
menu.mainloop()
c_element = Elemlist[ind_elem]
if event.type == KEYUP :
if event.key == K_SPACE :
if autoplay :
autoplay = False
else :
autoplay = True
if event.key == K_s :
slowmo = False
if event.key == K_r :
radius = 1
if event.type == KEYDOWN :
if event.key == K_s :
s = 0
slowmo = True
if event.key == K_r :
radius = 3
pygame.display.flip()
if autoplay :
Plateau.next()
time.sleep(0.002)
fenetre.fill((100,100,100)) # on efface l'écran
fenetre.blit(Changeelem, zone_clic_E)
fenetre.blit(Matiere, (900,183))
fenetre.blit(message_entite, (1100,183))
fenetre.blit(entite, (1240,183))
fenetre.blit(rect_ext, zone_ext)# on print le rect ext à la zone du rect ext
fenetre.blit(rect_int, zone_int)
fps = font.render(str(int(clock.get_fps())), True, pygame.Color('white'))
fenetre.blit(fps, (50, 50))
Affichage_plateau()
pygame.display.flip()
clock.tick(30)
pygame.quit()