-
Notifications
You must be signed in to change notification settings - Fork 0
/
universeview.py
242 lines (191 loc) · 8.33 KB
/
universeview.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
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsRectItem, QWidget
from PyQt5.QtCore import Qt, QTimer, QRectF
from PyQt5.QtGui import QBrush, QPen, QColor
from universe import Universe
from time import perf_counter, strftime, gmtime
import os
class constants():
DefaultCellToScreenRatio = 0.005
DefaultAtomicTick = 0.1
background = QColor(60, 60, 60)
grid = QColor(20, 20, 20)
cell = QColor(84, 158, 39)
class UniverseView(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
# visuals
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# set up the graphics
self._scene = QGraphicsScene()
self.setScene(self._scene)
# set up time
self._timer = QTimer()
self._timer.timeout.connect(self.timeTick)
self._timerTickPeriod = constants.DefaultAtomicTick
# defaults
self._showStatus = False
self._showGrid = False
self._mousePosition = (0,0)
self._CellToScreenRatio = 0.01
self._recording = False
def initialize(self, initialState, c1, c2):
self.universe = Universe(initialState, c1, c2)
def seed(self, state):
self.universe.seed(state)
def start(self):
self._timer.start(int(constants.DefaultAtomicTick * 1000))
self.frame_timestamps = []
self.start_t = perf_counter()
def stop(self):
self._timer.stop()
def rows(self):
return self.rows
def cols(self):
return self.colss
def resize(self, wscreen, hscreen):
# set a sensible value for the cell size relative to screen size
self.cell_size = int(wscreen * self._CellToScreenRatio)
if self.cell_size < 2:
self.cell_size = 2
# screen size is most certainly not a multiple of our cell size, so cut the extra
wscene = int(self.parent().parent().width() / self.cell_size) * self.cell_size
hscene = int(self.parent().parent().height() / self.cell_size) * self.cell_size
# set the scene size
self._scene.setSceneRect(QRectF(0, 0, wscene, hscene))
# compute the number of rows/columns that are needed to fit items of cellsize into the scene
self.rows = int(self._scene.height() / self.cell_size)
self.cols = int(self._scene.width() / self.cell_size)
# resize the 2D boolean representation of the universe according to grid size
s = None
try:
s = self.universe.state()
except:
return
s = s[:self.rows] # in case we grew smaller, cut extra rows
while self.rows > len(s):
s.append([False] * self.cols)
for rowi, row in enumerate(s):
s[rowi] = s[rowi][:self.cols] # in case we grew smaller, cut extra items in row
for coli, col in enumerate(range(self.cols)):
try:
s[rowi][coli] # try to access the indexes
except:
s[rowi].append(False) # if we grew bigger, previous index access will generate an exception.
# it's about time to add False items (dead cells) to the row
# feed the new universe representation to it as if we were starting all anew
self.universe.seed(s)
def drawCell(self, x, y):
item = QGraphicsRectItem(x * self.cell_size,
y * self.cell_size,
self.cell_size,
self.cell_size)
item.setBrush(QBrush(constants.cell))
item.setPen(constants.grid)
self._scene.addItem(item)
def draw(self, state):
'''
Draw the scene.
:param state: 2D list of booleans
'''
for rowi, row in enumerate(state):
for celli, cell in enumerate(row):
if cell:
self.drawCell(celli, rowi)
def status(self):
value = self._scene.addText('FPS: {:.2f}'.format(self._FPS))
value.setDefaultTextColor(Qt.white)
value.setPos(0, 0)
value = self._scene.addText('Desired FPS: {:.2f}'.format(1/self._timerTickPeriod))
value.setDefaultTextColor(Qt.white)
value.setPos(0, 15)
age = self._scene.addText('Universe age: {}'.format(self.universe._age))
age.setDefaultTextColor(Qt.white)
age.setPos(0, 30)
age = self._scene.addText('Mouse at: {}, {}'.format(self._mousePosition[0], self._mousePosition[1]))
age.setDefaultTextColor(Qt.white)
age.setPos(0, 45)
age = self._scene.addText('Universe size {}, {}'.format(self.rows, self.cols))
age.setDefaultTextColor(Qt.white)
age.setPos(0, 60)
age = self._scene.addText('Recording: {}'.format(self._recording))
age.setDefaultTextColor(Qt.white)
age.setPos(0, 75)
def drawGrid(self):
for row in range(self.rows - 1):
line = self._scene.addLine(0, (row + 1) * self.cell_size, self._scene.width(), (row + 1) * self.cell_size)
line.setPen(QPen(QBrush(constants.grid), 1))
for col in range(self.cols - 1):
line = self._scene.addLine((col + 1) * self.cell_size, 0, (col + 1) * self.cell_size, self._scene.height())
line.setPen(QPen(QBrush(constants.grid), 1))
def reDraw(self):
# delete everything on the canvas
self._scene.clear()
# draw the background
self._scene.setBackgroundBrush(constants.background)
# grid
if self._showGrid:
self.drawGrid()
# draw the universe
state = self.universe.state()
self.draw(state)
# status
if self._showStatus:
self.status()
def timeTick(self):
end_t = perf_counter()
time_taken = end_t - self.start_t
self.start_t = end_t
self.frame_timestamps.append(time_taken)
self.frame_timestamps = self.frame_timestamps[-5:]
self._FPS = len(self.frame_timestamps) / sum(self.frame_timestamps)
self.reDraw()
# evolve
self.universe.evolve()
if self._recording:
pixMap = QWidget.grab(self)
pixMap.save("{}/{}.png".format(self._recordingDir, str(self.universe._age).zfill(5)))
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Space:
if self._timer.isActive():
self.stop()
else:
self._timer.start(int(self._timerTickPeriod * 1000))
elif QKeyEvent.key() == Qt.Key_S:
self._showStatus = not self._showStatus
elif QKeyEvent.key() == Qt.Key_G:
self._showGrid = not self._showGrid
elif QKeyEvent.key() == Qt.Key_R:
if not os.path.exists('recordings'):
os.makedirs('recordings')
if not self._recording:
dir = strftime("recordings/%Y-%m-%d %H:%M:%S", gmtime())
if not os.path.exists(dir):
os.makedirs(dir)
self._recordingDir = dir
self._recording = True
else:
self._recording = False
elif QKeyEvent.key() == Qt.Key_Minus:
self._timerTickPeriod *= 1.05
self._timer.setInterval(int(self._timerTickPeriod * 1000))
elif QKeyEvent.key() == Qt.Key_Plus:
self._timerTickPeriod /= 1.05
self._timer.setInterval(int(self._timerTickPeriod * 1000))
self.reDraw()
QGraphicsView.keyPressEvent(self, QKeyEvent)
def mousePressEvent(self, QMouseEvent):
self.universe.toggleLifeform(int(QMouseEvent.x() / self.cell_size),
int(QMouseEvent.y() / self.cell_size))
self.reDraw()
QGraphicsView.mousePressEvent(self, QMouseEvent)
def mouseMoveEvent(self, QMouseEvent):
self._mousePosition = (int(QMouseEvent.x() / self.cell_size), int(QMouseEvent.y() / self.cell_size))
def wheelEvent(self, QWheelEvent):
# adjust the cell to screen ratio with input from mouse wheel
if QWheelEvent.angleDelta().y() > 0:
self._CellToScreenRatio *= 1.05
else:
self._CellToScreenRatio /= 1.05
# resize the grid accordingly
self.resize(self.parent().parent().width(), self.parent().parent().height())