Dodge the lights game

Build a game using javascript in makecode.microbit.org


You'll need

1 x Micro:bit
1 x Breakout board
1 x potentiometer
1 x OLED screen
1 x ADkeyboard

Step 1

Instructions

Connect the micro:bit to the breakout board, ensure that the orientation is correct

Connect the ADkeyboard to pin 1 (bumpy end to the ADkeyboard, smooth end to the breakout board)

Connect the potentiometer to pin 2 (bumpy end to the ADkeys, smooth end to the breakout board)

Connect the OLED screen to the I2C pins:
GND to GND, SDA to SDA




Materials

1 x potentiometer
1 x OLED screen
1 x ADkeyboard
1 x micro:bit
1 x breakout board


Step 2

Instructions

Go to: makecode.microbit.org

Create a new project, and go to javascript




Materials


Step 3

Instructions

Go to extensions, search for "tinker" and click on the "tinkercademy-tinker-kit" to download it




Materials


Step 4

Instructions

For the next few steps, the code will be shown in the image in each step, do remember to click on the image to see the full code for each step, and not miss out any part of the code

First, we will be defining the variables and arrays as seen in the picture, then initialising the OLED and displaying something on both the micro:bit and OLED to show that both are working

If your OLED screen does not display the message as clearly as seen in the cover photo for this tutorial, you may need to change to another OLED.




Materials


Step 5

Instructions

The next few steps will involve creating functions.
Why functions? So we don't need to keep rewriting the same code each time we need to use it, in this game, we will be reusing many functions multiple times.

The first two functions as seen in the image above controls the player movement when the function is called.




Materials


Step 6

Instructions

Next, create this function which randomises the LEDs that are lit on the first row




Materials


Step 7

Instructions

The next few functions will be moving the LED lights down the column of the micro:bit LED screen

These two functions help to move the lights from the first row to the third and fifth row of the LED screen




Materials


Step 8

Instructions

The function "startSecondRow" moves the LED from row 1 to row 2 on the LED screen, while the "unplotSecondRow" function unplots any LED found on the second row




Materials


Step 9

Instructions

These two functions are similar to the last two, except that they are for the third and fourth rows




Materials


Step 10

Instructions

Since the functions "startSecondRow" and "startFourthRow" already cleared the first and third row respectively, we still need one more function to clear the fifth row




Materials


Step 11

Instructions

This function will start the game, first by allowing the player to choose the difficulty of the game, the difficulty is controlled by the "delay", the lower the value, the harder the game. The options will be visible on the OLED screen for 2 seconds (you may extend it), and the difficulty can be chosen by pressing the correct button on the ADkeyboard




Materials


Step 12

Instructions

Still under the function "initialise", the code as seen above calls a series of functions that you have created previously in a particular order, to start the game. The final function to be called for in this "initialise" function, "startGame", will be created in the next step.




Materials


Step 13

Instructions

This function will be the main one running throughout the game.

Firstly, there is a "while" loop to check if the player managed to dodge the LED lights on the screen.

The 2 "if" statements thereafter prevents the player from cheating by by continuously pressing the "A" or "B" key. (The "break" command stops the "while" loop from running, and moves on to other code in the function)

A few functions are called after a delay, and the if statements are used again.

The comments (indicated by "//") are not neccessary but explains the reason for the 3 if statements.




Materials


Step 14

Instructions

Still in the "while" loop in the "startGame" function, there is yet another delay and a series of functions called

Thereafter, one point is added to the "counter"(note: counter += 1 is same as counter = counter + 1)

An if statement then checks if the "counter" value is divisible by 20 (this is done by checking if the remainder when counter is divided by 20 equals 0), if so, the difficulty is increased by lowering the value of "delay"

The "while" loop is closed, and the code outside of the loop displays the "counter" value (which happens when the "while" loop stops after certain criteria as set up by the if statements or the "while" loop are met) on both the microbit LED screen and the OLED screen




Materials


Step 15

Instructions

This is the basic.forever function that is part of micro:bit makecode, and runs in the background.

Firstly, Pin 2 where the potentiometer is has its value mapped (since potentiometer outputs 0 to 1023, LED brightness can only be from 0 to 255) Than the brightness is set based on input from the potentiometer

Next, the buttons on the ADkeyboard are used as controls using if statements to check if the buttons have been pressed, resulting in certain functions called, allowing the player to have some control in the game

In case your code doesn't work for whatever reason, here the link to the code I used: goo.gl/pTYR5q




Materials


Step 16

Instructions

You have completed the project!

