Micro:bit Snake Game
Build the classic snake game from scratch.
YOU WILL NEED
- 1 x BBC micro:bit
- 1 x Micro USB Cable
In this tutorial, we will build the classic snake game from scratch.
We will handle
(iii) win and lose conditions
(iv) drawing the game board
The snake and its food will appear on the micro:bit LEDs. Press button 'A' to turn the snake left and 'B' to turn right. But be careful so the snake doesn't eat itself!
Coding in Python
(a) To code in Python for micro:bit, we will use the official micro:bit Python editor.
(b) On your laptop, go to https://python.microbit.org/v/2.0, the official micro:bit Python editor. You can delete the automatic sample code on the page.
To code this game, we will need to import 2 libraries.
(a) Import the default micro:bit library. We will always need to import this when writing code for the micro:bit.
(b) Import randint from the random library to generate random numbers for the food's location.
Initialise Snake and Food
(a) Create a list 'snake'. As the snake will contain more than one point, it is a list of points on the micro:bit LEDs. Each point is represented by a list [x,y]. x represents the column and y represents the row.
(b) Initialise it as [2,2] on start to begin at the centre. More points will be appended to this list later.
(c) Initialise the food as a randomly positioned single pixel.
(a) Create a list 'directions' to store the 4 directions.
• Each direction is represented by increase/decrease in column or row.
• eg. right is represented by [1, 0] – increase in column by 1, no increase in row.
• [1,0] is the first option in the list, thus the snake moves right by default.
• To turn left, go to the next direction in the list (right -> up -> left -> down -> right).
(b) Set 'direction' to 0 on start.
Create Main Loop
(a) Create a 'while True:' loop. This is a loop that repeats infinitely, much like the 'forever' loop in MakeCode, until it is broken with the 'break' command.
(b) All subsequent code from this step onwards will be indented under this main loop so they will always be true and always repeated. Don't forget to indent them!
Display Snake and Food
(a) Clear any display on the micro:bit LEDs.
(b) Light up the food particle pixel at maximum brightness - set the x and y coordinates to random integers stored in 'food', and set the brightness to 9 (max).
(c) Loop through the 'snake' list and draw every pixel at medium brightness. 'brightness' stores the brightness value of these pixels, and int() converts this to an integer.
(d) Pause the program for 800ms (0.8s) before redrawing the screen.
Run the Code
(a) Let's check our code! Plug the micro USB cable from your laptop into the top of the micro:bit.
(b) On the Python editor site, click the 'Connect' icon, select your micro:bit and click 'Connect'.
(c) Click the 'Flash' icon. Wait for the orange light to stop flashing on the micro:bit.
(d) There should be 2 pixels lit up on the micro:bit LED screen - the snake and food.
(e) Pressing the reset button (back of micro:bit) will move the food to a different position.
Turn Left and Right
(a) To control the snake's movement (turn left and right), use the buttons on the micro:bit.
(b) If button A on the micro:bit is pressed, either reset 'direction' or subtract 1 to turn the snake left by 90º.
(c) In a new if statement, check if button B is pressed. If it is, reset 'direction' or add 1 to turn the snake right by 90º.
(d) Pause for 100ms (0.1s) before continuing.
Moving the Snake
(a) Create a list, 'next_block' to store the 'x' and 'y' coordinates of the next pixel for the snake to move to.
(b) This is equals to the current position of the start of the snake, plus direction (in terms of row and column), then calculating modulo (%) 5 of this to overflow the calculated value back to 0 when it reaches 5. This allows the snake to stay within the 5x5 LED screen of the micro:bit.
(a) However if 'next_block' is already occupied by the body of the snake, the snake will collide into itself. End the game with 'break'.
(b) Otherwise, 'next_block' should be the new head of the snake.
(c) If 'next_block' coincides with 'food', the snake has eaten the food. Generate a new piece of 'food'. As '[next_block]' is added to 'snake', the snake grows longer after eating.
(d) If the snake has not eaten, remove the snake's tail so it moves but does not grow longer.
Win the Game!
(a) One last thing - setting up the winning condition so the player can win! Add this below the turning code (step 8), above the 'next_block' code (step 9).
(b) When the snake is longer than 6 pixels, the player wins and 'break' stops the 'while True' loop from running further, ending the game.
(c) Feel free to customise this winning condition to make it easier or more difficult for the player!
(a) And we're done, congratulations! Don't forget to connect your micro:bit to your laptop and download the code to your micro:bit, like in step 7.
(b) Hold the buttons down for about 1-2 seconds when playing (don’t press and release immediately) to turn the snake left / right.
(c) We hope you've learnt more about Python and coding the micro:bit with Python.
(d) Enjoy playing!
Downloads: Snake Game.hex Snake Game.py
Made this activity? Good Job! Now spread the word. Share a photo of your creation on social media with #letsgethacking #Micro:bitSnakeGame