### Sesc Av. Paulista
## Grupo de estudos em Python
# `hackmd.io/@sesc-av-paulista/estudos-em-python-15-maio`
### Conversas paralelas
- https://www.rubi.com/us/blog/penrose-tiling/
- https://www.maths.cam.ac.uk/features/tip-hat-celebrating-aperiodic-monotile-discovery
- https://en.wikipedia.org/wiki/Voronoi_diagram
## As ferramentas
[como instalar Thonny e py5](https://abav.lugaralgum.com/como-instalar-py5/)
### Primeiro exemplo sem OO
```python!
x = 100
y = 100
vx = -5
vy = -2
def setup():
size(500, 500)
def draw():
global x, y
background(0, 200, 0) # R, G, B 0 a 255
diametro = random(10, 40)
circle(x, y, diametro)
x += vx # x = x + 5
y += vy # y + 2
if x > width:
x = 0
elif x < 0:
x = width
if y > height:
y = 0
elif y < 0:
y = height
```
### Primeiro exemplo com OO
```python!
def setup():
global bolinha
size(500, 500)
bolinha = Bola(100, 100)
print(bolinha)
def draw():
background(0, 200, 0) # R, G, B 0 a 255
bolinha.draw()
bolinha.move()
class Bola:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random(-2, 2)
self.vy = random(-2, 2)
self.d = random(10, 20)
def draw(self):
circle(self.x, self.y, self.d)
def move(self):
self.x += self.vx # x = x + 5
self.y += self.vy # y + 2
if self.x > width:
self.x = 0
elif self.x < 0:
self.x = width
if self.y > height:
self.y = 0
elif self.y < 0:
self.y = height
```
100 bolinhas
```python!
bolinhas = []
def setup():
global bolinha
size(500, 500)
for i in range(100):
bolinha = Bola(100, 100)
bolinhas.append(bolinha)
def draw():
background(0) # R, G, B 0 a 255
for bolinha in bolinhas:
bolinha.draw()
bolinha.move()
class Bola:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random(-5, 5)
self.vy = random(-5, 5)
self.cor = color(random(255),
random(255),
random(255))
self.d = random(10, 30)
def draw(self):
no_stroke()
fill(self.cor)
circle(self.x, self.y, self.d)
def move(self):
self.x += self.vx # x = x + 5
self.y += self.vy # y + 2
if self.x > width:
self.x = 0
elif self.x < 0:
self.x = width
if self.y > height:
self.y = 0
elif self.y < 0:
self.y = height
```
100 bolinhas com rastro
```python!
bolinhas = []
def setup():
global bolinha
size(600, 600)
for i in range(100):
bolinha = Bola(random(width),
random(height))
bolinhas.append(bolinha)
background(0) # R, G, B 0 a 255
def draw():
fill(0, 10) # preto translúcido
rect(0, 0, width, height)
for bolinha in bolinhas:
bolinha.draw()
bolinha.move()
class Bola:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random(-5, 5)
self.vy = random(-5, 5)
self.cor = color(random(255),
random(255),
random(255))
self.d = random(10, 30)
def draw(self):
no_stroke()
fill(self.cor)
circle(self.x, self.y, self.d)
def move(self):
self.x += self.vx # x = x + 5
self.y += self.vy # y + 2
if self.x > width:
self.x = 0
elif self.x < 0:
self.x = width
if self.y > height:
self.y = 0
elif self.y < 0:
self.y = height
```
Fogos de artifício
```python=
bolinhas = []
def setup():
global bolinha
full_screen()
#size(600, 600)
color_mode(HSB)
background(0)
def draw():
fill(0, random(5, 15)) # preto translúcido
rect(0, 0, width, height)
for bolinha in bolinhas.copy():
bolinha.update()
print(len(bolinhas))
def mouse_dragged():
b = Bola(mouse_x, mouse_y)
bolinhas.append(b)
def key_pressed():
if key == ' ':
bolinhas.clear()
class Bola:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random(-5, 5)
self.vy = random(-5, 5)
self.d = random(10, 30)
def update(self):
self.draw()
self.move()
self.d = self.d * 0.95
if self.d < 1:
bolinhas.remove(self)
def draw(self):
no_stroke()
fill(self.d * 8, 255, 255)
circle(self.x, self.y, self.d)
def move(self):
self.x += self.vx # x = x + 5
self.y += self.vy # y + 2
if self.x > width:
self.x = 0
elif self.x < 0:
self.x = width
if self.y > height:
self.y = 0
elif self.y < 0:
self.y = height
```