Microbit LED Eating Game

by 0714942 in Circuits > Microcontrollers

568 Views, 2 Favorites, 0 Comments

Microbit LED Eating Game

FP2LL6MKVNTLOZL.png

I made a simple game using Python in MakeCode for micro:bit where one LED has to hunt down other LED's to fuel it's endless blood lust before it runs out of brightness, eating other LED's will give it more brightness to keep going.

Supplies

Microsoft MakeCode: https://makecode.microbit.org/#editor

Create a New Micro:bit Project

Screen Shot 2021-11-07 at 11.37.46 pm.png

go to https://makecode.microbit.org/ and create a new project. Give it a good name.

Define Variables

Screen Shot 2021-11-07 at 11.41.21 pm.png

Define all the variables, you can call these what you like I've used the names Pellet_Location and Pellet_Location_Y to determine the random locations of the LED food. You can use the randint function to generate a random number from a range, in this case we use 0 through 4 on the x and y axis which covers every spot on the micro:bit. the rest of these values will be 0 but will change with other functions

Pixel Movement and Controls

Screen Shot 2021-11-07 at 11.48.51 pm.png

we start to write the conditions for when the LED the player controls overlaps with one of the pellets. starting with a forever loop, the rest of this code details that if the B button is pressed that the X value of the pixel should change by one and the variable Start should be changed to 1 to signify that the player has started playing. The last string says that the game should wait 1 millisecond while the B Button is pressed so the previous actions wont happen again instantaneously. then we write the same thing for the Y value and the A button.

Make Sure Your Pixel Doesn't Go Off the Screen

Screen Shot 2021-11-08 at 12.00.19 am.png

These two lines say that if the X or Y =5 than take away 5 so that the pixel will return to the other side of the grid.

What Happens When Your LED Kills a Fellow Brother

Screen Shot 2021-11-08 at 12.04.56 am.png

These lines detail what should happen if the players pixel overlaps with a pellet. A new pellet location is generated with the randint function, a radio signal is sent to the other micro:bit to change the score and the brightness is increased to extend the time you have left represented by the brightness of the grid.

Start the Clock

Screen Shot 2021-11-08 at 12.11.30 am 1.png

This Line makes sure that if the player has started and the timer is triggered than the brightness is lowered this should happen every few milliseconds based on your score, we will set up the timer later this is just what the brightness should do in reaction to the timer you can change how much brightness is lost to change the difficulty.

Game Over

Screen Shot 2021-11-08 at 12.16.11 am.png

This is what happens if brightness = 0 it should set the brightness back to the maximum and show the text string game over. it should then reset the position of the players LED and reset the check for if the player has started yet with the variable "start". It wit will also send an empty string with radio to let the other micro:bit to reset the score. we can end this forever loop now with "basic.forever(on_forever)"

Timer Time

Screen Shot 2021-11-08 at 12.24.31 am.png

in this code it basically says using the score in the mathematical formula is how many milliseconds between repeating the action of triggering the timer. this is the timer that will interact with the code in step 6

Receiving End

Screen Shot 2021-11-08 at 1.16.14 am.png

this is the code that will be executed when the other micro:bit receives the number or empty string. it should be put up at the top of the script. these lines say that when the other micro:bit receives the number 0, it will increase the score by one and show the current score. the second set says that when it receives the empty string it should set the score to 0, show a sad face then show 0.

Have Fun With It

See if you can improve this design or switch to blocks or Javascript to add or modify features this script should fit into 69 lines (nice). here is the working code that I have used.

def on_received_number(receivedNumber):

global Score

basic.pause(1)

basic.clear_screen()

Score += 1

basic.show_number(Score)

led.set_brightness(255)

radio.on_received_number(on_received_number)


def on_received_string(receivedString):

global Score

Score = 0

basic.show_icon(IconNames.SAD)

basic.show_number(0)

radio.on_received_string(on_received_string)


brightness_timer = 0

start = 0

Score = 0

radio.set_group(1)

led.set_brightness(255)

pixel = 0

pixel_y = 0

Pellet_Location = randint(0, 4)

Pellet_Location_Y = randint(0, 4)


def on_forever():

global pixel, start, pixel_y, Pellet_Location, Pellet_Location_Y

led.plot(pixel, pixel_y)

if input.button_is_pressed(Button.B):

led.unplot(pixel, pixel_y)

pixel += 1

start = 1

while input.button_is_pressed(Button.B):

basic.pause(2)

if input.button_is_pressed(Button.A):

led.unplot(pixel, pixel_y)

pixel_y += 1

start = 1

while input.button_is_pressed(Button.A):

basic.pause(2)

if pixel == 5:

pixel += -5

if pixel_y == 5:

pixel_y += -5

if led.brightness() == 0:

led.set_brightness(255)

basic.show_string("Game Over")

pixel_y = 0

pixel = 0

radio.send_string("")

start = 0

if pixel == Pellet_Location and pixel_y == Pellet_Location_Y:

Pellet_Location = randint(0, 4)

Pellet_Location_Y = randint(0, 4)

radio.send_number(0)

led.set_brightness(led.brightness() + 75)

if start == 1 and brightness_timer == 1:

led.set_brightness(led.brightness() - 2)

led.plot(Pellet_Location, Pellet_Location_Y)

basic.forever(on_forever)


def on_every_interval():

global brightness_timer

brightness_timer += 1

basic.pause(1)

brightness_timer = 0

loops.every_interval(100 / (Score * 0.5), on_every_interval)