Skip to content

Instantly share code, notes, and snippets.

@inglesp
Created November 13, 2015 08:42
Show Gist options
  • Save inglesp/057a7b1942a0e235e461 to your computer and use it in GitHub Desktop.
Save inglesp/057a7b1942a0e235e461 to your computer and use it in GitHub Desktop.
First attempts to get something tetris-like to run on a micro:bit, created at the London Python Dojo by lukasa and inglesp.
import microbit
SIZE = 5
TOP = 4
BOTTOM = 0
LEFT = 4
RIGHT = 0
def new_block():
return (microbit.random(SIZE), TOP)
def gravity(active_block, blocks):
new_location = (active_block[0], active_block[1] - 1)
if new_location in blocks:
return active_block, True
elif new_location[1] == BOTTOM:
return new_location, True
else:
return new_location, False
def draw(blocks):
microbit.display.clear()
for block in blocks:
microbit.display.set_pixel(block[0], block[1], 9)
def has_lost(blocks):
return any(block[1] == TOP for block in blocks)
def bottom_line_full(blocks):
return len([True for block in blocks if block[1] == BOTTOM]) == SIZE
def clear():
for y in reversed(range(SIZE)):
for x in reversed(range(SIZE)):
microbit.display.set_pixel(x, y, 0)
microbit.sleep(50)
for y in reversed(range(SIZE)):
for x in reversed(range(SIZE)):
microbit.display.set_pixel(x, y, 9)
microbit.sleep(10)
def main():
blocks = []
active_block = None
while True:
if active_block is None:
active_block = new_block()
draw([active_block] + blocks)
active_block, complete = gravity(active_block, blocks)
if complete:
blocks.append(active_block)
active_block = None
microbit.sleep(200)
if has_lost(blocks):
microbit.music.play(microbit.music.FUNERAL)
clear()
break
if bottom_line_full(blocks):
draw(blocks)
microbit.sleep(1000)
blocks = [(block[0], block[1] -1) for block in blocks if not block[1] == BOTTOM]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment