CodeX Lucky Charms
Share
Lucky Charms
Students draw clovers with the CodeX display, then use random numbers and probability to build a game of chance.
Overview
This project uses the graphics capabilities of CodeX to draw clovers with circles and rectangles. Students then use probability and random numbers to turn the clovers into a simple game of chance: press a button and see whether you get a 3-leaf or a 4-leaf clover. They write small functions and put everything together in the main program for a simple working game. It is a fun way to bring in St. Patrick's Day and its traditions.
You choose how far to take it. Give students the starter code with the clovers already programmed, or have them draw their own. You can stop once the basic game works, or keep going to add more clovers, an introduction, and an ending message. Optional extension ideas are on the last slide. Instructions are not given for the extensions, so they work well for students who want an extra challenge.
🎯 Mission Goal: Students will use random numbers and probability to code a CodeX game of chance with 3-leaf and 4-leaf clovers.
Project Video
Join the conversation in the Firia Labs Code Collective to share how your students did!
Learning Targets
- I can use the draw features of CodeX to draw circles and rectangles.
- I can use a random number and probability to determine an outcome.
- I can define and call a function with a return.
- I can define and call a function with parameters.
- I can write code that determines if a game is won.
- I can increment a variable that counts button presses.
- I can use variables for locations on the coordinate grid system to duplicate images drawn with circles and rectangles.
Key Concepts
- The project has four parts. Students can stop after Part 2 or Part 3, or keep going to Part 4.
- A random number from 0 to 99 is compared to a probability. The higher the probability, the easier it is to win.
- Small functions keep the program organized: one decides True or False, one draws the clover, and one flashes the pixels.
-
Adding
x+andy+to each location lets one clover function draw a clover anywhere on the screen. - Almost all student mistakes are typing mistakes. Check spelling, punctuation, and especially indenting first.
Assessment Opportunities
- Students run their game and show it working at the part they reached.
- Check that the game uses functions with parameters and a function that returns True or False.
- Check that the win condition matches the number of clovers on the screen.
- Ask students to explain how changing the probability changes how hard the game is.
- Students who continue to Part 4 show the introduction and ending message working.
Success Criteria
- Draw a 3-leaf clover and a 4-leaf clover using circles and rectangles
- Use a random number and probability to return True or False
- Display a 3-leaf or 4-leaf clover each time BTN-A is pressed
- Flash the pixels when the player wins
- Count the spins and display the count when the player wins
- Show three clovers and win only when all three are 4-leaf (Part 3)
- Add an introduction and an ending message (Part 4)
Digital Resources
Classroom Materials
- ▸CodeX
- ▸USB-C cable
- ▸Computer
Extensions & Cross-Curricular
📐
Standards
▾
CSTA Standards - Grades 6-8
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
- Download the slides.
- Be familiar with the final code.
- Read through the teaching guide.
- Optional: Use the accessibility guide as needed.
- The instructions for this project are not in the interactive textbook in CodeSpace. Download and follow the slides. They include step-by-step instructions and code segments to guide students.
- Students should already have an account in CodeSpace. They use the Sandbox for this project.
- Decide whether to give students the starter code and skip programming the clovers, or have students write all of the code. Consider the time you have and the abilities of your students.
- This project can be completed individually or with pair programming.
- The slides can be shown on a large screen and completed as a class, or given to students to work at their own pace. It is also a great project for students who finish other work early.
- In the folder: Lucky Charms project slides, starter code, and code solutions for Parts 2, 3, and 4. The starter code and all code files are listed under Digital Resources.
- If students get errors, look over the code for spelling, punctuation, and indenting before anything else.
-
A button seems to be ignored, or Python reports an indentation error: check the indentation. Everything inside
while True:must be indented, and eachifstatement for a button must line up with the others. -
The clovers are in the wrong place, or Step 8 gives an error: each clover function needs the parameters x and y, the first number in every circle and rectangle needs
x+, and the second number needsy+. -
The ending says the best win took 1000 tries: the player pressed BTN-B before winning, so lowest still has its starting value of 1000. Check that
ending()tests whether lowest is still 1000 and shows a different message, such as "No wins this time." The Part 4 solution code does this.
Lesson Outline
This outline gives one way to present the lesson.
Slide 2: Bring in St. Patrick's Day and some of its traditions.
- Make a list of traditions or ways people celebrate the holiday.
- Talk about the luck of the Irish, the lucky pot of gold at the end of a rainbow, or finding a lucky 4-leaf clover.
- Transition with the question on the slide: "Do you have the luck of the Irish?"
💻 Slides 3-4: Students log in to CodeSpace and open the Sandbox. They create a new file named LuckyCharms and import the codex library, sleep, and randrange.
Students connect the CodeX: click Select Target, choose CodeX via USB, and plug in the USB-C cable. If the connection bar at the lower left is red, click it to connect.
💻 Choose one option:
- Option 1: Slides 5-7, Steps 1 and 2. Give students the starter code to copy into their own file. They run it and adjust the clovers as needed. Then skip to slide 17.
- Option 2: Slides 8-16, Steps 1 and 2. Students work through the slides to design their own 3-leaf and 4-leaf clover. Example code is given. They build one clover of each type.
- Make sure students leave room on the screen. They will add more clovers in Part 3, so the clovers should not be too big.
💻 Slides 18-19 introduce probability and the basic algorithm for the game. Slides 20-23: Students define and call a function that uses a random number and a probability. It returns a Boolean value (True or False), which is displayed on the screen only for testing.
💻 Slides 24-27: Students use the Boolean value in another function that displays either the 4-leaf or the 3-leaf clover. When they test, the clovers draw on top of each other. That is a bug to fix in the next step.
💻 Slides 28-29: The fix. Students clear the display at the beginning of each button press.
💻 Slides 30-33: Students define another function that flashes the pixels when they win. Example code is given.
💻 Slides 34-36: The final step of the basic game. Students add a counter for the number of button presses, or "spins." The count is displayed when the pixels flash. You can stop here, continue with Part 3, or skip to Part 4.
💻 Slides 38-42: Students add x and y parameters to the clover functions so the clovers can be moved or duplicated. They also add arguments to display_choice().
x+ added to it. The second number needs y+ added to it. No other changes. Students should test the code on slide 42, pressing the button several times to see both clovers, before moving on.💻 Slides 43-45: Students call the functions a second time to display a second clover. They also change the condition for winning, because there are now two conditions.
💻 Slides 46-48: Students follow the same steps for a third clover and change the condition for winning again. Suggested starting values are x=55 and y=90. At this point students have a working game with three clovers. Stop here, or go on to Part 4 if time permits.
💻 Slides 49-52: Students make the game friendlier by adding an introduction with instructions. A sample introduction is given, but students can use their own creativity.
💻 Slides 53-60: Students add an ending message. They use a variable to keep track of the lowest number of spins needed to win and display it in the ending. They should also handle a player who quits before winning. The Part 4 solution code shows one way to do this.
💻 Slide 62: Several ideas are given for students who want to challenge themselves. Instructions are not given for the extensions. See Extensions & Cross-Curricular above for the full list.
The project does not require a wrap-up. You can use one of the activities from Extensions & Cross-Curricular, or have students reflect on the project and something they learned.