Bit Commander Game

 

Game Instructions

THIS CODE IS WRITTEN IN MICROPYTHON AND UPLOADED TO THE MICRO:BIT USING Mu.

Turn on Bit:Commander, press button A and B at the same time on the Micro:Bit to start a new game.

A number will be displayed on the Micro:bits screen, you must light up the same amount of NeoPixels using the control stick and press one of the 4 colour coded buttons. The button you press must be the same colour as the NeoPixels. For a correct answer you will get 1 point for an incorrect answer you will get -1 point. If your score is 0 it will not decrement any further therefore 0 is the lowest score you can get.

At the start of the game you will get 2500 ticks each tick is around 50mS giving you a game time of 50 seconds. When you get an answer correct you will be credited with more ticks. For each correct answer you get an extra 36 ticks which is around 1800 mS. Theoretically the game could last indefinitely if you answer the questions quick enough.

If you have a second Micro:Bit you can upload the display code that will display the score or the time or a mixture of both. To swap between time and score press the B button. To swap between the 2 every 5 seconds press the A button. The second Micro:Bit also outputs the score and time via the serial port so you can plug the second Micro:Bit into a computer and display the score and time simultaneously. I have included a processing sketch to show this.

Game Code to be uploaded to the Micro:Bit plugged into the Bit:Commander

THIS CODE IS WRITTEN IN MICROPYTHON AND UPLOADED TO THE MICRO:BIT USING Mu.

