-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
110 lines (80 loc) · 2.52 KB
/
game.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
import pygame
import numpy as np
import time
pygame.init()
width, height = 500, 500
screen = pygame.display.set_mode ((height, width))
bg = 25 , 25, 25
screen.fill(bg)
nxC, nyC = 25, 25
dimCW = width / nxC
dimCH = height / nyC
gameState = np.zeros((nxC, nyC))
# Automata palo.
gameState[5,3] = 1
gameState[5,4] = 1
gameState[5,5] = 1
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,23] = 1
gameState[20,23] = 1
gameState[21,21] = 1
gameState[22,22] = 1
gameState[22,23] = 1
gameState[21,23] = 1
gameState[20,23] = 1
#Controlar la ejecucion del juego.
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()
#Detectamos si se presiona una tecla.
for event in ev:
if event.type == pygame.KEYDOWN:
pauseExect = not pauseExect
#Detectamos si se presiona el raton.
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_neigh = 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_neigh == 3:
newGameState[x, y] = 1
#Rule #2: una celula viva con menos de 2 o mas de 3 vecinas vivas, "muere".
elif gameState[x, y] == 1 and (n_neigh < 2 or n_neigh > 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)
pygame.display.flip()