In this assignment you will create a "catch the egg" game on your micro:bit in which an egg (represented by a single LED) "falls" from the top of the micro:bit LED display and can be caught by a moveable basket at the bottom.
By default, the first "egg" LED starts to drop from the center of the top line. The basket at the bottom will also be at the center, so the first egg will be very easy to catch. Subsequent "eggs" will then fall from random positions at the top of the display. The basket can be moved by pressing the buttons or by tilting the micro:bit.
You may find slides 95 - 98 of the slide presentation helpful in completing this assignment.
- Your program must use the Python
randint()
function - Your program will use at least 3 variables:
eggX
,eggY
,basketX
- If an egg falls to the bottom of the screen without being caught, the game will display a "Game Over" message
- The position of the egg and the basket will be set with the
display.set_pixel()
function
- Write two
import
statements at the top of your program:from microbit import *
import random
- Declare and initialize a variable
eggX
with a value of 2 - Declare and initialize a variable
eggY
with a value of 0 - Underneath the variable declarations, create a
while True:
loop- Inside the
while
loop calldisplay.clear()
- On the next line, call
display.set_pixel()
to display the egg at positioneggX,eggY
with a value of9
- Set
eggY
to be larger by 1 sleep
for 500 milliseconds
- Inside the
- Run the program, you should see the egg drop. If the egg falls past the bottom of the screen, you should see an error message
- Under the other two variable declarations, declare and initialize a variable
basketX
with a value of 2 - After
display.clear()
, calldisplay.set_pixel()
to display the basket at positionbasketX,4
with a value of4
- Now we'll see if the egg was caught by the basket. Underneath the
sleep(500), create a
ifthat checks if
eggYis equal to
5is equal to
basketX`- If so, set
eggY
to0
- Set
eggX
to a random integer from 0 to 4
- If so, set
- Run the program, you should see the egg drop. When it reaches the basket, it should drop again from a random position at the top of the screen
- After
display.clear()
, add twoif
statements - If
basketX
is greater than 0and
button a was pressed- Set
basketX
to be smaller by 1
- Set
- If
basketX
is less than 4and
button b was pressed- Set
basketX
to be larger by 1
- Set
- Run the program, you should be able to move the basket to catch the egg. If you miss the egg and it falls past the bottom of the screen, you should see an error message
- After
display.clear()
, write anif
statement that checks ifeggY
is greater than 4- if so, use
display.scroll()
to display a game over message
- if so, use
- Directly underneath, add an
else:
- Indent all the code under the
else:
so that the game continues to play only if the egg has not gone off the bottom of the screen - Run the program, you should have the basics of a working game
- Now you can modify the game to add extra features like a score, animations or levels
- Submit the code for your finished program and a short video of it running
You can move the basket with the accelerometer rather than the buttons. You could also keep score of the number of eggs caught and display the score along with the game over message.