This project makes a simple fun game with the LED screen, by using the key knowledge of lists operations. It can be used as a practical learning step after getting basic understanding of lists. Goals:
Basic operation of lists, add and remove element
Use count-controlled for-looping to access list elements
Comprehensive practice of logical thinking and structural designing
START A BIT > Projects Library > This page
Here is the demo of the basic requirement.
This project uses 1 Micro:bit board with a USB cable, nothing else needed.
Basic
Shake to randomly add dot on the screen.
Add the dot's (x, y) coordinates to lists, replay the dots in orders when pressing LOGO.
Advanced
Detect collided dot, warn a sound, avoid adding duplicated dot.
When the screen is fully filled, stop generating or adding dots, just show celebration icon
Challenges (Optional)
When detecting a collided dot, automatically retry generating new dot, and add it
Use 1 list to store transformed (x, y)
[x] Simple: 30-60 minutes
[ ] Moderate: 1-2 hours
[ ] Challenging: more than 2 hours
The estimation bases on average situation without unexpected troubleshooting, assuming that the student meets prerequisites. It only indicates rough time needed to complete this project, but not about technical difficulty, not covering discussion and sharing time. Here is a possible breakdown:
5 minutes: Understand requirement and solution, collect materials
20-30 minutes: Basic features
20-30 minutes: Advanced features
The challenge topic is not included in effort estimation, it could take extra 30-50 minutes for that part.
One should have prior knowledge
Micro:bit V2 board: Know what they are, better to have prior experience with it
MakeCode by Microsoft, the programming tool, better to have prior experience using it with Micro:bit
It's better to have done PJ017: Debug The List Changes And Find The Max first, but not a must
1x Micro:bit V2 board , with USB cable
List v.s. Array
Index
Elements/Objects/Values
On-shake:
Generate random coordinate of a dot
Show the dot on LED screen
Store this (x, y) coordinate to list(s)
On-logo:
Show the dots one by one in their born orders on the LED screen
The key technique of this basic feature is using list(s) to store the (x, y) coordinates.
It's simple and straightforward logic to use 2 lists, to store values of x and y separately, please keep in mind that you must ensure the 2 lists are always "synchronized".
The "replay" is basically showing the added dots one by one in their born orders.
On-shake (Improved logic with duplication detection):
Generate random coordinate of a dot, check if this dot is already existed
If existed:
Play a warning sound
Blink this dot one time on the LED screen
Don't store this dot (x, y) to list
If not existed:
Show the dot on LED screen
Store this (x, y) coordinate to list(s)
The purpose of this task is to practice using for-looping to access elements in the list, and a structural logical designing practice.
There are many ways to do so, one of the solutions:
Go through all the values from the list using for-looping, compare with the new generate number, if yes, then a duplication is found
Use "break" to jump out of the looping when it's found already
No lazy working copy here, please solve it.
It's smart to use a variable, e.g. "duplicated" to mark the result of duplication checking, then based on this flag to handle the branches later, instead of filling in mixing logic inside the duplication checking codes.
This technique "2-steps" logical structure, i.e.
Step 1: Check duplication, only focus on the logic of duplication checking, don't handle the next step actions too soon, but mark a checking result.
Step 2: Based on the checking result, focus on handling the next steps actions
Keep previous logic, add a little extra codes to
If the LED screen is fully filled, stop the game (no more new dots or coordinates creation), but show a happy face as celebration
The purpose of this task is to practice using variables, and keep structures of all logic blocks clearly.
The basic idea could be using a variable, e.g. "count" to remember to dots created. Keep in mind that the "count" should be increased only when there is no "duplication", meaning that it only counts the new dots.
No lazy working copy here, please figure out the codes.
Keep all other functions working, but improve this part:
On-shake (Improved logic with duplication detection and auto retry):
Generate random coordinate of a dot, check if this dot is already existed
If existed:
Play a warning sound
Blink this dot one time on the LED screen
Retry to generate a new (x, y), come back to the beginning of duplication checking
If not existed:
Show the dot on LED screen
Store this (x, y) coordinate to list(s)
The purpose of this task is to practice using for-looping to access elements in the list, using functions to keep the code well organized, and a structural logical designing practice.
In this challenge, the "duplication checking" and "random (x, y) generating" will be triggered for more than 1 time, then it's necessary to improve the code structure to group those parts as Functions for better logic readability and reusing.
Example function:
generate_random_x_y
Example function:
generate_random_x_y
This example uses a "global" variable "duplicated" to pass the checking result back to the main logic flow, just as a simpler example to avoid "return" value.
A better implementation of this function should be self-contained, check the duplication and return the result from the function, instead of relying on global variable to tell the caller.
The algorithm is shown in the "Knowledge Preparations" part, please explore and figure out the codes for this challenge.
Stand in front of the class, share with your group what you have made, please cover important points
Tell what it is your product
Demonstrate how it works
Explain how it works
Share with your group about
What troubles you met
How you analyze
How you get help
How you solve it
What you have learnt
Find out working copy as a MakeCode shared project.