Unit 7: Boogie Bot Flashcards
Algorithm
a series of steps that perform a task
API
Application Programme Interface
a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application or other service
Function
combines detailed steps into a single block of code that can be used repetitively; some functions have inputs and change their behaviour based on arguments from their caller, some have outputs and give a result, and some functions have neither
Boogie Bot
In this playground you’ll get to know BoogieBot, a dancing robot.
You’ll use your knowledge of functions to make BoogieBot dance by calling existing functions. Then you’ll define functions of your own to create dance routines. Finally you’ll add a text title, have a chance to sign your work and save it as an animated image like the one below: Next, find out how to configure your playground ready to see some dancing.
Live Views
Live views are a playground feature that let you see instant updates to your work, without having to add inline results as you did in the Strings playground. Live views also allow you to play with animation.
This playground has some special behind-the-scenes features to give it BoogieBot capabilities. These features won’t work in other playgrounds. Before you get started, make sure your playground is set up to work with live views. The following line of code sets up BoogieBot and makes it dance: runBoogieBotDemoMode()
/*: The live view shows up in the **assistant editor** on the right. 👉
If you don’t see anything there, choose View > Assistant Editor > Show Assistant Editor. The editor area should now be divided in two. The assistant editor should be showing the playground’s live view. If it isn't, click the first entry in the bar at the top and choose “Live View” so it looks like this: ![](LiveViewSelected.png) You should see BoogieBot doing some moves in the assistant editor. On the next page you’ll make BoogieBot dance to your tune.
Dance School
To set BoogieBot up to get ready to dance, you use this line of code:
startBot() /*: BoogieBot can do these moves: - `leftArmUp()`, `leftArmDown()`, `rightArmUp()`, `rightArmDown()` - `leftLegUp()`, `leftLegDown()`, `rightLegUp()`, `rightLegDown()` - `shakeItLeft()`, `shakeItRight()`, `shakeItCenter()` - `jumpUp()`, `jumpDown()` - `fabulize()`, `defabulize()` The moves should all be pretty clear, except `fabulize()` and `defabulize()`. The first changes BoogieBot into fabulous new colors; the second changes BoogieBot back to a dull robot gray.
You tell BoogieBot to move like this: ``` fabulize() shakeItLeft() shakeItRight() shakeItCenter() ``` Once BoogieBot gets to the end of the routine it will stop. You can use Editor > Execute Playground to see the moves again. Changing the routine itself will start things over again. */ fabulize() fabulize() fabulize() fabulize() leftArmUp() rightArmUp()
leftLegUp() rightLegUp() leftLegDown() rightLegDown() shakeItLeft() shakeItRight() shakeItCenter() jumpUp() jumpDown()
leftArmDown() rightArmDown() defabulize()
//: - experiment: Rearrange the lines of code above to remix the dance routine. BoogieBot performs the moves in the order they appear in the playground.\ //:What happens if you fabulize or defabulize in the middle of a dance routine? //: //: //: Where do these dance functions come from? Find out on the next page. //:
APIs
App developers always work with code provided by others. In the case of BoogieBot, a number of functions are defined that make BoogieBot start, move, and change colors.
The set of functions that control BoogieBot is an example of an Application Programming Interface, more commonly known as an API. An API is a specific set of functionality that can be used by a software developer to accomplish a task. In this case, the BoogieBot API is making a software robot dance.
BoogieBot API
Start BoogieBot:
startBot() Move BoogieBot: leftArmUp(), leftArmDown(), rightArmUp(), rightArmDown() leftLegUp(), leftLegDown(), rightLegUp(), rightLegDown() shakeItLeft(), shakeItRight(), shakeItCenter() jumpUp(), jumpDown() Change BoogieBot Color: fabulize(), defabulize() Next, learn how to put these functions together to make dance routines.
Routines
Dancing is fun, but if you write out the moves one by one, describing even a simple dance gets long and repetitive. Instead, people usually string individual moves together, and then give a name to the whole routine.
You create routines in code by building functions like this:
func doTheDisco() { leftArmUp() shakeItRight() leftArmDown() shakeItCenter() } /*: Once you've decided what moves go in the routine, start the robot dancing by calling the function you just made: */ startBot() fabulize() doTheDisco() doTheDisco() doTheDisco() /*: Functions are the way programmers group blocks of work together. Recall from the Functions playground that functions help you in these ways:
- A function is reusable, which saves on reading and typing. - A function can be understood on its own, so you don’t have to think of every single step. - If a function is changed, the changes apply everywhere the function is used. You’ll get all these benefits with `doTheDisco()`. - experiment: Change the `doTheDisco()` function into a mirror image, so anything done “left” becomes “right”, and anything “right” becomes “left”. Imagine how much work it would have been if you’d typed out the same moves three times. Want to personalize your BoogieBot? Learn how to sign your work on the next page.
Sign Your Work
BoogieBot also has an API for personalizing the moves you’ve created.
Give your bot a title: setBotTitle("My Awesome Dance") And a subtitle. This is a chance to add your name: setBotSubtitle("By The Boogiemaster") startBot() setBotTitle("Sunday Night Fever") setBotSubtitle("By John Showboater")
func doTheDisco() { fabulize() leftArmUp() leftArmDown() } doTheDisco() doTheDisco() doTheDisco() doTheDisco() //: - callout(Exercise): Set your own title and subtitle for your BoogieBot dance. //: //: Next, learn how to share the dances you make with your friends.
Share the Boogie
BoogieBot has one final feature.
Besides viewing your BoogieBot moves in the assistant editor, you can save an animated GIF of your work so it’s easy to share with your friends. To do this, add startRecording() right after you start your bot: startBot() startRecording() /*: Then add your routine below. When BoogieBot gets to the end of the dance, a Save button appears. Click the button to save your dance routine as an animated GIF.
Remember, once BoogieBot gets to the end of the routine it will stop. You can use Editor > Execute Playground to see the moves again. Have fun! */ // If you're going to send it, sign it. setBotTitle("The next dance craze") setBotSubtitle("You saw it here first!") // Start the dance! fabulize() leftArmUp() rightArmUp()
leftLegUp() rightLegUp() leftLegDown() rightLegDown() shakeItLeft() shakeItRight() shakeItCenter() jumpUp() jumpDown()
leftArmDown() rightArmDown() defabulize()
//: Next, find out a new term for the work you’ve been doing. //:
Algorithms
During this lesson you’ve been creating different dance routines for BoogieBot by combining moves into functions. Computer scientists would say that you’ve spent this lesson developing algorithms.
When you develop an algorithm, you’re defining a series of steps to be performed. They don’t have to be dance steps, of course; any self-contained step-by-step series of operations is an algorithm. Defining algorithms is a large part of programming. There are complex mathematical algorithms that do things like compress video so it can be sent efficiently over the Internet. There are algorithms that don’t involve mathematics at all, like an algorithm to check if an app user has any new messages and to present a notification if they do. And, of course, algorithms for getting software robots to dance. Next, get an overview of what you’ve learned.
WrapUp
In this playground you’ve learned about the concept of an API, a set of functions provided by another developer to help you work.
Once you’ve learned the basics of programming, you’ll learn a lot more about working with APIs. You’ve also had more practice with using and defining functions and using functions to group repeated work together. Dance routines are all about performing a preplanned sequence of moves in repeating patterns, so functions made up of functions (made up of functions, and so on…) are perfect for making great dances! At the end of the lesson, you signed your work and made some fun GIFs to share with your friends. The next few pages are blank BoogieBots, so you can create a few routines and GIFs without overwriting your work. Now’s your chance to do some interesting things with groups of functions. Give it a try. runBoogieBotDemoMode()
Boogie Workshop
This page is here for you to create your own routines.
Remember the moves: leftArmUp(), leftArmDown(), rightArmUp(), rightArmDown() leftLegUp(), leftLegDown(), rightLegUp(), rightLegDown() shakeItLeft(), shakeItRight(), shakeItCenter() jumpUp(), jumpDown() fabulize(), defabulize() To sign your work: setBotTitle("My Awesome Dance") setBotSubtitle("By The Boogiemaster") startBot() startRecording()
Boo