APC Australia

Escape from the evil space squids

Captain Les Pounder of the USS Rentaprize has to evade the clutches of the evil space squids, but can he do the maths to escape the planet?

-

Block-based coding is a great way to introduce the concepts of coding, via an easy-to-understand mechanism. Blocks of code are stacked to create a flow, and if we encounter a mistake, we can easily pull the code apart to “debug”.

Scratch is a popular block editor and in this tutorial we’ll create a space escape game, where our rocket is trapped on an alien world and we need to calculate the correct thrust for lift-off. If we get it wrong more than 10 times then the evil space squids will get us.

Scratch 3 should come preinstall­ed on your Raspberry Pi OS image, but if it’s missing it can be installed from the main menu, under Preference­s> Recommende­d Software. Scratch 3 is found in the Programmin­g category; place a tick in the box and click Apply to install. Once installed,Scratch 3 can be found in the main menu under Programmin­g.

Open Scratch 3 . Before we write any code, let’s understand the interface. On the left are a series of colour-coded, categorise­d blocks of code. These blocks fall under categories that identify what they do: Motion to move a sprite, Looks to change their appearance, Control to create the logic for our code. The blocks are dragged into the centre area and then assembled into a project. In the top right of the screen we have the Stage – this is where our code will run, we see things move and we can interact with the code. Finally, in the bottom right is where all of the sprites, the characters in our game and the backdrop of our stage can be edited.

We’ll start in the bottom right of the screen. The default sprite is Scratch the cat, but for this game we can delete the cat by clicking the trashcan icon in the top right of the sprite. If you look to the bottom right there’s a blue cat icon. Click this icon to add a new sprite. Search for rocket to use the sprite. Repeat this process and search for a sprite to act as our alien – we chose a squid. Just to the right is a blue icon that changes the look of the stage, a backdrop. Click the icon and select a spacetheme­d backdrop.

Make sure that your rocket sprite is selected, left click the sprite. We’ll start writing the code for our game. First we go to Events and drag When Green Flag Clicked . This will start our game when the green flag above the stage is clicked. Drag the rocket sprite so that it’s resting on the planet. Go to Motion and drag the Go to x: y: block so that it connects with the previous. Now when the game starts, the rocket returns to the planet.

The next three blocks are all from Looks. The first is Switch costume – drag this and connect to the previous blocks. Change the costume to rocketship-e . Now drag two Say ___ for 2 seconds blocks and connect to the previous. In the first Say block, we tell the player that aliens are coming; in the next block we instruct the player to calculate the thrust for take-off.

Feeling gravity’s pull

Next we go to Variables and create three variables. The first is called gravity. Drag Set gravity to 0 and connect to the previous block. From operators drag the Pick random block and place it over the zero of the Set block. Change the numbers to be from one to 20. Create another variable, Accelerati­on and repeat Pick random but keep the numbers as one to 10.

So now we have two variables that will generate random numbers for our game. The third variable is called force

and for now we just need to create it – we’ll use it later.

We need to give the player a clue to escape the planet. For this we use another Say block, but this time we go to Operators and drag a Join block over the text of Say . In the first blank of Join we say Sensors show the gravity is , and in the second we drag the gravity block from Variables. This block updates with a random value each time the game starts. Next we drag Set force from Variables and connect it to the previous. In our case force is a calculatio­n, so we need to go to Operators and drag __ * __ into the Set block. Then drag the gravity and accelerati­on blocks from Variables so that force = gravity * accelerati­on. Note we only

want the gravity variable visible. To turn off the others click the tickbox next to their name in Variables.

Ten chances and counting…

So we have the data now we need to code the game mechanism. For this we need to give the player 10 chances for take-off. From Control drag repeat 10 into the coding area and connect to the previous blocks. We now need a means for the player to make their guess – for this we go to Sensing and drag the Ask __ and wait

block into the repeat 10 loop. Change the text to read Thrust calculatio­n? . Each time the player guess, their answer is stored in a special variable called answer which is located in the Sensing section.

Next drag three if blocks and place them underneath the question, inside the repeat 10 loop. Ensure they’re stacked upon one another and not inside each other. Each if is a question to check the player’s guess against the answer stored in the force variable. The if

statement has a hexagon-shaped box, in which we’re going to drag a block that compares two values. Found in Operators, we first drag __ ≥ 50 and place it in the first if hexagon. From Sensing we drag the answer

block and place in the first blank. Then from Variables we drag force and place that over the 50 . So this statement checks if the user’s answer is greater than the force needed to escape. If that is correct (true) inside that if statement we place a

say block from Looks and we change the text to alert the player that there’s too much thrust and we must abort.

For the next if statement we repeat the same process, but use a

__ ≤ 50 and change the text to say that there’s insufficie­nt thrust for take-off. The final if statement checks to see if the user’s guess is the same as the answer stored in the force variable. This uses the

__ = 50 block from Operators and the same answer and force blocks as before. If this statement is correct, we go to Looks and drag the think block into the if statement. We alter the text to

Checking thrust calculatio­ns… , then go back to Looks, drag Say and alter the text to BLAST OFF .

Again, back to Looks. We chose

switch the costume to rocketship-a , which changes the sprite so that we have a plume of fire thrusting the rocket upwards. To give the rocket motion, drag the rocket to the top centre of the screen, so that only the plume of fire is visible. Go to Motion, and drag glide _ secs to x: _ y: _ into the code. The x and y values will be populated by the current position of the rocket. The final block inside this if statement is

Stop all and it’s found in Control.

The final block in this game is outside the repeat 10 and if statements. From Events we drag

broadcast and change the message to read attack . This broadcast will signal our alien sprite.

Click the alien to start writing code for that sprite. The first block is from Events and it’s When I receive __ , drag this and change it to attack . This means the code will run when the player loses. From control drag a repeat 10 block and inside place the following blocks from Looks, Show , change color effect by 25

, next costume and change size by 10 . Lastly from Control place

wait 1 seconds inside the loop. Change the duration to 0.5 seconds. Our last piece of code uses When Green Flag Clicked from Events, and from Looks we use hide to make sure the alien hides until the end!

Click the Green Flag to start the game, and try to escape from the evil space squids!

 ??  ?? Our finished game is a colourful and engaging way to test number skills and logic, but sadly it won’t get us into outer space.
Our finished game is a colourful and engaging way to test number skills and logic, but sadly it won’t get us into outer space.
 ??  ?? Sprites are characters in a game. In our case the rocket and squid alien are sprites. Each sprite can have its own programmin­g and they can talk to one another.
Sprites are characters in a game. In our case the rocket and squid alien are sprites. Each sprite can have its own programmin­g and they can talk to one another.
 ??  ?? If conditiona­l statements are questions in code. They check to see if something is true, and if so then a section of code is activated.
If conditiona­l statements are questions in code. They check to see if something is true, and if so then a section of code is activated.

Newspapers in English

Newspapers from Australia