Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Projects

Nature rover
Use Scratch to create a wilderness scene for a nature
rover to explore

Step 1 You will make

Create a wilderness scene for a nature rover to explore.

Rovers are robots. They can be used to carry out science experiments in remote places, like Mars! They examine
their surroundings and they can be directed to interesting features and take samples. If they are solar powered,
they can place themselves in a sunny position to recharge their batteries.

You will:

Move sprites using perspective to give the appearance of distance


Organise costume changes with my blocks
Create a realistic scene using scrolling

Scrolling is when graphics (or objects in your scene) move left, right, up, or down on a computer screen.
Scrolling makes scenes look more realistic.
Step 2 Control the rover

In this step, you will use an on-screen controller or the


keyboard to move your rover up and down.

Open the starter project online at rpf.io/nature-rover-starter (https://rpf.io/nature-rover-starter).

Working offline
If you are working offline, the starter project can be found at rpf.io/p/en/nature-rover-go (https://rpf.i
o/p/en/nature-rover-go)

You should see a scene with a robotic rover, a hilly background, and a controller in the bottom, left-hand corner.

You are going to use the controller or keyboard controls to make the rover look as if it is moving towards or away from
the viewer.
Look at the Code tab for each of the buttons. The code will look something like this:

when this sprite clicked

broadcast up

when I receive start

forever

go to front layer

go to x: -190 y: -121

This keeps the buttons in the correct position, and broadcasts their directions when they are clicked.

The rover is going to control the start of the game for all the other sprites; so when the green flag is clicked, the rover
sprite needs to broadcast a start message.

Add the broadcast to a green flag clicked block.

when clicked

broadcast start
If you are working on a computer, it might be easier to use the keyboard controls, rather than using the
buttons. Keyboard controls can be added to the rover sprite.

when up arrow key pressed

broadcast up

when down arrow key pressed

broadcast down

when right arrow key pressed

broadcast right

when left arrow key pressed

broadcast left

If you don’t want to use the controller, then click on each of the button sprites and click on the hide block
in the looks
menu.

hide

When the up button is clicked or the up arrow is pressed, the rover should change its y position by a small amount.
Increasing y will make the rover move up. Decreasing y will make the rover move down.
Add code so the up button makes the rover move up.

when I receive up

change y by 10

when I receive down

change y by -10

You don’t need to worry about the left and right motion yet. Left and right motion will be added in the next step
of the project.

Perspective is used in computer graphics to make a scene more realistic. Objects that are far away normally
appear to be smaller and higher up the screen. Objects that are close appear to be larger and lower down the
screen.
Add perspective to your rover by making it smaller when it moves upwards, and larger when it moves
downwards.

when I receive up

change y by 10

change size by -1 Smaller looks further away

when I receive down

change y by -10

change size by 1 Bigger looks closer

You’ll need to reset the rover’s size at the start of the game.

when I receive start

set size to 50 %

Test: Click the up and down buttons to check the control of your rover, or use the arrow keys.
Now reset the position of the rover each time the game starts.

when I receive start

set size to 50 %

go to x: 0 y: -90

For now, the rover should appear in front of the other sprites. Move the rover to the front layer.

when I receive start

set size to 50 %

go to x: 0 y: -90

go to front layer

Test: Click the green flag to test that your game resets correctly.

Save your project


Step 3 Scroll the background

For the rover to look like it’s moving left and right, instead of the
rover sprite moving, the background sprite moves or scrolls to
the left or right.

Select the hills sprite. At the start of the game, you need to make sure that it is in the correct position and
on the back layer.

when I receive start

go to back layer

go to x: 0 y: 0

Layers are like stacked sheets of clear plastic that you can draw images on. If an image on the top of the stack is
covering the image below it, you will not be able to see the bottom image properly. Background images should
be near the back layer. Images closer to the viewer should be near the front layer.
The hills sprite needs to make a copy of itself. These are called clones. Then, the original sprite can be
moved to the far right-hand side of the screen.

when I receive start

go to back layer

go to x: 0 y: 0

create clone of myself Create a copy of the hills

change x by 460 Move the original hills to the right of the screen

When the left and right broadcasts are received, the hills sprite should move. To give the appearance of moving in
the correct direction, the background moves left when the rover is moving right. The direction of motion should be
opposite to the broadcast.
So, if the broadcast is left, then the x position will increase. If the broadcast is right, then the x of the hills will
decrease.
Add blocks to control the motion of the hills sprite and its clone.

when I receive left

change x by 3

when I receive right

change x by -3

Test: Use the controller or the arrow keys to move around. The rover should appear to be moving left and
right.

At the moment, there are two copies of the hills sprite: the original and a clone. When you get to the end of either one,
you’ll notice that the screen is just white.
To fix this, the sprite and its clone need to be moved to the other side of the screen when they go too far.
Create a new broadcast called scroll and add it to the start script.

when I receive start

go to back layer

go to x: 0 y: 0

create clone of myself

change x by 460

broadcast scroll
Add code to detect if the hills sprite or its clone have moved too far to the left or right, and then reset their
positions to the other side of the screen.

when I receive scroll

forever

if x position > 460 then The hills sprite is off the right side of the screen

set x to -460 Reset to the left side of the screen

if x position < -460 then The hills sprite is off the left side of the screen

set x to 460 Reset to the right side of the screen

Test: Use the controller or arrow keys to move the rover. The background should scroll, and the rover
should never reach the end.

Save your project


Step 4 Scroll more sprites

When adding more sprites to your scene, these need to scroll


left and right as well.

Now you can add some more objects to your scene, and scroll them in a similar way.

Add a tree sprite to your project, and then set its starting position.

when I receive start

go to x: 0 y: -80

The tree sprite should also move in the opposite direction to the broadcast as well.

As the tree is closer to the viewer, it should appear to move a greater distance than the hills each time the button or
key is pressed.
To get this moving effect, change the x values that the tree sprite moves by when the left and right
broadcasts are received.

when I receive left

change x by 10 Use a bigger number than for the hills

when I receive right

change x by -10 Use a bigger number than for the hills

Test: Check your left and right buttons now. The tree should move each time you click on the controller.
Test: What happens if you go as far away from the tree as you can?

Did you notice that when the tree reaches the very edge of the screen, it stops moving? You can fix this by moving the
tree to the other side of the screen, when its x coordinate is too high or too low.
Using a forever loop, and if blocks, check the x coordinate of the tree, and move it to the other side of
the screen when x is higher than 290 or lower than -290.

when I receive start

go to x: -90 y: -80

forever

if x position > 290 then The tree is at the far right

set x to -280 Move the tree to the far left

if x position < -290 then The tree is at the far left

set x to 280 Move the tree to the far right

Now move your rover sprite around the screen. When the tree reaches the edge, it should vanish off the
edge of the screen and reappear on the other side.
Lastly, make the rover turn left and right so that it faces the direction it is moving in.

when clicked

broadcast start

set rotation style left-right

when I receive left

point in direction -90

when I receive right

point in direction 90

Test: Run your project and test it. Make sure the tree appears to fall off the edge of the screen and appears
on the other side when the rover moves.
Step 5 Collect a sample

In this step, you will change the appearance of a sprite and the
rover to show the rover collecting samples.

Look at the rover sprite’s costumes. There are six animations available. The rover can:
Extend its arm

Drill into the ground


Suck in air
Extend a solar panel
Take a picture
Scoop something up

When you want to organise a lot of code in Scratch, such as several costume changes, it is useful to use My Blocks.
This allows you to create your own custom blocks.

Your rover sprite will have a My Block for each animation.

In the My Blocks menu, click on Make a Block, and name your new block sample fruit.

A new block should appear in your script. It will look like this:
define sample fruit

Beneath this block, attach some switch costume blocks and wait blocks, to animate the robot.

Tip: It is quicker to create your first switch costume block and wait block, then duplicate them, and
change the costume being used.

define sample fruit Animates the robot to collect fruit

switch costume to inactive

wait 0.3 seconds

switch costume to arm 1

wait 0.3 seconds

switch costume to arm 2

wait 0.3 seconds

switch costume to arm 1

wait 0.3 seconds

switch costume to inactive


Add a block so that the rover sprite plays a sound when it collects the fruit sample.

define sample fruit Animates the robot to collect fruit

switch costume to inactive

wait 0.3 seconds

switch costume to arm 1

wait 0.3 seconds

switch costume to arm 2

wait 0.3 seconds

start sound Collect

switch costume to arm 1

wait 0.3 seconds

switch costume to inactive

You can click on the define sample fruit block to see the animation. If you are on a small screen, you
might need to look closely.
The animation won’t run when you click the green flag though, as you have not yet used your new sample
fruit block in your project.
To use your new block, you can attach it to an event block. In the My Blocks menu, you should see the
block you made. Use it in the following script.

when this sprite clicked

sample fruit Run the animation

Click on the rover sprite, and you should see the animation.

Now you need to make the rover actually collect a sample. In this example, the rover will collect a fruit from a tree.

The tree sprite should be edited to give it two different costumes. One with a fruit on (tree with fruit),
and one without a fruit (tree without fruit). Edit one of the costumes, so that the tree has two
different costumes.
On the tree sprite, add blocks to set the costume of the tree at the start of the project, and the costume it
should switch to when it receives a sample fruit broadcast.

when I receive start

go to x: -90 y: -80

switch costume to tree with fruit

forever

if x position > 290 then

set x to -280

if x position < -290 then

set x to 280

when I receive sample fruit

switch costume to tree without fruit


On the rover sprite, you can use the new broadcast to trigger the costume change. Add this new
broadcast into your define sample fruit function.

define sample fruit

switch costume to inactive

wait 0.3 seconds

switch costume to arm 1

wait 0.3 seconds

switch costume to arm 2

wait 0.3 seconds

broadcast sample fruit

switch costume to arm 1

wait 0.3 seconds

switch costume to inactive

Test: To check that your code is working, click on the flag, and then click on your rover sprite. Its arm
should extend, and the tree sprite should change costumes.
Tip: Switch to full screen mode and you will be able to see the animation more easily.

The rover should only be able to collect the fruit, if it is touching it.
On the rover sprite, change the when this sprite clicked set of blocks, so that the sample fruit
function is only called if the rover sprite is touching the colour of your fruit.
Tip: Your costume change from testing might mean that the fruit is not visible. Just click on the costumes
tab for the tree sprite, and switch to the costume with the visible fruit.

when this sprite clicked

if touching color ? then Colour of fruit

sample fruit
Now that the tree sprite changes when a fruit is sampled, you need to reset the sprite to its first costume
when it goes off the screen.

when I receive start

go to x: -90 y: -80

switch costume to tree with fruit

forever

if x position > 290 then

set x to -280

switch costume to tree with fruit

if x position < -290 then

set x to 280

switch costume to tree with fruit

Test: Move the rover sprite so that it is touching the fruit, then click on the rover sprite and watch it collect
the fruit from the tree.

Save your project


Step 6 Create another sample

Choose what the rover samples next! Does it see a new species
of plant, bug, or animal? Does it see some water or mysterious
liquid? Does it want to sample some dirt or a rock, or even the
air? Or something else?

Choose: Add a new sample sprite for the rover to collect. You can add this to your project either by
selecting one from the library or drawing your own.

Get your sample sprite to scroll. If it is low down on the screen it should move more (change x by should be higher
than 5), and if it is high up on the screen it should move less (change x by should be lower than 5).

Add code so that your sprite scrolls.

Choose: Depending on what you want the rover to sample next, you can either use the rover animations that are
there already, or create more animation sequences by duplicating and editing the costumes.
Add a My Blocks block to your rover sprite to animate the rover when it collects the new sample. You can
copy and paste the code from one of the other My Blocks you have created.
For instance, here is a costume sequence for collecting solar energy:

To animate this series of costumes you could use the following code:
define recharge

switch costume to inactive

wait 0.3 seconds

switch costume to solar 1

wait 0.3 seconds

switch costume to solar 2

wait 0.3 seconds

switch costume to solar 3

wait 0.3 seconds

switch costume to solar 2

wait 0.3 seconds

switch costume to solar 1

wait 0.3 seconds

switch costume to inactive

wait 0.3 seconds

Next, broadcast a new message in your new My Block block that will cause a costume or graphic
effect change in the sample sprite.

Use an if block to detect if the rover is touching the sprite or touching a colour on the sprite, so that the
new My Block is only used then.
If you like, add an animation to the sprite, so that its appearance changes when the rover has collected the
sample.

Save your project


Upgrade your project
Make your rover interact with the nature scene even more, similar to how a real rover would behave.
You can:

Add even more samples to your project, using the prebuilt animations or ones you create yourself
Use variables to count samples; increase the count each time a sample is collected
Introduce an energy variable so that the rover has to use a renewable energy source like the sun, to recharge
Animate the rover when it has collected enough samples (for example, it could do a dance and then fly away)
You can use any of the blocks that you learned about in this project, as well as those that you already know.

define collect sample

set energy to 100

change energy by -1

if touching color ? then

You can view the complete project here (https://scratch.mit.edu/projects/536887721).

Save your project


What next?
If you are following the Further Scratch (https://projects.raspberrypi.org/en/pathways/further-scratch)
pathway, you can move on to the Puzzle room (https://projects.raspberrypi.org/en/projects/puzzle-room)
project. In this project, you will make a space ship puzzle room with a character that solves the puzzles.

If you want to have more fun exploring Scratch, then you could try out any of these projects (https://projects.raspb
errypi.org/en/projects?software%5B%5D=scratch&curriculum%5B%5D=%201).

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (ht


tps://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/nature-rover)

You might also like