Alexandre B A Villares 2018-11-30 22:25:40 -02:00 zatwierdzone przez GitHub
rodzic 04b6e98090
commit 298fbe0106
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 95 dodań i 0 usunięć

BIN
s336/cell$py.class 100644

Plik binarny nie jest wyświetlany.

69
s336/cell.py 100644
Wyświetl plik

@ -0,0 +1,69 @@
class Cell():
#neighbours list
NL = ((-1, -1), (+0, -1), (+1, -1),
(-1, +0), (+0, +0), (+1, +0),
(-1, +1), (+0, +1), (+1, +1))
ONL = ( (+0, -1),
(-1, +0), (+0, +0), (+1, +0),
(+0, +1) )
def __init__(self, index, cell_size):
# Cell.NL = ((-1, -1), (+0, -1), (+1, -1),
# (-1, +0), (+0, +0), (+1, +0),
# (-1, +1), (+0, +1), (+1, +1))
self.index = index
self.state = False
self.size_ = cell_size
self.mouse_down = False
i, j = index[0], index[1]
self.pos = PVector(self.size_/2 + i * self.size_,
self.size_/2 + j * self.size_)
# self.ngbs = []
# for ni, nj in Cell.NL:
# self.ngbs.append(
# Cell.grid.get((i-ni, j-nj), None))
def play(self):
hs = self.size_ / 2
px, py = self.pos.x, self.pos.y
self.mouse_on = (px - hs < mouseX < px + hs and
py - hs < mouseY < py + hs)
if self.mouse_on and mousePressed:
self.mouse_down = True
if self.mouse_down and not mousePressed:
self.state = not self.state
self.mouse_down = False
self.plot()
def plot(self):
fill(255)
rect(self.pos.x, self.pos.y, self.size_, self.size_)
if self.state:
third = self.size_ / 3
fill(0)
if key == CODED:
nbs = Cell.NL
else:
nbs = Cell.ONL
i, j = self.index[0], self.index[1]
for (ni, nj) in nbs:
nb = Cell.grid.get((i+ni, j+nj), None)
if nb and nb.state:
fill(255, 0, 0)
else:
fill(250)
#noStroke()
rect(self.pos.x + ni * third,
self.pos.y + nj * third,
third, third)
if self.mouse_on:
with pushStyle():
fill(128, 128)
rect(self.pos.x, self.pos.y,
self.size_,
self.size_
)
def keyPressed():
saveFrame("###.png")

BIN
s336/s336.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 49 KiB

26
s336/s336.pyde 100644
Wyświetl plik

@ -0,0 +1,26 @@
from cell import Cell
CELL_SIZE = 25
Cell.grid = dict()
def setup():
size(500, 500)
rectMode(CENTER)
init_grid(width//CELL_SIZE, height//CELL_SIZE)
def init_grid(w, h):
for i in range(w):
for j in range(h):
Cell.grid[(i, j)] = Cell((i,j), CELL_SIZE)
def draw():
for c in Cell.grid.values():
c.play()