Skip to content

Commit

Permalink
By pressing R user can now record *.png in some subfolder. gif.sh con…
Browse files Browse the repository at this point in the history
…verts them to a *.gif file
  • Loading branch information
Darius Berghe committed Dec 21, 2016
1 parent 02aabf3 commit f2bc764
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion design.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def setupUi(self, MainWindow):

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Conwell\'s Game of Life"))
MainWindow.setWindowTitle(_translate("MainWindow", "Conway\'s Game of Life"))

from universeview import UniverseView
2 changes: 1 addition & 1 deletion design.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Conwell's Game of Life</string>
<string>Conway's Game of Life</string>
</property>
<property name="windowOpacity">
<double>0.950000000000000</double>
Expand Down
6 changes: 6 additions & 0 deletions gif.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# This script assumes imagemagick is installed
# and converts the latest generated
lastfolder=$(ls 'recordings' | tail -n 1)
cd "recordings/$lastfolder"
convert -delay 20 -loop 0 *png animation.gif
11 changes: 7 additions & 4 deletions life.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ class LifeformState(Enum):
resurrecting = 3

class Lifeform():
def __init__(self, state=False):
def __init__(self, c1, c2, state=False):
'''
Create a lifeform
:param state: True stands for alive, False stands for dead.
'''
self._age = 0
self._state = LifeformState.alive if state else LifeformState.dead
self._neighbors = []
self._c1 = c1
self._c2 = c2


def alive(self):
'''
Expand Down Expand Up @@ -46,9 +49,9 @@ def play(self):
if self.alive():
self._age += 1

if (nc < 2 or nc > 3) and self.alive():
if (nc < self._c1 or nc > self._c2) and self.alive():
self._state = LifeformState.dying
if nc == 3 and not self.alive():
if nc == self._c2 and not self.alive():
self._state = LifeformState.resurrecting

def updateState(self):
Expand All @@ -62,4 +65,4 @@ def kill(self):
self._state = LifeformState.dead

def resurrect(self):
self._state = LifeformState.alive
self._state = LifeformState.alive
6 changes: 4 additions & 2 deletions universe.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from life import *

class Universe():
def __init__(self, state):
def __init__(self, state, c1 = 2, c2 = 3):
'''
Initialize the game with the initial state (the only input to the original Game of Life)
:param state: 2D list of booleans
'''
self._age = 0
self._c1 = c1
self._c2 = c2
self.seed(state)

def seed(self, state):
# create all the Lifeforms and store them into the Universe's state
self._state = [[Lifeform(i) for i in row] for row in state] # 2D list of Lifeforms
self._state = [[Lifeform(self._c1, self._c2, i) for i in row] for row in state] # 2D list of Lifeforms
self.updateNeighbors()

def getNeighbors(self, col, row):
Expand Down
30 changes: 26 additions & 4 deletions universeview.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsRectItem
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
from time import perf_counter, strftime, gmtime
import os

class constants():
DefaultCellToScreenRatio = 0.005
Expand Down Expand Up @@ -33,6 +34,7 @@ def __init__(self, parent=None):
self._showGrid = False
self._mousePosition = (0,0)
self._CellToScreenRatio = 0.01
self._recording = False

def initialize(self, initialState):
self.universe = Universe(initialState)
Expand Down Expand Up @@ -126,14 +128,18 @@ def status(self):
age.setDefaultTextColor(Qt.white)
age.setPos(0, 30)

age = self._scene.addText('Mouse at x {}, y {}'.format(self._mousePosition[0], self._mousePosition[1]))
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 {}x{}'.format(self.rows, self.cols))
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)
Expand Down Expand Up @@ -174,6 +180,10 @@ def timeTick(self):
# 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():
Expand All @@ -187,6 +197,18 @@ def keyPressEvent(self, QKeyEvent):
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(self._timerTickPeriod * 1000)
Expand Down

0 comments on commit f2bc764

Please sign in to comment.