Created
December 27, 2015 16:24
-
-
Save ntoll/30802de42f393f13ff87 to your computer and use it in GitHub Desktop.
NeoPixel Pong using MicroPython for the BBC micro:bit
This file contains 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
# Play a game of squeeky pong with a 1m (30 led) NeoPixel strip. :-) | |
from microbit import * | |
import music | |
import neopixel | |
neopixel.init(pin1, 30) | |
def show_pixel(position, wait): | |
neopixel.set_colour(position, 0, 0, 64) | |
neopixel.show() | |
music.pitch(440 + position * 16, wait) | |
neopixel.set_colour(position, 0, 0, 0) | |
def run(): | |
pos = 0 | |
score = 0 | |
pixels = [x for x in range(30)] | |
pause = 100 | |
while True: | |
if button_a.is_pressed(): | |
for pos in pixels: | |
show_pixel(pos, pause) | |
pos += 1 | |
for pos in pixels[::-1]: | |
show_pixel(pos, pause) | |
pos -= 1 | |
score += 1 | |
pause -= 5 | |
display.show("{}".format(score)) | |
else: | |
display.scroll("Score: {}".format(score)) | |
break | |
while True: | |
if button_a.is_pressed(): | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I tried the code above, I got a number of error messages which I suspect is because Nicholas was probably using a beta test version of MicroPython. So, I modified his code to compile without errors, and I modified it to run on the Proto-PIC "Micro:Pixel", a 4x8 Neopixel display add-on board for the BBC Micro:bit. The only change to make it run on the Micro:Pixel board was to change the number of neopixels from 30 to 32. The other change was to make a cut and jump on the Micro:Bit board to use Microbit pin8 instead of pin0 to drive the string of NeoPixels. The Proto-PIC website has a note on how to do this on their page that describes the Micro:Pixel board. Below is the code listing (an image file):