Press the "C" and "E" key simultaneously to start, follow the instructions on the screen to set the diffculty, and use the "A" and "B" key to make the player move left and right to dodge the LED lights.




Materials


Step 17

Instructions

Connect the micro:bit to the breakout board, ensure that the orientation is correct

Connect the ADkeyboard to pin 1 (bumpy end to the ADkeyboard, smooth end to the breakout board)

Connect the potentiometer to pin 2 (bumpy end to the ADkeys, smooth end to the breakout board)

Connect the OLED screen to the I2C pins:
GND to GND, SDA to SDA




Materials

1 x potentiometer
1 x OLED screen
1 x ADkeyboard
1 x micro:bit
1 x breakout board


Step 18

Instructions

Go to: makecode.microbit.org

Create a new project, and go to javascript




Materials


Step 19

Instructions

Go to extensions, search for "tinker" and click on the "tinkercademy-tinker-kit" to download it




Materials


Step 20

Instructions

For the next few steps, the code will be shown in the image in each step, do remember to click on the image to see the full code for each step, and not miss out any part of the code

First, we will be defining the variables and arrays as seen in the picture, then initialising the OLED and displaying something on both the micro:bit and OLED to show that both are working

If your OLED screen does not display the message as clearly as seen in the cover photo for this tutorial, you may need to change to another OLED.




Materials


Step 21

Instructions

The next few steps will involve creating functions.
Why functions? So we don't need to keep rewriting the same code each time we need to use it, in this game, we will be reusing many functions multiple times.

The first two functions as seen in the image above controls the player movement when the function is called.




Materials


Step 22

Instructions

Next, create this function which randomises the LEDs that are lit on the first row




Materials


Step 23

Instructions

The next few functions will be moving the LED lights down the column of the micro:bit LED screen

These two functions help to move the lights from the first row to the third and fifth row of the LED screen




Materials


Step 24

Instructions

The function "startSecondRow" moves the LED from row 1 to row 2 on the LED screen, while the "unplotSecondRow" function unplots any LED found on the second row




Materials


Step 25

Instructions

These two functions are similar to the last two, except that they are for the third and fourth rows




Materials


Step 26

Instructions

Since the functions "startSecondRow" and "startFourthRow" already cleared the first and third row respectively, we still need one more function to clear the fifth row




Materials


Step 27

Instructions

This function will start the game, first by allowing the player to choose the difficulty of the game, the difficulty is controlled by the "delay", the lower the value, the harder the game. The options will be visible on the OLED screen for 2 seconds (you may extend it), and the difficulty can be chosen by pressing the correct button on the ADkeyboard




Materials


Step 28

Instructions

Still under the function "initialise", the code as seen above calls a series of functions that you have created previously in a particular order, to start the game. The final function to be called for in this "initialise" function, "startGame", will be created in the next step.




Materials


Step 29

Instructions

This function will be the main one running throughout the game.

Firstly, there is a "while" loop to check if the player managed to dodge the LED lights on the screen.

The 2 "if" statements thereafter prevents the player from cheating by by continuously pressing the "A" or "B" key. (The "break" command stops the "while" loop from running, and moves on to other code in the function)

A few functions are called after a delay, and the if statements are used again.

The comments (indicated by "//") are not neccessary but explains the reason for the 3 if statements.




Materials


Step 30

Instructions

You have completed the project!

Press the "C" and "E" key simultaneously to start, follow the instructions on the screen to set the diffculty, and use the "A" and "B" key to make the player move left and right to dodge the LED lights.




Materials


Step 31

Instructions

Still in the "while" loop in the "startGame" function, there is yet another delay and a series of functions called

Thereafter, one point is added to the "counter"(note: counter += 1 is same as counter = counter + 1)

An if statement then checks if the "counter" value is divisible by 20 (this is done by checking if the remainder when counter is divided by 20 equals 0), if so, the difficulty is increased by lowering the value of "delay"

The "while" loop is closed, and the code outside of the loop displays the "counter" value (which happens when the "while" loop stops after certain criteria as set up by the if statements or the "while" loop are met) on both the microbit LED screen and the OLED screen




Materials


Step 32

Instructions

This is the basic.forever function that is part of micro:bit makecode, and runs in the background.

Firstly, Pin 2 where the potentiometer is has its value mapped (since potentiometer outputs 0 to 1023, LED brightness can only be from 0 to 255) Than the brightness is set based on input from the potentiometer

Next, the buttons on the ADkeyboard are used as controls using if statements to check if the buttons have been pressed, resulting in certain functions called, allowing the player to have some control in the game

In case your code doesn't work for whatever reason, here the link to the code I used: goo.gl/pTYR5q




