-
Notifications
You must be signed in to change notification settings - Fork 1
/
conway.py
151 lines (148 loc) · 4.77 KB
/
conway.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
#Conway project ver 2.0
import pygame
import math
from copy import copy, deepcopy
from rendering import render
from screenUpdater import updateScreen
from saver import save, load, readRLES
from screenshotter import screenshot
import decodeRLE
# --init--
pygame.init()
scr = (width, height) = (960, 960)
screen = pygame.display.set_mode((width, height))
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Conway's game of life")
game = True
clock = pygame.time.Clock()
RLE_list = readRLES()
gridSize = (96, 96)
cells = {}
neighbors = (
(-1, 1),
(-1, 0),
(-1, -1),
(0, 1),
(0, -1),
(1, 1),
(1, 0),
(1, -1)
)
pixSize = (scr[0] / (gridSize[0]), scr[1] / (gridSize[1]))
def warp(x, min, max):
if x < min: x = x + max
if x > max: x = x - max
return x
def initGrid():
newCells = {}
newCells["birthRules"] = [3]
newCells["surviveRules"] = [2, 3]
for i in range(gridSize[0]):
newCells[str(i)] = [copy(False) for i in range(gridSize[1])]
return newCells
def initRLE(rle):
cells = initGrid()
rleCells = decodeRLE.openRLE("rle/"+ rle)
size = rleCells["size"]
cells["birthRules"] = rleCells["birthRule"]
cells["surviveRules"] = rleCells["surviveRule"]
print(size)
for i in range(0, size[1]):
for j in range(0, size[0]):
try:
cells[str(i)][j] = rleCells[str(i)][j]
except:
a = 0
return cells
# -- pre launch --
cells = initGrid()
empty_grid = initGrid()
paused = True
ticker = 0
speed = 120
i = 0
font = pygame.font.SysFont('Comic Sans MS', 10)
# -- loop --
state = False
isTicking = True
toScreenshot = False
x = 0
y = 0
while game:
# -- general init --
clock.tick(speed)
if(not isTicking and not paused):
cells = updateScreen(cells, gridSize, neighbors, cells["surviveRules"], cells["birthRules"])
elif paused == False:
ticker += 1
if ticker >= 12:
cells = updateScreen(cells, gridSize, neighbors, cells["surviveRules"], cells["birthRules"])
ticker = 0
# -- Inputs --
pixSel = (math.floor(pygame.mouse.get_pos()[0] / pixSize[0]), math.floor(pygame.mouse.get_pos()[1] / pixSize[1]))
realPos = pixSel
pixSel = (warp(pixSel[0] - x, 0, gridSize[0]), warp(pixSel[1] + y, 0, gridSize[1]))
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
paused = not paused
if event.key == pygame.K_LSHIFT:
cells = updateScreen(cells, gridSize, neighbors, cells["surviveRules"], cells["birthRules"])
if event.key == pygame.K_f:
isTicking = False
if event.key == pygame.K_s:
isTicking = True
if event.key == pygame.K_F4:
cells = initGrid()
if event.key == pygame.K_F5:
save(cells, gridSize[0], gridSize[1])
if event.key == pygame.K_F6:
cells = load(i)
if event.key == pygame.K_F7:
cells = initRLE(RLE_list[i])
if event.key == pygame.K_F2:
toScreenshot = True
if event.type == pygame.MOUSEWHEEL:
i += int(event.y)
if i < 0:
i = 0
while True:
if i > len(RLE_list) - 1: i -= 1
else : break
if pygame.mouse.get_pressed(num_buttons=3)[0] == True:
if not clicked:
try:
state = not cells[str(pixSel[0])][pixSel[1]]
except:
print("pixel is out of bounds", str(pixSel[0]), str(pixSel[1]))
try:
cells[str(pixSel[0])][pixSel[1]] = state
except:
print("Clicked out of bounds", str(pixSel[0]), str(pixSel[1]))
clicked = True
else:
clicked = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]: y += 1
if keys[pygame.K_DOWN]: y -= 1
if keys[pygame.K_LEFT]: x -= 1
if keys[pygame.K_RIGHT]: x += 1
x = warp(x, 0, gridSize[0])
y = warp(y, 0, gridSize[1] - 1)
# -- Render logic --
screen.fill("#d7dedc")
render(cells, gridSize, scr, screen, x, y)
if(toScreenshot):
screenshot(screen)
toScreenshot = False
pygame.draw.rect(screen, (120, 120, 120), (realPos[0] * pixSize[0], realPos[1] * pixSize[1], pixSize[0], pixSize[1]))
txt = font.render(str(i) + " : "+ RLE_list[i], False, (0, 0, 0))
fps = font.render("fps: " + str(math.floor(pygame.time.Clock.get_fps(clock))), False, (0, 0, 0))
screen.blit(txt, (0, 0))
screen.blit( fps, (920, 0))
pygame.display.flip()
pygame.quit()