[pastacode lang=”python” manual=”%23Micro%3Abit%20game%20designed%20for%20Bit%3ACommander%0A%23Author%20David%20Bradshaw%202018%0A%0Afrom%20microbit%20import%20*%0Aimport%20radio%0Aimport%20random%0Aimport%20music%0Aimport%20neopixel%0A%0Anp%20%3D%20neopixel.NeoPixel(pin13%2C%206)%0A%0Aradio.on()%0Aradio.config(channel%3D33)%0Aradio.config(power%3D7)%0A%0AyVal%20%3D%200%0A%0Abutton_green%20%3D%20pin14%0Abutton_red%20%3D%20pin12%0Abutton_yellow%20%3D%20pin16%0Abutton_blue%20%3D%20pin15%0Adial%20%3D%20pin0%0A%0Adef%20setLEDs(start%2C%20end%2C%20red%2C%20green%2C%20blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(start%2C%20end)%3A%20%20%23%20Start%20of%20for%20loop%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(red%2C%20green%2C%20blue)%20%20%23%20Code%20to%20be%20executed%20in%20the%20loop%0A%20%20%20%20np.show()%0A%0Awhile%20True%3A%0A%20%20%20%20inProgress%20%3D%20False%0A%20%20%20%20isSelecting%20%3D%20False%0A%20%20%20%20isCorrect%20%3D%20False%0A%20%20%20%20ticks%20%3D%200%0A%20%20%20%20score%20%3D%200%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20while%20(inProgress%20%3D%3D%20False)%3A%0A%20%20%20%20%20%20%20%20display.show(Image.SMILE)%0A%20%20%20%20%20%20%20%20if(button_a.is_pressed()%20%3D%3D%20True%20and%20button_b.is_pressed()%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20inProgress%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.POWER_UP)%0A%20%20%20%20%20%20%20%20%20%20%20%20score%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20ticks%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20maxTicks%20%3D%202500%20%23%20about%2050%20seconds%2C%20each%20tick%20around%2050%20mS%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23Game%20loop%0A%20%20%20%20while%20(inProgress%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20target%20%3D%20random.randint(1%2C6)%0A%20%20%20%20%20%20%20%20random.seed(ticks)%0A%20%20%20%20%20%20%20%20display.show(str(target))%0A%20%20%20%20%20%20%20%20value%20%3D%20-1%0A%20%20%20%20%20%20%20%20direction%20%3D%200%0A%20%20%20%20%20%20%20%20targetSelected%20%3D%20False%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20colour%20%3D%20random.randint(0%2C3)%0A%20%20%20%20%20%20%20%20RGB%20%3D%20%5B0%2C0%2C0%5D%0A%20%20%20%20%20%20%20%20%23set%20colour%0A%20%20%20%20%20%20%20%20if(colour%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23red%0A%20%20%20%20%20%20%20%20%20%20%20%20RGB%20%3D%20%5B8%2C0%2C0%5D%0A%20%20%20%20%20%20%20%20elif(colour%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23green%0A%20%20%20%20%20%20%20%20%20%20%20%20RGB%20%3D%20%5B0%2C8%2C0%5D%0A%20%20%20%20%20%20%20%20elif(colour%20%3D%3D%202)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23blue%0A%20%20%20%20%20%20%20%20%20%20%20%20RGB%20%3D%20%5B0%2C0%2C8%5D%0A%20%20%20%20%20%20%20%20elif(colour%20%3D%3D%203)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23yellow%0A%20%20%20%20%20%20%20%20%20%20%20%20RGB%20%3D%20%5B8%2C8%2C0%5D%0A%20%0A%20%20%20%20%20%20%20%20isSelecting%20%3D%20True%20%20%0A%20%20%20%20%20%20%20%20%23colour%20and%20number%20selection%20loop%0A%20%20%20%20%20%20%20%20while(isSelecting%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20targetSelected%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20sleep(1)%0A%20%20%20%20%20%20%20%20%20%20%20%20ticks%20%3D%20ticks%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20txPacket%20%3D%20str(score)%20%2B%20%22%2C%22%20%2B%20str(ticks)%20%2B%20%22%2C%22%20%2B%20str(maxTicks)%0A%20%20%20%20%20%20%20%20%20%20%20%20radio.send(txPacket)%0A%09%09%09print(txPacket)%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20yVal%20%3D%20pin2.read_analog()%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(yVal%20%3E%20512%20and%20yVal%20%3E%2025%20%2B%20512)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20direction%20%3D%200%20%23%200%20%3D%20up%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20yVal%20%3D%20(yVal%20-%20512)%20*%202%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%2025%20%2B%20512%20and%20yVal%20%3E%20512%20-%2025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20yVal%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20direction%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20512)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20direction%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20yVal%20%3D%20yVal%20*%202%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(yVal%20%3C%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20yVal%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%23LED%20light%20up%20stuff%0A%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C6%2C0%2C0%2C0)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if(yVal%20%3C%2020%20and%20direction%20%3D%3D%200)%3A%20%23Stick%20is%20centered%20turn%20lights%20off%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C6%2C0%2C0%2C0)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C1%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C6%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%206%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20400)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C2%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%202%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C5%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%205%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20550)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C3%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%203%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C4%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%204%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20725)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C4%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%204%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C3%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%203%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%20850)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C5%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%205%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C2%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%202%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20(yVal%20%3C%201030)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(direction%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C6%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%206%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C1%2CRGB%5B0%5D%2CRGB%5B1%5D%2CRGB%5B2%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if(value%20%3D%3D%20target)%3A%20%23%20correct%20amount%20of%20lights%20lit%20up%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23now%20check%20the%20buttons%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(colour%20%3D%3D%200%20and%20button_red.read_digital()%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isCorrect%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(colour%20%3D%3D%201%20and%20button_green.read_digital()%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isCorrect%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(colour%20%3D%3D%202%20and%20button_blue.read_digital()%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isCorrect%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(colour%20%3D%3D%203%20and%20button_yellow.read_digital()%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isCorrect%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(isCorrect%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23incorrect%20choice%0A%20%20%20%20%20%20%20%20%20%20%20%20if(button_yellow.read_digital()%20%3D%3D%201%20or%20button_red.read_digital()%20%3D%3D%201%20or%20button_green.read_digital()%20%3D%3D%201%20or%20button_blue.read_digital()%20%3D%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(value%20!%3D%20target)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%20%23%20new%20number%20and%20colour%20is%20chosen%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.JUMP_DOWN)%0A%20%20%20%20%20%20%20%20%20%20%20%20if(button_red.read_digital()%20%3D%3D%201%20and%20colour%20!%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%20%23%20new%20number%20and%20colour%20is%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.JUMP_DOWN)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif(button_green.read_digital()%20%3D%3D%201%20and%20colour%20!%3D%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%20%23%20new%20number%20and%20colour%20is%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.JUMP_DOWN)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif(button_blue.read_digital()%20%3D%3D%201%20and%20colour%20!%3D%202)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%20%23%20new%20number%20and%20colour%20is%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.JUMP_DOWN)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif(button_yellow.read_digital()%20%3D%3D%201%20and%20colour%20!%3D%203)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%20%23%20new%20number%20and%20colour%20is%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.JUMP_DOWN)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(ticks%20%3E%20maxTicks)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20inProgress%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2C6%2C0%2C0%2C0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.PUNCHLINE)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.scroll(str(score)%2C%20200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20isSelecting%20%3D%20False%0A%0A%20%20%20%20%20%20%20%20if(isCorrect%20%3D%3D%20False)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20score%20%3D%20score%20-%201%0A%20%20%20%20%20%20%20%20%20%20%20%20if(score%20%3C%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20%3D%200%0A%0A%20%20%20%20%20%20%20%20if(isCorrect%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20score%20%3D%20score%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20setLEDs(0%2Cvalue%2C12%2C12%2C12)%0A%20%20%20%20%20%20%20%20%20%20%20%20music.play(music.BA_DING)%0A%20%20%20%20%20%20%20%20%20%20%20%20isCorrect%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20maxTicks%20%3D%20maxTicks%20%2B%2036%20%23add%2036%20ticks%20for%20correct%20answer%201800%20mS” message=”Bit:Commander Game” highlight=”” provider=”manual”/]

Be aware that the above code uses 99% of the memory on the Micro:Bit so if you modify it you could have memory issues. Timings for the code are relative and not absolute this means that you have 2500 ticks at the start of the game however because when music is played and I use the sleep command it will take longer than 50 seconds for the game to complete. the sleep and music command are blocking commands that stop the Micro:Bit from carrying out other tasks while their executing. This can be demonstrated in the below video; notice how the time does not decrease because the Bit:Commander is busy playing the wrong answer tune.

This was an intentional design decision to demonstrate the affects of having your game loop bound to your logic and in the case of a computer game your render function. For a computer game it could mean that the faster your graphics card and frame rate the quicker the game clock runs. This was seen in Codemasters F1 2012, I believe that during online races people with better GPU’s were reporting better laptimes. In the world of robotics this could mean that your robot is not very responsive making it sluggish or causing it to crash.

Display Code to be Uploaded to the Second Micro:Bit

THIS CODE IS WRITTEN IN MICROPYTHON AND UPLOADED TO THE MICRO:BIT USING Mu.

[pastacode lang=”python” manual=”%23Remote%20display%20for%20micro%3Abit%20game%0A%23Author%20David%20Bradshaw%202018%0A%0Afrom%20microbit%20import%20*%0Aimport%20radio%0A%0Aradio.on()%0Aradio.config(channel%3D33)%0Aradio.config(power%3D7)%0A%0Awhile%20True%3A%0A%20%20%20%20score%20%3D%20’0’%0A%20%20%20%20time%20%3D%201%0A%20%20%20%20maxTime%20%3D%201%0A%20%20%20%20display.show(score)%0A%20%20%20%20mode%20%3D%200%20%23%200%20%3D%20score%20mode%2C%201%20%3D%20time%20mode%2C%202%20%3D%20both%205%20second%20delay%0A%20%20%20%20subMode%20%3D%200%0A%20%20%20%20%0A%20%20%20%20currentTicks%20%3D%200%0A%20%20%20%20lastTicks%20%3D%200%0A%20%20%20%20incomingList%20%20%3D%20%5B%220%22%2C%220%22%2C%220%22%5D%0A%20%20%20%20%0A%20%20%20%20while%20(-1)%3A%0A%20%20%20%20%20%20%20%20if(button_a.is_pressed())%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%20%3D%202%0A%20%20%20%20%20%20%20%20%20%20%20%20sleep(750)%0A%20%20%20%20%20%20%20%20if(button_b.is_pressed())%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if(mode%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sleep(750)%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sleep(750)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20sleep(30)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20incoming%20%3D%20radio.receive()%0A%20%20%20%20%20%20%20%20%20%20%20%20elapsedPercent%20%3D%20(int(time))%20%2F%20int(maxTime)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20incoming%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20incomingList%20%3D%20incoming.split(‘%2C’)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(incoming)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20%3D%20incomingList%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20time%20%3D%20incomingList%5B1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maxTime%20%3D%20incomingList%5B2%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elapsedPercent%20%3D%20(int(time))%20%2F%20int(maxTime)%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if(mode%20%3D%3D%202)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20currentTicks%20%3D%20int(incomingList%5B1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(currentTicks%20-%20lastTicks%20%3E%20360)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lastTicks%20%3D%20currentTicks%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(subMode%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20subMode%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20subMode%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(mode%20%3D%3D%200%20or%20(mode%20%3D%3D%202%20and%20subMode%20%3D%3D%200))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(int(score)%20%3C%2010%20and%20int(score)%20%3E%20-10)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(score)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(int(score)%20%3C%20-10)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.scroll(score%2C%20100)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.scroll(score%2C%20100)%0A%20%20%20%20%20%20%20%20%20%20%20%20if(mode%20%3D%3D%201%20or%20(mode%20%3D%3D%202%20and%20subMode%20%3D%3D%201))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(elapsedPercent%20%3C%200.04)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘09999%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.08)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00999%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.12)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00099%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.16)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00009%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.24)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A09999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.28)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.32)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00099%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.36)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00009%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.40)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.44)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A09999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.48)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.52)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00099%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.56)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00009%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.60)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.64)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A09999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.68)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00999%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.72)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00099%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.76)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00009%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.80)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A99999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.84)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A09999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.88)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A00999’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.92)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A00099’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%200.96)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A00009’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif(elapsedPercent%20%3C%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘00000%3A00000%3A00000%3A00000%3A00000’))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image(‘99999%3A99999%3A99999%3A99999%3A99999’))%0A%20%20%20%20%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20pass” message=”Remote Display Code” highlight=”” provider=”manual”/]

