CodeX Lucky Charms

CodeX Lucky Charms
Free Mission Lesson Plan

Lucky Charms

Students draw clovers with the CodeX display, then use random numbers and probability to build a game of chance.

⏰ 50 min 🎯 Grades 6-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
A CodeX board surrounded by green clovers with the words Lucky Charms Monthly Project.
📋

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+ and y+ 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)
🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🚀

Extensions & Cross-Curricular

ExtensionKeep track of the highest number of spins needed to win, and display it in the ending with the lowest number of spins.
ExtensionUse a random number for the probability.
ExtensionAdd a fourth clover to the game, or even more.
ExtensionAdd levels to the game. For example, press U for an easy game with high probabilities, or D for a hard game with low probabilities.
ExtensionAdd a third image besides the clovers, maybe a hat, and give it its own probability. If it shows up all three times, you lose and the game is over.
Lang ArtsHave students come up with their own tradition for St. Patrick's Day and write about it. Or have them invent their own holiday and explain it orally or in writing.
MathDeciding the location of each leaf in the clover takes some math. Work through it together and see who can calculate their way to a perfect clover.
MathThis project uses probability. Teach a lesson on probability and try other unplugged probability activities.
Visual ArtsThis project draws with circles and rectangles (and text). Have students design their own image on graph paper, then write the code to display it.
Social StudiesHave students research the background of St. Patrick's Day and how it is celebrated across the world.
📐

Standards

▾

CSTA Standards - Grades 6-8

2-CS-03 2-AP-11 2-AP-12 2-AP-13 2-AP-14 2-AP-17

CSTA Standards - Grades 9-10

3A-CS-03 3A-AP-13 3A-AP-16 3A-AP-17 3A-AP-18

CSTA Standards - Grades 11-12

3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-21 3B-AP-22
📝
Preparing for the Lesson
  • Download the slides.
  • Be familiar with the final code.
  • Read through the teaching guide.
  • Optional: Use the accessibility guide as needed.

🧑‍🏫
Teacher Notes
  • 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.

🔧
Troubleshooting
  • A button seems to be ignored, or Python reports an indentation error: check the indentation. Everything inside while True: must be indented, and each if statement 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 needs y+.
  • 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.

🗣️Warm-up

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?"
💻Getting Started

💻 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.

Teaching tip: The instructions are not in the CodeSpace textbook. Show the slides on a large screen and go through the project as a class, or give students the slides and let them work at their own pace.
💻Part 1 - Drawing the Clovers

💻 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.
Teaching tip: Choosing between the options depends on the time you have and the abilities of your students.
💻Part 2 - Step 3: Random Number and Probability

💻 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.

Teaching tip: Remind students that the higher the probability, the easier it is to win. A low probability makes the game harder.
💻Part 2 - Step 4: Display a Clover

💻 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.

💻Part 2 - Step 5: Clear the Display

💻 Slides 28-29: The fix. Students clear the display at the beginning of each button press.

Teaching tip: This is a good place to test different probabilities. Students can decide how easy or difficult their game is.
💻Part 2 - Step 6: Flash the Pixels

💻 Slides 30-33: Students define another function that flashes the pixels when they win. Example code is given.

Teaching tip: Encourage students to design their own flashy win. They can even include sound.
💻Part 2 - Step 7: Count the Spins

💻 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.

💻Part 3 - Step 8: Add Parameters to the Clovers

💻 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().

Teaching tip: The first number in each circle and rectangle needs 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.
💻Part 3 - Step 9: Add a Second Clover

💻 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.

Teaching tip: Each leaf has a radius of 20, so a diameter of 40. Students can work out where the next clover should go, or use trial and error. They can keep y at 0. A suggested second x value is 110.
💻Part 3 - Step 10: Add a Third Clover

💻 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.

💻Part 4 - Step 11: Introduction

💻 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.

💻Part 4 - Step 12: Ending

💻 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.

🚀Extensions

💻 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.

🧑‍🤝‍🧑Wrap-Up

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.

Back to blog