Pick a Random Card With Micro:bit

by bhambarikadam in Teachers > 5

45 Views, 1 Favorites, 0 Comments

Pick a Random Card With Micro:bit

Microbit.png

In this game, students use arrays to store card values and implement random selection to simulate picking cards. By pressing buttons A and B, players draw cards, and on shake event compare them to determine a winner based on card indices. The project introduces arrays, random number generation, and conditional statements in a fun, game-based context using MakeCode. I designed it to align with coding standards, engage students with hands-on learning, and foster understanding of key programming concepts through an interactive two-player game.

Supplies

The tools and materials used for the "RandomCardGame" Micro:bit project are:

  1. Micro:bit
  2. Micro:bit programming environment (e.g., MakeCode) or Micro:bit Python
  3. Computer with internet access

Set Up the Project

  1. Access MakeCode: Open a web browser and navigate to the MakeCode editor for Micro:bit at https://makecode.microbit.org/.
  2. Create a New Project: Click the "New Project" button in the MakeCode editor. Name the project "RandomCardGame" to keep it organized.
  3. Familiarize with Interface: Briefly show students the MakeCode interface, including the block-based coding area, the simulator on the left, and the download button for transferring code to the Micro:bit.


random.PNG


Introduction to Arrays

Explain that an array is like a list that can hold multiple items (e.g., card names). Show how the array (named faces) in the code stores card values from "two" to "Ace".

Set Up the Project

Open the MakeCode editor for Micro:bit (https://makecode.microbit.org/).

Create a new project and name it "RandomCardGame".

  1. Initialize the Array
  2. Go to the "On Start" block.
  3. Use the "set [variable] to" block and create an array variable named faces.
  4. Add the following card values to the array: "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Jack", "Queen", "King", "Ace".
  5. Pick Random Cards
  6. Add an "on button A pressed" block.
  7. Use the "set [variable] to" block to set variable a to a random value from the faces array.
  8. Add a "show string" block to display the selected card on the Micro:bit LED screen.
  9. Repeat the process for "on button B pressed" to set variable b with another random card.
  10. Determine the Winner
  11. Add an "on Shake" block to compare the cards of player A and B
  12. Use an "if" block to check if the index of Player A in faces is greater than the index of Player B.
  13. If true, show "A wins"; if false, check if the index of b is greater and show "B wins"; otherwise, show "Tie".
  14. Test and Debug
  15. Download the code to the Micro:bit.
  16. Press buttons A and B to pick cards, then shake the Microbit to see the result.

Implement Random Card Game in Python

arrays_py.PNG


  1. Introduce Python for Micro:bit:
  2. Explain that the Micro:bit supports Python programming via the Micro:bit Python Editor (https://python.microbit.org/). This allows students to write text-based code instead of using MakeCode’s block-based interface, reinforcing the same concepts (lists, random selection, and conditionals) in a different format.
  3. Highlight that in Python, arrays are called "lists," but they serve the same purpose as the faces array in MakeCode.
  4. Set Up the Python Environment:
  5. Open a web browser and navigate to the Micro:bit Python Editor (https://python.microbit.org/).
  6. Click "New Project" to start a new Python script. Save the project as "RandomCardGamePython" for clarity.


Downloads

Python and Makecode

Adding a Python implementation allows students to see how the same programming concepts (lists, random selection, conditionals) apply in both visual (MakeCode blocks) and text-based (Python) coding. This dual approach deepens their understanding, builds confidence in transitioning between coding styles, and prepares them for more advanced programming by exposing them to text-based syntax while maintaining the fun, interactive context of the card game.