Processing Application

THIS CODE IS WRITTEN IN JAVA AND EXECUTED USING PROCESSING 3

Processing is a great application where you can receive data from your project and display it in a meaningful way on you computer screen or phone. It can be downloaded from the following link;

https://processing.org/download/support.html

Applications are written in Java and it’s a great tool to display telemetry data from robots and to send commands. I have included a simple program below that will enable you to connect the display or game Micro:Bit to your computer and have both the time and score displayed in real time. You could expand the program to include stats such as reaction time and save high scores to a database or text file.

The code for this processing application is shown below;

[pastacode lang=”python” manual=”import%20processing.serial.*%3B%0A%0ASerial%20mySerial%3B%0A%0Aint%20score%20%3D%200%3B%0Aint%20ticks%20%3D%200%3B%0Aint%20maxTicks%20%3D%200%3B%0AString%20incomingData%20%3D%20null%3B%0Aint%20CR%20%3D%2010%3B%20%2F%2FCarage%20return%0A%0Avoid%20setup()%0A%7B%0A%20%20size(400%2C200)%3B%0A%20%20String%20myPort%20%3D%20Serial.list()%20%5B2%5D%3B%20%2F%2Fchange%20this%20for%20your%20serial%20port%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fthis%20is%20comm%20port%205%20for%20me%20not%20sure%20why!!!!%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fremember%20the%20display%20will%20only%20transmit%20serial%20data%20when%20a%20game%20is%20in%20progress!!!!%0A%20%20mySerial%20%3D%20new%20Serial(this%2C%20myPort%2C%20115200)%3B%0A%20%20PFont%20f%20%3D%20createFont(%22Georgia%22%2C%2064)%3B%0A%20%20textFont(f)%3B%0A%20%20textSize(32)%3B%0A%20%20text(%22Game%20Not%20Started%22%2C%200%2C%2050)%3B%0A%7D%0A%0Avoid%20draw()%0A%7B%0A%0A%0A%20%20%0A%20%20while(mySerial.available()%20%3E%200)%0A%20%20%7B%0A%20%20%20%20incomingData%20%3D%20mySerial.readStringUntil(CR)%3B%0A%20%20%20%20%0A%20%20%20%20if%20(incomingData%20!%3D%20null)%20%2F%2FSerial%20port%20has%20data%20on%20it%20lets%20process%20it%0A%20%20%20%20%7B%0A%20%20%20%20%20%20background(0)%3B%20%2F%2Fupdate%20background%0A%20%20%20%20%20%20String%5B%5D%20data%20%3D%20split(incomingData%2C%20’%2C’)%3B%0A%20%20%20%20%20%20Double%20timeLeft%20%3D%200.0d%3B%0A%20%20%20%20%20%20Double%20a%20%3D%200.0d%3B%0A%20%20%20%20%20%20Double%20b%20%3D%200.0d%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20try%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20data%5B2%5D%20%3D%20data%5B2%5D.replaceAll(%22%5C%22%22%2C%223%22)%3B%0A%20%20%20%20%20%20%20%20a%20%3D%20Double.parseDouble(data%5B2%5D)%3B%0A%20%20%20%20%20%20%20%20b%20%3D%20Double.parseDouble(data%5B1%5D)%3B%0A%20%20%20%20%20%20%20%20timeLeft%20%3D%20(a%20-%20b)%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20catch%20(Exception%20e)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20println(e)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20timeLeft%20%3D%20(timeLeft%20*%2020)%20%2F%201000%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20text(%22Score%22%2C%200%2C%2050)%3B%0A%20%20%20%20%20%20text(%22Time%20Left%22%2C%20200%2C%2050)%3B%0A%20%20%20%20%20%20text(data%5B0%5D%2C%2025%2C%20100)%3B%0A%20%20%20%20%20%20text(Double.toString(timeLeft)%2C%20220%2C%20100)%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20%0A%7D” message=”Processing App for your Computer” highlight=”” provider=”manual”/]

PLEASE NOTE: You will have to amend line 14 with the COMM port that you’re using on your computer.

All the code from this example can be downloaded from BC_Game.zip or the resources section.