poveffect.py

Created by schraf

Created on February 17, 2024

1.24 KB

Visitez ma chaine YouTube (NUMWORKS, fx92…)

Persistence of vision / Persistance de la vision

Vous avez l’impression de voir une ligne ?? Restez appuyé sur la touche EXE (ou Entrée si vous êtes sur ordinateur) pour stopper l’animation et… la ligne disparait !

Do you feel like you’re seeing a line? Keep pressing the EXE or ENTER key to stop the animation, and… the line disappears!


from kandinsky import *
from random import randint
from time import sleep
from ion import keydown, KEY_EXE

nb = 50
d = 25
BL, WH = (0,0,0), (255,) * 3

def line(x1, y1, x2, y2):
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    slope = dy > dx
    if slope:
        x1, y1 = y1, x1
        x2, y2 = y2, x2
    if x1 > x2:
        x1, x2 = x2, x1
        y1, y2 = y2, y1
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    error = dx // 2
    y = y1
    ystep = 1 if y1 < y2 else -1
    for x in range(x1, x2 + 1):
        (xe, ye) = (2 * y, 2 * x) if slope else (2 * x, 2 * y)
        if 0 <= xe <= 320 and 0 <= ye <= 222:
          coul = BL if get_pixel(xe, ye)[0] !=0 else WH
          fill_rect(xe, ye, 2, 2, coul)
        error -= dy
        if error < 0:
            y += ystep
            error += dx

for c in range(160):
  for l in range(111):
    if randint(0, 1): fill_rect(2 * c, 2 * l, 2, 2, BL)

c = 0
while True:
  if c < 110:
    x1, y1 = 0, c
    x2, y2 = 160, 110 - c
  elif c < 270:  
    x1, y1 = c - 110, 110
    x2, y2 = 270 - c, 0
  elif c < 380:
    x1, y1 = 160, 380 - c
    x2, y2 = 0, c - 270
  else:
    x1, y1 = 540 - c, 0
    x2, y2 = c - 380, 110
  line(x1, y1, x2, y2)
  c = (c + 1) % 540
  while keydown(KEY_EXE): continue