Skip to content

Instantly share code, notes, and snippets.

@villares
Created August 27, 2021 01:15
Show Gist options
  • Save villares/36fa06fc1dc24b940b5706c4860c456f to your computer and use it in GitHub Desktop.
Save villares/36fa06fc1dc24b940b5706c4860c456f to your computer and use it in GitHub Desktop.
# estruturas de dados listas, tuplas
# loop for
# enumerate()
# grades, grade de elementos numerados
from random import shuffle
pos_celulas = []
tam = 100 # tamanho
celula_arrastada = None
def setup():
size(400, 400)
rectMode(CENTER)
textSize(20)
textAlign(CENTER, CENTER)
for y in range(0, 400, tam):
for x in range(0, 400, tam):
pos_celulas.append((x + tam / 2, y + tam / 2))
shuffle(pos_celulas)
# print(pos_celulas)
def draw():
background(0)
for i, (x, y) in enumerate(pos_celulas):
if i == celula_arrastada:
fill(255, 0, 0)
elif dist(mouseX, mouseY, x, y) < tam / 2:
fill(128)
else:
fill(255)
rect(x, y, tam, tam)
fill(0)
text(i, x, y)
# fill(255, 0, 0)
# circle(x, y, 5)
def mousePressed():
global celula_arrastada
if celula_arrastada is None:
for i, (x, y) in reversed(list(enumerate(pos_celulas))):
if dist(mouseX, mouseY, x, y) < tam / 2:
celula_arrastada = i
break
def mouseReleased():
global celula_arrastada
celula_arrastada = None
def mouseDragged():
if celula_arrastada is not None:
pos_celulas[celula_arrastada] = (mouseX, mouseY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment