-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAplicacion.py
464 lines (440 loc) · 16.6 KB
/
Aplicacion.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import pygame
import numpy as np
import time
from Widgets.Button import *
from Widgets.Text import *
from Widgets.ButtonToggle import *
#Iniciacion de Pygame
pygame.init()
#Screen de la Pantalla
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
running = True
#Name Window
pygame.display.set_caption('Game of Life')
#Font
font = pygame.font.Font('Fonts/Sixtyfour.TTF', 40)
font_other = pygame.font.Font('Fonts/Sixtyfour.TTF', 20)
font_widgets = pygame.font.Font('Fonts/Sixtyfour.TTF', 15)
font_textplano = pygame.font.Font('Fonts/8-BIT WONDER.TTF', 18)
font_copy = pygame.font.Font('Fonts/Pixelinter.ttf', 14)
#Widgets
#Widgets de la Pantalla Principal
Button_Option = Button('Opciones', 250, 50, (260, 400), font_widgets, 6)
TitleCenter = Text('Juego de la Vida', font, '#000000', 75, 150)
SubTitle = Text('de Conway', font_other, '#000000', 90, 190)
StartText = Text('Presione SPACE para comenzar', font_other, '#000000', 110, 340)
CopyText = Text('Copyrights (C) 2024, Astrofamily. All Rights Reserveds', font_copy, '#000000', 415, 550)
CopyText_By = Text('By AstroLui and Moi', font_copy, '#000000', 650, 570)
#Widgets de las Opciones
Button_Back = Button('Atras', 100, 50, (20, 20), font_widgets, 6,)
Text_ColorCell = Text('Cambiar Color de la Celula', font_textplano, '#000000', 30, 100)
Text_Figura = Text('Cambiar Figura', font_textplano, '#000000', 30, 250)
Button_Classic = ButtonToggle('Clásico', 125, 50, (50, 150), font_widgets, 6, False)
Button_Red = ButtonToggle('Rojo', 125, 50, (190, 150), font_widgets, 6, False)
Button_Azul = ButtonToggle('Azul', 125, 50, (330, 150), font_widgets, 6, False)
Button_Verde = ButtonToggle('Verde', 125, 50, (470, 150), font_widgets, 6, False)
Button_FigClassic = ButtonToggle('Básico', 125, 50, (50, 300), font_widgets, 6, False)
Button_FigParpadeo = ButtonToggle('Parpadeo', 140, 50, (185, 300), font_widgets, 6, False)
Button_Daga = ButtonToggle('Daga', 125, 50, (335, 300), font_widgets, 6, False)
Button_Reina = ButtonToggle('Reina', 125, 50, (470, 300), font_widgets, 6, False)
Button_Pollo = ButtonToggle('Pollo', 125, 50, (610, 300), font_widgets, 6, False)
#Widgets de Inicio del Juego
Button_Exit = Button('Atras', 100, 50, (20, 520), font_widgets, 6,)
Button_Paused = Button('Pausar', 125, 50, (315, 520), font_widgets, 6,)
def texto():
if game_paused:
return Button('Continuar', 150, 50, (315, 520), font_widgets, 6,)
else:
return Button('Pausar', 125, 50, (315, 520), font_widgets, 6,)
def Colors():
if game_classic:
return '#000000'
elif game_red:
return "#f20c0c"
elif game_azul:
return '#5754A8'
elif game_verde:
return '#A5CC1B'
else:
return '#000000'
def Figuras():
if figura_base:
gameState[21, 21] = 1
gameState[22, 22] = 1
gameState[22, 23] = 1
gameState[21, 23] = 1
gameState[20, 23] = 1
if figura_parpadeo:
gameState[35, 21] = 1
gameState[35, 22] = 1
gameState[35, 23] = 1
gameState[35, 24] = 1
gameState[35, 25] = 1
if figura_daga:
gameState[34, 22] = 1
gameState[33, 23] = 1
gameState[34, 24] = 1
gameState[35, 23] = 1
gameState[36, 24] = 1
gameState[36, 25] = 1
gameState[37, 27] = 1
gameState[38, 26] = 1
gameState[39, 26] = 1
gameState[39, 27] = 1
gameState[36, 28] = 1
gameState[35, 28] = 1
gameState[34, 28] = 1
gameState[33, 28] = 1
gameState[32, 28] = 1
gameState[31, 27] = 1
gameState[32, 26] = 1
gameState[32, 25] = 1
gameState[32, 24] = 1
gameState[34, 26] = 1
gameState[29, 26] = 1
gameState[29, 27] = 1
gameState[34, 30] = 1
gameState[35, 31] = 1
gameState[33, 31] = 1
gameState[30, 26] = 1
gameState[34, 32] = 1
gameState[36, 26] = 1
if figura_reina:
gameState[21, 18] = 1
gameState[22, 18] = 1
gameState[21, 19] = 1
gameState[22, 19] = 1
gameState[21, 22] = 1
gameState[22, 22] = 1
gameState[21, 23] = 1
gameState[22, 23] = 1
gameState[24, 16] = 1
gameState[24, 17] = 1
gameState[24, 18] = 1
gameState[24, 19] = 1
gameState[24, 20] = 1
gameState[24, 21] = 1
gameState[24, 22] = 1
gameState[24, 23] = 1
gameState[24, 24] = 1
gameState[24, 25] = 1
gameState[25, 26] = 1
gameState[26, 26] = 1
gameState[26, 25] = 1
gameState[25, 15] = 1
gameState[26, 16] = 1
gameState[26, 15] = 1
gameState[26, 16] = 1
gameState[26, 19] = 1
gameState[26, 20] = 1
gameState[26, 21] = 1
gameState[26, 22] = 1
gameState[28, 20] = 1
gameState[28, 21] = 1
gameState[29, 22] = 1
gameState[30, 22] = 1
gameState[31, 21] = 1
gameState[31, 20] = 1
gameState[30, 19] = 1
gameState[29, 19] = 1
gameState[33, 19] = 1
gameState[33, 20] = 1
gameState[33, 21] = 1
gameState[33, 22] = 1
gameState[33, 25] = 1
gameState[33, 26] = 1
gameState[34, 26] = 1
gameState[35, 25] = 1
gameState[35, 24] = 1
gameState[35, 23] = 1
gameState[35, 22] = 1
gameState[35, 21] = 1
gameState[35, 20] = 1
gameState[35, 19] = 1
gameState[35, 18] = 1
gameState[35, 17] = 1
gameState[35, 16] = 1
gameState[34, 15] = 1
gameState[33, 15] = 1
gameState[33, 16] = 1
gameState[37, 18] = 1
gameState[38, 18] = 1
gameState[38, 19] = 1
gameState[37, 19] = 1
gameState[37, 22] = 1
gameState[38, 22] = 1
gameState[38, 23] = 1
gameState[37, 23] = 1
if figura_pollo:
gameState[28, 25] = 1
gameState[28, 26] = 1
gameState[29, 26] = 1
gameState[29, 27] = 1
gameState[29, 28] = 1
gameState[30, 29] = 1
gameState[31, 29] = 1
gameState[32, 29] = 1
gameState[33, 30] = 1
gameState[32, 31] = 1
gameState[32, 32] = 1
gameState[34, 29] = 1
gameState[34, 28] = 1
gameState[34, 27] = 1
gameState[34, 26] = 1
gameState[33, 26] = 1
gameState[31, 27] = 1
gameState[31, 26] = 1
gameState[31, 24] = 1
gameState[31, 25] = 1
gameState[30, 24] = 1
gameState[36, 28] = 1
gameState[37, 28] = 1
gameState[37, 29] = 1
gameState[36, 29] = 1
gameState[33, 32] = 1
#Game Variables
game_option = False
game_start = False
game_paused = False
#Variables de Colores
game_classic = True
Button_Classic.Toggle = True
game_red = False
game_azul = False
game_verde = False
#Variables de las formas
figura_base = True
Button_FigClassic.Toggle = True
figura_parpadeo = False
figura_daga = False
figura_reina = False
figura_pollo = False
ncX, ncY = 70, 70
dimCW = SCREEN_WIDTH / ncX
dimCH = SCREEN_HEIGHT / ncY
#Game Loop
while running:
# Color de Background
screen.fill('#DCDDD8')
# Bucle que escucha el evento de QUIT
# de la ventana y del SPACE para iniciar el juego
# Main Menu
if game_option == False and game_start == False:
pygame.display.set_caption('Game of Life | Menu')
# Estados de la Celulas. Vivos = 1, Muertos = 0
gameState = np.zeros((ncX, ncY))
# Iniciamos algunas Celulas para probar su comportamiento
Figuras()
# Copiamos gameState, esta lista es donde haremos
# los cambios
newGameState = np.copy(gameState)
# Dibujamos los Widgets en pantalla
TitleCenter.Draw(screen)
SubTitle.Draw(screen)
StartText.Draw(screen)
Button_Option.draw(screen)
CopyText.Draw(screen)
CopyText_By.Draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
game_start = True
if event.type == pygame.MOUSEBUTTONDOWN:
if Button_Option.Click():
game_option = True
pass
# Menu Opciones
if game_option == True:
pygame.display.set_caption('Game of Life | Opciones')
# Dibujamos el Boton de Atras en
# el menu de opciones
Text_ColorCell.Draw(screen)
Text_Figura.Draw(screen)
Button_Classic.draw(screen)
Button_Back.draw(screen)
Button_Red.draw(screen)
Button_Azul.draw(screen)
Button_Verde.draw(screen)
Button_FigClassic.draw(screen)
Button_FigParpadeo.draw(screen)
Button_Daga.draw(screen)
Button_Reina.draw(screen)
Button_Pollo.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if Button_Back.Click():
game_option = False
if Button_Classic.Click():
game_classic = True
game_red = False
game_azul = False
game_verde = False
Button_Red.Toggle = False
Button_Azul.Toggle = False
Button_Verde.Toggle = False
if Button_Red.Click():
game_red = True
game_classic = False
game_azul = False
game_verde= False
Button_Classic.Toggle = False
Button_Azul.Toggle = False
Button_Verde.Toggle = False
if Button_Azul.Click():
game_red = False
game_classic = False
game_azul = True
game_verde = False
Button_Classic.Toggle = False
Button_Verde.Toggle = False
Button_Red.Toggle = False
if Button_Verde.Click():
game_red = False
game_classic = False
game_azul = False
game_verde = True
Button_Classic.Toggle = False
Button_Azul.Toggle = False
Button_Red.Toggle = False
if Button_FigClassic.Click():
figura_base = True
figura_parpadeo = False
figura_daga = False
figura_reina = False
figura_bellota = False
Button_FigParpadeo.Toggle = False
Button_Daga.Toggle = False
Button_Reina.Toggle = False
Button_Pollo.Toggle = False
if Button_FigParpadeo.Click():
figura_base = False
figura_parpadeo = True
figura_daga = False
figura_reina = False
figura_pollo = False
Button_FigClassic.Toggle = False
Button_Daga.Toggle = False
Button_Reina.Toggle = False
Button_Pollo.Toggle = False
if Button_Daga.Click():
figura_base = False
figura_parpadeo = False
figura_daga = True
figura_reina = False
figura_pollo = False
Button_FigParpadeo.Toggle = False
Button_FigClassic.Toggle = False
Button_Reina.Toggle = False
Button_Pollo.Toggle = False
if Button_Reina.Click():
figura_base = False
figura_parpadeo = False
figura_daga = False
figura_reina = True
figura_pollo = False
Button_FigParpadeo.Toggle = False
Button_Daga.Toggle = False
Button_FigClassic.Toggle = False
Button_Pollo.Toggle = False
if Button_Pollo.Click():
figura_base = False
figura_parpadeo = False
figura_daga = False
figura_reina = False
figura_pollo = True
Button_FigParpadeo.Toggle = False
Button_Daga.Toggle = False
Button_Reina.Toggle = False
Button_FigClassic.Toggle = False
# Codicion para regresar al Main Menu
pass
# Juego
if game_start == True:
pygame.display.set_caption('Game of Life | Juego')
# Dibujamos los botones para Pausar
# y salir
Button_Paused.draw(screen)
Button_Exit.draw(screen)
# Condicion para salir al Main Menu
# Bucle que escucha los eventos de click del
# mouse en la Pantalla
for event in pygame.event.get():
# Tupla para saber si presiona la pantalla
mouseEvent = pygame.mouse.get_pressed()
if sum(mouseEvent) > 0:
# Posicion de donde se dio click en la
# pantalla
posX, posY = pygame.mouse.get_pos()
# Determinamos la celdas
celX, celY = int(np.floor(posX / dimCW)), int(np.floor(posY / dimCH))
# Cambiamos el estado de la cedula
try:
newGameState[celX, celY] = 1
except(IndexError):
pass
if event.type == pygame.MOUSEBUTTONDOWN:
if Button_Exit.Click():
game_start = False
game_paused = False
Button_Paused = texto()
if Button_Paused.Click():
if game_paused==False:
game_paused = True
elif game_paused:
game_paused = False
Button_Paused = texto()
if event.type == pygame.QUIT:
running = False
# Le damos un Delay
time.sleep(0.045)
# Bucle para escribir las celdas en pantalla
for y in range(2, ncX-14):
for x in range(2, ncY-2):
if not game_paused:
# Determinamos los vecinos cercanos de cada celda
n_Neigh=gameState[(x - 1) % ncX, (y - 1) % ncY] + \
gameState[(x) % ncX, (y - 1) % ncY] + \
gameState[(x + 1) % ncX, (y - 1) % ncY] + \
gameState[(x - 1) % ncX, (y) % ncY] + \
gameState[(x + 1) % ncX, (y) % ncY] + \
gameState[(x - 1) % ncX, (y + 1) % ncY] + \
gameState[(x) % ncX, (y + 1) % ncY] + \
gameState[(x + 1) % ncX, (y + 1) % ncY]
#Regla 1: Una celda muerta con exactamente 3 vecinas vivas, "revive"
if gameState[x, y] == 0 and n_Neigh == 3:
newGameState[x, y] = 1
if game_classic:
pygame.draw.polygon(screen, '#50ca80', poly, 0)
#Regla 2: Una celda viva con menos de 2 o más de 3 celdas vivas alrededor muere.
elif gameState[x, y] == 1 and (n_Neigh < 2 or n_Neigh > 3):
newGameState[x, y] = 0
if game_classic:
pygame.draw.polygon(screen, '#c85656', poly, 0)
# Creamos el poligono para dibujar la celda
poly = [((x) * dimCW, y * dimCH),
((x + 1) * dimCW, y * dimCH),
((x + 1) * dimCW, (y + 1) * dimCH),
((x) * dimCW, (y + 1) * dimCH)]
# Dibujamos la celda
if newGameState[x, y] == 0:
Tcolor = Colors()
pygame.draw.polygon(screen, '#606060', poly, 1)
else:
pygame.draw.polygon(screen, Tcolor, poly, 0)
# Al final de bucle actualizaremos el estado de todas
# las celdas al mismo tiempo
gameState = np.copy(newGameState)
pass
#Actualizacion de Display
pygame.display.update()
#Limitacion de Fps
clock.tick(60)
# Finalizamos el Pygame
pygame.quit()