In 1982 the world met Pitfall Harry, and the great platformer tradition was born. Activision’s game has a little pixel man jumping over rolling barrels, landing on crocodile heads to cross lakes, and climbing down ladders to get to a second, underground level that runs parallel to the main level of the game but contains scorpions that can stop Harry in his tracks. Jumping, landing, racing, and collecting are the hallmarks of the platformer genre. Think of Pitfall as a jumping puzzle game or video obstacle course—one that would challenge even an American Ninja Warrior. Harry has to leap over rattlesnakes and land on the other side. He leaps over holes in the ground, leaps up to grab onto a vine, and leaps over the scorpions when he’s underground. He’s also racing against a clock to collect treasure. Harry only has 20 minutes to collect all the gold and silver bars or diamond rings scattered across the game.
Beach Blast is a simple platformer, so you’re not going to focus on time limits or treasure collecting in this game (though you will in the next platformer, School Escape). Instead you’ll focus on a single idea shared by every platformer on the market, from Pitfall to Leo’s Fortune to Geometry Dash: simulated gravity.
What happens when you jump up? You don’t hang out in the air, looking around until you force yourself back down. Gravity instantly starts pulling you back toward the ground after liftoff. You need to program gravity into video games so that what goes up will eventually come down, as seen in figure 11.1.
X and Y coordinates are the stars of Beach Blast, noting where Buffy is on the screen and bringing her back down to the ground every time she jumps. Beach Blast is also a side-scroller, which means Buffy can run to the side and she won’t hit the end of the Stage. Instead, she’ll keep on going as the game scrolls out in that direction. You’ll accomplish this by having the backdrop change every time Buffy touches the left or right side of the Stage.
One last thing to consider with Pitfall, which is a mark of any good game, is that the skills you’ll need on the harder levels are all learned and practiced on the easier levels. Jumping, for instance, has lower stakes on the first Pitfall screen because the obstacles are rolling barrels, which remove points but don’t remove lives. Once the player has gotten a chance to practice jumping over barrels, Pitfall ups the stakes by making Pitfall Harry have to sail over deadly obstacles. Finally, once players have gotten the hang of jumping, they’ll have to jump and land precisely on the crocodiles to avoid being eaten or time their jumps to sail over the disappearing and reappearing sand pit.
As you make Beach Blast, think about that game design idea, allowing players to learn new skills on easier boards as you plan extra backdrops and obstacles into your game. For instance, programming Beach Blast follows this same step-by-step learning process, having players jump over sandcastles (stationary), crabs (moving), and finally onto the vine (definitely moving and the dismount needs to be well timed). If you insert new obstacles and backdrops, make sure to order them so the player can become comfortable with the mechanics of the game before the difficulty level goes up.
In this chapter, you will learn
This simple platformer will teach you everything you need to know in order to make more complicated platformers in the future. Ready to start coding?
It’s time for your pre-coding ritual. Get your sprites and the Stage ready for Beach Blast.
If you skipped chapter 10, either return to it and finish it or go to the Manning site and download the background and sprites for Beach Blast. The directions for importing are the same as chapter 5. You should have Buffy, a sandcastle, a crab, a vine, two side barrier lines, and three beach-themed backgrounds.
I used the Grow tool in the Grey Toolbar to increase the size of Buffy (20 clicks), the castle (25 clicks), and the crab (12 clicks). Then we duplicated both the crab and the sandcastle, as shown in figure 11.2.
Switch the background to backdrop1 (the basic beach backdrop) by clicking the thumbnail in the Sprite Zone and making sure the blue box is around the first backdrop on the Costumes tab. Position the two sandcastles on either side of the Stage, about an inch from the left or right wall. Look at figure 11.3 to see the Stage layout.
Beachy Buffy is the player character—the sprite on the screen controlled by the player. The player controls all of her actions using the arrow keys, and she has ten scripts to enable her to run, jump, fall, and grab the vine: a movement script, a falling velocity script, a falling script, a jumping script, a positioning script, a life deduction script, a background changing script, a zipline dismount script, and a two-part grabbing vine script. All scripts in this section are applied to Buffy, so go put the blue box around Buffy in the Sprite Zone and don’t move the blue box until you program the sandcastles 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.
Buffy can’t jump at the moment, but she also can’t even run back and forth along the sand, as in figure 11.4.
Let’s enable Buffy to run left and right:
Your script should look like the script in figure 11.5, and it will move Buffy five coordinate spaces to the left or right whenever the arrow keys are pressed. You don’t want Buffy to move too quickly and accidentally run into a sandcastle, so five coordinate spaces at a time is a perfect speed.
Simulating gravity is tricky. What happens to an object as it falls? It picks up speed. That includes people, like Beachy Buffy, as seen in figure 11.6.
what is velocity?
what happens when you jump? Go ahead, get up from your chair and jump. Your feet leave the ground and now there is a Y coordinate marking your feet’s location in the air. But wait, you don’t hang in the air indefinitely. You start falling back toward the ground. You fall faster than you rise due to gravity. That is your velocity—the speed at which your body (or, in the case of a game, the sprite) is moving in a certain direction. Your Y velocity is how fast the person (or sprite) is falling within the Y coordinate, which is directionally up or down.
This first of three simulated gravity scripts sets the changing rate at which Buffy will fall:
why make the y lowercase but the word Velocity uppercase?
programmers often write multi-word variables in something called camelcase, mostly because your words will start to resemble the humps of a camel. The first letter or word is always lowercase (in this example, y). The first letter of the next word is uppercase (in this example, Velocity). If there were more words smushed together to make this variable, all the other words would always start with an uppercase letter, too.
If Buffy is not touching the sand—if her feet are in the air—then she will start to fall, with her speed increasing by half of a coordinate each loop. The script in figure 11.7 will work together with Buffy’s falling script.
You now know how Buffy’s speed will incrementally increase as she falls, but you haven’t written a script that allows her to return to the sand, as in figure 11.8.
This script will allow Buffy to fall if she jumps up in the air:
That final instruction is true whether Buffy has stopped falling or hasn’t fallen at all. Anytime Buffy is on the sand, the Else portion in figure 11.10 will be true, and anytime Buffy is in the air, the If portion is true.
Buffy can return to the sand (falling script) and even have her body move in a realistic manner (falling velocity), but right now, her body can’t get up in the air so those two other scripts can kick into action. She is stuck on the sand, as in figure 11.11.
Make a script that will allow Buffy to jump off the sand.
Until you run the game, you won’t know whether the nine coordinates in Step Four will be enough to take Buffy over the crabs and sandcastles. In the future, you may need to increase this number if it’s not high enough, or you can decrease it to make it even harder for Buffy to sail over obstacles.
Check the script in figure 11.12. You’ll notice that the yVelocity variable is now set to a positive number (9) rather than a negative number. This is because you want Buffy to have the ability to leave the sand and go up. It’s working with the motion block to bring Buffy nine coordinate spaces into the air (because positive numbers go up), versus having Buffy receive conflicting information about where her body should go. This script works in harmony with the last two scripts to perform an action.
Geocaching is a game where people hide caches (usually a small box containing tiny toys or a log book) around the world and give the coordinates for the cache. Players can plug those coordinates into a GPS device and try to find the treasure. Similarly, Letterboxing is a game where people hide caches and write clues for the treasurer seeker, talking about things they’ll see along the way such as a tree or a bridge. Using latitude and longitude coordinates is an exact way of giving directions; using landmarks is not because the landscape may change over time. Scratch likes exact directions, programmed into a game using X and Y coordinates. Scratch can’t understand directions like “jump over the sand-castle,” though it can understand “go up nine coordinates, go to the right five coordinates, and then go down nine coordinates.” Going geocaching can help you to build better Scratch games because working with GPS coordinates will help you to think about the Stage in X and Y coordinates.
You have three backdrops, but which one will be on the screen when the game begins? Moreover, Buffy needs a starting point, a place to stand when the game begins, as in figure 11.13. Finally, you need to determine how many chances the player will get.
This script ensures that Buffy begins on the first backdrop and gets three chances:
The script in figure 11.14 sets three important elements at the beginning of the game: the backdrop, Buffy’s position, and the number of chances.
Currently Buffy can touch the obstacles, such as the sandcastle or the crab, and nothing happens, as in figure 11.15.
This script removes a “life” every time Buffy touches a sandcastle, crab, or water obstacle. It’s a long script, so check your work against the examples:
This long script is done! Look at figure 11.18 to make sure all your blocks are in the correct places.
Buffy can currently run to the left or right, but nothing happens when she touches a wall, as you can see in figure 11.19.
This script will cause the backdrops to change whenever Buffy runs to the edge of the Stage. It’s another long script, so check your work along the way:
Figure 11.23 shows the completed script. Because you are a seasoned Scratch programmer, you know that you still need to make a receiver script to catch the message that this script sends out to the game. That receiver script will change the backdrop once it receives this broadcasted message that Buffy has touched the left or right wall. Additionally, this script sets actions to occur if Buffy happens to touch the right wall while she’s holding onto the vine. They’re all negative actions because Buffy is supposed to drop off the vine when she reaches the other side of the water obstacle.
Soon you’ll code Buffy to jump up so she can catch the zipline or vine, as she’s done in figure 11.24, but before that happens, you need to make it so she can drop off the zipline. You know from the last script that bad things happen if she’s still holding onto the zipline when she touches the right wall.
This script will allow Buffy to release the vine after she is past the water obstacle:
Look at figure 11.25 to see if you have your blocks in the same order.
The last script gave Buffy the ability to drop off the vine after she gets over the water, but you also need to give Buffy the ability to grab onto the zipline when she jumps up. Currently, she can keep jumping and touching the zipline, but she isn’t able to grab it as she can in figure 11.26.
The first part of this script detects whether Buffy has touched the vine. The second part allows Buffy to grab and remain on the zipline until you want her to drop off:
The first part of this script checks if Buffy is on the vine. If she is touching the vine, it broadcasts a message to the second part of the script to keep her on the vine. Check the first part of the script in figure 11.27 and get ready to write the second part of the script.
The second part of the script is written separately as a new script, but these two parts work in tandem (together) with each other. The second script keeps Buffy on the vine until the player presses the down arrow to return to the sand:
The second part of the vine grabbing script is complete. Check your work against figure 11.29 and breathe a huge sigh of relief. You’ve finished programming Beachy Buffy.
Beach Blast has stationary sandcastle obstacles, which are like Pitfall’s flickering fires. Buffy has to jump over them to get to the next backdrop. A single script powers the sandcastles. To program them, make sure the blue box is around the first sandcastle in the Sprite Zone.
The sandcastles are always on the screen, though you can only see them on certain backdrops. They appear when Buffy is running on the basic beach backdrop, and they disappear when Buffy runs into the other two scenes. They are still technically on the screen, though they are hidden, as you can see from the empty squares that appear on the Stage in figure 11.30 whenever I click the sandcastles in the Sprite Zone.
This script will cause them to appear or disappear depending on the backdrop:
The script in figure 11.31 sets up a simple condition: if backdrop1 is on the screen, the sandcastles will be visible, or else if it’s any other backdrop, the sandcastles will be hidden.
There are two sandcastles in the game, but you don’t need to build the script a second time. Instead, apply the script to the other sandcastle by clicking the When Flag Clicked block and dragging all the blocks (they should move as a set) to the Sprite Zone. Hover over the other sandcastle sprite, as in figure 11.32, and release the mouse button.
The script should now appear in both sandcastle sprites’ Script Areas. Click back and forth between the two sprites in the Sprite Zone and check the Script Area to make sure the same script is in both places.
Beach Blast has two snapping crabs that run back and forth along the sand, similar to the scorpions in Pitfall. You only need to write these scripts for one crab, and then you will transfer them to the other crab using the hover method. Make sure the blue box is around the first crab in the Sprite Zone, and keep it on the crab for both scripts in this section. The crab requires two scripts: a similar show or hide script as used with the sandcastles and a movement script.
The same script that you used to show or hide the sandcastles also works with the crabs, which remain on the screen but are only seen on backdrop2 (as opposed to the sandcastles which are seen on backdrop1). Rather than remake the script, go to either sandcastle sprite and drag the script into the first crab in the Sprite Zone.
The script needs to be tweaked in order to show the crabs at the correct time. Change backdrop1 to backdrop2, as in figure 11.33.
Once you’ve made this change, drag this tweaked script and hover over the second crab in the Sprite Zone. Release and check that both crabs have the same script, showing backdrop2 as the condition.
Now that Buffy can jump over stationary sandcastles, it’s time to give her a harder obstacle—moving crabs, as seen in figure 11.34.
The crabs move back and forth on either side of the hole. Remember, the crabs are technically on the screen at all times, though you can only see them on backdrop2. This script will program the crabs to only move when you are on the crab’s backdrop. You can code either crab sprite because you will duplicate and tweak the code for the other sprite:
That’s the entire script. Check your work against figure 11.36.
One crab is now to the left of the hole, moving back and forth when Buffy is on backdrop2. But what about the second crab? You’re going to use the hover method to drop the script into the second crab’s Script Area and then tweak a single number to place it on the right side of the hole.
By turning the –148 into a 48, as seen in figure 11.37, the second crab turns up to the right of the hole, making a true obstacle course for Buffy. You may need to tweak that number again when you start troubleshooting the game, but for now, you should have the two scripts in both crabs in the Sprite Zone.
The obstacle sprites are out of the way, but now you need to program a tool that will take you over the pool of water. The water is drawn on the backdrop, but the vine is a sprite that moves.
According to David Crane, the maker of Pitfall, the vine is a one-pixel sprite that has its angle constantly computed by the game. The position of that sprite keeps changing due to some fancy math going on behind the scenes in the game. To simplify the code, your vine for Beach Blast is not going to swing back and forth diagonally. Instead it will move horizontally the moment Beachy Buffy jumps out and grabs it.
Make sure the blue box is around the vine in the Sprite Zone, and keep it on the vine for all scripts in this section. The vine requires three scripts: a starter script, a positioning script, and a movement script.
Like the sandcastles and crabs, the vine is always on the screen, but you can only see it on the water backdrop (backdrop3), as shown in figure 11.38.
This script hides the vine if Buffy is not on backdrop3, and shows the vine dangling from the top center of the Stage if she is on the water background:
Check your script against figure 11.40 before you move on to positioning the vine.
The last script sent the vine to the center of the Stage, over the water, but the vine isn’t a stationary object like the sandcastles. The vine moves during the game, as shown in figure 11.41.
This script will track where the vine is on the screen and make that X position or Y position the value of the zipX or zipY variable. For instance, when the vine is at X:55, the value of zipX is 55:
The script in figure 11.43 is now complete and will keep checking the position of the vine and setting the value of the zipX and zipY variables according to where it is on the screen. This is important to know because Buffy needs to move with the vine, so the value of those variables will affect where Buffy is on the screen, too.
But wait! The vine can’t move yet as it’s doing in figure 11.44, so that last script has nothing to track.
This script will make the vine move over the water hazard and keep going until it reaches the right side of the Stage or Buffy jumps off:
Does your script look like the one in figure 11.45?
All the sprites have been programmed, but you still have a few odds and ends to cover before you can begin troubleshooting the game.
Although you’re accustomed to having a barrier line at the bottom of the Stage, this game uses barrier lines on the left and right sides of the Stage to help the background change as Buffy runs across the screen. Additionally, you’ll need to write two short programs for the backdrops themselves to ensure the correct one pops up when Buffy runs to the left or right side of the Stage.
The left and right lines need to be placed on the left and right sides of the Stage to ensure that they’re in the correct positions, as seen in figure 11.46. Start with the Left Line (and make sure the blue box is around it in the Sprite Zone).
Make the usual, brief positioning script in order to send the left line (that was drawn on the far left side of the canvas) into the proper position:
You need to use the hover method to send this script to the other line, too. Drop this script into the Right Line sprite in the Sprite Zone and check that this tiny script appears in both places before you move on to programming the backdrops. Both values should still be set to zero (0).
Although you’ve tucked code into the backdrop before, these two scripts will be a little different. In order to write a program that applies to all the backdrops, navigate to the Sprite Zone and make sure the blue box is around the thumbnail of the backdrop (marked Stage) on the far left side of the Sprite Zone. You should see a blank Script Area open up on the right side of the screen. This is where you’ll be dragging your blocks.
You need to write a program that allows Buffy to return to the previous backdrop if she runs to the left. If she’s on the crab backdrop (backdrop2) and she runs to the left, she will find herself again on the sandcastle backdrop (backdrop1):
The script in figure 11.49 switches the backdrop if Buffy goes to the left. But what happens if she goes to the right?
Making the backdrop switch when Buffy goes to the left required a little workaround script with a real-time subtraction problem. Making the backdrop switch when Buffy goes to the right is a little easier due to an included Scratch block that allows for backdrop switches:
That’s it! Figure 11.50 shows the final script. It’s time to play Beach Blast.
There are two types of platformers: multi-screen platformers and single-screen platformers. Pitfall is a multi-screen platformer. When you move Pitfall Harry, he switches screens—256 of them, to be exact. Making a game like Pitfall on Scratch means making a lot of backgrounds. Super Mario Bros is a single-screen platformer. When you move Mario, he’s running in place and the screen is moving. Don’t worry, you’ll get a chance to try your hand at a single-screen platformer with School Escape.
The first time I played my version of the game, the crabs were floating in the sky! It’s difficult to know the exact X and Y positions of sprites, especially if you didn’t draw your crabs in the center of the canvas. The first step will be to center both of your crabs. Turn back to chapter 9 if you need a reminder of how to center sprites. If your crabs are still above the sand, tweak their Y position so they can be running along the surface. Any sprite positioned with code can be helped by centering.
The second problem encountered was a layering issue with the vine. Buffy could grab on, but she couldn’t drop off. If you run into this problem, navigate Buffy to backdrop3 by running to the left after starting the game. Click the stop sign above the Stage to stop the game, and click the vine, moving it slightly so it becomes the top layer.
The third possible issue is tweaking the values in various blocks, such as the Y-position of Buffy’s feet on the sand at 50, or the placement of the vine at 120. Look back at all the times you were told to play with the value and adjust the numbers. Although I didn’t need to adjust any values, you may want Buffy to jump higher or for the vine to hang lower.
X and Y coordinates featured heavily in Beach Blast. You even made variables that will track the X and Y coordinates of the vine, ensuring that Buffy can travel with the vine, over the water. You can always check a sprite’s X and Y by touching your mouse pointer to the sprite and looking at the X and Y position listed under the Stage.
Pitfall has 256 screens. Beach Blast only has 3. Expanding the game means hours of play.
Create new backdrops. You won’t need to write any additional code, though you will need to add obstacles to these backdrops for Buffy to jump over. You can either use the same obstacles or create other objects or animals found on the beach.
Right now, Buffy always regenerates on the left side of the current backdrop. Can you figure out a way to use a variable that will have Buffy start from the left side or the right side depending upon where she lost a turn on the Stage?
Create another character, either by drawing them on the backdrop or making a sprite. Have Buffy win the game by reaching this character on the last screen.
Add a points system to the game. Make shell sprites and scatter them through the game. Every time Buffy collects one, it adds to her overall score. You can also make obstacles that will remove points, such as stepping on a towel or running into a beach umbrella.
Play with the difficulty level of the game. You can change the height that Buffy can jump to make getting over sandcastles harder or easier. You can place obstacles such as the sandcastles closer together, and you can play with the speed at which the vine moves or Buffy runs to make the game more challenging. If you add more levels by adding more backdrops, you may also want to make the obstacles more difficult, such as increasing the speed or range of the crabs.
Before you jump back to running down the beach, take a moment to reflect which common computer science ideas from chapter 3 were used in this game:
Another seven out of eight common programming ideas! Additionally, in Beach Blast you learned
Pitfall influenced Super Mario Bros, so it’s fitting that you will use everything you learned in this platformer to make a single-screen platformer called School Escape. If you’re ready to start making your player character teacher jump onto desks and over kindergarteners, turn the page.
3.129.148.210