Before Donkey Kong, games had goals, but the player didn’t know their motivation or how they had ended up in the situation. Donkey Kong changed that by giving the player a story with a beginning, middle, and end that unfolded as the player moved from level to level, usually in the form of cutscenes, videos that occur before or after a level to pause the gameplay and give information. That story starred a little plumber known as Mario.
Yes, our friend, Mario, that you know from Super Mario Bros, started out as a carpenter at a construction site, climbing ladders in Donkey Kong to save his girlfriend who was stolen by a giant gorilla. Nintendo brought back Mario for Mario Bros and then Super Mario Bros, turning him into a plumber who needs to jump up on brick platforms or slide down pipes in order to save Princess Toadstool from the evil Bowser. Mario can only run to the right. That’s because Super Mario Bros is a single-screen platformer.
Single-screen platformers may look and perform similarly to multiscreen platformers like Pitfall, but there is a key difference. In Pitfall or Beach Blast, the player character (Pitfall Harry or Beachy Buffy) is moving from screen to screen, interacting with the other sprites on each screen. In Super Mario Bros or School Escape, seen in figure 13.1, the player character remains in the same X position, though they can jump up and down to change their Y position. The sprites in the game roll toward them whenever the player presses the right arrow. It looks as if Mario or the teacher is running, but the player character is staying in place while the objects move toward them.
To understand the mechanics of a single-screen platformer, it may help to imagine the character standing at the end of a conveyer belt. The belt only moves when the person marches in place. If the person stops moving, all the objects on the conveyer belt (as well as the belt itself) stop moving, and if the person marches in place, the items on the conveyer belt move toward them.
In School Escape, the teacher’s X position is on the left side of the Stage. As the player presses the right arrow to “move” the teacher, the desk sprites (platforms) and kindergarteners (obstacles) generate and slide toward the teacher. It looks as if the teacher is moving, but it’s the other sprites that are sliding across the screen, moving toward the left when the player presses the right arrow and stopping whenever the teacher pauses.
This game also introduces a common video game item: a power-up. Power-ups are items in games that the player can use to increase their chance of success or give them additional abilities. In School Escape, the power-up is a clock that players can jump up and grab in order to buy more time to reach the exit door because, yes, School Escape is also your first timed game.
To accomplish all this, School Escape uses lists, which are also known in coding as arrays. Lists allow the game to track multiple values for a single variable. It helps that Scratch calls them lists instead of arrays because you can think of them as you would any list: it’s a place to write down information you want to remember and use later. In this case, the lists contain the X-positions for where we want students and desks to appear.
In this chapter, you will learn
With fewer scripts, you’re going to make a much more complicated platformer. Take everything you learned in Beach Blast and apply it forward to create your own Super Mario-like School Escape.
It’s time for your pre-coding ritual. Get your sprites and the Stage ready for School Escape.
If you skipped chapter 12, either go back and complete it or go to the Manning site and download the background and sprites for School Escape. The directions for importing are the same as in chapter 5. You should have Ms. Finebean, four students, the clock, the desk, the door, and the hallway background.
You need to combine all your students into a single kindergartener. Navigate to the Sprite Zone and click Student2. Open the Costumes tab in the Block Menu and drag the sprite from the Costumes tab into the Student sprite in the Sprite Zone. Make sure that Student2 transferred into the Student sprite by clicking the Student sprite and looking at the Costumes tab. You should see two costumes, as in figure 13.2.
Repeat this step two more times for Student3 and Student4 until all four versions of the student are seen in the Costumes sidebar when you click the Student sprite. Once all the sprites are combined into a single sprite, you can delete the original copies of the Student2, Student3, and Student4 sprites.
What happens if you delete a sprite by accident? Don’t worry, you can get it back. Navigate to the Edit menu in the Grey Toolbar and choose the Undelete option. You’ll see the sprite you deleted return to the Sprite Zone.
You’ll need to resize a few sprites to have your code match our code. Use the Grow tool in the Grey Toolbar to enlarge the door (29 clicks), teacher (14 clicks), and student (4 clicks). The clock and desk are already the correct size.
Drag Ms. Finebean to the left side of the Stage, a centimeter from the edge. Place the clock sprite near the top third of the Stage on the far right side, around the X:200 and Y:80 position. (Check the bottom of the Stage as you move the clock to see the position. The clock doesn’t need to be perfectly on those numbers; they’re just a guide.)
Place the desk on the right side of the Stage, lining up the shadow with the top of the floor, and place the student sprite in the same place as the desk. Make sure neither sprite touches the wall. Finally, position the door in the middle of the Stage, also lining up the bottom of the door with the wall. All sprites should be in the same places as the ones in figure 13.3.
Even if you made your own sprites, you will need to download two lists from the Manning site, https://www.manning.com/books/helloscratch. These lists tell Scratch all the times you want the desk or kindergarteners to appear in the game. The lists are long, and instead of asking you to type in these values, we’ve made lists that you can import into your game.
Go to the Manning site and download the two .txt files (deskpositions.txt and kindergarteners.txt). You now need to create two blank lists in Scratch in order to import these .txt files into your game.
Go to Data and click Make a List. Name this list Desk Positions, as seen in figure 13.4, and create it For All Sprites.
Make a second list and call it Kindergarteners. Keep both lists checked for the time being so you can see them on the Stage. Right-click (or control-click if you’re on a Mac) the Desk Positions list on the Stage. A pop-up window will give you a few options, including Import, as seen in figure 13.5. Choose Import and navigate to where you’ve saved the deskpositions.txt file on your computer.
You should see 14 values appear in the previously empty Desk Positions list. Do the same thing for the Kindergarteners list, importing the kindergartener.txt file into the empty list. You should see 30 values appear in the previously empty Kindergarteners list, as in figure 13.6. Leave the lists on the screen for the time being, through you can uncheck the lists before you play so they’re not visible on the Stage.
The teacher, Ms. Finebean, is the equivalent of Mario in Super Mario Bros. She’s the player character that the player controls, making her jump over students and onto desks, grabbing clocks along the way. She has five scripts: a movement script, jumping script, falling velocity script, falling script, and desk detection script. All scripts in this section are applied to Ms. Finebean, so go put the blue box around the Teacher sprite in the Sprite Zone and don’t move the blue box until you program the kindergarteners in the next section. Remember, the names or values on your blocks may differ slightly from time to time, so use the completed script images to make sure you chose the correct block.
Ms. Finebean needs to escape from school, but she can’t move forward, as shown in figure 13.7.
This script will make it look as if Ms. Finebean is running toward the right whenever the right arrow is pressed. In reality, Ms. Finebean will be remaining in the same place, and this script sends the desks and kindergarteners toward her to give the illusion of movement:
Check your script against the one in figure 13.8.
Ms. Finebean’s X-position isn’t going to change, but you’ll make it look like it’s changing using the scrollX variable. This variable is tracking her virtual position, imagining her X-position increasing by 1 every time the right arrow is pressed. Get it? Ms. Finebean is always on the left side of the screen, but this value pretends her X-position is 1, 2, 3, and so on until she hits the thousands. When she reaches a certain X-position, different things will happen, such as a desk spawning or the door appearing. In this way, Ms. Finebean can technically run infinitely to the right, even though there are only 480 X-coordinate points (–240 to 240) if she is moving across the Stage. And different things happen, such as a student moving toward her, when she reaches a certain virtual point, because the value of scrollX has increased from the right arrow being pressed.
could you play with the number and make scrollX change by 2 or 3 each time to make Ms. Finebean run faster?
technically, yes, but you need to hit every value on the list, and that can only happen if scrollX increases one digit at a time. For instance, what if you set the desk to spawn when scrollX is 303, which means that Ms. Finebean is at her virtual X:303? If the value of scrollX jumps two “coordinates” at a time, she’ll move from 302 to 304 without ever hitting that 303 value. Keep the number at 1 in order to use every value on the list.
Ms. Finebean needs to be able to go over the kindergarteners instead of mowing them down while she runs down the hall. She also needs to be able to jump up and grab the clocks, as in figure 13.9, to gain more time.
This script will enable Ms. Finebean to leave the ground and jump into the air:
If the up arrow is pressed and Ms. Finebean is either on the floor or on a desk (meaning, she’s not already up in the air), she will go up 100 coordinates, and at the same time the velocity variable will kick in so she starts falling a tiny bit as she’s rising. This will give her a smooth arc. Look at the completed script in figure 13.10.
Once Ms. Finebean is in the air, she needs to fall realistically, floating back down to the ground smoothly, as she is doing in figure 13.11. This script will mimic a falling person’s velocity, having her speed up the closer she gets to the ground so she floats back down instead of dropping like a dead weight.
To make the falling velocity script
You’ve set a condition in the script in figure 13.12: if the teacher is not touching the floor or the desk—meaning, she’s in the air—then change the value of the yVelocity variable to –0.5. This means that Ms. Finebean’s speed will increase by a half a coordinate as she falls.
Ms. Finebean can go up, and you’ve set how she’ll fall when she starts moving downward. Now it’s time to make a script that will allow Ms. Finebean to fall, as in figure 13.13.
This script will allow Ms. Finebean to start falling when she reaches the peak height:
Look at the script in figure 13.14. Does your work match the image?
Currently, if Ms. Finebean tries to jump on a desk, she’ll go straight through it. You want the teacher to be able to land on the desk and run across it in order to use the height to leap over nearby kindergarteners, as in figure 13.15.
This script will recognize when Ms. Finebean has landed on the desk:
The condition is if the teacher’s shoe is touching the top of the desk, set the yVelocity variable to zero (0). This is an important script because you created a script earlier that said that if Ms. Finebean wasn’t touching the ground, then she should be falling. This script changes that by creating a condition—standing on the desk—where Ms. Finebean isn’t touching the ground, but you don’t want her falling. Look at figure 13.16 to check your script.
Ms. Finebean is ready to go. It’s now time to code the students.
The kindergarteners are the obstacle in School Escape, similar to the Goombas and Koopa Troopas in Super Mario Bros. Unlike those obstacles, you’ll want to jump over the students rather than land on the students and squash them. The students have five scripts: a list adding script, cloning script, movement script, game stopping script, and pause movement script. Make sure the blue box is around the student in the Sprite Zone, and keep it on the student for all five scripts. It doesn’t matter which costume is showing in the Sprite Zone thumbnail.
As mentioned earlier, a list is Scratch’s name for an array, which is a collection of data. Think about lists as actual shopping lists. When you go to the store, you write down all the items you want to remember to purchase, one item on each line. For example, you may name your list Grocery Items and then start listing out what you need: (1) Apples, (2) Bread, (3) Milk. When you go to the store, you look down the list and put each item in your basket. A Scratch list works the same way. You give it a name (for instance, Kindergarteners) and then write out the information you want to remember and track in an ordered list. In the case of the Kindergarteners list, you’re tracking a list of numbers: 10, 60, 110, and so on. Those numbers are virtual points in the game—times when you want a kindergartener to appear on the Stage. Scratch will know that it’s time to throw a student in Ms. Finebean’s path because it will keep checking to see whether the value of the scrollX variable (which goes up by one every time the right arrow is pressed) matches one of the items on the list. When they match, it will spawn a clone of the student sprite. You’ll see a student when the scrollX variable equals 10, 60, 110, and so on.
You currently have your imported list that states when each kindergartener should spawn. Yet when you go to play the game, the values will begin deleting off the list after they’re used. To solve this problem, you need to generate a second, identical list, as seen in figure 13.17, at the beginning of every game so the values will be deleted from the list copy instead of the original list.
This script enables you to play round after round of the game because the original list will always be intact, even though other scripts will delete values from the duplicate list:
There will be 30 loops, one for each item on the original list, and in each loop the script will add an item from the Kindergarteners list to the Kindergarteners2 list. It knows to move to the second, third, and fourth item (and so on) due to that #ofKindergarteners variable. When that variable equals 2, it copies the second item on the list. When that variable equals 3, it copies the third item on the list, and so on. The script in figure 13.19 stops running once all the items are duplicated from the original list onto the list copy.
You have a duplicate of your list, but it’s currently hanging out as a group of numbers. It’s time to put those numbers into action and have a kindergartener generate, as in figure 13.20, every time a number on the list matches up with the value of the scrollX variable, which marks the virtual location of Ms. Finebean.
To make the cloning script
Four things will happen if the value of the scrollX variable matches an item on the Kindergartener2 list, as seen in the completed script in figure 13.22: (1) a student will appear on the Stage, (2) the student will be standing where you positioned her when setting up the Stage, (3) the student will take the form of one of four possible options, and (4) that item will be deleted off the Kindergartener2 list.
There are students on the Stage, but they need to move toward Ms. Finebean (even though it will look as if it’s Ms. Finebean moving forward to reach the students). See figure 13.23.
This script will make the students move, but it will also tell Scratch what should happen when the students reach the edge of the Stage or if a student touches the teacher:
The completed movement script in figure 13.24 states what will happen if the right arrow is pressed (the student will move to the left), the student reaches the end of the Stage (they’ll disappear), or if the student touches the teacher (the player will lose one of their three chances, and the sprite will disappear).
School Escape would be an easy game if you had unlimited chances to reach the door. Giving Ms. Finebean three chances, as seen in figure 13.25, makes it harder to get to that ultimate goal.
This script ends the game when Ms. Finebean runs out of lives because she has landed on three kindergarteners:
In figure 13.26, if the value of the Lives variable equals zero (0), all scripts stop.
When Ms. Finebean runs up against a desk, she stops moving as if she has hit a solid object. But the kindergarteners keep coming, as in figure 13.27. This ruins the illusion that it’s Ms. Finebean running across the Stage instead of moving in place while everything comes to her.
This script tells the kindergarteners to stop moving if Ms. Finebean is touching the side of the desk:
Figure 13.28 shows the completed touching desk script.
Super Mario Bros has brick walls that Mario can walk on to get over the Goombas below. School Escape has desks. Ms. Finebean can jump up on the desks and use them to launch herself over the kindergarteners. The desks have three scripts: a list adding script, cloning script, and movement script—all similar to the kindergarteners. Keep the blue box around the desk for all three scripts in this section.
Like the Kindergarteners list, you want the original list of desk positions to remain untouched so you can play this game over and over again. This script will generate a second copy of the Desk Positions list by importing the items into the second list, as seen in figure 13.29.
This script enables you to play round after round of the game because the original list will always be intact and other scripts will delete values from the duplicate list:
This time there will be 14 loops, one for each item on the original list. The script in figure 13.30 operates in the exact same way as the list adding script for the kindergarteners.
The desk continues to operate like the kindergarteners, spawning—as seen in figure 13.31—whenever the value of scrollX matches one of the items on the Desk Positions2 list.
To make the cloning script
The script in figure 13.32 is a little shorter than the kindergartener cloning script because there’s only one desk, whereas there are four possible students who can show up on the Stage.
it makes sense to create a clone of the list in order to make the game playable again and again, but why do you also need to delete the values from the duplicate list?
if you don’t delete the values, every time a person plays the game, it will generate a new list, growing it from 14 items to 28 items to 42 items until later players are dealing with hundreds of desks at a time! By deleting values, you’re not only playing the current game but you’re setting up another game to be able to begin after the current game is done.
Like the kindergarteners, the desks will also move toward Ms. Finebean, starting near the right side of the Stage and moving toward the left. Desks, like the one in figure 13.33, will also create the illusion that it’s Ms. Finebean running to the desks rather than the other way around.
This script will make the desk move, plus it will tell Scratch what should happen if the desk is not touching the teacher but is touching the side of the Stage, or if the teacher is up against the side of the desk:
The completed movement script in figure 13.34 states what will happen if the right arrow is pressed (the desk moves to the left), if the desk reaches the edge of the Stage as long as the teacher isn’t currently on top of the desk (the desk disappears), or if the teacher is standing against the desk (stop moving and send a message to the other script).
The clocks are the power-ups in your game—the collectable sprites that make the player character better able to tackle the obstacles in the game. In this case, the clocks add 10 additional seconds onto the timer, buying the player more time to reach the door. The clocks have four scripts: a cloning script, timer script, movement script, and pause movement script. Place the blue box around the clock sprite in the Sprite Zone and keep it there for all four scripts.
The first power-up came in the game Pac-Man. The little yellow guy travels around the board eating up the dots and trying to avoid the ghosts. But in all four corners of the board are white circles that make Pac-Man invincible for a few seconds when he gobbles one of the power-up pellets. He can travel through ghosts and eat them instead of the other way around. Power-ups are items in a game that give the player a super ability, add time on a clock, or restore health. These items are usually collectable, like the mushrooms that cause Mario to grow or the flowers that give him firepower. Some can be used indefinitely through multiple levels of the game, and others expire after a set amount of time. Think about ways to work power-ups into your games to make the games more exciting. In fact, a fun exercise would be to go back through the older games in this book and see if there are power-ups you could add, like a shell in Beach Blast that gives Buffy the ability to run through crabs without being pinched.
Like the kindergarteners and desks, the clocks in figure 13.35 need to pop up at the correct time so Ms. Finebean can grab them as she runs down the hall.
Unlike obstacles or platforms, you want the appearance of power-ups to be special. You’ll make it a rare occurrence by programming that fact into the script. This script contains a cool new block: the mod block.
The mod block divides the two numbers you give it and prints out the remainder. In the script below, you will give it the current value of scrollX (which is always changing) and divide it by 100. Scratch will do that math problem on the fly and determine the remainder. You’re going to use that remainder to set a condition: when the remainder equals zero, something will happen. If the math problem yields any remainder other than zero, nothing will happen:
If the first and second conditions are both true, the clock will spawn on the wall. This means that it will be a rare, random occurrence because the two conditions in figure 13.36 need to line up for this event to happen.
Currently Ms. Finebean can run forever, which means that if she can get her jumps right, she will always reach the door. You can make the game harder by giving her a limited amount of time by placing a timer on the screen, like the one in figure 13.37, that counts down how many seconds she has to reach the door or have the game end.
To make the timer script
Check your script against the one in figure 13.38.
Like the kindergarteners and desks, the clock needs to move toward the teacher (as it is doing in figure 13.39), though it will look as though the teacher is running toward the clock.
This script is similar to the one you used for the kindergarteners, and it will move the clock as well as tell Scratch what should happen when the clock reaches the edge of the Stage or if the teacher touches the clock:
The completed movement script in figure 13.40 states what will happen if the right arrow is pressed (the clock will move to the left), the clock reaches the end of the Stage (it will disappear), or if the teacher touches the clock (the timer will add 10 seconds, and the sprite will disappear).
The clock will keep coming toward Ms. Finebean as long as the right arrow is pressed, but there’s one situation when you don’t want the clock to keep moving: when Ms. Finebean is leaning against a desk, as in figure 13.41.
This script stops the clock from moving when this situation happens so you can keep up the illusion that it is Ms. Finebean who is running forward rather than reality—all the objects are coming toward Ms. Finebean:
Figure 13.42 shows the whole script.
The door is like the flagpole and castle at the end of each level of Super Mario Bros. It’s the exit into the next level, though in this case, it’s the end point for the game. There are two scripts that power the door: a cloning script and a game stopping script. Keep the blue box around the door sprite in the Sprite Zone for both scripts.
Like the kindergarteners, desks, and clocks, the door needs to appear at a certain time. The door in figure 13.43 signals the end of the game.
This script will tell Scratch to put the door on the screen at a certain time:
Check your work against figure 13.44.
There are three ways for the game to end: run out of lives, run out of time, or reach the door, as in figure 13.45.
This script moves the door toward Ms. Finebean, but it also states what will happen when the teacher walks through the door.
That’s it! The last script in figure 13.46 officially ends the game.
Try playing your version of School Escape. Ours worked perfectly on the first try, but you may run into some bugs with your game. Whenever you run into bugs, first check that your sprites are centered. Open each sprite in the Art Editor and center it using the tool in the top right corner of the canvas.
Next, make sure the teacher is on the top layer. She should be on the top layer because you coded it into the game, but it can’t hurt to click the teacher on the Stage and move her a tiny bit to ensure she is the top sprite.
There are many variables in this game, including a few that create the illusion of movement by counting upward as the right arrow is pressed and using that value to stand in place of an X-coordinate. But there are also lists, which are similar to variables in the sense that they are tracking information, though in the case of lists, there are multiple items. Lists (also known as arrays) are an important part of programming—not only in Scratch but in every form of coding, from storing your friends list on a social media site to tracking a character’s inventory in a game.
Single-screen platformers are already pretty complicated, but you can still kick it up a notch by trying out some of these challenges.
The lists contain 30 (Kindergarteners) or 14 (Desk Positions) items. You can add more items to the list so that students or desks spawn more frequently. You can also lengthen the game by adding to the end of the list, though make sure you also tweak the value that spawns the door or the player will exit the game before they reach the later items.
Play with the timer and the clocks, not only rewarding or removing different time amounts, but also creating other reasons to deduct time. If Ms. Finebean stands on a desk, should it remove a few seconds? If she jumps over a kindergartener, should it add a second back on the timer?
Add new obstacles. What if she has to jump over lunch boxes or book bags? Use the code in the game to tweak it for additional sprites.
What if the door leads to the next level just as the flagpole in Super Mario takes the player to another screen? How could you design multiple single-screen platformers to hook together into one game?
Before you return to helping Ms. Finebean get to her brother’s wedding, take a moment to reflect on which common computer science ideas from chapter 3 were used in this game:
Another game that uses eight out of eight common programming ideas! Additionally, in School Escape you learned
After you enjoy a few more rounds of jumping over kindergarteners, allow Ms. Finebean to get to her wedding and turn the page.
Congratulations! You are officially a game maker with five games under your belt. But wait! What if you want a little more practice before you finish off this book? Once again, we created a set of extra practice chapters that push these eight core coding concepts a little farther. In the racing game Mermaid Splash, you’ll make the sprite’s arms look as if they’re swimming as she moves through the water, dodging jellyfish and turtles.
If you’re reading the print version, this extra practice can be downloaded at the Manning site, or you can register your book and download a free e-book version that contains the extra practice. If you’re reading the e-book version, this extra practice is at the back of the book.
But you may be ready to move on. You have internalized eight core concepts of computer science and can probably recite them in your sleep. The final chapter will help you take a leap into the Scratch community, design new games, and maybe even turn your eye to other coding languages.
3.129.148.210