CHAPTER 5
Animating with Simple Conditionals
Haunt the city! In this chapter, you’ll create a fun, side-scrolling animation that introduces you to your third big idea in coding: selection. You’ll make a cute ghost that hovers above a cityscape. The ghost appears to fly over a seemingly endless street of skyscrapers — but it’s really the buildings moving, not the ghost! The code you write executes in a selection, or “if-then” command. If a specific condition is met, then a consequence is selected for execution. In your code, each building moves slowly across the screen, and if it reaches the edge, it then hides and reappears on the other side, but with a new color and costume.
You’ll set up your scene with a background, a ghost sprite, and many building sprites. You’ll then write code so that when the user clicks on the green flag, the ghost hovers and the buildings move in a repeat loop, using selection to know when and how to reposition. With just a little math, art, and selection, you’ll have your ghostly scene coded before you can scream “Boo!”

Brainstorm
Brainstorm the design of your program by thinking about the overall look of your scene. What about an airplane flying over farmland with trees and cornstalks on the ground? Or a fire-breathing dragon soaring above a bustling city of princes, princesses, witches, and wizards?
Start a New Project
Begin creating your Haunt ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access