Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Scratch 2.0 Game Development Hotshot

You're reading from   Scratch 2.0 Game Development Hotshot Get up to date with Scratch 2.0 and build brilliant games without having to code. Including 10 exciting projects that cover most game genres, you'll quickly learn the sophisticated possibilities of Scratch. Have fun!

Arrow left icon
Product type Paperback
Published in Feb 2014
Publisher
ISBN-13 9781849697569
Length 330 pages
Edition Edition
Tools
Arrow right icon
Toc

Table of Contents (18) Chapters Close

Scratch 2.0 Game Development HOTSHOT
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Blowing Things Up! 2. Beating Back the Horde FREE CHAPTER 3. Start Your Engines 4. Space Age 5. Shoot 'Em Up 6. Building a Worthy Boss 7. Creating a Level Editor 8. Dungeon Crawl 9. Hunger Run 10. Sprites with Characters The New Scratch Interface Index

Starting scripts


Let's have some fun making the cannon shoot its cannonball. It's always a good idea to script and test the interactive parts of your game as early as possible. Without scripts, it's just a bunch of pretty pictures! They might be nice to look at, but they won't keep the player entertained for long.

Engage thrusters

We have two objects to script. Object is an official programmer word that means something that performs an action in a program.

In this case the objects are visible. Our sprites are our objects. We have a cannonball and a cannon. The player will be able to control the direction of the cannon. The cannonball will fly away in a certain direction based on which way the cannon is pointing. So the way things are controlled is:

player → cannon → cannonball

Let's create a short script for the cannon. This script will save the direction the cannon is pointing to, so that the cannonball will know in which direction to fly.

We have to create a variable to store the direction of the cannon. If you're unfamiliar with variables, read the information box on the following page. To create a variable, follow these steps:

  1. Click on the Data category. This is where you can create variables.

  2. We will now click on the Make a Variable button.

  3. Name the variable direction and make it available for all sprites.

  4. We start the program with the when <green flag> clicked block. This is the easiest way to set any program in motion.

  5. Underneath it, we will place a forever block, because we will check the cannon's direction indefinitely.

  6. If you want, you can tick the checkbox to make the variable visible on stage. Then, you can see the direction that the cannon is facing in, at all times.

  7. Put a set () to () block in the forever block and select direction.

  8. View the Motion category and look down at the list of blocks to find the built-in cannon direction variable. Place it in the open space. It may look superfluous to send the built-in variable value to a self-made variable. We do this because Scratch can't send built-in sprite variables to other sprites directly. Our self-made variable can be used in all sprites.

This is all the scripting that has to be done for the cannon. The following is the finished script:

Note

About variables

Variables are an important part of programming. They allow you to store information for later use and to transfer information between different objects. A variable consists of two things; a name by which it is recognized and the word/number/object that it stores.

The easiest way to think about it is to compare the variable to a jar of pickles in a grocery store. The store clerk is the computer. He handles this jar and all the other jars that are available in the store. The jar is a variable.

On the face of it they all look the same, like glass containers. It's hard to distinguish one jar from another. That's why every jar has a label with a word on it. This is the name of the jar/variable. Most probably, the name of the jar will say something about what's in the jar. The jar of pickles, for example, will be called "Pickles".

You move up to the counter and ask the clerk, "How many pickles are in the Pickles jar?" and the clerk checks the jar, counts the pickles, and says, "There are 9 pickles in the jar." You now know something about the content of the Pickles jar. You feel like having a snack and decide to buy two pickles. After you paid and received the pickles, you ask again, "How many pickles are in the Pickles jar?" and the clerk counts the contents of the jar again (just to make sure) and answers, "There are 7 pickles in the jar."

It's most common to store numbers in a variable, because computers like to work with numbers, but variables can also contain names or even whole objects. Scratch keeps it simple, though. Scratch variables can only contain numbers or names/words.

Perhaps, it's better to illustrate the explanation of variables with the following screenshot:

We will go on to create a script to move the cannonball:

  1. Click on the cannonball sprite to open its script editor.

  2. The cannonball will also be triggered with a when <green flag> clicked block. Its actions will also be contained inside a forever loop.

  3. Place a move () steps block inside the forever block. That's the basic necessity to set the sprite in motion.

Now we build a few more controlling scripts, so the cannonball actually goes where we want it to go:

  1. Place a go to () block on top of the move () steps command and select cannon. This makes the cannonball hop back to its starting position.

  2. Make the cannonball copy the cannon direction with a point in direction () block and by insetting the direction variable. Note that this is our added variable (orange background) from the Data category, not the built-in (blue background) variable from the Motion category. The variable with the orange background is the saved cannon position. In this case, the variable with the blue background is the current direction of the cannonball, which wouldn't change anything when applied to itself.

To make the cannonball move forward, instead of constantly resetting, we use another kind of loop with a condition:

  1. Put a repeat until () block around the move command.

  2. Then, place a touching () condition block in the vacant space and select edge.

Now the cannonball will angle itself in the same direction as the cannon, and it will keep moving forward until it reaches the edge of the stage. At that point, the script repeats and the cannonball is reset to its starting position in the cannon. The following is the completed script:

We're not building any fancy controls at this point. Click on the i button in the top-left corner of the sprite in the Sprites window:

This will switch the sprites view to the sprite properties screen. Here you can view and edit some information about your sprites. An important one is the sprite name; you can name the sprite here if you didn't already do so in the sprite editor during its creation. Notice the sprite name in the top field of the properties screen:

What we are really looking for at this moment is the little direction tool located on the right side of the window.

Objective complete – mini debriefing

Click on and drag the little blue pin to change the direction that the cannon is facing in. Try it and see how the cannonball shoots in different directions depending on the direction of the cannon.

Don't forget to click on the green flag at the top of the stage (if you haven't already) to activate both scripts!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image