Ayuda no encuentro o identifico mi error en mi mini juego
Publicado por finetex (1 intervención) el 11/05/2020 00:28:24
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
import pygame
import numpy as np
import time
pygame.init()
#Ancho y alto de la pantalla
width, height = 1000, 1000
#Creacion de la pantalla
screen = pygame.display.set_mode((height, width))
#Color de fondo = casi negro, casi oscuro
bg = 25, 25, 25
#Pintamos el fondo con el color elegido previo
screen.fill(bg)
nxC, nyC = 25, 25
dimCW = width / nxC
dimCh = height / nyC
# Estadoo de las celdas. Vivas= 1; Muertas= 0;
gameState = np.zeros((nxC,nyC))
#Automata palo
gameState[5, 3] = 1
gameState[5, 4] = 1
gameState[5, 5] = 1
#Automata movil
gameState[21, 21] = 1
gameState[22, 22] = 1
gameState[22, 23] = 1
gameState[21, 21] = 1
gameState[20, 21] = 1
#Control de ejecucion
pauseExect = False
#Bucle de ejecucion
while True:
newGameState = np.copy(gameState)
screen.fill(bg)
time.sleep(0.1)
# registrar eventos de teclado y raton.
ev = pygame.event.get()
for event in ev:
# Detectamos si se presiona una tecla
if event.type == pygame.KEYDOWN:
pauseExect = not pauseExect
#Detectamos si se presiona el reton
mouseClick = pygame.mouse.get_pressed()
if sum(mouseClick) > 0:
posX, posY = pygame.mouse.get_pos()
celX, celY = int(np.floor(posX / dimCW)), int(np.floor(posY / dimCh))
newGameState[celX, celY] = not mouseClick[2]
for y in range(0,nxC):
for x in range(0, nyC):
if not pauseExect:
#Calculamos el numero de vecinos cercanos
n_neight = gameState[(x - 1) % nxC, (y -1) % nyC] + \
gameState[(x) % nxC, (y - 1) % nyC] + \
gameState[(x + 1) % nxC, (y - 1) % nyC] + \
gameState[(x - 1) % nxC, (y) % nyC] + \
gameState[(x + 1) % nxC, (y) % nyC] + \
gameState[(x - 1) % nxC, (y + 1) % nyC] + \
gameState[(x) % nxC, (y + 1) % nyC] + \
gameState[(x + 1) % nxC, (y + 1) % nyC]
#Rule #1 : Una celula Muerta con exactamente 3 vecinas vivas, "revive".
if gameState[x, y] == 0 and n_neight == 3:
newGameState[x, y] = 1
#Rule #2 :Una celula viva con menos de dos o mas de de 3 vecinas vivas, "muere"
elif gameState[x, y] == 1 and (n_neight < 2 or n_neight > 3):
newGameState[x, y] = 0
#Creamos el poligono de cada celda a dibujar.
poly = [(x * dimCW, y * dimCh),
((x+1) * dimCW, y * dimCh),
((x+1) * dimCW, (y+1) * dimCh),
((x) * dimCW, (y+1) * dimCh)]
#Y dibujamos la celda para cada par de X e Y.
if newGameState[x, y] == 0:
pygame.draw.polygon(screen, (128,128,128), poly, 1)
else:
pygame.draw.polygon(screen, (255, 255, 255), poly, 0)
#Actualizamos el estado del juego.
gameState = np.copy(newGameState)
#Actualizacion de la pantalla.
pygame.display.flip()
Valora esta pregunta


0