Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Scratch game

Scratch game

Published by Rahul Praharaj ICT Trainer DPS Wgl, 2022-08-26 10:11:56

Description: Coding Games in Scratch ( PDFDrive )

Search

Read the Text Version

0 = x } 0 1 : )10 ( = 0*> 1 0 =/ o inC D g01(1 *y1 : ameG S x 0 1 0 IN SCRATCH™ 1 0 1 = 0 1 0: 1 : *1 y* x) 0 A STEP-BY-STEP VISUAL GUIDE TO BUILDING YOUR OWN COMPUTER GAMES



CGoaDmneSg IN SCRATCH™



CGoaDmneSg IN SCRATCH™ JON WOODCOCK

DK UK Senior editor Ben Morgan Project art editor Laura Brim Editors Lizzie Davey, Ashwin Khurana, Steve Setford US editors Jill Hamilton, Margaret Parrish Designers Mabel Chan, Peter Radcliffe, Steve Woosnam-Savage Jacket design development manager Sophia MTT Jacket editor Claire Gell Producer, pre-production Francesca Wardell Producer Mary Slater Managing editor Paula Regan Managing art editor Owen Peyton Jones Publisher Andrew Macintyre Associate publishing director Liz Wheeler Art director Karen Self Design director Stuart Jackman Publishing director Jonathan Metcalf DK DELHI Project editor Suefa Lee Project art editor Parul Gambhir Editor Sonia Yooshing Art editors Sanjay Chauhan, Upasana Sharma Assistant art editor Simar Dhamija Senior DTP designers Harish Aggarwal, Vishal Bhatia Senior managing editor Rohan Sinha Managing art editor Sudakshina Basu Pre-production manager Balwant Singh Jacket designer Suhita Dharamjit Managing jackets editor Saloni Singh First American Edition, 2016 Published in the United States by DK Publishing 345 Hudson Street, New York, New York 10014 Copyright © 2015 Dorling Kindersley Limited DK, a Division of Penguin Random House LLC 16 17 18 19 20 10 9 8 7 6 5 4 3 2 1 001—283034—January/2016 All rights reserved. Without limiting the rights reserved under copyright above, no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright owner. Published in Great Britain by Dorling Kindersley Limited. A catalog record for this book is available from the Library of Congress. ISBN: 978-1-4654-3935-2 DK books are available at special discounts when purchased in bulk for sales promotions, premiums, fund-raising, or educational use. For details, contact: DK Publishing Special Markets, 345 Hudson Street, New York, New York 10014 or [email protected] Printed in China A WORLD OF IDEAS: SEE ALL THERE IS TO KNOW www.dk.com

DR. JON WOODCOCK MA (OXON) has a degree in physics from the University of Oxford and a PhD in computational astrophysics from the University of London. He started coding at the age of eight and has programmed all kinds of computers, from single-chip microcontrollers to world-class supercomputers. His many projects include giant space simulations, research in high-tech companies, and intelligent robots made from junk. Jon has a passion for science and technology education, giving talks on space and running computer programming clubs in schools. He has worked on numerous science and technology books as a contributor and consultant, including DK’s Computer Coding for Kids and Computer Coding Made Easy.

Contents CHEESE CHASE 8 FOREWORD 50 How to build Cheese Chase COMPUTER GAMES Score 30 High Score 90 12 What makes a good game? CIRCLE WARS 14 Atmosphere 16 Types of games 74 How to build Circle Wars 18 How coding works Score 8 GETTING STARTED Time 23.5 22 Introducing Scratch 24 Getting Scratch 26 Scratch tour STAR HUNTER 30 How to build Star Hunter Score 0 JUMPY MONKEY 90 How to build Jumpy Monkey SET LAUNCH ANGLE SET LAUNCH SPEED SPACE TO FIRE LaunchSpeed 11

DOOM ON THE BROOM TROPICAL TUNES 108 How to build Doom on the Broom 190 How to build Tropical Tunes Score 25 Lives 3 Score 0 DOG’S DINNER WHAT NEXT? 130 How to build Dog’s Dinner 206 Remixing and beyond 208 Better Scratch GLACIER RACE 210 The next level 212 Jobs making games 166 How to build Glacier Race 214 Have fun! Ben Gems: 20 Countdown 11 GLOSSARY & INDEX Laura Gems: 13 218 Glossary 220 Index 224 Acknowledgments Find out more at: www.dk.com/computercoding

Foreword Many of the people who have shaped our digital world started out by coding games for fun. Bill Gates, cofounder of Microsoft, wrote his first computer program at the age of 13—a tic tac toe game. Just a few years later a teenage Steve Jobs and his friend Steve Wozniak, who later founded Apple together, created the arcade game Breakout. They started coding simply because they enjoyed it. They had no idea how far it would take them or that the companies they were to build would change the world. You might be the next one like them. Coding doesn’t have to become a career, but it’s an amazing skill and can unlock exciting doors to your future. Or you might just want to play around with code for the fun of it. Computer games open up worlds of imagination. They reach out across the internet and allow us to play together. They are packed with creativity, from music, stories, and art to ingenious coding. And we’re hooked on them: so much so that the games industry is now worth more than the movie industry. It’s huge.

And now, instead of being just a player, you can become a game maker too. You can take control of every aspect of those imaginary worlds: how they look, sound, and feel. You get to invent the stories, the heroes, the villains, and the landscapes. But first you need to take control of your computer. To tell a computer what to do, you need to speak its language and become a programmer! Thanks to languages like Scratch, it’s never been easier. Just follow the simple steps in this book to build each game and you’ll see what goes on inside each one. Follow the chapters in order, and you’ll pick up the essential skills you need to design and build your very own games. Let’s get coding!



Computer games

12 C O M P U T E R G A M E S I have the perfect recipe! What makes a good game? Some games have a magical quality that makes you want to play them time and again. Game designers call it playability. To make a game with great playability, you need to think about all the ingredients that make up the game and how they work together. ◁ Characters In most games, the player uses an on-screen character to enter the game world. It could an animal, a princess, a racecar, or even just a simple bubble. To create a sense of danger or competition, such games usually also have enemy characters that the player has to defeat or escape from. △ Mechanics These are the “verbs” in a game—actions such as running, jumping, flying, capturing objects, casting spells, and using weapons. The mechanics are the core of the game, and well-designed mechanics make a good game. △ Objects ◁ Rules Nearly all games include objects, from stars and coins that The rules of a game tell you boost health or scores to keys that unlock doors. Not all what you’re allowed and not objects are good—some get in the player’s way, sap their allowed to do. For example, can health, or steal their treasures. Objects can also work you walk through walls or do together to create puzzles for the player to solve. they block your path? Can you stop and think or do you have to beat the clock?

WHAT MAKES A GOOD GAME? 13 YOU SCORED ◁ World 25,547,010 Think about the world in which a game is played. Is POINTS!!! it 2D or 3D? Does the player view the game from above, △ Goals from the side, or from within? Every game challenges the player to achieve some Does the game world have kind of goal, whether it’s winning a race, conquering walls or boundaries that limit an enemy, beating a high score, or simply surviving the player’s movement or is for as long you can. Most games have lots of small it open like the outdoors? goals, such as unlocking doors to new levels or winning new vehicles or skills. ◁ Controls △ Difficulty level Keyboards, mice, joysticks, and motion sensors all make A game’s no fun if it’s too easy or too hard. Many good controllers. Games are games make the challenges easy at the start, while more fun when the player the player is learning, and more difficult later as the feels in complete control player’s skills improve. Getting the difficulty level of the character, so the just right is the key to making a great game. controls should be easy to master and the computer should respond instantly. GAME DESIGN Playability Games don’t have to be complicated to make people want to play them over and over again. One of the first successful computer games was a simple tennis simulator called Pong. The ball was a white square and the racquets were white lines that could only move up and down. Although there were no fancy graphics, people loved Pong because it had great playability. They could compete against friends, just like in real tennis, and it was just hard enough to demand intense concentration and a steady hand, leaving players always wanting another game.

14 C O M P U T E R G A M E S Atmosphere A good game, just like a movie or a book, can draw you in and change the way you feel by creating a certain atmosphere. Here are some of the tricks game designers use to conjure up an atmosphere. ◁ Telling stories A background story helps set the scene for a game and gives meaning to the player’s actions. Blockbuster games have movielike plots with twists, but even simple games △ Boo! can benefit from some kind of story if it makes players feel they’re on a mission. Do things jump out at the Thinking of a story also helps you give a player? Fear and suspense can game a consistent theme. make a game scary and put the player on edge. What’s around the next corner? What’s behind ▷ Sound that door? The wait can be Sounds can have a strong effect worse than the scare! on how we feel. Changing the tune can make the same scene ▷ Faster, faster! feel exciting, scary, or even silly, and a sudden noise after a quiet The speed of a game spell can cause a jolt of terror. changes the level of Modern games use realistic sound excitement a player feels. effects to make players feel like It’s easy to stay calm they’re inside the action. when you can stop and think about what to do next, but with a ticking clock and fast music, you can’t help but feel under pressure. ◁ Color scheme You can change the atmosphere in a game simply by altering the colors. Bright blue, yellow, and green feels warm and sunny, for instance, while icy blues and white feel wintry, and darker colors make a game feel spooky.

AT M O S P H E R E 15 ▽ Graphics GAME DESIGN a player feels inside the game world rather than The graphics in the first games were simple Virtual reality watching it through geometric shapes, but as computers became a screen. more powerful, the graphics in games got Virtual reality goggles better. Many console games now feature could make the games photorealistic 3D images, but games based of the future much more on simple, cartoonlike graphics are as realistic. They work by popular as ever and can help create a more presenting each eye with playful atmosphere. a slightly different image, creating a 3D experience. Motion sensors in the headset track the player’s movements and adjust the images to match, allowing the player to turn around and look in any direction, just like in the real world. As a result, Where are you? ◁ Snow and ice A snowy scene is the One of the easiest ways to create backdrop for a race atmosphere is to give a game a location along an icy road. by adding a background image. To make the illusion more convincing, make sure the game’s characters match the setting—don’t put racecars in the deep sea or unicorns in outer space, for instance. △ Spooky forest △ Tropical beach △ Deep-sea adventure A dark forest is the perfect setting A sunny beach creates a carnival Octopuses and starfish fit well for ghosts, ghouls, and witches. mood for the colorful steel drums. with this underwater scene.

16 C O M P U T E R G A M E S Types of games Games come in all shapes and sizes, but most fit into one of just a few main categories, called genres. Some gamers like the platform games genre best, whereas others prefer racing games or strategy games. What are your favorite genres? ◁ Traditional △ Role-playing When you can’t find an opponent to play with Dungeons, dragons, and castles you, a computer can feature in these adventure games. challenge you to a game of Players may roam freely or follow cards, chess, or a million a set storyline, with their character other popular board games. developing specialized skills as it advances, such as casting spells or ▷ Racing sword-fighting. Some role-playing Racing games create the illusion of speed games are played online, allowing by making the scenery scroll past the lots of players to interact in the player’s viewpoint. To succeed, you need same game world. to learn each racetrack inside out so you can start tricky maneuvers in advance. △ Sandbox △ Combat Some games force players along a set path, but Nimble fingerwork is vital for games involving close- sandbox games are the opposite: they give you quarters combat. The key to success is knowing when complete freedom to explore the game world at and how to use many different attack and defense your own pace and choose different quests within it. moves, from slams and somersaults to special powers.

TYPES OF GAMES 17 ▷ Strategy Decisions, decisions. What are the best choices to make if you’re running a zoo, fighting a war, or building a whole civilization? Strategy games give the player godlike powers over many different characters at once, but you have to manage resources cleverly or your empire will collapse. △ Simulator ◁ Music and dance If you want a puppy but don’t want the trouble of feeding Dance-mat games involve and walking it, a virtual pet might suit you. Simulators aim tapping the feet or jumping to re-create real-life situations. Some are more than just a over a stream of obstacles game: flight simulators are so accurate and realistic that in time to the rhythm. Music professional pilots use them for training. games allow you to play along with a virtual band using a pretend instrument. You need to hit the right notes on time to complete each level. △ Sport △ Puzzle Play the game of your choice as your favorite team, set in Some people love to exercise their brains with a realistic stadium with roaring crowds. Sports games let puzzles. There are many different types, from you compete in famous tournaments such as the soccer colorful tile-matching games to number puzzles and World Cup, with the computer referee ensuring fair play. escape games, in which you need to use your imagination to find your way from room to room.

18 C O M P U T E R G A M E S How coding works A computer can’t think for itself—it works by blindly following instructions. It can only carry out a complex task if that task has been broken down into simple steps that tell it exactly what to do and in what order. Writing these instructions in a language a computer understands is called coding. The player makes the Pressing the space key makes parrot fly left and right with the parrot dive, but the game the left and right arrow keys. ends if you touch the lion. Planning a game Imagine you want to create a game Score 10 in which you fly a parrot over a river, collecting apples as they drift downstream but avoiding an angry lion. You would need to give the computer a separate set of instructions for each object in the game: the apple, the parrot, and the lion. The player wins a point each time the parrot gets an apple. The apple drifts downstream The lion walks left over and over. It reappears on and right, following the parrot. the left if the parrot takes it. ▽ Apple Jump to the left edge of the screen. Repeat the following steps over and over again: You can’t simply tell the computer that the apple drifts down the river Move a bit to the right. and vanishes when the parrot eats it. If I get to the right edge of the screen then Instead, you need to break down this complicated task into a set of very simple steps as shown here. jump back to the left edge. If I touch the parrot then add one to the parrot’s score and jump back to the left edge.

HOW CODING WORKS 19 ▷ Parrot The parrot is more complicated than the apple because the player controls it and it can move up, down, left, and right. Even so, it’s possible to make all of this work by writing a sequence of simple instructions. Jump to the top right of the screen. Repeat these steps in turn: If the player presses the left arrow then move a bit to the left if I can. If the player presses the right arrow then move a bit to the right if I can. If the player presses the space key then move all the way to the bottom of the screen taking a second and move all the way back to the top taking a second ▷ Lion LINGO The lion is the player’s enemy and can end the game if the parrot touches it. Programming languages It is controlled by a simple program. The instructions on this page are in simple Jump to the middle of the screen. English, but if you wanted to create the game on a computer, you would need to translate Repeat these steps in turn: them into special words that the computer If the parrot is to my left then can understand: a programming language. move a bit to my left. Writing programs with a programming If the parrot is to my right then language is called coding or programming. move a bit to my right. This book uses the programming language If the parrot touches me then Scratch, which is ideal for learning about stop the game. coding and great for making games.



Getting started

22 G E T T I N G S T A R T E D Introducing Scratch The characters and other objects in Scratch games are All the games in this book are made with a programming language called Scratch. Scratch is easy to learn because called sprites. you don’t have to type any complicated code. Instead, you build programs from ready-made blocks. Starting from scratch A project in Scratch usually starts with choosing the objects, or sprites, that will appear in the game. Scratch has a large library of sprites, or you can create your own. Sprites Scripts Hello! Sprites are the things that move around or react in Scripts are made of text blocks that you can the game. They can be anything from animals and drag with a computer mouse and join like people to pizzas or spaceships. You can bring each pieces of a jigsaw puzzle. Each block has one sprite to life on screen with a list of instructions instruction so it’s easy to understand. called a script. The cat sprite when space ▾ key pressed appears whenever move 20 steps you start a new say Hello! Scratch project. Working together EXPERT TIPS Games are usually made up of several sprites working Experimenting together, each controlled by its own script. Scripts make sprites move around, crash into each other, Scratch is all about experimenting. Once create sounds, and change color or shape. you’ve built a game, it’s easy to add things to it or change how it works by tinkering Some sprites act as HELP! with the script. You can see the effect of enemies to make a your changes straight away. game more difficult.

INTRODUCING SCRATCH 23 A typical Scratch project The red button stops a program. Once you’ve built a script, you can click the green flag to see what it does. All the action takes place in a part of the Scratch window The green flag starts, called the “stage”. Sprites move about on the stage, often in front or runs, the program. of a background image that helps create atmosphere. ▷ Running a program Cat Cruncher Starting, or “running”, a by GreenDino99 program activates the scripts that you’ve built. To make the stage fill your whole computer screen, click the blue symbol in the top left. Background image The enemy dinosaur sprite chases the player’s cat sprite. Several sprites can be on the stage at once. ▽ Making sprites move In a typical game, the player moves one sprite and the other sprites are programmed to move automatically. The script below makes the dinosaur in this project chase the cat. when clicked The “forever” forever block keeps point towards Cat ▾ the sprite move 15 steps moving endlessly.

24 G E T T I N G S T A R T E D LINGO Getting Scratch Why “Scratch”? In order to try the projects in this book, you’ll Scratch is named after “scratching”, a need to set up Scratch on a desktop or laptop technique rappers and DJs use to computer. The two ways of setting up Scratch remix music on a turntable. The (online and offline) are shown below. Scratch programming language lets you copy other people’s projects Online Scratch and remix them to make your own unique If you have a reliable internet connection, versions. you can run Scratch online in a browser window without downloading anything. You will need to set up a Scratch account. 1 Join Scratch 2 Sign in To set up the online version, visit the Scratch website at After you’ve joined the Scratch website, click “Sign in” and scratch.mit.edu and click “Join Scratch”. You will need enter your username and password. It’s best not to use your to set up an account with a username and password. real name as your username. Click “Create” at the top of the Your games will stay private unless you click “Share”, screen to start a new project. If you use the online version which will publish them on the web. of Scratch, you can access your projects from any computer. Offline Scratch You can also download the Scratch program to your computer so you can use it offline. This is particularly useful if your internet connection is unreliable. 1 Install Scratch 2 Launch Scratch For the offline version of Scratch, go to scratch.mit.edu/ Double-click the icon on the scratch2download. Follow the instructions on screen desktop and Scratch will to download the installation files, then double-click open, ready for you to them. After installation, a Scratch icon will appear begin programming. on your desktop. There’s no need to create a user account if you use the offline version of Scratch.

GET TING SCRATCH 25 △ Operating system △ Hardware You can use Scratch on desktop or laptop The online version of Scratch works well computers, but it’s easier if you use a on Windows, Ubuntu, and Mac computers, mouse than a touchpad. Scratch apps for although it won’t work on tablets. The tablets and smartphones are also being offline version of Scratch works well on developed. Windows and Mac computers. If your computer uses Ubuntu, try the online ◁ Saving version instead. If you use Scratch offline, remember to save from time to time. The online version saves automatically. Online, you can undo all the changes you’ve made since you last opened a project by choosing “Revert” in the File menu. Old and ▽ Version 1.4 new versions In older versions of Scratch, such as Scratch 1.4, the stage is on the right This book is based on and the scripts area is in the middle. Scratch 2.0, the latest SCRATCH File Edit Share Help version at the time of writing. The projects Motion Events Cat Cat game in this book will not Looks Control Sound Sensing Scripts x: –126 y: 96 direction 0.0 Pen Operators Data More Blocks Costumes Sounds work with older when clicked versions of Scratch, so make sure forever you have 2.0. move 10 steps go to mouse-pointer ▾ turn 15 degrees turn 15 degrees move 10 steps point in direction 90 ▾ point towards ▾ SCRATCH File ▾ Edit ▾ Tips ? go to x: 0 y: 0 go to mouse-pointer ▾ Untitled Scripts Costumes Sounds glide 1 secs to x: 0 y: 0 by abcd (unshared) Motion Events Looks Control x: -126 LAUNCH ANGLE Sound Sensing y: 96 LAUNCH SPEED Pen Operators PRESS SPACE TO FIRE Data More Blocks when clicked move 10 steps forever turn 15 degrees turn 15 degrees go to mouse pointer ▾ point in direction 90 ▾ point towards ▾ move 10 steps go to x: 0 y: 0 ▷ Version 2.0 LaunchSpeed 11 go to mouse pointer ▾ when space ▾ key pressed glide 1 secs to x: 0 y: 0 play sound boing ▾ This version of Scratch was released Sprites go to Launcher ▾ in 2013. New features include a “Backpack” for storing costumes, x: 153 y: -61 media, and scripts; a cloning function; a sound editor; and a New sprite: more sophisticated paint editor. Stage Monkey 2 Arrow 1 Numbanas 1 Numbanas 2 1 backdrop New backdrop: Backpack

26 G E T T I N G S T A R T E D Scratch tour Switch to full Change Menus Cursor tools screen view language The Scratch window is divided into several SCRATCH File ▾ Edit ▾ Tips ? different areas. Scripts are built on the right, while the Jumpy Monkey Name of the game stage on the left shows the game running. by Gabby Gibbon (unshared) The stage LAUNCH ANGLE When you play a game or run any LAUNCH SPEED other kind of project in Scratch, you PRESS SPACE TO FIRE see the action happening on the stage, which serves as a miniature screen. You can see changes to your script take effect immediately on the stage simply by clicking the green flag button to run the project. Click a sprite on the stage or in the sprites list to select it. STAGE AREA BLOCKS SCRIPTS LaunchSpeed 11 x: 153 y: -61 PALETTE AREA Sprites New sprite: SPRITES LIST STAGE INFO BACKPACK △ Scratch window Stage Monkey2 Launcher NumBananas1 NumBananas2 The stage and sprites list occupy the left of 4 backdrops the Scratch window, while script-building areas are on the right. The tabs above the New backdrop: scripts area reveal other Scratch features. Click these icons to Sprites list Selected Buttons to add change the backdrop All the sprites used in your project appear here. When sprite new sprites image on the stage. you select a sprite, its scripts appear in the scripts area.

SCRATCH TOUR 27 Keep the The Costumes Use the Sounds Scripts area Scripts tab tab lets you tab to add music You can drag blocks into this part selected to change how and sound effects of the Scratch window and join build scripts. sprites look. to games. them together to build scripts for each sprite in your game. Scripts Costumes Sounds Motion Events Click these headings x: –126 Current Looks Control to reveal different y: 96 sprite Sound Sensing sets of blocks. selected Pen Operators Data More Blocks The x and y coordinates when clicked give the sprite’s move 10 steps set LaunchSpeed ▾ to 10 location on the stage. go to x: -200 y: -140 turn 15 degrees point in direction 45 ▾ turn 15 degrees go to front Blocks snap together—use point in direction 90 ▾ the mouse to move them around. point towards ▾ set Left arrow ▾ key pressed turn 2 degrees go to x: 0 y: 0 go to mouse-pointer ▾ These scripts control the Launcher sprite. glide 1 secs to x: 0 y: 0 Backpack Backpack Zoom in Store useful scripts, on scripts Blocks palette sprites, costumes, and Instruction blocks for making sounds in the backpack scripts appear in the middle of so you can use them in the Scratch window. Drag the other projects. ones you want to use to the scripts area.



Star Hunter

30 S T A R H U N T E R How to build Click this icon to Type in the name The score shows Star Hunter make the game fill of your game. how many stars your screen. you’ve collected. Welcome to your first Scratch game: Star Hunter, a fast-paced, underwater Star Hunter treasure hunt. Just follow the simple steps in this chapter to build the by Octoblaster99 (unshared) game, then challenge a friend to beat your score. Score 0 AIM OF THE GAME An underwater backdrop image The aim of this game is to collect as many gold stars as you can. Use the cat sets the scene. to collect the stars, but watch out for deadly octopuses. You’ll need to move quickly to succeed. The main sprites in the game are shown below. ◁ Cat Move the cat around the screen with your computer mouse—the cat sprite follows the mouse-pointer. ◁ Octopuses The octopuses patrol the seas but they swim more slowly than you. If you touch one, the game is over! ◁ Stars These appear one at a time in random places. Touch a star to score a point.

HOW TO BUILD STAR HUNTER 31 Click the green flag GAME CONTROLS to start a new game. Use a computer mouse Collect stars or touchpad to control to score points. this game. Click the stop sign to end a game. Don’t touch the octopuses! There are three octopuses and they move in different ways. ◁ Under the sea Star Hunter is set in the deep sea, but you can change the backdrop to anything you like, from outer space to a picture of your bedroom. Ready? Let’s code! You play the game as a cat. Move your computer mouse to move the cat.

32 S T A R H U N T E R Building scripts Like any Scratch program, Star Hunter is made by joining colored blocks like the pieces of a jigsaw puzzle. Each block is an instruction that tells a sprite what to do. Let’s start by programming the game’s main sprite: the cat. 1 Start Scratch and choose either “create” or Clicking the “New Project”. You’ll see a screen like the one buttons here below, with the cat sprite in place. In the reveals different middle is a set of blue instruction blocks. sets of blocks. SCRATCH File ▾ Edit ▾ Tips Star Hunter Scripts Costumes Sounds by Octoblaster99 (unshared) Motion Events x: -126 Looks Control y: 96 Sound Sensing Pen Operators Data More Blocks move 10 steps go to mouse-pointer ▾ turn 15 degrees x: 153 y: -61 turn 15 degrees point in direction 90 ▾ Sprites New sprite: point towards ▾ Drag your chosen blocks go to x: 0 y: 0 here to build a script. go to mouse pointer ▾ glide 1 secs to x: 0 y: 0 Stage Sprite 1 1 backdrop New backdrop: Backpack 2 We’ll program the cat to move Choose blocks 3 Now select the yellow Control wherever the player moves the from the list in button and look for a “forever” block. computer mouse. Click on the “go to the middle. mouse-pointer” block and drag it to the right part of the screen—the Scripts Costumes Sounds scripts area. Motion Events go to mouse-pointer ▾ Click Control Looks Control to reveal the Sound Sensing Some blocks include yellow blocks. Pen Operators a drop-down menu. Data More Blocks The blue Motion wait 10 secs blocks control the way repeat 10 sprites move. forever Drag the “forever” block to the scripts area.

GAME PROGRESS 1 4 % 33 4 Drag it to the right and drop it over 5 Next, select the brown Events button. Look for the blue block. It will wrap around a block with a green flag. Drag it to the right and add it like this: it to the top of your script. Read through the script and think about what each block does. forever go to mouse-pointer ▾ when clicked This block starts the game when you click forever the green flag. go to mouse-pointer ▾ 6 Now look at the top right Click the You can stop the This block makes the of the stage—you’ll see green flag script by clicking the cat move with the a green flag. Click this to to play. red stop button. player’s mouse-pointer. run your script. This block makes the block inside it repeat over and over again. SCRATCH File ▾ Edit ▾ Tips Star Hunter Scripts Costumes Sounds by Octoblaster99 (unshared) Motion Events x: -126 Looks Control y: 96 Sound Sensing Pen Operators Data More Blocks move 10 steps when clicked forever go to mouse-pointer ▾ turn 15 degrees turn 15 degrees point in direction 90 ▾ point towards ▾ go to x: 0 y: 0 x: 153 y: -61 go to mouse pointer ▾ Sprites New sprite: glide 1 secs to x: 0 y: 0 Bravo! Stage Sprite 1 1 backdrop New backdrop: Backpack 7 Move your mouse and watch what ▷ Well done! happens. If you followed all the steps, the cat will move with the You have created your first mouse-pointer around the stage. Scratch project. Let’s add some more things to the project to build a game.

34 S T A R H U N T E R Type the sprite’s name here. 8 The cat is called “Sprite1”. Let’s fix that. In the sprites list, select Sprite1 (the cat) and click on the blue “i” in the corner to get more information about the sprite. Change the name to “Cat”. Click here to Cat direction: 90° bring up the information x: 84 y: -69 Cat pop-up box. rotation style: can drag in player: The new Sprite 1 show: name appears. Setting the scene At the moment, the stage is just a boring white rectangle. Let’s create some atmosphere by adding scenery and sound effects. To change the scenery, we add a “backdrop” image. 9 To the left of the sprites list is a Star Hunter button to add a picture from the backdrop library. Click it and look for by Octoblaster99 (unshared) “underwater2”. Select the image and click “OK”. The backdrop will now fill the stage. The backdrop is just decoration and doesn’t affect the sprites. Stage Click this icon to open the 1 backdrop backdrop library. New backdrop:

GAME PROGRESS 2 9 % 35 Sound effects Delete sounds here. Now we’ll add a bubbling sound to the cat sprite to make it sound like we’re underwater. 10 Highlight the cat in the sprites list and 11 Look for “bubbles” in the 2 then click the Sounds tab above the library. You can preview blocks palette. Click the speaker icon to choose a sound from the library. sounds by clicking the play symbol. To load a sound into the game, click the Scripts Costumes Sounds speaker icon and then “OK”. bubbles Now you’ll see bubbles in 00:04.08 your list of sounds. New sound: Use a sound from This is how long your computer. the sound lasts. Record a sound You can add sounds to the stage as Choose a sound from the library well as to sprites. Sounds tab 12 Click the Scripts tab and add the when clicked Click Sound in the following script to the cat sprite, but forever blocks palette to find leave the old script in place because this block. you need both. The new script repeats the bubbles sound. The “play sound ... play sound bubbles ▾ until done until done” block waits for the sound to finish before letting it start again. Run the game to hear the sound effect. EXPERT TIPS Blocks run from top The “forever” block makes to bottom. the program return to the Loops start of the block. clicked A loop is a section of code that when repeats over and over again. The “forever” block creates a loop that forever carries on forever, but other types of loop can repeat an action a fixed play sound bubbles ▾ until done number of times. Loops are very common in almost all computer programming languages.

36 S T A R H U N T E R This block runs the Motion blocks are dark script when the blue and control the Add an enemy game begins. way sprites move. The game needs an enemy to make things more interesting. Let’s add an octopus with a deadly sting. The octopus will patrol the stage, moving left and right, and the player will have to keep out of its way or the game is over. 13 To add a second sprite to the 14 Add the following script to when clicked project, click the icon shown the octopus sprite. To find below to open up the sprite the blue blocks, click on forever library. Choose the octopus Motion in the blocks and click “OK”. palette. The two Motion move 10 steps blocks used here make the New sprite: octopus move left and if on edge, bounce right across the stage. Click here to open the sprite library. The octopus sprite will The “forever” This block stops appear in your sprites list. block repeats the octopus from everything moving off the inside. edge of the stage. Octopus 15 Now run the script. The octopus will patrol left and 16 Choose the middle option and run the right, but you’ll notice it’s upside down half the project. The octopus should now stay right time. We can fix this by changing the way the sprite side up and facing forward all the time. You turns around when it changes direction. Highlight can adjust its starting position on the screen the octopus and click the blue “i”. In the pop-up by dragging it with the mouse. box, there are three options after “rotation style”. Star Hunter The middle option makes the sprite flip sideways by Octoblaster99 (unshared) when it bounces. Octopus direction: 90° x: 84 y: -69 rotation style: can drag in player: show: The left option makes The right option makes the sprite turn upside the sprite bounce down when it bounces. without turning round.

GAME PROGRESS 4 3 % 37 Collisions then So far the octopus and cat move through each other This block detects without anything happening. We need to add a collision. a script to make them stop moving when they collide. then Collision detection is very important in computer games. 17 Highlight the octopus and drag if touching Cat ▾ ? a yellow “if then” block to an empty part of the scripts area. Now add mouse-pointer a pale blue “touching” block to the top of the “if then” block. Click the edge drop-down menu and choose “Cat”. This script will help the octopus Sensing blocks Cat detect the cat. are pale blue. 18 Choose Control in the blocks palette if touching Cat ▾ ? again, and add a “stop all” block to stop all ▾ the middle of the “if then” block. This will stop all action if the octopus is touching the cat, ending the game. 19 Now add the “if then” blocks you’ve built This block EXPERT TIPS to the octopus’s main script, placing it ends the game carefully after the blue Motion blocks. when the “if then” Also, add a “wait 0.5 sec” before the loop. sprites collide. Run the project and see what happens. You make decisions every day. If it’s raining, you might use when clicked The “wait” block adds a an umbrella. If it isn’t, you don’t. slight delay before the Computer programs do the same octopus starts moving. thing by using what programmers call conditional statements, such wait 0.5 sec as “if then”. When Scratch reaches an “if then” block, it runs the blocks forever The “stop all” block only inside only if the statement is true. move 10 steps runs if the answer to if on edge, bounce the question in the Octopus touching cat? “touching” block is yes. True False if touching Cat ▾ ? then stop all ▾ Stop the sprites Keep going

38 S T A R H U N T E R More enemies Let’s add more enemies to the game, but to make things more challenging, we’ll make them move in different directions. We can tell each sprite exactly which way to go by using a block that works like a compass. 20 Add a purple “set size” block to the top of the 21 To change the octopus’s direction, click on octopus’s script, after the “when clicked” block. Set the window in the “point in direction” block the octopus’s size to 35% to make the game a bit and type 135 in place of 90. This will make easier. Then add a blue “point in direction” block. the octopus move diagonally. Click in this window point in direction 135 ▾ and type 35 to set the when clicked octopus’s size to 35%. (90) right (–90) left wait 0.5 sec Type 135 into this window. set size to 35% (0) up point in direction 135 ▾ (180) down This number tells the The drop-down octopus which direction menu gives you four quick options. to set off in. 22 Now we can duplicate our octopus to create more EXPERT TIPS enemies. Right-click on the octopus in the sprites list (or control-click if you have a Mac) and choose Directions “duplicate”. Copies of the Octopus sprite will appear in the sprites list, named Octopus2 and Octopus3. Scratch uses degrees to set direction. You Each will have a copy of the first octopus’s script. can choose any number from –179° to 180°. Negative numbers point sprites left; positive numbers point them right. Use 0° to go up and 180° to go straight down. –90° moves a 0° Sprites This menu appears sprite straight if you right-click to the left. on a sprite. –90° 90° Cat Octopus info duplicate 180° 180° moves a Choose “duplicate”. delete sprite straight save to local file down.

GAME PROGRESS 5 7 % 39 23 To make the octopuses move in different directions, 24 If it’s too hard to stay alive, make the change the number in the “point in direction” block for octopuses slower by lowering the each new octopus. Leave the first Octopus sprite’s number of steps in their “move” blocks direction as 135, but set Octopus2 to 0 and Octopus3 to two. Remember to change the to 90. Run the project and try to avoid all the enemies. script for all three octopus sprites. Changing this number adjusts the octopus’s speed. move 2 steps if on edge, bounce 25 For more variety, let’s make one when clicked Type –179 in the of the octopuses set off in a first window. random direction. To do this, we use a green “pick random” block. set size to 35% This is Scratch’s way of rolling a dice to generate a random point in direction pick random –179 to 180 number. Choose Operators in the blocks palette to find the wait 0.5 sec Type 180 in the block and add it to the first forever second window. octopus’s script. Run the project a few times to see the octopus move 2 steps choose different starting if on edge, bounce directions. if touching Cat ▾ ? then stop all ▾ EXPERT TIPS 2 Random numbers when clicked say pick random 1 to 6 Why do so many games use dice? Dice create surprises in a game This block picks a random because they make different things number from 1 to 6. happen to each player. A random number is one you can’t predict in advance, just like the roll of a dice. You can get the cat to say a random dice roll using this simple code.

40 S T A R H U N T E R Collecting stars In many games, the player has to collect valuable items to win points or to stay alive. In Star Hunter, we use gold stars as underwater treasure that the player has to collect. We’ll use random numbers again to make each star appear in a new place. 26 Click the “choose new sprite” symbol in the sprites list and choose the “Star1” sprite from the library. Sprites New sprite: Click this symbol to open the sprite library. Star1 Cat Octopus Octopus2 Octopus3 The Star1 sprite will Star1 appear in your 27 Add the following script to Star1. This script will make the star move to a random new location whenever the cat touches it. The sprites list. green blocks create random numbers called coordinates, which Scratch uses to pinpoint locations on the stage. The “if then” block checks whether the cat is touching the star. when clicked then The “go to” block only forever touching Cat ▾ ? runs if the answer to the question is yes. if y: pick random –150 to 150 go to x: pick random –200 to 200 The “forever” block repeats Type the numbers shown the blocks inside it. here into the green blocks. 28 To see the star’s coordinates change when it Star1: x position 60 moves, choose Motion in the blocks palette and Star1: y position 78 put ticks by “x position” and “y position”. Now run the game: you’ll see the star’s x and y coordinates update each time the cat makes it move. Untick both boxes before you carry on.

GAME PROGRESS 7 1 % 41 EXPERT TIPS Star Hunter Using coordinates by Octoblaster99 To pinpoint a location on the stage, Scratch y uses numbers called coordinates. These 180 work just like graph coordinates, with x numbers for horizontal positions and (x:–100, y:100) 120 y numbers for vertical. To find the 60 coordinates for a spot on the stage, just count the steps across and up from the –240 –180 –120 –60 0 (x:180, y:50) x center of the stage. Positive coordinates –60 60 120 180 240 are up or right, negative coordinates are down or left. Every spot on the stage has –120 a unique pair of coordinates that can be used to send a sprite to that position. –180 (x:90, y:–130) (x:–190, y:–150) The x axis is longer than the y axis and extends from –240 to 240. 29 You can add a sound effect that plays Insert the “play sound” block into when the cat touches a star. First Star1’s existing script, then use make sure that the star is selected in the drop-down menu to choose the sprites list, then click the Sounds which sound to play. tab above the blocks palette. Click the speaker symbol to open the sound library. Choose “fairydust” and click “OK”. Now add the pink “play sound” block to the star’s script and choose “fairydust” in the drop-down list. if touching Cat ▾ ? then play sound fairydust ▾ go to x: pick random –200 to 200 y: pick random –150 to 150

42 S T A R H U N T E R 7 69 Keeping score Computer games often need to keep track of vital statistics such as the player’s score or health. We call these changing numbers “variables”. To keep track of the player’s score in Star Hunter, we’ll create a variable that counts the number of stars the player has collected. 30 With any sprite selected, choose 31 A pop-up box appears asking you to Data in the blocks palette. Click on give your variable a name. Type “Score” the button “Make a Variable”. in the box. Make sure the option “For all sprites” is selected and hit “OK”. Scripts Costumes Sounds Motion Events Click here to New Variable Looks Control create a new Variable name: Score Sound Sensing variable. Pen Operators For all sprites For this sprite only Data More Blocks OK Cancel Make a Variable This option makes the Make a List variable available for every sprite. 32 You’ll see a new set of blocks appear, including 33 The score counter will appear in the one for the score. Make sure the box next to it is top left of the stage but you can drag checked to make the score appear on the stage. it anywhere you like. Make a Variable Star Hunter Score by Octoblaster99 (unshared) set Score ▾ to 0 Score 0 change Score ▾ by 1 show variable Score ▾ You can use the hide variable Score ▾ mouse to move the score display.

GAME PROGRESS 8 6 % 43 34 We want the score to start at zero and increase by one each time the cat touches a star. Select the star sprite and add the two orange Data blocks below to its script. when clicked Add this block to set the score to zero at the start of a game. If you use the offline set Score ▾ to 0 version of Scratch, don’t forget to save your work forever from time to time. if touching Cat ▾ ? then Add this one to make the change Score ▾ by 1 score increase when the play sound fairydust ▾ cat catches a star. go to x: pick random –200 to 200 y: pick random –150 to 150 35 Now click the green flag to Hey, I’m X run the script and see what years old! happens when the cat collects each star. See if you Big deal, I’m can collect 20 stars without Y years old! bumping into an octopus. EXPERT TIPS Variables A variable works like a box that you can store information in, such as a number than can change. In math, we use letters for variables, such as x and y. In computer programming, we give variables names such as “Score” and use them for storing not just numbers but any kind of information. Try to choose a name that tells you what the variable is for, such as “Speed” or “Score”. Most computer languages won’t let you put spaces in the names of variables, so a good tip is to combine words. Instead of using “dog speed”, for instance, type “DogSpeed”.

44 S T A R H U N T E R Better enemies Now we have a working game, we can test it and experiment with changes that make it easier, harder, or—most important—more fun. One way to make the game more interesting is to make the three octopuses do different things. 36 Right-click the script for Octopus2 and select “delete” 37 Run the project and see how the to remove it. Replace it with the following script. This game plays. You’ll probably find it will make the octopus chase the cat. hard to escape the octopus because it moves quickly. To slow it down, when clicked change the number of steps to two. set size to 35% This block starts move 2 steps wait 0.5 sec the chase. forever This number then controls the point towards Cat ▾ octopus’s speed. move 5 steps Drop the “Score” if touching Cat ▾ ? block into the circular window in stop all ▾ the “move” block. Score The octopus will move the same 38 You can make the game get harder as you play. move 2 steps number of steps Select the original octopus sprite and click Data as the score. in the blocks palette. Drag the “Score” block into the octopus’s “move” block. Now try the steps game. The more points you get, the faster the octopus swims. move Score 39 If it gets too hard too quickly, we can make things The green block divides the more gradual. Choose Operators in the blocks score by 3 to make the octopus palette and find the small green “divide” block. speed up more gradually. Rearrange the “move” block so it looks like the image below. Type “3” in the second round window. move Score / 3 steps

GAME PROGRESS 100% 45 40 Now we’ll make Octopus3 patrol in a regular pattern. To do this, The two scripts we’ll use a new Motion block that makes it glide smoothly from are separate in point to point, rather than moving in steps. Replace the script the scripts area. for Octopus3 with the following two scripts. These run at the same time, one checking for collisions and the other moving the octopus around its patrol route. Scripts Costumes Sounds Motion Events when clicked Looks Control Sound Sensing forever x: –126 Pen Operators y: 96 Data More Blocks glide 3 secs to x: 0 y: –150 glide 3 secs to x: 200 y: 100 glide 3 secs to x: –200 y: 100 move 10 steps when clicked Type these turn 15 degrees numbers into the turn 15 degrees set size to 35% “glide” blocks. point in direction 90 ▾ point towards ▾ wait 0.5 sec then go to x: 0 y: 0 go to mouse-pointer ▾ forever glide 1 secs to x: 0 y: 0 if touching Cat ▾ ? stop all ▾ 41 Now run the project Star Hunter I feel like I’m and watch Octopus3. swimming in It should swim in a by Octoblaster99 (unshared) repeating triangle circles... pattern. Score 0 To change the shape of the triangle, try different numbers in the “glide” blocks.

46 S T A R H U N T E R LINGO Hacks and tweaks Bugs You’ve built a fun game, but that’s just the A bug is an error in a program. The beginning. Scratch makes it easy to change first computers made mistakes and adapt games as much as you want. You when real insects, or bugs, got in might find bugs that need fixing, or you might their circuits. The name stuck. want to make the game harder or easier. Here Today, programmers often spend are some suggestions to get you started. as much time finding and fixing bugs as they do writing code in ▽ Debug Octopus2 the first place. If Octopus2 ends up in the top-right corner at the end of a game, it can trap the player in the next game and end it too quickly. This is a bug. To fix it, you could drag the octopus away from the corner before starting, but it’s better to use a script that moves it automatically. Insert a “go to” block at the start of the script for Octopus2 to send it to the center of the stage. Star Hunter by Octoblaster99 (unshared) Octopus2 can trap the player in the top-right corner. when clicked Add this block to make △ Fine-tuning Octopus2 start in the center of the stage. The best games have been carefully tested to make sure they play well. Test every change go to x: 0 y: 0 you make and get friends to play your games to see how well they work.

HACKS AND TWEAKS 47 ▽ Different colors ▽ Flashing colors Make your octopuses different colors by using the “set color” block from You can make an octopus change color continually the Looks section. Place it under the to create a flashing effect. Add the script below to “set size” block at the start of the script. any octopus. Try experimenting with different numbers in the “change color” block. set color ▾ effect to 50 when clicked Change this Try setting this number number to make anywhere from –100 to 100 to colors change faster or slower. see the full range of colors. forever Hey! Turn me back into a cat! change color ▾ effect by 25 △ Scuba diver ▽ Play with size You can change how easy the game is by adjusting To make the underwater theme more the size of the sprites. Change the number in the convincing, replace the cat with a diver. octopuses’ blue “move” blocks to alter their speed. Click on the cat in the sprites list, then Change the purple “set size” blocks to make sprites open the Costumes tab and click on the larger or smaller. Fine-tune the numbers until the sprite symbol to open the library. Load game is just hard enough to be fun. the costume called “diver1”. set size to 50% when clicked ◁ Swimming animation forever To add a professional touch to Star set size to 100% Hunter, animate the octopuses next costume so that they look as if they’re swimming. Add this script to an wait 0.1 secs empty part of the scripts area for each octopus to make them switch between two different poses.


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook