Linux Format

Scratch: Hack it

Les Pounder seeks to capture the Space Dragon, but first he needs to build and code a rocket using the visual programmin­g tool, Scratch.

- Les Pounder travels the UK working with the Raspberry Pi Foundation’s Education team. He loves hacking with Raspberry Pis and toys. He also writes a blog at http://bigl.es.

September 2015 saw the release of Raspbian Jessie, and with it the Raspberry Pi Foundation released its own version of Scratch. In this project we’ll learn more about Scratch and use it to hack together a space game, which we will control using our very own controller.

For this project you’ll need any model of Pi, the latest Raspbian, three momentary switches, Male to Female jumper cables, three 220-ohm resistors, a breadboard and three LEDs.

We’ll start by building our controller. We’ll connect our buttons to the breadboard and then use the jumper cables to attach one corner of the button to a GPIO pin, and another corner to a Ground (GND) pin. Similarly, we connect our LED’s anode, the longer leg of an LED, to a GPIO pin and connect the shorter leg, the cathode, to GND via a 220-ohm resistor. For a detailed overview and all the required code see http://bit.ly/LXF207-Scratch-Diagram. The preferred layout of the GPIO pins is Broadcom (see http://pinout.xyz) For the GPIO pins we’ll use for the buttons, please see the table on this page (right).

Let’s power up our Pi and start building our game. Raspbian will automatica­lly log you into the desktop. Navigate to the main menu and open Scratch in the Programmin­g menu. In Scratch, the left-hand column contains a palette of blocks, separated into groups according to their function. The blocks can be dragged into the centre column, which is where we build our code. In the final column is the Stage, where all the output for our game will take place. At the bottom right you can see all the sprites used in the game.

To start, let’s make the Stage look more spacey. Click the ‘Stage’ icon, at the bottom right of the screen. This changes the focus of any code to the Stage. In the centre column, find the tab labelled ‘Background­s’. Click this, then Import a new background. We chose ‘Stars’, found in the Nature folder. Now click on the cat sprite, and change its name at the top of the screen to something appropriat­e like ‘Rocket’.

Click on ‘Costumes’ and you will see that this sprite has two, used for animation. To change our cat into something more space-related, we can paint a new costume. I used a small rocket, which is included in the code download for this project. Click ‘OK’ to load the new costume, and then select that costume as the default by clicking on it.

Keeping your focus on the Rocket sprite, click ‘Scripts’. We’ll now create our first script for the Rocket. From the Control palette, drag ‘Click on Green Flag’ to the coding area. Also from the Control palette drag the Broadcast block to the coding area and attach it to the Green Flag block. In the Broadcast block click on the drop-down, select New/Edit and type gpioserver­on . This will start the Scratch GPIO server when our game starts, enabling access to the GPIO in Scratch. Now we need to configure our buttons as inputs. Add another Broadcast, and in its drop-down select New/Edit and type config2in to set GPIO2 as an input. Add two more Broadcasts and repeat, using config14in and config23in respective­ly for GPIO pins 14 and 23, our left and right buttons. To ensure that the pins are configured, click on the ‘Green Flag’, just above the Stage.

Next grab another Green Flag block from the Control

palette, and add a ‘Wait Until’ block to it. Inside the blank for the Wait Until block place a ‘__ = __’ block from the Operators palette. Now move to Sensing palette and look for ‘Slider Sensor value’. Click the drop-down and change it to gpio2 . To the left you will see a tickbox; tick it and the sensor value for gpio2 is printed to the top left of the Stage. Currently the sensor is reading 1, or high. Press the button and the sensor value changes to 0, or low. Test complete, untick the Sensor Value tickbox. Now that we know it works, drag the ‘gpio2 sensor value’ block to the first blank in the ‘__ = __’ block, then type 0 (zero) in the remaining box. Our last block for this section is another Broadcast called Start.

Interactio­n with sprites

Now let’s create more sprites for our game. Draw a planet by clicking the ‘New Sprite’ icon. The planet has no code attached to it. Now we’ll create two sprites to act as obstacles for our ship to avoid. Add a new sprite by clicking the ‘Add Sprite’ icon – the middle icon in the row below the Stage. We’ll choose a robot, but at its default size it is a little large, so rightclick on the sprite and choose Resize.

Earlier, we created a Broadcast called Start, and in the Control palette we can see that block now exists. Drag ‘When I receive start’ to the coding area, followed by a Forever loop. In the Motion palette drag ‘Turn clockwise 15 degrees’ and ‘Move 10 steps’. Place both of these blocks inside the loop.

Our last section of code for the robot starts with another ‘When I receive start’ block. Under that we add a Forever loop, and inside the loop we add an If statement. This will constantly check to see if we are touching the Rocket sprite – the touching block is the top block in the Sensing palette. If this is True, then our game will play a sound effect, found in the Sound palette, then say “Ouch” for 0.5 seconds. The ‘Say’ block is found in the Looks palette. If you want to add more obstacles, right-click on the robot sprite and duplicate until you have the required number.

Our next sprite is an enemy to hunt, a Space Dragon. Choose a new sprite and then drag a ‘When I receive start’ Broadcast from the Command palette, and also grab a Forever loop. Drag the Space Dragon to some part of the screen. Go to the Motion palette and look for ‘Glide 1 secs to x: y:’. This will be pre-populated with coordinate­s, but they’ll be wrong. To fix this, change to another palette and then back to Motion, and it will update. Drag the ‘Glide’ block to the loop. Repeat this action four or more times to create a pattern for our enemy to follow. Remember that we had a sound play when an obstacle is hit. Well, the same can happen with our Space Dragon: just add the same code from our Robot sprite.

Return to the Rocket sprite and create another section of code that starts ‘When I receive start’, using a Forever loop. Now turn on our LEDs by using a Broadcast to turn a pin on and off – for example gpio17on and gpio17off . Our LEDs are on 17, 27 and 22, so construct a light sequence, rememberin­g to use ‘Wait’ blocks to control the LEDs’ speed.

To create controls for the rocket drag another ‘When I receive start’ block into the coding area, along with a Forever loop and two If statements. Both If statements will be inside the loop, on top of each other. Grab two ‘__=__’ blocks from the Operators palette and place one in each If statement. Next grab a gpio14 sensor value from Sensing and place it in the first blank of ‘__=__’ and type 0 in the other. Repeat for the second If statement but change gpio14 to gpio23.

To finish our Rocket code, add one more ‘When I receive start’ block. Use ‘Go to x: y:’ to set a starting position for the rocket, say the bottom left of the screen (x:0, y:0). Next, we use a Forever loop to play a laser sound, then move the ship forward 10 steps before waiting for 0.1 seconds. We add ‘If on edge, bounce’ to stop the Rocket getting lost.

With all of the code complete as shown below, save your work and blast off for an adventure!

 ??  ??
 ??  ?? Our project is a crazy space game where our rocket must attack the mysterious Space Dragon while also avoiding a deadly Space Robot and a Comet.
Our project is a crazy space game where our rocket must attack the mysterious Space Dragon while also avoiding a deadly Space Robot and a Comet.
 ??  ?? We built a custom controller from spare components, an old slide box and – for that authentic space experience – a launch switch found on eBay.
We built a custom controller from spare components, an old slide box and – for that authentic space experience – a launch switch found on eBay.

Newspapers in English

Newspapers from Australia