kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
f3c9a8b536
commit
31872ae2da
|
@ -0,0 +1,71 @@
|
|||
def hex_color(s):
|
||||
"""
|
||||
This function allows you to create color from a string with hex notation in Python mode.
|
||||
|
||||
On "standard" Processing (Java) we can use hexadecimal color notation #AABBCC
|
||||
On Python mode one can use this notation between quotes, as a string in fill(),
|
||||
stroke() and background(), but, unfortunately, not with color().
|
||||
"""
|
||||
if s.startswith('#'):
|
||||
s = s[1:]
|
||||
return color(int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16))
|
||||
|
||||
class Cell():
|
||||
colors = (hex_color('#264653'),
|
||||
hex_color('#2a9d8f'),
|
||||
hex_color('#9c46a'),
|
||||
hex_color('#f4a261'),
|
||||
hex_color('#e76f51'))
|
||||
|
||||
def __init__(self, index, cell_size):
|
||||
self.index = index
|
||||
self.state = False
|
||||
self.s = cell_size
|
||||
self.mouse_down = False
|
||||
i, j = index[0], index[1]
|
||||
self.pos = PVector(self.s/2 + i * self.s,
|
||||
self.s/2 + j * self.s)
|
||||
self.ngbs = []
|
||||
NL = ((-1, -1), (+0, -1), (+1, -1),
|
||||
(-1, +0), (+1, +0),
|
||||
(-1, +1), (+0, +1), (+1, +1))
|
||||
for ni, nj in NL:
|
||||
self.ngbs.append(
|
||||
Cell.grid.get((i-ni, j-nj), None))
|
||||
|
||||
def play(self):
|
||||
# mouse_on = dist(self.pos.x, self.pos.y,
|
||||
# mouseX, mouseY) < self.s/2
|
||||
hs = self.s / 2
|
||||
px, py = self.pos.x, self.pos.y
|
||||
mouse_on = (px - hs < mouseX < px + hs and
|
||||
py - hs < mouseY < py + hs)
|
||||
if mouse_on and mousePressed:
|
||||
self.mouse_down = True
|
||||
if self.mouse_down and not mousePressed:
|
||||
self.state = (self.state + 1) % len(Cell.colors)
|
||||
self.mouse_down = False
|
||||
|
||||
# if mouse_on and mousePressed:
|
||||
# self.mouse_down = True
|
||||
# elif self.mouse_down and mouse_on:
|
||||
# self.state = not self.state
|
||||
# self.mouse_down = False
|
||||
# else:
|
||||
# self.mouse_down = False
|
||||
noStroke()
|
||||
fill(Cell.colors[self.state])
|
||||
rect(self.pos.x, self.pos.y,
|
||||
self.s,
|
||||
self.s
|
||||
)
|
||||
if mouse_on:
|
||||
with pushStyle():
|
||||
stroke(255)
|
||||
noFill()
|
||||
rect(self.pos.x, self.pos.y,
|
||||
self.s-1,
|
||||
self.s-1
|
||||
)
|
||||
|
||||
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 66 KiB |
|
@ -0,0 +1,31 @@
|
|||
from cell import Cell
|
||||
|
||||
CELL_SIZE = 25
|
||||
Cell.grid = dict()
|
||||
grid_lines = True
|
||||
|
||||
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():
|
||||
if grid_lines:
|
||||
stroke(0)
|
||||
else:
|
||||
noStroke()
|
||||
for c in Cell.grid.values():
|
||||
c.play()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -26,6 +26,12 @@ Here are listed some of the tools I have been using:
|
|||
|
||||
---
|
||||
|
||||

|
||||
|
||||
[sketch_2021_01_23a](https://github.com/villares/sketch-a-day/tree/master/2021/sketch_2021_01_23a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
[sketch_2021_01_22b_recursive_grid](https://github.com/villares/sketch-a-day/tree/master/2021/sketch_2021_01_22b_recursive_grid) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
|
|
Ładowanie…
Reference in New Issue