Last active
August 8, 2020 01:41
-
-
Save andreburto/3098733a09969ca19d487c9812a53330 to your computer and use it in GitHub Desktop.
gamer:bit + micro:bit = 5x5 canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAX_B = 9 | |
MAX_X = 5 | |
MAX_Y = 5 | |
b = MAX_B | |
x = 0 | |
y = 0 | |
d_s = [[0 for c in range(0, MAX_X)] for r in range(0, MAX_Y)] | |
def mr(): | |
global x | |
x = x + 1 if x < MAX_X - 1 else MAX_X - 1 | |
def ml(): | |
global x | |
x = x - 1 if x > 0 else 0 | |
def mu(): | |
global y | |
y = y - 1 if y > 0 else 0 | |
def md(): | |
global y | |
y = y + 1 if y < MAX_Y - 1 else MAX_Y - 1 | |
def bu(): | |
global b | |
b = b + 1 if b < MAX_B else MAX_B | |
def bd(): | |
global b | |
b = b - 1 if b > 0 else 0 | |
def poff(): | |
global d_s, x, y | |
d_s[x][y] = 0 | |
def pon(): | |
from microbit import display | |
display.clear() | |
global d_s, x, y, b | |
d_s[x][y] = b | |
actions = [mu, ml, mr, md, poff, pon, bd, bu] | |
def act(p): | |
global actions | |
actions[p]() | |
def up_d(d): | |
global b, d_s, x, y | |
d.clear() | |
for col in range(0, MAX_X): | |
for row in range(0, MAX_Y): | |
d.set_pixel(col, row, d_s[col][row]) | |
d.set_pixel(x, y, b) | |
def init(d): | |
global b, x, y | |
d.clear() | |
d.set_pixel(x, y, b) | |
def main(): | |
import microbit | |
display = microbit.display | |
sleep = microbit.sleep | |
pins = [microbit.pin0, microbit.pin1, microbit.pin2, microbit.pin8, | |
microbit.pin12, microbit.pin16, microbit.pin5, microbit.pin11] | |
pin_cnt = len(pins) | |
for p in pins: | |
p.set_pull(p.PULL_UP) | |
init(display) | |
while True: | |
for p in range(0, pin_cnt): | |
if pins[p].read_digital() == 0: | |
act(p) | |
up_d(display) | |
sleep(100) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment