-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifegame.pyde
95 lines (73 loc) · 2.91 KB
/
lifegame.pyde
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
# The implementation of the Conway's Game of Life.
# The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
# The code written by Alexander Kilinkarov.
from copy import deepcopy
from random import choice
from math import sqrt
class Cell:
def __init__(self, x, y, s, c=255):
self.x = x
self.y = y
self.s = s
self.c = c
self.alive = False
def show(self):
if self.alive:
fill(self.c)
else:
fill(90)
ellipse(self.x, self.y, self.s, self.s)
def setup():
# fullScreen()
size(1000, 1000)
frameRate(30)
global cols, rows, game_plays, cells
s = 20
cols = width//s
rows = height//s
game_plays = False
ellipseMode(CENTER)
cells = [[Cell(s//2 + (col-1)*s, s//2 + (row-1)*s, s, c=color(0, 255, 0)) for col in range(cols+2)] for row in range(rows+2)]
def draw():
global cells, cols, rows
background(30)
if game_plays:
# game plays and cells change their state
cells2 = deepcopy(cells)
for row in range(1, len(cells2)-1):
for col in range(1, len(cells2[row])-1):
cells[row][col].show()
# count neibours
neibours_count = sum([i.alive for i in cells2[row-1][col-1:col+2]]) + cells2[row][col-1].alive + cells2[row][col+1].alive + \
sum([i.alive for i in cells2[row+1][col-1:col+2]])
if neibours_count == 3 and not cells2[row][col].alive:
cells[row][col].alive = True
elif cells2[row][col].alive and neibours_count not in (2, 3):
cells[row][col].alive = False
else:
# if game stopped only show the field
for row in range(1, len(cells)-1):
for col in range(1, len(cells[row])-1):
cells[row][col].show()
# set cells to alive state with mouse click
def mousePressed():
global cells
for row in range(1, len(cells)-1):
for col in range(1, len(cells[row])-1):
deltaX = abs(mouseX - cells[row][col].x)
deltaY = abs(mouseY - cells[row][col].y)
if sqrt(deltaX**2 + deltaY**2) <= cells[row][col].s//2 and not cells[row][col].alive:
cells[row][col].alive = True
def keyPressed():
global game_plays, cells
# start/pause the game
if key == "1":
game_plays = False if game_plays else True
# set random values on the field
elif key == "2":
for row in range(1, len(cells)-1):
for col in range(1, len(cells[row])-1):
cells[row][col].alive = choice([True, False])
# exit game. Doesn't work
elif key == ESC:
exit()