Cheese Chase
50 C H E E S E C H A S E Only the ghosts can move through walls. How to build Cheese Chase Cheese Chase by SuperMimi (unshared) Some of the world’s first and most popular computer games were Score 30 maze games. In a maze game, quick thinking is essential as you race The beetles are small around tight corners, avoiding enough to let the mouse monsters and collecting treats. squeeze past. AIM OF THE GAME Mimi the mouse is hungry and stuck in a maze. Help her find the cheese but avoid the evil beetles. And watch out for ghosts—the maze is haunted! ◁ Mimi You play the game as the mouse. Use the arrow keys on your keyboard to make her run up, down, left, or right. ◁ Beetles Beetles scuttle along the edges and make random turns when they hit a wall. ◁ Ghosts Ghosts can float through walls. They can appear anywhere without warning and then disappear.
HOW TO BUILD CHEESE CHASE 51 Click the green flag Click the GAME CONTROLS to start a new game. stop sign to end a game. Players use the arrow keys on a keyboard as game controls. High Score 90 The game remembers the highest score. Can you beat it? Collect blocks of cheese to score points. ◁ Chase the cheese There are countless ways to change Cheese Chase and create your own version of the game. For instance, you can adjust the speed, increase the number of beetles, and change the shape of the maze. You can create a maze with any arrangement of walls.
52 C H E E S E C H A S E Keyboard control Many games let the player use the keyboard to control the action. In Cheese Chase, the player uses the arrow keys on the keyboard to move Mimi the mouse around the stage. Start by creating a keyboard control script for Mimi. 1 Start Scratch and choose “New Project”. Delete the 2 Click the “New sprite” symbol and cat by right-clicking and selecting “delete”. If you look through the sprite library use a Mac computer, instead of right-clicking for Mouse1. Click “OK”. The mouse you can hold down the control key and click. should now be on the stage and in the sprites list. Sprites New sprite: Sprite 1 Click here to open the sprite library. duplicate delete The mouse is save to local file highlighted in hide blue to show it’s your current sprite. Mouse1 3 Add this script to the mouse to Everything inside move the sprite up the stage the “forever” loop using the up arrow key. To find repeats endlessly. the different-colored blocks, remember to click the different when clicked Click the triangle and select options in the Scripts tab. Read “up arrow” to choose the through the script carefully and forever correct keyboard key. think about what it does. Run the script by clicking the green if key up arrow ▾ pressed? then flag. You should be able to move This block makes the the Mouse sprite up the stage point in direction 0 ▾ mouse face upward. using the up arrow key. move 5 steps The blocks inside the This block makes “if then” block only run the mouse move. when the answer to the question is yes.
GAME PROGRESS 1 1 % 53 4 To make the other arrow keys work, add three more “if then” blocks GAME DESIGN like the first one, but choose a different arrow key and direction for each one. To move right, select the right arrow key and set Controllers the direction to 90. For down, set it to 180. For left, set it to –90. Read through the finished script to make sure you understand it. In Cheese Chase, we use the arrow keys to control the game, and in when clicked Each “if then” block should Star Hunter we used the mouse. forever be inside the “forever” loop, Other computer games use very different types of controller. but not inside any of the other “if then” blocks. ▷ Console controller if key up arrow ▾ pressed? then Console controllers usually have two point in direction 0 ▾ small joysticks move 5 steps controlled with your thumbs, along with a range of other buttons. They if key down arrow ▾ pressed? then are ideal for complex games that need a lot of different controls. point in direction 180 ▾ Make sure the arrow move 5 steps key matches the ▷ Dance mats direction value. You control the game by stepping on giant if key right arrow ▾ pressed? then keys. Dance mats point in direction 90 ▾ are good for games move 5 steps involving physical activity, but they if key left arrow ▾ pressed? then don’t give fine control. point in direction –90 ▾ move 5 steps ▷ Motion sensor These controllers 5 Now click the green flag to run the script. You should be able detect movement, to move the mouse in all directions around the stage using the which makes them arrow keys. If it’s not working, go back and check all the steps. ideal for sports games where you swing your arms to use a racquet or bat, for example. ▷ Camera Special cameras in some game consoles allow the player to use body movements to control the game.
54 C H E E S E C H A S E Using the paint editor My cheese piece is a masterpiece! Cheese Chase now has its mouse heroine and she’s hungry, but there’s no cheese yet for her to chase. The sprite library in Scratch doesn’t include a picture of cheese, so you’ll need to make one yourself. You can do this with Scratch’s paint editor. 6 Create a blank sprite by clicking the small paintbrush Undo Use this tool to symbol above the sprites list. This will open Scratch’s Redo set the center of paint editor in a screen like the one below. Make sure that “Bitmap Mode” is selected at the bottom. the sprite. Crop Flip costume1 Clear Add Import Paintbrush tool Use this tool Straight line tool to fill a shape Rectangle tool Circle tool with color. Use this tool Eraser to select part of a drawing. Outline Solid Color palette Use this tool to 100% duplicate part of Bitmap Mode a drawing. Convert to vector Line thickness Use this tool to draw wiggly lines. 7 Now draw the cheese. Use the paintbrush tool and choose black from the color palette Use this tool to at the bottom of the screen. Draw the outline draw straight lines. of the cheese. If you want perfectly straight lines, use the line tool. Your cheese drawing might be too big at first, but you can make it smaller later.
GAME PROGRESS 2 2 % 55 8 If you like, use the circle tool to draw 9 To add color, choose yellow and use the holes in the cheese. Make the circle fill tool to fill in the cheese. If your color spills out an outline rather than a solid circle and fills the whole background, click on the “undo” by choosing the outline option at button. Make sure your lines don’t have any gaps, the bottom. then try again. Use this tool Select this to draw holes. tool and click inside the shape to fill it with color. The background should stay this pattern to show it’s transparent. 10 Now set the center of your cheese. 11 To keep score, we need to create a variable called Click the “Set costume center” tool in “Score”. Choose Data in the blocks palette and the top right and then click the middle click on “Make a Variable”. Type the word “Score” of the cheese. The cheese is now ready in the pop-up box. The score counter will now to be added to the game. appear on the stage. Score 0 Select this This shows the tool and click number in the in the middle “Score” variable. of the cheese. when clicked 12 Now add a script to make the cheese appear in a random location. When the mouse touches it, there will be a “pop” noise, the player set Score ▾ to 0 will score ten points, and the cheese will move to a new location. forever Run the script and try catching the cheese. It should be easy— but that’s because you haven’t added enemies yet... go to x: pick random –220 to 220 y: pick random –160 to 160 wait until touching Mouse1 ▾ ? This block moves the change Score ▾ by 10 Cheese sprite to random play sound pop ▾ locations around the stage. The script pauses here until the mouse arrives.
56 C H E E S E C H A S E Getting spooky Adding our first enemy to the project will make Cheese Chase into a proper game. A ghost is a good first enemy for this game because it can float through walls, so you won’t need to change the ghost’s script when we add the maze. 13 Click the “New sprite” symbol and 14 Add the following script to the ghost to make select a ghost sprite from the it chase the mouse. If it touches the mouse, the sprite library. Click “OK” to add it to the project. game will end. You might recognize most of this New sprite: code from Star Hunter. Click here to The ghost starts open the library. off hidden. The ghost is now your selected sprite. when clicked This block stops the Ghost1 hide ghost from spinning upside down. set rotation style left-right ▾ forever This block point towards Mouse 1 ▾ makes the move 1 steps ghost move slowly. if touching Mouse 1 ▾ ? then stop all ▾ Starts a new script. This block ends the game if Ghost1 will now the ghost touches the mouse. have two scripts. when clicked 15 Now add a separate script to make the ghost appear and disappear for random amounts of time. The “hide” forever This block keeps block makes the sprite disappear, and “show” makes it hide the ghost hidden appear again. for 5–10 seconds. secs wait pick random 5 to 10 go to x: pick random –220 to 220 y: pick random –160 to 160 show This block makes the ghost appear wait pick random 3 to 6 in a random place on the stage. secs This block keeps the ghost on screen for 3–6 seconds.
GAME PROGRESS 3 3 % 57 16 Next, add music to the game. We usually add music GAME DESIGN to the stage rather than a sprite. Click the stage area on the left of the sprites list to highlight it in blue. Click Music in games the Scripts tab and add the following script to play a sound over and over. Click “Sound” in the blocks Watch a scary film with the sound palette to find the “play sound until done” block. off and it’s not so scary anymore. Games are the same—the music when clicked sets the mood. A fast-paced game will use music with a forever driving beat to make you hurry. A spooky game should have play sound pop ▾ until done haunting music to make you feel uneasy—happy, bouncy music The sound would break the spell. A puzzle repeats until game might have echoing, eerie the game ends. music to create a sense of mystery. Some games use music as a key 17 Now click the Sounds tab above the blocks palette. part of the game play, such as Click the speaker symbol to open the sound library. those where the player has to Select the category “Music Loops” on the left, then dance or push buttons in time choose the music “xylo1” and click “OK”. Repeat the to the beat. process to load “dance celebrate” into the game too. Sounds tab Scripts Costumes Sounds New sound: This opens the sound library. 18 Return to the Scripts tab and change the selected You never know where sound from “pop” to “xylo1”. Run the game and think I’ll appear next! about how it feels to play. Next try the sound “dance celebrate”. Which one is better? Click the triangle to choose the sound. forever play sound xylo1 ▾ until done
58 C H E E S E C H A S E Making mazes ENTER MAZE HERE Mimi the mouse can run anywhere she likes on the This opens the stage. Put a stop to that by adding a maze. The maze paint editor. will make it difficult for her to move from one place to another, adding an extra challenge to Cheese Chase. 19 The maze will be a sprite, not a backdrop, Sprites New sprite: because that makes it easier to detect when another sprite touches it. Draw Mouse1 Cheese Ghost1 Sprite1 it in Scratch’s paint editor. Click on the paintbrush symbol in the sprites list, then click on the blue “i” and rename the sprite “Maze”. 20 Now you can start using the paint editor. Make Name this Rename the sure “Bitmap Mode” is selected in the bottom sprite “Cheese”. sprite “Maze”. right. If not, click the “Convert to bitmap” button to change the mode. Choose the line tool Draw the maze in the and set the line width control to the middle. empty space here. Then pick a dark color for the maze walls. costume1 Clear Add Import Line tool Slide the Choose a line width color before control to you start the middle. drawing. Select “Bitmap Mode” before you draw the maze. 100% Bitmap Mode Convert to vector
GAME PROGRESS 4 4 % 59 21 Now draw the maze. Start by drawing the outside of Make sure that the the maze at the outer edge of the checkered drawing lines of the maze are area. Hold down the shift key on your keyboard to perfectly straight. make sure lines are perfectly vertical or horizontal. Then add the inside walls. costume1 Clear Add Import 22 Finally, we need to add a script to make sure 100% the maze is always in the center of the stage so it’s fully visible. With the Maze sprite Bitmap Mode selected, click on the Scripts tab and add Convert to vector the following script. 23 Run the project. You’ll find that Mimi can run through walls, but don’t worry because we’ll fix that later. when clicked go to x: 0 y: 0 At the center of the stage, x is 0 and y is 0.
60 C H E E S E C H A S E 24 Mimi, the ghost, and the cheese are all too 25 Now add a purple “set size to” block to the ghost’s big for the maze, so we need to shrink them. main script. Set the size to 35 percent. Add a “set Add the following blocks at the beginning size to” block to the Cheese sprite too, and adjust of Mimi’s script, before the “forever” block, the percentage until the cheese is about twice the and fill in the numbers below. size of Mimi. when clicked This makes Mimi about a third of her current size. set size to 35 % This sends her Mimi is half go to x: –200 y: 75 at the top left when the size of the point in direction 90 ▾ the game starts. cheese. This block makes her face right. 26 You might need to fine-tune your Maze Passages should be costume to make sure Mimi can fit through wide enough for Mimi all the passages with enough room to pass to pass her enemies. her enemies (which we’re going to add later). To alter the maze, select the Maze sprite and click the Costumes tab. Use the eraser tool to remove walls or the selection tool to move them. 27 If you use the eraser, be careful not to leave any 28 Add a background color to the flecks of paint behind because Mimi will stop if she game by painting a backdrop, not hits them. Check the corners of the maze for bumps the Maze sprite. At the bottom left that Mimi might get stuck on and remove them. of the screen, click the paintbrush symbol in the stage info area. This opens the paint editor. Make sure “Bitmap Mode” is selected at the bottom. Stage 1 backdrop New backdrop: Remove bumps with Click here the eraser tool. to paint the backdrop.
GAME PROGRESS 56% 61 29 Choose a color, Score 30 High Score 96 Score 30 High Score 96 select the fill tool , and then click on the backdrop to fill it with color. Score 30 High Score 96 Score 30 High Score 96 Try different colors to see which one looks best in the maze. GAME DESIGN Walls restrict movement. Space in games How the obstacles in a game are laid out has a big effect on how you play. A maze is the perfect obstacle to demonstrate this. △ Open space △ Closed-in space △ Balanced space The player can move in any The player is forced to move in a very This is what the maze in Cheese direction most of the time. limited way. Just one enemy patrolling Chase is designed to be. It limits A game like this needs fast- the corridors of this maze would make the player’s movement enough moving enemies or lots of life hard. The player has to think ahead to make the game interesting, enemies to make it challenging. to avoid getting trapped. but allows some freedom.
62 C H E E S E C H A S E Mousetrap Uh oh! Mimi can currently run straight through the walls of the maze like a ghost, but we want her to stay trapped inside the passages. Time to change her script. 30 Select Mimi and drag the if touching Maze ▾ ? then following blocks to an empty move –5 steps part of the scripts area. This set of blocks will make Mimi This block makes reverse if she runs into a wall. Mimi move five steps backward. 31 Insert the blocks four times into Mimi’s forever then main script. To make copies, right-click if key up arrow ▾ pressed? (or control-click if you use a Mac) on point in direction 0 ▾ the new blocks and select “duplicate”. Place the duplicates after each “move 5 steps” block. move 5 steps if key down arrow ▾ pressed? then point in direction 180 ▾ move 5 steps if touching Maze ▾ ? then move –5 steps if key right arrow ▾ pressed? then point in direction 90 ▾ Insert the new blocks move 5 steps four times into the mouse’s script. if key left arrow ▾ pressed? then point in direction –90 ▾ move 5 steps
GAME PROGRESS 6 7 % 63 Mimi moves five Ouch! Mimi moves five steps forward. steps backward. Touching the wall triggers ▷ How does it work? the reverse move. You might wonder why Mimi has to move five steps backward. The reason is that she normally moves forward five steps at a time. The backward move reverses the forward one, making her stand still. This happens so quickly that you don’t see her reverse. 32 If Mimi’s tail or paws touch a wall when she 33 Select Mouse2 in the sprites list and click turns around, she can get stuck. We can fix the Costumes tab above the blocks palette. this bug by making some changes to Mimi’s Choose “Convert to bitmap” at the bottom, costume in the paint editor. and then use the eraser tool to trim Mimi’s tail. If Mimi’s tail overlaps the wall, she might stop moving. 34 There’s another problem that we can fix. Every EXPERT TIPS sprite has a center point, but if this isn’t in the exact center, the mouse will wobble when its Bounding boxes direction changes and might overlap a wall and get stuck. Choose the “Set costume center” tool One of the big challenges that game and then click in the exact middle of Mimi to programmers face is detecting when correct her center point. sprites with complicated shapes collide. Even in simple 2D games, Set costume center collision detection can cause problems, such as sprites getting Mouse1 Clear Add Import stuck or solid objects merging. A common solution is to use “bounding boxes”—invisible rectangles or circles that surround the sprite. When these simple shapes intersect, a collision is detected. In 3D games, spheres or 3D boxes can do the same job.
64 C H E E S E C H A S E Beetle mania STOP Now for Mimi’s main enemies: a small army of evil beetles that scurry around inside the maze. If she bumps into one, the game ends. 35 To make the beetles move automatically, you need to create a sequence of steps for them to follow. Programmers call this an algorithm. Our algorithm will tell each beetle to move forward until it hits a wall. Then it will stop, turn, and move forward again. 36 Click the “New sprite” symbol and choose the Beetle sprite from the library. The beetle is Beetle 38 Run the script. You might notice a glitch: the now your beetle always turns right and ends up going around in loops. We need to change the script so that the selected sprite. beetle turns left or right at random. To make a random choice, use a “pick random” block. Drag 37 Add the following script to set the beetle’s size, it to an empty part of the scripts area and set the location, and direction. It uses a “forever” loop to second number to 2. move the beetle, and an “if then” block to make it stop and turn right whenever it hits a wall. Type “2” here 1 when clicked You may need to change pick random 1 to 2 these numbers if the set size to 30 % beetle starts on a wall. Click the “pick random” block. You’ll see “1” or “2” appear in This block a speech bubble at random. makes the go to x: –130 y: 80 beetle reverse 39 Now drag the “pick random” block into the point in direction 90 ▾ and then turn first window of an “equal to” block. Then right when it drag the “equal to” block into an “if then forever hits a wall. else” block. move 5 steps ? then if touching Maze ▾ if then This is an “equal to” block. move –5 steps This block =1 turn 90 degrees makes the beetle else turn right. Type “1” in this window. pick random 1 to 2
GAME PROGRESS 7 8 % 65 40 Add two “turn 90 degrees” blocks to make the beetle GO turn left or right. Read through the script carefully and see if you can figure out how it works. if pick random 1 to 2 =1 then turn 90 degrees else 90 degrees turn 41 Remove the“turn 90 degrees”block from the beetle’s original script and EXPERT TIPS put the“if then else”block in its place, as below. Run the project and watch what happens. Check there’s enough room for Mimi to squeeze past the if then else beetle. If not, adjust the maze in the paint editor. The “if then else” block is just like when clicked an “if then” but with an extra trick. A normal “if then” asks a question set size to 30 % and runs the blocks inside only if the answer is yes. The “if then else” go to x: –130 y: 80 block can hold two groups of blocks: one to run if the answer is point in direction 90 ▾ The blocks inside the yes, and another if the answer is “if then” block run no. The words “if”, “then”, and “else” forever only when the beetle are used in nearly all computer touches the maze. languages to make decisions between two options. move 5 steps then if touching Maze ▾ ? The blocks inside the first move –5 steps gap run if the answer is yes. if pick random 1 to 2 =1 then if then turn 90 degrees else 90 degrees Question turn else The blocks inside the second gap run if the answer is no.
66 C H E E S E C H A S E I have a message for you… Sending messages 43 Now give the message a name. The next step is to make the beetle end the game if Mimi Select “message1” in the “broadcast” bumps into it. Instead of using another “touching” block block, choose “new message”, and in Mimi’s script, you can use a message. Scratch lets you type “GameOver”. send messages between sprites to trigger scripts. The broadcast message1 ▾ beetle will send a message to Mimi that stops her script. message1 new message… 42 Add the “if then” blocks shown below to the beetle’s script. The new blocks check whether the beetle is New Message touching Mimi and, if it is, send a message. Select Message Name: GameOver “Mouse1” in the “touching” block. OK Cancel when clicked Type \"GameOver\". set size to 30 % go to x: –130 y: 80 point to direction 90 ▾ forever move 5 steps if touching Maze ▾ ? then = 1 then move –5 steps if pick random 1 to 2 turn 90 degrees else 90 degrees turn Select “Mouse1”. if touching Mouse1 ▾ ? then broadcast message 1 ▾ This block is found under Events. It sends a message when the beetle hits Mimi.
GAME PROGRESS 8 9 % 67 44 Now add an extra script to Mimi to receive the when I receive GameOver ▾ This block message. Drag the following blocks to an empty stop other scripts in sprite ▾ stops Mimi’s part of her scripts area. Try the game out. Mimi main script. should stop moving when she touches the beetle, all but the beetle will continue to move. Later we’ll this script use a message to show a “GAME OVER!” sign as well. other scripts in sprite 45 The game needs more beetles. Copy Sprites the Beetle sprite by right-clicking on it (use control-click if you work on a Beetle info Beetle3 Beetle4 Mac) and then choose “duplicate”. duplicate Make three new beetles. These will all have the same script. See what happens when you run the project. delete Select “duplicate” to save to local file create new beetles. hide 46 You’ll need to change the numbers in the “go to” EXPERT TIPS blocks for each new beetle so they don’t all start Messages in the same place. Starting in different corners Messages provide a neat works quite well. Experiment! Beetles start way of making sprites react to each other. We could in corners. have made the mouse check if it’s touching a Score 30 beetle, but that would mean adding “if then” and “touching” blocks to Mimi’s script for all four beetles. By using messages, we can add more enemies without changing Mimi’s code.
68 C H E E S E C H A S E New Variable Variable name: High Score High score For all sprites For this sprite only You can make a game more fun by adding a high score for players to beat. We create this in the same way as the score tracker: by making a variable and displaying it on the stage. 47 Select Data in the blocks palette. Click “Make a Variable” and create a new variable called “High Score”. A new block will appear, and the high score counter will appear on the stage. Drag it wherever you like. OK Cancel when clicked 48 Now add an extra set of blocks to the Cheese sprite’s “forever” loop to test for a new high score set size to 70 % each time the player gains points. Run the project and see if anyone can beat your high score. set Score ▾ to 0 forever go to x: pick random –220 to 220 y: pick random –160 to 160 wait until touching Mouse1 ▾ ? change Score ▾ by 10 Find the “more play sound pop ▾ than” block in Operators. if Score > High Score then The “if then” block tests set High Score ▾ to Score whether the high score has been beaten. If so, the score becomes the new high score.
GAME PROGRESS 100% 69 Game over! Don’t forget to check At the moment, the only signal the game has ended is that the mouse the sprite’s stops moving. You can add a finishing touch to any game by displaying center with a large, bold “GAME OVER!” sign. To do this you need to create a “Game the set Over!” sprite and use the “GameOver” message to make it appear. center tool. 49 Click the paintbrush costume1 Clear Add Import symbol in the sprites list to create GAME Use the a new sprite with the OVER! selection paint editor. Using tool to ”Bitmap Mode”, draw Font: Donegal 100% enlarge a rectangle and fill it Scratch ▾ Gloria the text. with a dark color. Helvetica Vector Mode Now switch to “Vector Marker Convert to bitmap To change Mode”. Choose a bright Mystery the font, color and use the text Scratch highlight tool to type “GAME the text OVER!” in the rectangle. first. Change the font to “Scratch” and use the selection tool to make the text large. Change the font to Scratch. 50 You don’t want the when clicked 52 Run the game. You should now see “GAME OVER!” sign hide the “GAME OVER!” sign when you to show until the get caught by a beetle. To make game is really over, the sign work with the ghost too, so let’s hide it with replace its “stop all” block with a a script. Switch to “broadcast GameOver” block. the Scripts tab and add these blocks. This places the “GAME OVER!” 51 Now add a script sign in the middle. to make the sprite appear when the Score 30 High Score 96 game ends. You can use the same when I receive GameOver ▾ message that stops Mimi to go to x: 0 y: 0 GAME trigger this script. OVER! go to front show This block ensures other sprites are behind the sign.
70 C H E E S E C H A S E Hacks and tweaks Take Cheese Chase to the next level by tweaking the rules of the game and the way the sprites behave. You can also experiment with big changes that turn Cheese Chase into a totally different kind of game. ◁ Play on △ Add sounds You need to play the game a lot Jazz up the game with some sound effects using to find out what works and what the “play sounds” block when the ghost appears, can be improved. Get other people when the game ends, or when you get a high to play. You can adjust many score. There are lots of sounds in Scratch’s sound properties of the game until you library that you can experiment with. get the right configuration: a game where the abilities of the player and enemies are well balanced. ▷ Tweak timings ▷ Rocket power You might find Cheese Chase harder Add a power boost that hides all than Star Hunter. To make it easier, the enemies for ten seconds when you can make the beetles slower or the mouse touches it. To do this, make the ghost appear for a shorter you would need to add a new sprite time. You can also speed up Mimi. For and a message to trigger a hide- variety, try making each beetle run wait-show script in each enemy. at a different speed. when clicked ▽ Vanishing cheese forever For an extra challenge, make the cheese spend only ten seconds or so in each spot before moving to a new location. This will force the player to move fast. To do this, give the cheese an extra script with a “forever” loop containing a “wait 10 secs” block, followed by a copy of the “go to” block from the main script. wait 10 secs go to x: pick random –220 to 220 y: pick random –160 to 160 This block picks a random location for the cheese.
HACKS AND TWEAKS 71 ▷ Don’t touch the walls GAME OVER! Make the game end if Mimi touches the walls of the maze. Add a script to the Maze sprite to send the message “GameOver” if she touches the maze. This makes the game much harder. To make it even harder, try switching the player’s controls from the keyboard to the computer mouse. The game then becomes a test of a steady hand. Adding instructions ▽ Project page Type the instructions Players like to see a game’s The easiest way to include instructions here. instructions clearly before they is to simply type them in the instructions box start playing. Here are three on the project page. You need to log in to an 9 scripts ways of including instructions. online Scratch account to do this. 6 sprites ▽ Instructions sprite Cheese Chase DRAFT You can use the paint editor to create an Instructions sprite in the same way that by SuperMimi Instructions you created the Game Over sprite. Give it the following script to show the sprite Score 30 High Score 96 Tell people how to use your project at the start of the game and to hide (such as which keys to press) it once the player presses the space bar. Notes and Credits How did you make the project ? Did you use ideas, scripts or artworks from other people ? Thank them here. when clicked c Unshared Add project tags. 00 Modified: 8 Jun 2015 The Instructions sprite 11 appears until you press go to x: 0 y: 0 the space bar. go to front ▽ Speech bubbles Make your game characters tell the player show the instructions using speech bubbles. Add a “say” block to the start of Mimi’s script to wait until key space ▾ pressed ? explain the game. Don’t forget to add “wait” blocks to the enemies’ scripts—otherwise hide there’s a risk you’ll lose before you start! when clicked Add this “wait until” block Do as to the start of every other I say! sprite’s script so they don’t start moving until the game begins. set size to 35 % wait until key space ▾ pressed ?
Circle Wars
74 C I R C L E W A R S The timer shows The score rises or how long each falls as green and red How to build game takes. clones are touched Circle Wars by the player. Circle Wars Lightning reactions are essential in Circle Wars, a fast-paced game in by HappyShrimp321 (unshared) which you hunt green circles while being chased by red ones. The Score 8 game uses Scratch’s clones feature, which can turn a single sprite into Time 23.5 an army of sinister copies. The solid green circle lays AIM OF THE GAME the friendly clones. Move the blue circle around the screen using the mouse. Collect the pale green circles, but avoid the red ones that march toward you like a zombie army. The solid green and solid red circles drop clones of themselves as they roam around. Score more than 20 points to win and go below –20 to lose. ◁ Player The player is the blue circle. If you don’t keep moving quickly, the enemy circles will soon overwhelm you. ◁ Friends The friendly circles are green. When you touch one, you score a point and the circle disappears with a pop. ◁ Enemies Steer clear of the red enemy circles. Touch one and it takes three points off your score, before vanishing with a clash of cymbals.
HOW TO BUILD CIRCLE WARS 75 The solid red circle lays Click the green flag Click the stop sign GAME CONTROLS the enemy clones. to start a new game. to end a game. Use a computer mouse or touchpad to control this game. Player It’s all my own work! ◁ Is it art? Not all games use cartoon sprites. With its colored circles, this game looks more like a piece of modern art. Once you’ve built it, why not make up a story to explain the game. You can change the sprites, colors, and backdrop to tell that story. Don’t let them get you! Clones of the solid red Clones of the solid green circle chase the player. circle chase the player.
76 C I R C L E W A R S Creating the sprites First you need to create the three sprites for the main game. These are all simple colored circles, so you can draw them yourself. Start by following these instructions to create the player’s character—the blue circle. Player Friend Enemy circle circle circle 1 Start a new project and name it 2 To draw a blue “Circle Wars”. Click the paintbrush circle, first select symbol at the top of the sprites “Bitmap Mode” list to paint a new sprite. (bottom right). Then choose New sprite: blue in the color palette. Click here to paint Circle tool a new sprite. 3 Click the circle tool on the left 4 While holding down the shift and then select a solid color key (this gives you a circle (rather than an outline) at the rather than an oval), click with bottom left of the paint editor. the mouse and drag to draw a circle. The circle should be Select solid color. about the size of the cat’s head. When you’re happy with the circle’s size, delete the cat sprite (right-click on it and select “delete”). Look on the stage to compare the size of your new sprite to the cat. EXPERT TIPS Resizing the circle 5 You now need to center If your circle is too big the sprite. Select the “Set costume center” tool (top or too small, you can right) and then click in the very center of the circle. change the size of it Grow Rename the sprite “Player” by selecting either the by clicking on the blue “i” in the sprites list. “Grow” or “Shrink” tool on the bar along the top of the Scratch screen, Shrink then clicking on the circle.
GAME PROGRESS 2 0 % 77 Making friends and enemies Selecting “duplicate” You can now make the green friend and red makes a copy enemy circles. You can use other colors if you like, of the sprite. but make sure you can easily tell the three different circles apart. info Player duplicate 6 Start by right-clicking on the Player sprite and selecting “duplicate”. Do this twice. You’ll now have three blue circles. delete Rename Player2 as “Friends” and Player3 as “Enemies”. 7 Select the Friends sprite and click the “Fill with Costumes tab. Choose green in the color” tool color palette. Select the “Fill with color” tool and click inside the blue circle to make it turn green. 8 Repeat the steps Sprites I have some friends Click inside the for the Enemies and quite a few blue circle to sprite, but color Player enemies! make it green. this sprite red. You should now have Friends Enemies three different colored sprites. Instant player control 10 Add the script below to get the blue circle following the mouse. Read it through and Now add a score display and a script to make make sure you understand what it does. the Player sprite stick to the mouse-pointer— Run the script to check it works. The red just like in Star Hunter. and green circles won’t do anything yet. 9 Select the Player sprite, click Data, and make a variable when clicked called “Score” for all sprites. Then put a check in the variable’s box to show “Score” on the stage. set Score ▾ to 0 New Variable Checking this box forever Variable name: Score ensures that the score will appear on the stage. go to mouse-pointer ▾ For all sprites For this sprite only OK Cancel Score This block “glues” the Player sprite to the mouse-pointer.
78 C I R C L E W A R S March of the clones We’re all We’re all We’re all different! different! different! From just two sprites—the green and red circles—you can create an army I’m not! of friends and enemies to pursue the player’s blue circle. You can do this through the magic of cloning. Before you create your clones, first get the Friends sprite moving randomly around the stage. when clicked Picks random forever direction 11 Select the green Friends sprite. Add this script to make the point in direction pick random –180 to 180 circle bounce around the stage with a random change of repeat 25 direction every 250 steps. move 10 steps if on edge, bounce 12 Run the project and The “repeat” loop watch the green circle’s runs the blocks unpredictable journey. The inside it 25 times. Friends sprite moves 250 steps in 10-step jumps but it EXPERT TIPS doesn’t get stuck to the walls. After 250 steps, the “forever” Repeat loops loop goes back to the start. The sprite changes direction You’ve already seen The blocks inside randomly and sets off again. “forever” loops that repeat 4 times. repeat a group of blocks Random direction nonstop. A “repeat” loop when clicked changes happen does a similar job, but it every 250 steps. only repeats the blocks pen down inside a fixed number of times. This type of loop repeat 4 is sometimes called a “for” loop, because it move 100 steps repeats for a certain number of times. The turn 90 degrees example shown here repeats an action four pen up After 4 repeats, the times to draw a square. next block is run.
GAME PROGRESS 4 0 % 79 Making clones when clicked Now we’re going to make our forever friendly clone army. These are the clones you need to catch point in direction pick random –180 to 180 to score points. repeat 25 13 Add a “create clone of myself” block as the last block in the move 10 steps Drag this block into “forever” loop. You’ll find it in if on edge, bounce the bottom of the the yellow Control section. “forever” loop. This block will create a clone of the Friends sprite after create clone of myself ▾ each 250-step movement. A new clone is created every 250 steps. 14 Run the project. At each change of direction, the sprite leaves a copy of itself—a clone. The clones aren’t just pictures—they are fully working copies of the original sprite, and you can give them their own instructions. 15 New clones are controlled by a special All clones run their own This block makes script that starts with the block “when copy of this script. all the clones I start as a clone”. Add the script below transparent. to the Friends sprite. The script tells when I start as a clone each clone to move toward the Player sprite for 300 steps, after which the set ghost ▾ effect to 50 clone is deleted and vanishes from the stage. The clones move one step at a repeat 300 The instructions time. They move more slowly than the point towards Player ▾ within the block original Friends sprite, which moves move 1 steps are repeated in 10-step jumps. 300 times. delete this clone 16 Run the script and watch the green The clone moves slowly clones advance slowly toward the toward the Player sprite Player sprite. Don’t worry—they’re in 1-step jumps. the good guys! The clone disappears after 300 steps.
80 C I R C L E W A R S Destroying clones when I start as a clone Make sure the set ghost ▾ effect to 50 “if then” block is inside The last part of the script for repeat 300 the Friends clone checks if the the “repeat” loop. clone is touching the Player. point towards Player ▾ If it is, the clone gets deleted. move 1 steps then 17 Add an “if then” block if touching Player ▾ ? containing the blocks shown change Score ▾ by 1 here to check whether the play sound pop ▾ clone is touching the Player delete this clone sprite after each move. Try running the project now—the score should increase as you touch green circles, which instantly disappear with a pop. POP! When the Player sprite touches the clone, the clone is destroyed. EXPERT TIPS delete this clone Clones Clones are useful any time you want lots of Such languages are called “object oriented” copies of a sprite. Many programming languages and include Java and C++. In Scratch, languages let you make copies of things, but there are three orange blocks that control they are often called objects rather than clones. clones, all found in the Control section. create clone of myself ▾ delete this clone △ This block creates a clone of the sprite. The clone △ This block gets rid of the clone. All clones is identical to the sprite and appears in the same disappear from the stage when a project stops, position and facing the same direction, so you leaving just the original sprite. won’t be able to see it until it moves. when I start as a clone △ When a clone starts, it runs the script headed with this block. Clones don’t run the sprite’s main script, but they can run all other scripts in the sprite’s scripts area, such as scripts triggered by messages.
GAME PROGRESS 6 0 % 81 Enemy clones Now you need to add scripts to the Enemies sprite to make it produce clones that chase the Player. You can do this by copying the scripts from the Friends sprite across to the Enemies sprite. 18 To copy scripts, just click, drag, and Sprites Release the mouse drop scripts from one sprite onto when the mouse- another. Drag the two scripts you pointer is over the made for the Friends sprite onto the red circle. Enemies sprite, one at a time. This makes copies of the scripts in the Player Friends when clicked Enemies sprite. forever 19 Select the Enemies sprite. The The “clean up” point in direction pick random –180 to 180 scripts you dragged and dropped option reveals repeat 25 will probably be on top of one any hidden scripts. another, because any copied script move 10 steps just appears at the top left of the scripts area. To rearrange them, if on edge, bounce right-click on the background and select “clean up”. clean up create clone of myself ▾ add comment 20 Now adjust the Enemies clone change Score ▾ by –3 This reduces the player’s script so that it takes points away score by 3 points. when the Player touches a red clone. Alter the “change Score by” block so Change the script to it changes the score by –3 instead of play a cymbal sound. +1. You really want to avoid those nasty red enemies! She may not be the best player, but she is 21 Add a sound to tell the player that play sound cymbal ▾ points have been lost. Load the the loudest! cymbal sound into the Enemies sprite by selecting “cymbal” in the sound library. Alter the script to play “cymbal”, not ”pop”. You’ll now hear which type of clone you’ve touched. 22 Run the project. Check that you now have both red and green clones, and that touching a red clone takes 3 points off your score.
82 C I R C L E W A R S Win or lose? when clicked This block sets the score to 0 at the start. You’ve created two ever-expanding set Score ▾ to 0 clone armies: one of friendly circles that You can find this help you win points, and one of evil forever block in the green circles that make you lose points. Next Operators section. you need to add the code that tells you go to mouse-pointer ▾ if you’ve won or lost the game. then if Score > 20 23 Add the new “if then” blocks shown here These blocks run to the Player sprite. They check your score. think Victory! when the score If the score is greater than 20, you win, and stop this script ▾ is more than 20. a thought bubble with the word “Victory!” appears. If the score is less than –20, you lose, and the sprite thinks “Defeat!” Victory! Defeat! if Score < –20 then think Defeat! These blocks run stop this script ▾ when the score is less than –20. Score 21 Score –21 The Player sprite stops following the mouse-pointer when either the winning or losing score is reached. LINGO When we add these to “if then” blocks, they create statements that are either true or false. In Circle Comparison operators Wars, the “is more than” operator tells you that you’ve won the game when you score over 20. Earlier we saw how you can use “if then” blocks to create true or false statements—also known if Score > 20 then as Boolean expressions—that lead to different outcomes. For example, in Star Hunter, “if touching cat then play sound fairydust” makes a sound play only when the cat gets a star. We can do the same thing with numbers by using what are called comparison operators: 2<5 3=3 5>1 think Victory! is less than equals is more than stop this script ▾
GAME PROGRESS 80% 83 24 Run the game. Try to touch only the green I am the circles. Check that the game ends when the champion! key scores are reached, and check that the Player sprite thinks “Victory!” or “Defeat!”You can reduce the score needed to win if you find it too difficult. But don’t make the game too easy—Circle Wars is meant to be a challenge! Adding a timer when clicked To add some competition to the set Score ▾ to 0 game, you can include an on-screen timer that shows players how long reset timer This block starts the they take to complete a game. timing from 0. 25 Click on the Data section and make a forever variable “Time” for all sprites. To show it on the stage, check the box next to go to mouse-pointer ▾ the variable’s block. Select the Player sprite. Click on Sensing in the blocks if Score > 20 then palette. Add “reset timer” to the Player’s script, just before the “forever” loop. Go think Victory! back to Data and drag a “set Time to” stop this script ▾ block to the script and add “timer” to it, making it the last instruction in the if Score < –20 then forever loop. 26 By copying “timer” to the variable think Defeat! “Time”, each trip around the loop will stop this script ▾ now display the time on the stage. But the moment the player wins or loses, set Time ▾ to timer the time stops being updated (the script is stopped) and the total time it took to win or lose is shown. Time 41.573 I think it must be This block updates lunch time! the Time display Total number every time the of seconds loop repeats. in the game
84 C I R C L E W A R S Use black Choosing a light background color for the text. will make the text easier to read. Instructions You may want to decorate your Players need to know the rules of the instructions with colored circles. game. Create a special sprite that shows the instructions for Circle Wars when the game begins. 27 Use the paintbrush symbol to create a new sprite and rename it “Instructions”. Select “Bitmap Mode” and choose a color. Select the “Fill with color” tool and click on the drawing area to fill it with your chosen color. “Fill with color” tool 28 Now select black from the palette as the You are the blue circle. color for the text. Then choose the text tool Move using the mouse. and type out the instructions shown here. Be quick! Text tool Try to touch the friendly green circles. Each one gives you 1 point. 29 If the text doesn’t fit, use the select tool to Avoid the enemy red circles. resize it by pulling the corner points in or Each one you touch takes 3 points. out. When you’ve finished, click outside the box around the text to stop editing. Score more than 20 to win. Score less than –20 and you lose. “Select” tool Press the space bar to start! GAME DESIGN Game stories Computer games usually have a story to explain why the action in the game is happening. At the moment, Circle Wars has no story. Can you make one up? It could be a battle in space, with a blue spaceship saving friendly green spaceships and trying to avoid being hit by the red enemy craft. Let your imagination run riot! Including some of the story in your instructions will help make the game more interesting and exciting for the player.
GAME PROGRESS 100% 85 30 Add this script to the sprite Did someone to show the instructions mention space? on the stage at the start of the game. Read it carefully. when clicked Can you see how it works? go to x 0 y: 0 These blocks show the instructions in the center of the screen in front go to front of other sprites. show This block hides the Instruction wait until key space ▾ pressed? sprite when the player presses the space bar to start playing. hide 31 You also need to add when clicked a “wait until key space pressed” block immediately wait until key space ▾ pressed? after the green flag blocks in the Player, Friends, and set Score ▾ to 0 Add a “wait until key Enemies sprites’ scripts. This reset timer space pressed” block will hold back all the action forever to the scripts of all until the space bar is pressed. three sprites. go to mouse-pointer ▾ 32 Run the project and your then instructions should appear, if Score > 20 filling the screen until you press the space bar. Players will think Victory! have plenty of time to read and stop this script ▾ understand the instructions, letting them start the game when they’re ready. if Score < –20 then I’m ready think Defeat! to play! stop this script ▾ set Time ▾ to timer
86 C I R C L E W A R S Hacks and tweaks You’ve got Circle Wars working—well done! △ Find a balance Now to personalize it and make it your own. Try these suggestions and your own ideas. Experiment with different speeds, or change Once you’ve created something unique, why how many points you win or lose for touching not share it on the Scratch projects website? Friends and Enemies. It’s not difficult to make the game very hard or very easy, but can you ▽ What’s the story? find a balance to make it just the right level? Did you think of a story to explain what’s going on in Circle Wars? Maybe it’s the attack of the dragons, and the princess player has to eat cakes to survive? Add some scenery and music to the game to fit with that story. Experiment with different stories and looks. ▷ The war’s over! GAME OVER! Add a broadcast message to reveal a “Game over!” sprite when the player wins or loses, like you did in Cheese Chase. You can change the text of the “Game over!” sprite so that it relates to your story about the game. ▷ Slow down, blue! go to mouse-pointer ▾ Add the “point towards” block and an instruction To make things tricky, change Remove the to “move 5 steps”. the blue circle’s script so that it “go to” block. no longer “sticks” to the mouse point towards mouse pointer ▾ pointer but chases slowly after it. You could also invent simple move 5 steps keyboard controls for the sprite. ▽ Tweak the timer That’s their best The number in the timer flickers because it shows lots of time yet! decimal places. To round the value so it shows only whole seconds, use the green “round” block near the bottom of the Operators section. Try adding a “Best time” for winning players, just as you added a “High score” in Cheese Chase. set Time ▾ to round timer
HACKS AND TWEAKS 87 ▽ Change the colors Green and blue circles Vary the clones’ colors. Click on the Friends sprite. Add are friends. the “set color effect to” block from the Looks section to the sprite’s clone script. Then drag “pick random” from Operators into the block’s window and change the values to –30 and 30. Do the same for the Enemies sprite. New clones will now have different colors! when I start as a clone pick random –30 to 30 Orange, yellow, set color ▾ effect to pink, and purple set ghost ▾ effect to 50 Insert this instruction immediately circles are enemies. repeat 300 after “when I start as a clone”. ▷ Change the size From green Operators section Add the “change size by” block to the scripts of both the Friends and when I start as a clone Enemies sprites to make each clone a random size. Alter the scoring so change size by pick random –30 to 30 that the size of the circle you touch determines how many points you set ghost ▾ effect to 50 Change the values score. You’ll also need to change repeat 300 to “–30” and “30”. the totals needed to win or lose. Try more than 2,000 points for victory, Change the Friends’ and less than –2,000 for defeat. score value to this. change Score ▾ by size change Score ▾ by 0 – size Use this The bigger the clone, the more scoring for points you win or lose. Enemies. ◁ Shape shifting Introduce another shape into the game. It could be a square that eats red circles, a triangle that runs away from the player, a hexagon that makes the player shrink or grow, or anything else you want to try.
Jumpy Monkey
90 J U M P Y M O N K E Y The instructions appear on the How to build Jumpy Monkey game at the start. In the real world there are laws Jumpy Monkey you just can’t break. For example, the law of gravity means that by FunkyMonkey66 (unshared) something that goes up must always come down again. Jumpy SET LAUNCH ANGLE Monkey shows you how to add SET LAUNCH SPEED gravity to your game worlds. PRESS SPACE TO FIRE AIM OF THE GAME The monkey is on a mission to collect bananas. Choose which direction he leaps in and how fast he goes. You need to send him over the palm tree to grab the bananas using the fewest possible jumps. ◁ Launcher Point this arrow in the direction you want to launch the monkey by using the left and right arrow keys. ◁ Monkey Select the monkey’s launch speed with the up and down arrow keys, then press the space key to launch him. ◁ Bananas The monkey is launched LaunchSpeed 11 from the arrow when If the monkey touches any This number shows you of the bananas he will eat you press the space key. how fast the monkey will them. Keep going until he eats all the bananas. fly once he is launched.
HOW TO BUILD JUMPY MONKEY 91 The monkey flies GAME CONTROLS through the air like a cannonball. Players use the arrow keys and space key on the keyboard as game controls. Space ◁ Flying monkey Try to collect all the bananas using as few launches as possible. The game will record how many launches you use. There are three bunches of bananas to collect each time you play the game. Down with gravity! Avoid the tree— the monkey can’t fly through it.
92 J U M P Y M O N K E Y Launching the monkey This game uses a big arrow to help the player choose the monkey’s precise launch direction. We’ll ignore gravity to start off with, but you’ll need to add it later to get the monkey past the tree. 1 Start a new project and call it “Jumpy Monkey”. 2 Go to Data, select “make a variable”, and Delete the cat sprite and load two sprites from add a variable called “LaunchSpeed”. the library—“Monkey2” and “Arrow1”. Select the The new variable will automatically show arrow sprite and rename it “Launcher” by clicking up on the stage. on the “i” and typing the new name into the box. Select this New Variable option, then Variable name: LaunchSpeed click “OK”. For all sprites For this sprite only Click here Monkey2 Arrow1 OK Cancel Launcher direction: 90° x: 73 y: -17 Type here rotation style: to rename can drag in player: the sprite. show: 3 Select the Launcher sprite, then add these three scripts to set up the Launcher and allow the player to control its angle using the left and right arrow keys on the keyboard. The direction of the arrow is the direction that the monkey will launch. Run the scripts and try turning the arrow. This script runs when the left arrow This block moves the key is pressed. when clicked arrow to the bottom when left arrow ▾ key pressed left of the stage. set LaunchSpeed ▾ to 10 turn 1 degrees go to x: –200 y: –140 This script runs point in direction 45 ▾ when the right arrow go to front key is pressed. This keeps the arrow when right arrow ▾ key pressed visible, with the monkey behind it. turn 1 degrees
GAME PROGRESS 17% 93 4 Now that you can aim, you need controls to set the This increases the speed of the launch. Add these scripts to change launch speed. the speed using the up and down arrow keys. when up arrow ▾ key pressed Maximum speed if LaunchSpeed < 20 then change LaunchSpeed ▾ by 0.1 when down arrow ▾ key pressed Minimum speed if LaunchSpeed >1 then change LaunchSpeed ▾ by –0.1 LINGO This reduces the launch speed. Events when space ▾ key pressed The key presses and mouse clicks that when this sprite clicked a computer detects are known as events. The brown Events blocks in Scratch trigger when loudness ▾ > 10 a script whenever a particular event occurs. loudness We’ve seen them used with messages in timer Cheese Chase, but Scratch also lets you trigger video motion scripts using keys, mouse clicks, sound levels, and even movement detected by a webcam. Don’t be afraid to experiment. ▷ Setting things off Events blocks such as these are used to trigger a script whenever the event they describe occurs.
94 J U M P Y M O N K E Y 5 Now select the when clicked Monkey sprite. Add this script to shrink set size to 35 % him down to the right size and move him set rotation style don’t rotate ▾ behind the Launcher. go to Launcher ▾ 6 To launch the monkey when the when space ▾ key pressed This makes the space bar is pressed, add this new go to Launcher ▾ monkey’s direction script to the Monkey sprite. “Repeat match the direction until” is a new type of loop block of the launch arrow. that keeps repeating the block inside until the condition becomes point in direction direction ▾ of Launcher ▾ true—in this case, the monkey keeps moving until it touches the repeat until touching edge ▾ ? edge of the stage. move LaunchSpeed steps The “repeat until” block go to Launcher ▾ keeps the monkey moving to the edge of the stage. EXPERT TIPS “repeat until” Do you want to keep repeating an action only until something happens and then move on to the rest of the script? The “repeat until” block can help your code when “forever” and “repeat” loops aren’t flexible enough. Most programming languages use similar loops, but some call them “while” loops—these continue while the condition is true, rather than looping until the condition is true. There are always different ways to think about the same problem.
GAME PROGRESS 3 3 % 95 7 Try setting the Launcher angle and Jumpy Monkey speed using the arrow keys, and pressing the space bar to fire the by FunkyMonkey66 (unshared) monkey. He goes in a completely straight line until he hits the edge of the stage. Real things don’t do this—they fall back toward the ground as they move. We’ll add gravity to the game later to make the monkey behave realistically. LaunchSpeed 11 Bananas and palm trees The point of this game is for the monkey to collect bananas. By using clones, you can add just one Bananas sprite but give the monkey plenty of fruit to aim for. 8 Add the Bananas sprite to the project. Make a variable for all sprites called “NumBananas” to keep track of the number of bananas on the stage—start with three. Build the following script to clone the bananas, but don’t run it yet because you still need to tell the clones what to do. when clicked We only need the clones, so hide the original hide Bananas sprite. set NumBananas ▾ to 3 repeat NumBananas create clone of myself ▾ The loop runs three times.
96 J U M P Y M O N K E Y 9 Add the next script to place each banana clone in a random spot on the right of the stage, change how it looks, and make sure it’s not hidden. The clone will wait for the monkey to touch it and then disappear. If it’s the last banana, it sends a “GameOver” message, which you need to create as a new message. when I start as a clone go to x: pick random 0 to 200 y: pick random –140 to 140 set size to pick random 50 to 100 % set color ▾ effect to pick random –10 to 20 The ranges of the “pick show random” blocks send bananas wait until touching Monkey2 ▾ ? only to the right side of the stage. change NumBananas ▾ by –1 if NumBananas =0 then Choose “new message” broadcast GameOver ▾ and call it “GameOver”. delete this clone Position the palm tree so that part of its trunk is off the stage. 10 Run the project. You 11 The game is too easy— Jumpy Monkey should be able to get the we need an obstacle. Add monkey to collect all the the Palmtree sprite to the by FunkyMonkey66 bananas. There is no script project. Drag and drop run by the “GameOver” the tree at the bottom of message yet. the stage. Yum yum! △ Tree on stage Make sure your palm tree is slightly off- center, toward the left of the stage, or the bananas will get stuck behind the tree and the game won’t work.
GAME PROGRESS 5 0 % 97 12 At the moment, the monkey can fly straight through the tree. Change his script so that he stops flying if he touches it. The current script repeat until touching edge ▾ ? move LaunchSpeed steps Modify the script by adding the “or” block from the Operators menu. repeat until touching edge ▾ ? or touching Palmtree ▾ ? move LaunchSpeed steps Add a “touching Palmtree?” block from the Sensing section. 13 Run the project. The monkey should EXPERT TIPS stop flying when he hits the tree, which makes any bananas to the “or”, “and”, “not” right of the tree impossible to reach. Don’t worry, gravity will come to the So far, most of the “if then” blocks in this book have tested only a rescue soon. single condition, such as “if touching cat” in the Star Hunter game. In this chapter, however, you need to test two conditions at once: I want those bananas! “touching edge or touching Palmtree”. Complex sets of conditions like this occur a lot in coding, so you need a way to combine them. In Scratch, the green Operators blocks do the job. You’ll see words like “or”, “and”, and “not” in almost every programming language, or special symbols that do the same job. The block is true if or either or both blocks inside are true. and The block is only true not if both blocks inside are true. The block is only true if the block inside is false. △ Logic blocks Logical Operators blocks such as these three let you test for complex sets of conditions.
98 J U M P Y M O N K E Y Make a Variable Make a List 14 Make two more variables for all sprites: “FallSpeed” and “Gravity”. Then add a “set Gravity” block to the monkey’s FallSpeed “when clicked” script and amend his “when space key pressed” script as shown below. The new blocks use Gravity variables to simulate gravity. “FallSpeed” keeps track of how many steps the monkey needs to be moved LaunchSpeed down by gravity. The value of “Gravity” is how much “FallSpeed” increases each time the monkey moves. NumBananas when clicked Deselect the box next △ Hiding variables to a variable to stop it If you don’t want variables to appear set size to 35 % appearing on the stage. on the stage, you need to uncheck the box next to them in the Data section. set rotation style don’t rotate ▾ Add this block Do this for these two new variables. to the “when go to Launcher ▾ clicked” script. set Gravity ▾ to –0.2 when space ▾ key pressed This new block go to Launcher ▾ shows that at the moment of launch, point in direction direction ▾ of Launcher ▾ the monkey isn’t falling yet. set FallSpeed ▾ to 0 touching Palmtree ▾ ? repeat until touching edge ▾ ? or This new block moves move LaunchSpeed steps the monkey down. change y by FallSpeed change FallSpeed ▾ by Gravity go to Launcher ▾ This new block contains the variable “Gravity”, which makes the monkey fall faster each time the loop runs.
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226