Materials


Step 33

Instructions

Connect the micro:bit to the breakout board, ensure that the orientation is correct

Connect the ADkeyboard to pin 1 (bumpy end to the ADkeyboard, smooth end to the breakout board)

Connect the potentiometer to pin 2 (bumpy end to the ADkeys, smooth end to the breakout board)

Connect the OLED screen to the I2C pins:
GND to GND, SDA to SDA




Materials

1 x potentiometer
1 x OLED screen
1 x ADkeyboard
1 x micro:bit
1 x breakout board


Step 34

Instructions

Go to: makecode.microbit.org

Create a new project, and go to javascript




Materials


Step 35

Instructions

For the next few steps, the code will be shown in the image in each step, do remember to click on the image to see the full code for each step, and not miss out any part of the code

First, we will be defining the variables and arrays as seen in the picture, then initialising the OLED and displaying something on both the micro:bit and OLED to show that both are working

If your OLED screen does not display the message as clearly as seen in the cover photo for this tutorial, you may need to change to another OLED.




Materials


Step 36

Instructions

Go to extensions, search for "tinker" and click on the "tinkercademy-tinker-kit" to download it




Materials


Step 37

Instructions

The next few steps will involve creating functions.
Why functions? So we don't need to keep rewriting the same code each time we need to use it, in this game, we will be reusing many functions multiple times.

The first two functions as seen in the image above controls the player movement when the function is called.




Materials


Step 38

Instructions

Next, create this function which randomises the LEDs that are lit on the first row




Materials


Step 39

Instructions

The next few functions will be moving the LED lights down the column of the micro:bit LED screen

These two functions help to move the lights from the first row to the third and fifth row of the LED screen




Materials


Step 40

Instructions

Since the functions "startSecondRow" and "startFourthRow" already cleared the first and third row respectively, we still need one more function to clear the fifth row




Materials


Step 41

Instructions

The function "startSecondRow" moves the LED from row 1 to row 2 on the LED screen, while the "unplotSecondRow" function unplots any LED found on the second row




Materials


Step 42

Instructions

These two functions are similar to the last two, except that they are for the third and fourth rows




Materials


Step 43

Instructions

This function will start the game, first by allowing the player to choose the difficulty of the game, the difficulty is controlled by the "delay", the lower the value, the harder the game. The options will be visible on the OLED screen for 2 seconds (you may extend it), and the difficulty can be chosen by pressing the correct button on the ADkeyboard




Materials


Step 44

Instructions

Still under the function "initialise", the code as seen above calls a series of functions that you have created previously in a particular order, to start the game. The final function to be called for in this "initialise" function, "startGame", will be created in the next step.




Materials


Step 45

Instructions

This function will be the main one running throughout the game.

Firstly, there is a "while" loop to check if the player managed to dodge the LED lights on the screen.

The 2 "if" statements thereafter prevents the player from cheating by by continuously pressing the "A" or "B" key. (The "break" command stops the "while" loop from running, and moves on to other code in the function)

A few functions are called after a delay, and the if statements are used again.

The comments (indicated by "//") are not neccessary but explains the reason for the 3 if statements.




Materials


Step 46

Instructions

Still in the "while" loop in the "startGame" function, there is yet another delay and a series of functions called

Thereafter, one point is added to the "counter"(note: counter += 1 is same as counter = counter + 1)

An if statement then checks if the "counter" value is divisible by 20 (this is done by checking if the remainder when counter is divided by 20 equals 0), if so, the difficulty is increased by lowering the value of "delay"

The "while" loop is closed, and the code outside of the loop displays the "counter" value (which happens when the "while" loop stops after certain criteria as set up by the if statements or the "while" loop are met) on both the microbit LED screen and the OLED screen




Materials


Step 47

Instructions

You have completed the project!

Press the "C" and "E" key simultaneously to start, follow the instructions on the screen to set the diffculty, and use the "A" and "B" key to make the player move left and right to dodge the LED lights.




Materials


Step 48

Instructions

This is the basic.forever function that is part of micro:bit makecode, and runs in the background.

Firstly, Pin 2 where the potentiometer is has its value mapped (since potentiometer outputs 0 to 1023, LED brightness can only be from 0 to 255) Than the brightness is set based on input from the potentiometer

Next, the buttons on the ADkeyboard are used as controls using if statements to check if the buttons have been pressed, resulting in certain functions called, allowing the player to have some control in the game

In case your code doesn't work for whatever reason, here the link to the code I used: goo.gl/pTYR5q




Materials


Step 49

Instructions

