< Unit 3 - Interactive Animations and Games ('21-'22)

Lesson 11: The Draw Loop

45 minutes

Overview

Question of the Day: How can we animate our images in Game Lab?

In this lesson students are introduced to the draw loop, one of the core programming paradigms in Game Lab. To begin the lesson students look at some physical flipbooks to see that having many frames with different images creates the impression of motion. Students then watch a video explaining how the draw loop in Game Lab helps to create this same impression in their programs. Students combine the draw loop with random numbers to manipulate some simple animations with dots and then with sprites.

Purpose

The draw loop is a core component of Game Lab. The fact that the Game Lab environment repeatedly calls this function many times a second (by default 30) is what allows the tool to create animations. This lesson has two goals. The first is for students to see how animation in general depends on showing many slightly different images in a sequence. The second goal is for students to understand how the draw loop allows them to create this behavior in Game Lab. Students should leave the lesson understanding that the commands in the draw loop are called after all other code but are then called repeatedly to create animation. Students will have a chance to continue to develop an understanding of this behavior in the next two lessons, but laying a strong conceptual foundation in this lesson will serve them well for the rest of the unit.

Assessment Opportunities

  1. Explain how the draw loop allows for the creation of animations in Game Lab

    In the wrap up discussion, check that students mention that the code in the draw loop is run over and over, whereas code outside the draw loop is just run once, at the beginning of the program.

  2. Use the draw loop in combination with the randomNumber() command, shapes, and sprites to make simple animations

    See level 8 in Code Studio.

CSTA K-12 Computer Science Standards (2017)
    • 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
    • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
    • 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
    • 2-AP-16 - Incorporate existing code, media, and libraries into original programs, and give attribution.
    • 2-AP-17 - Systematically test and refine programs using a range of test cases.
    • 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.

Agenda

Objectives

Students will be able to:
  • Explain how the draw loop allows for the creation of animations in Game Lab
  • Use the draw loop in combination with the randomNumber() command, shapes, and sprites to make simple animations

Preparation

  • Print and assemble the manipulatives
  • Prepare the video

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the students

Teaching Guide

Lesson Modifications

Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please click here to access modifications that can be used during this lesson.

Warm Up (5 minutes)

Display: Show Flipbook Example - by Marnic Bos - Video.

Prompt: This video shows a flipbook to make animation. In your own words, how is it working? Why does it "trick our eyes" into thinking something is moving?

Discuss: Have students write their ideas independently, then share with partners, then share as a whole group.

Discussion Goal

Goal: This discussion should introduce some key understandings about animation. Students should understand that the key is seeing many pictures in a row that are slightly different. Introduce the vocabulary word "frame" as being one of those many pictures--a single image within an animation. Then transition to the fact that soon students will be creating animations of their own.

Remarks

We're going to start learning how to make animations, not just still images. In order to do this we need a way to make our programs draw many pictures a second. Our eyes will blur them together to make it look like smooth motion. To do this, though, we're going to need to learn an important new tool.

Question of the Day: How can we animate our images in Game Lab?

Activity (35 minutes)

Video: Watch the video introducing the draw loop.

Teaching Tip

Pair Programming: This lesson introduces a challenging new paradigm. Consider having students pair program. Remember to give students clear instructions on when to switch driver and navigator.

Demo: If you choose, use the provided manipulatives to provide a hands-on demo for exploring how the draw loop and animations are connected.

Question to Consider with Video:

  • What does the draw function do?
Discussion Goal

Goal:

The draw function runs the same code over and over in a loop, which is why it's often called "the draw loop". This creates an animation effect by changing what is a drawn by a small amount each time the draw function runs. This can be compared to a physical flip book, which can animate an image by changing it slightly on each page.

Misconception Alert!

When the draw loop runs, it does not "clear" out previous drawing, so it will continue to show anything that has not been covered up by the new draw loop. If students do not add a "background" at the beginning of the draw loop, they will still see all the images drawn to the screen previously.

Teaching Tip

Notice that the blue background is never visible because it is immediately drawn over by the red background. Also there's only ever one yellow dot visible since as the draw loop runs over and over it is placing down new backgrounds.

Assessment Opportunity

You can use this level as a formative assessment for students. Click inside the level to view a rubric and leave feedback to your students

Assessment Opportunity

You can use this level as a formative assessment for students. Click inside the level to view a rubric and leave feedback to your students

Wrap Up (5 minutes)

Question of the Day: How can we animate our images in Game Lab?

Prompt: How does the draw loop help us make animations?

Assessment Opportunity

Key Understandings: There are many common misconceptions with the draw loop. Make sure students understand the following:

  • The draw loop is run after all other code in your program. It does not actually matter where it is located in your program.
  • The draw loop is run by Game Lab at a constant frame rate of 30 frames per second. You do not actually need to call the function yourself.
  • The "frames" in Game Lab can be thought of as transparency sheets. Unless you draw a background, then all your new shapes or sprites will simply appear on top of your old ones.
  • You should only have one draw loop in your program.

Review: Return to the resources students saw at the beginning of the lesson (Video, physical flip books) or under Help and Tips during the lesson. Address misconceptions that have arisen in the lesson.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.