-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-of-life.py
71 lines (55 loc) · 2.02 KB
/
game-of-life.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QSize
import design
from random import randint
class MainWindow(QMainWindow):
def __init__(self, screen_width, screen_height, c1, c2):
super().__init__()
# build ui
self.ui = design.Ui_MainWindow()
self.ui.setupUi(self)
# resize the main window to a sensible value
self.resize(QSize(int(screen_width / 2), int(screen_height / 2)))
# resize the graphics scene to match the window
uv = self.ui.graphicsView
uv.resize(screen_width, screen_height)
# create a random initial state for the universe
initial = [[(randint(0, 10) == 9) for i in range(uv.cols)] for j in range(uv.rows)]
uv.initialize(initial, c1, c2)
# start the animation directly
uv.start()
def resizeEvent(self, QResizeEvent):
# resize the graphics scene to match the window
self.ui.graphicsView.resize(screen_resolution.width(), screen_resolution.height())
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_F11:
if self.windowState() == Qt.WindowFullScreen:
self.showNormal()
else:
self.showFullScreen()
if __name__ == '__main__':
c1 = 2
c2 = 3
try:
c1 = int(sys.argv[1])
if not (c1 > 0 and c1 < 10):
raise ValueError
c2 = int(sys.argv[2])
if not (c2 > 0 and c2 < 10):
raise ValueError
except IndexError:
pass
except ValueError:
print("c1 and c2 must be positive integers between 1 and 9\ngame-of-life [c1 c2]")
sys.exit(0)
# set up graphics
app = QApplication(sys.argv)
# get screen resolution and create the main window
screen_resolution = app.desktop().screenGeometry()
main = MainWindow(screen_resolution.width(), screen_resolution.height(), c1, c2)
# draw, launch qt app
main.show()
s = app.exec_()
# finish properly
sys.exit(s)