from machine import Pin, Timer
from neopixel import NeoPixel
from random import randint
class Sprite:
all = []
def __init__(self, x, y, col, dirc, v):
self.x = x
self.y = y
self.col = col
self.dirc = dirc
self.v = v
np[self.pos(x, y)] = col
np.write()
self.timer = Timer(period=v, callback=self.step)
self.all += [ self ]
def step(self, event):
np[self.pos(self.x, self. y)] = (0, 0, 0)
if self.dirc == 1:
self.x += 1
self.y += 1
if self.x == 7:
if self.y == 7:
self.dirc = 3
else:
self.dirc = 4
elif self.y == 7:
self.dirc = 2
elif self.dirc == 2:
self.x += 1
self.y -= 1
if self.x == 7:
if self.y == 0:
self.dirc = 4
else:
self.dirc = 3
elif self.y == 0:
self.dirc = 1
elif self.dirc == 3:
self.x -= 1
self.y -= 1
if self.x == 0:
if self.y == 0:
self.dirc = 1
else:
self.dirc = 2
elif self.y == 0:
self.dirc = 4
else:
self.x -= 1
self.y += 1
if self.x == 0:
if self.y == 7:
self.dirc = 2
else:
self.dirc = 1
elif self.y == 7:
self.dirc = 3
np[self.pos(self.x, self. y)] = self.col
np.write()
@classmethod
def stop(cls):
for sprite in cls.all:
sprite.timer.deinit()
np[sprite.pos(sprite.x, sprite.y)] = (0, 0, 0)
np.write()
@staticmethod
def pos(x, y):
return 8*x + y
disp = Pin(0, Pin.OUT)
np = NeoPixel(disp, 64)
for ball in range(4):
Sprite(randint(1, 6), randint(1, 6), (randint(10, 250), randint(10, 250), randint(10, 250)), randint(1, 4), randint(30, 500))
try:
while True:
pass
except KeyboardInterrupt:
Sprite.stop()