Connect the micro:bit to the breakout board, ensure that the orientation is correct

Connect the ADkeyboard to pin 1 (bumpy end to the ADkeyboard, smooth end to the breakout board)

Connect the potentiometer to pin 2 (bumpy end to the ADkeys, smooth end to the breakout board)

Connect the OLED screen to the I2C pins:
GND to GND, SDA to SDA




Materials

1 x potentiometer
1 x OLED screen
1 x ADkeyboard
1 x micro:bit
1 x breakout board


Step 50

Instructions

Go to: makecode.microbit.org

Create a new project, and go to javascript




Materials


Step 51

Instructions

Go to extensions, search for "tinker" and click on the "tinkercademy-tinker-kit" to download it




Materials


Step 52

Instructions

For the next few steps, the code will be shown in the image in each step, do remember to click on the image to see the full code for each step, and not miss out any part of the code

First, we will be defining the variables and arrays as seen in the picture, then initialising the OLED and displaying something on both the micro:bit and OLED to show that both are working

If your OLED screen does not display the message as clearly as seen in the cover photo for this tutorial, you may need to change to another OLED.




Materials


Step 53

Instructions

The next few steps will involve creating functions.
Why functions? So we don't need to keep rewriting the same code each time we need to use it, in this game, we will be reusing many functions multiple times.

The first two functions as seen in the image above controls the player movement when the function is called.




Materials


Step 54

Instructions

Next, create this function which randomises the LEDs that are lit on the first row




Materials


Step 55

Instructions

The next few functions will be moving the LED lights down the column of the micro:bit LED screen

These two functions help to move the lights from the first row to the third and fifth row of the LED screen




Materials


Step 56

Instructions

The function "startSecondRow" moves the LED from row 1 to row 2 on the LED screen, while the "unplotSecondRow" function unplots any LED found on the second row




Materials


Step 57

Instructions

These two functions are similar to the last two, except that they are for the third and fourth rows




Materials


Step 58

Instructions

Since the functions "startSecondRow" and "startFourthRow" already cleared the first and third row respectively, we still need one more function to clear the fifth row




Materials


Step 59

Instructions

This function will start the game, first by allowing the player to choose the difficulty of the game, the difficulty is controlled by the "delay", the lower the value, the harder the game. The options will be visible on the OLED screen for 2 seconds (you may extend it), and the difficulty can be chosen by pressing the correct button on the ADkeyboard




Materials


Step 60

Instructions

Still under the function "initialise", the code as seen above calls a series of functions that you have created previously in a particular order, to start the game. The final function to be called for in this "initialise" function, "startGame", will be created in the next step.




Materials


Step 61

Instructions

This function will be the main one running throughout the game.

Firstly, there is a "while" loop to check if the player managed to dodge the LED lights on the screen.

The 2 "if" statements thereafter prevents the player from cheating by by continuously pressing the "A" or "B" key. (The "break" command stops the "while" loop from running, and moves on to other code in the function)

A few functions are called after a delay, and the if statements are used again.

The comments (indicated by "//") are not neccessary but explains the reason for the 3 if statements.




Materials


Step 62

Instructions

Still in the "while" loop in the "startGame" function, there is yet another delay and a series of functions called

Thereafter, one point is added to the "counter"(note: counter += 1 is same as counter = counter + 1)

An if statement then checks if the "counter" value is divisible by 20 (this is done by checking if the remainder when counter is divided by 20 equals 0), if so, the difficulty is increased by lowering the value of "delay"

The "while" loop is closed, and the code outside of the loop displays the "counter" value (which happens when the "while" loop stops after certain criteria as set up by the if statements or the "while" loop are met) on both the microbit LED screen and the OLED screen




Materials


Step 63

Instructions

This is the basic.forever function that is part of micro:bit makecode, and runs in the background.

Firstly, Pin 2 where the potentiometer is has its value mapped (since potentiometer outputs 0 to 1023, LED brightness can only be from 0 to 255) Than the brightness is set based on input from the potentiometer

Next, the buttons on the ADkeyboard are used as controls using if statements to check if the buttons have been pressed, resulting in certain functions called, allowing the player to have some control in the game

In case your code doesn't work for whatever reason, here the link to the code I used: goo.gl/pTYR5q




Materials


Step 64

Instructions

You have completed the project!

Press the "C" and "E" key simultaneously to start, follow the instructions on the screen to set the diffculty, and use the "A" and "B" key to make the player move left and right to dodge the LED lights.




Materials


Code and References

Completed this tutorial?

Spread the word! Let's get this trending on social media with #letsgethacking #Dodgethelightsgame