Skip to content

Instantly share code, notes, and snippets.

@Vamoss
Created February 27, 2021 19:22
Show Gist options
  • Save Vamoss/736519bce7d3eca0af6d63471dba0e41 to your computer and use it in GitHub Desktop.
Save Vamoss/736519bce7d3eca0af6d63471dba0e41 to your computer and use it in GitHub Desktop.
Pyp5js Random Walke
# Code by @Vamoss
# Run at Pyp5js editor
# https://abav.lugaralgum.com/pyp5js/pyodide/
positions = []
GRID_SIZE = 20
def setup():
global positions
size(500, 500)
background("#1a0633")
colors = []
colors = [color("#1a0633"), color("#1a0633"), color("#581845"), color("#900C3F"), color("#C70039"), color("#FF5733"), color("#FFC30F"), color("#581845")]
l = len(colors)
for i in range(floor(l/2)):
position = Position(round(width / 2 / GRID_SIZE) * GRID_SIZE, round(height / 2 / GRID_SIZE) * GRID_SIZE, colors[(i*2+0)%l], colors[(i*2+1)%l])
positions.append(position)
def draw():
global positions
for position in positions:
position.curX += round(random(-1, 1)) * GRID_SIZE
position.curY += round(random(-1, 1)) * GRID_SIZE
position.curX = constrain(position.curX, GRID_SIZE, round(width / GRID_SIZE) * GRID_SIZE - GRID_SIZE)
position.curY = constrain(position.curY, GRID_SIZE, round(height / GRID_SIZE) * GRID_SIZE - GRID_SIZE)
stroke(position.color1)
strokeWeight(GRID_SIZE)
line(position.curX, position.curY, position.prevX, position.prevY)
stroke(position.color2)
strokeWeight(2)
line(position.curX, position.curY, position.prevX, position.prevY)
position.prevX = position.curX
position.prevY = position.curY
class Position:
def __init__(self, x, y, color1, color2):
self.curX = x
self.curY = y
self.prevX = x
self.prevY = y
self.color1 = color1
self.color2 = color2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment