AP CSP CodeX - Mission 14: Line Art

Mission 14 Lesson Plan

Mission 14: Line Art

Students use pixels, loops and functions to draw string art on the CodeX display.

⏰ 50-90 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

In this mission, students use computer graphics to create string art on the CodeX display. They start by setting single pixels, then use loops, the step parameter and nested loops to draw axes and a grid. Next they draw lines with display.draw_line() and build an envelope (a web pattern) with a loop and a custom function. The coding can be finished in one class period, but two days leaves time for discussion and the challenges.

🎯 Mission Goal: Students will use computer graphics to create beautiful string art.

🎯

Learning Targets

  • I can use pixels to draw a line on my LCD screen.
  • I can use the bitmap function to get display width and height.
  • I can explain how to convert a number from a float to an integer.
  • I can use a loop to create a pixel grid on the LCD.
  • I can draw an envelope (webbing) using the display.draw_line() function.
💡

Key Concepts

  • The CodeX display screen is a bitmap, and every bitmap has a width and height.
  • Using built-in line drawing functions is faster, simpler and supports drawing diagonal lines.
  • One way to optimize code is to replace many lines of code that draw lines with a function instead.
  • Creating string art is easy by using an envelope, a loop and a constant.
  • Defining a custom function with line points that spin counterclockwise makes a web pattern.
☑️

Success Criteria

  • Draw an x- and y-axis on the display screen
  • Draw a white grid of dots on the display screen
  • Draw a blue square as a border around the screen
  • Use built-in screen information for drawing lines (display.width and display.height)
  • Create a function for drawing a web
  • Call the web function at least 6 times
📁

Digital Resources

🧰

Classroom Materials

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

Real-World Applications

🎨You are learning skills that are important for graphic designers! Learning bitmap functions and mastering how to draw an envelope are valuable skills in fields such as graphic design, game development, computer vision, and more. These skills are foundational for creating visually appealing and interactive digital content.
🖼️Bitmap functions are essential for applications like Adobe Photoshop or GIMP.
🎮In game development, bitmap functions are crucial for rendering sprites, backgrounds and visual elements.
🖥️In user interface design, drawing envelopes can be used for creating custom shapes, text effects, or intricate patterns.
🚀

Extensions & Cross-Curricular

ExtensionTry the "CodeX and ASCII Art" lesson in the additional CodeX projects to make your own bitmap images, like pixel art.
ExtensionAdd a second web that spins the opposite direction. Change the colors and compare the two patterns.
MathThe center of the screen is (120, 120). Explain why display.width / 2 gives 120.0 and what int() does to it.
MathSketch an envelope on graph paper by joining points on the x-axis to points on the y-axis. Then compare it to the web on the CodeX.
ScienceScreens mix red, green and blue light to make every color. Look at the RGB tuple that get_pixel() returns and explain how it makes cyan.
Lang ArtsWrite a short artist statement that describes your line art and the choices you made.
🔤

Vocabulary

▾
Bitmap - Graphics bits, drawing images and text. A bitmap is an object that can hold a 2D image of a given width and height; a list of pixel RGB values.
Pixel - Elements of a picture, short for "picture element." They are the tiny dots that make up larger images.
Magic number - Numbers that just appear in code with no explanation. When something changes in the future, the number does not work anymore and you have to change it.
Literal - A specific value, like 1 or "hello".
Envelope - In geometry, a curve created by straight lines moving down and across a grid.
Float - Short for "floating-point number." It is a data type for numbers that can have a decimal point.
Matrix - A grid created by using a loop within a loop.
Docstring - A comment used in Python to describe functions. It is designated with triple quotes.
Delta - A small change to a value. It is designated with a "d", like dx and dy.
🐍

New Python Code

▾
display.set_pixel(50, 120, WHITE)Turn on a single pixel a specific color.
display.get_pixel(120, 120)Return a tuple of the color of a single pixel at a given location.
display.width display.heightFunctions that return the CodeX display width and height.
# Variables for screen center x_center = int(display.width/2) y_center = int(display.height/2)Use the int() function to convert a decimal value to an integer.
for x in range(display.width): display.set_pixel(x, y_center, RED) for y in range(display.height): display.set_pixel(x_center, y, RED)Two for loops that draw a straight line using individual pixels. The first loop draws a horizontal line, and the second loop draws a vertical line.
y = 20 for x in range(0, display.width, 10): display.set_pixel(x, y, WHITE)Use a step parameter in a for loop. The step is like "skip" counting. In this example, the x control variable counts by 10.
# Draw a grid of white pixels for y in range(0, display.height, GRID): for x in range(0, display.width, GRID): display.set_pixel(x, y, WHITE)Nested for loops that draw a grid. The constant GRID is the "step" for the loop.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-DA-11 3A-AP-13 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-23

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-22 3B-AP-23

AP CSP Framework

AAP-2.K - Write iteration statements.
AAP-3.C - Develop procedural abstractions to manage complexity in a program by writing procedures.
Computational Thinking Practice 1.B - Determine and design an appropriate method or approach to achieve the purpose.
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • Plan for one or two class periods. If you split the mission, stop after Objective 5 on day one and have the Mission 14 Obj. 1-5 Kahoot! Review ready.
  • Have the Mission 14 Obj. 6-9 Kahoot! Review ready for day two.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • This can be a one or two day assignment. Your students can probably finish the mission as-is in one day. However, if you want to take two days, you will have time for discussion. Students can also add functionality to the program, like a remix, with the challenges. This gives them an opportunity to practice programming concepts learned earlier on a new project.
  • The math for the envelopes can look complicated. Students do not need to understand it. The main idea is selecting a beginning and ending position for each line point. Experimentation is fine. The finished picture does not have to look like anything in particular.
  • Encourage the students to do as much code on their own as they can, and use CodeTrek to check their work, or as a hint when needed.
  • The assignment adds an extra step to complete after Objective 9, and a challenge.
  • If students are interested in creating their own bitmap images (like pixel art), you can find the lesson "CodeX and ASCII Art" in the additional CodeX projects.
  • Refer to the lesson plans in the Python with CodeX Mission Pack Curriculum for additional challenges, cross-curricular suggestions and additional information.
🗺️

Lesson Outline

This outline gives the narration for one way to present the lesson.

🗣️Warm-up

In previous missions and lessons, you learned to draw using lines, circles and rectangles. How do you think you can use them in loops to create art? Have a brief discussion.

💻Activity - Mission 14 Objectives 1-5

Randomly group students into pairs for pair programming (or they can work individually).

Students log in to one computer. Two computers can be used if they want to have the assignment open on one computer and CodeSpace on the other computer.

Students go to make.firialabs.com and should be at the beginning of Mission 14.

Teaching tip - Objective 1: This objective introduces setting a single pixel on the screen. It reviews bitmaps and the 240x240 screen. Then students learn about getting information from the screen using a "get" command. You may want to practice or review "set" and "get". The objective uses the Console to observe the results of a print().
Teaching tip - Quiz: Objective 1 sets them up for the quiz. Students should follow the instructions in their assignment so they are prepared. The added code for Objective 1 gives them the second answer for the quiz.
Teaching tip - Objective 2: Students replace their code with a for loop. They delete all prior code except the import statement. The number for the range is not given. They need to remember the width of the screen is 240 pixels, which will be the range.
Teaching tip - Objective 3: Students add another loop to draw a vertical line. They must use variables for the range, not a literal value.

NOTE: Students answer the question "What can you do to avoid magic numbers?" The answer can be straight from the textbook (get the display width and height straight from the source), or more general, like using variables and constants to represent the numbers. The goal in the objective is to eliminate magic numbers by using the functions rather than literal numbers. You can remind students that a literal number IS a magic number.

NOTE: Running the code WILL cause an error. Students may realize it is because of the type of division.

Teaching tip - Objective 4: Students fix a bug by converting the division into integers. Using the integer division method (//) will work nicely, but the validator will check that the int() function is used.
Teaching tip - Objective 5: Students use nested for loops to create a grid. They use the "step" parameter in the for loop. During the objective, have students experiment with the step parameter so they can make a mental model of what it is doing before going to the nested for loops.
Teaching tip - Quiz: The quiz gives a code segment and asks questions about it. You may want to type it in and run the code on a screen for the class, or have them try the code on their computers, to see what it does. You could do this either before or after they answer the questions.

If this is the end of your class period, you can do a wrap-up with the Mission 14 Obj. 1-5 Kahoot! Review.

💻Activity - Mission 14 Objectives 6-9 and Challenges

Continue either in pairs or individually. Students go to make.firialabs.com and should be on Objective 6 of Mission 14.

Teaching tip - Objective 6: This objective introduces display.draw_line() and reviews display.draw_rect(). It should be review for most of your students. Go over any of this material as needed.

NOTE: Students are asked to do a "Save As" and give the file a new name. This keeps the original file and starts a new file with code already there. They will delete some code from the new file before adding more. If they do not do the "Save As", it is okay. They just will not keep their original code.

Teaching tip - Objective 7: A lot of information here about creating string art with envelopes. A link to Wikipedia is included. This is really advanced math! Do not worry about the math. Students can understand the concept without knowing the details. You can even skip the part about the envelope if you think it will be frustrating, and just use the term.
Teaching tip - Objective 8: This objective helps students understand the math by using their fingers to "trace" the lines and see the movement. You might want to try this yourself first and demonstrate for the students. They can use their own CodeX, or you can print the webbing on paper and have them "trace" on the paper.

This objective shows students how to use a for loop to draw the lines to create the web. After they create the for loop, run the program. Then they change the value of WEB_SPACING and run again to meet all the goals. CodeTrek uses literal numbers for 120 and 239. After Objective 9, students are asked to use the variables to replace the magic numbers.

Teaching tip - Objective 9: This is a fairly long objective. It starts with adding another for loop to the code. Then students learn about creating a function that does all the work for them. There is a lot of math! Do not stress about it. Just teach the math at their level. They do not have to understand all of it, just that it changes the values so the lines create a curve.

Then students create the function and replace the for loops with function calls. Four function calls are provided. Students must call the function two more times. They can try to complete the webs that are drawn, but they do not have to. Any two function calls will meet the goal. The solution provided shows the completed webbings as a possible solution.

Teaching tip - After Objective 9: Students add functionality to the code by using programming concepts learned in earlier missions. They create two functions and use variables instead of magic numbers. Solution code for the final program is provided.
Teaching tip - Challenge: If students have time, have them try at least one challenge. This gives them an opportunity to apply their knowledge in a new situation. It is like a remix of the mission, without starting from scratch. Several ideas are given, but students can use their own creativity to add to the project. Solution code for some of the challenges is provided.

The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

The post-mission reflection asks students to think about using their creativity and working through frustration. You can change the questions if there is something else you want to emphasize with your students.

Discussion: You have learned a lot about pixel art! How did you use your creativity to complete the program? This program can be frustrating. How did you manage your frustrations and work through problems?

Students complete the wrap-up questions on the assignment. You can have a short discussion on their answers.

Optional. Use a formative assessment for the wrap-up.

Formative assessment:

  • Daily reflection journal
  • Mission 14 Obj. 1-5 Kahoot! Review
  • Mission 14 Obj. 6-9 Kahoot! Review (in class or individual)
  • Exit ticket on display functions
  • Exit ticket on for loops and the step parameter