Вы находитесь на странице: 1из 13

Raspberry Pi Learning Resources rpf.

io/learn 1

micro:bit game controller


with Scratch

What you will learn


By creating a micro:bit game controller with a micro:bit and your Raspberry Pi, you
will learn:

• How to send data between the micro:bit and Scratch


How to control a Scratch sprite using the micro:bit
• How to make a cool Spaceship game in Scratch
• How to create custom images to display on the micro:bit

What you will need


Hardware
As well as a Raspberry Pi with an SD card and the usual peripherals, you’ll need:

x1 x1

This learning resource is provided for free by the Raspberry Pi Foundation under a Creative
Commons licence. See more at raspberrypi.org/resources and github.com/raspberrypilearning.
Raspberry Pi Learning Resources rpf.io/learn 2

micro:bit game controller


In this resource, you’ll use a micro:bit as a game controller in Scratch, and help
regain control of the galaxy!

Finding the USB port

For the micro:bit to be able to communicate with Scratch, you need to know how
the micro:bit is connected to the Raspberry Pi.

With the micro:bit disconnected from the Raspberry Pi, open a terminal and type
the following:

ls /dev/ttyA*

Plug your micro:bit in via the USB cable and type the command again:

ls /dev/ttyA*

There should be a new entry in the output, probably something like dev/ttyACM1.
You need to note this down.
Raspberry Pi Learning Resources rpf.io/learn 3

Setting up the micro:bit

Open Python 3 (IDLE) (Menu > Programming > Python 3), create a new file (File >
New File), and copy the code below into the file. Save it as rpi.py.

import serial
from time import sleep
import scratch

scr = scratch.Scratch()
## THE NEXT LINE MIGHT NEED TO BE CHANGED - TYPE ls /dev/ttyA* into the terminal to
see which port is needed.
PORT = “/dev/ttyACM1”
##
BAUD = 115200

s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE

while True:
data = s.readline().decode(‘UTF-8’)
data_list = data.rstrip().split(‘ ‘)
try:
x, y, z, a, b = data_list
scr.sensorupdate({‘x’ : x})
scr.sensorupdate({‘y’ : y})
scr.sensorupdate({‘z’ : z})
scr.sensorupdate({‘a’ : a})
scr.sensorupdate({‘b’ : b})

except:
pass

s.close()
Raspberry Pi Learning Resources rpf.io/learn 4

This file, once running, will listen to any data being sent out from the micro:bit,
and send it over to Scratch. You may have to change this line:

PORT = “/dev/ttyACM1”

It needs to be the same as the port you noted down earlier.

Coding the micro:bit

The micro:bit needs to run some code that will constantly print out its
accelerometer readings and button pushes.

Open mu by going to Menu > Programming > mu.

Now copy the code below into the editor:

from microbit import *

def get_sensor_data():
x, y, z = accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z()
a, b = button_a.was_pressed(), button_b.was_pressed()
print(x, y, z, a, b)

while True:
sleep(100)
get_sensor_data()

You can flash this file into your micro:bit straight away.
Raspberry Pi Learning Resources rpf.io/learn 5

Setting up Scratch

Open Scratch on the Raspberry Pi (Menu > Programming > Scratch).

Go to Sensing, then right-click on slider sensor value near the bottom of the
screen and choose enable remote sensor connections from the context menu.
Click on OK when the dialogue box opens.

Now switch back over to Python 3 (IDLE) and press F5 to run your rpi.py script.

In Scratch, you should now be able to view the values from the micro:bit’s sensors.
Simply click on the arrow on the slider sensor value block, choose a and then
check the box:
Raspberry Pi Learning Resources rpf.io/learn 6

If you repeat this for sensors b, x, y, and z, then your Scratch stage should look
something like this:

If you tilt the micro:bit you should see the x, y, and z values changing. Pushing the
buttons will switch a and b from False to True.

If the readings aren’t working, check the micro:bit port again, and make sure the
code is running on both the micro:bit and the Raspberry Pi.

Making the assets

You’re going to need three new sprites for this game. Delete the cat sprite and then
find a rocket sprite, a UFO sprite, and a missile sprite. You can search on the web,
or even draw your own.
Raspberry Pi Learning Resources rpf.io/learn 7

Import each of your sprites into Scratch:

Rename the sprites by changing the name in the scripts pane:

The sprites will also need to be resized. You can use the ‘shrink sprite’ tool to
do this:

Lastly, you should rotate the rocket and missile sprite, so they both point towards
the right-hand side of the screen. You can do this by editing the sprite in the
costume tab and choosing the rotate tool:
Raspberry Pi Learning Resources rpf.io/learn 8

Coding the rocket

To code your game, you can start by adding some scripts to the rocket. Because
you want the game to be able to be played over and over again, you should start it
by using a broadcast block. You’ll also need to make a variable called score and set
it to 0 at the start of the game:

To start the game, the rocket needs to be placed in the centre of the screen,
pointing towards the right:

Next, you need to control the rotation of the rocket. This is going to be decided by
the accelerometer reading from the micro:bit, in particular from the x sensor value.
At the moment, this is a value between about -1000 and 1000, so it needs to be
reduced a little. Create a new variable called turn and set it as shown below:
Raspberry Pi Learning Resources rpf.io/learn 9

Test out your game so far: when you tilt the micro:bit left and right, the rocket
should spin around.

If it’s not working, try restarting the Python 3 program on your Raspberry Pi and
re-flashing the micro:bit with its program. You might also have to check that the
micro:bit hasn’t reconnected to a different port; you can check by using the
command ls /dev/ttyA*.

Next, you want the rocket to move. The speed can be determined by how far
forwards or backwards the micro:bit has been tilted. You can use the y sensor
value for this, but as before, you’ll need to reduce the value a little (and in this case
reverse it).

Test your flight controls now by tilting the micro:bit left and right, forwards
and backwards.

Coding the UFO

The UFO needs to start in a random location and then chase after the rocket.
This is fairly easy to set up. The Scratch stage stretches from about -250 to 250
horizontally (the x-axis) and -180 to 180 vertically (the y-axis), so picking two
random numbers in these ranges will enable you to position the rocket:
Raspberry Pi Learning Resources rpf.io/learn 10

Next, you can use a forever loop to get the UFO to chase the rocket:

In a minute, you’ll code the missile to launch at the UFO. If the missile hits the UFO,
the game should restart and the player’s score should go up by one. A new script is
needed for this:

To finish off the UFO, the game needs to end if it catches the rocket:
Raspberry Pi Learning Resources rpf.io/learn 11

Coding the missile

To make the missile always point in the correct direction, you can make it turn
exactly the same way as the rocket:

Next, whenever the a button is pressed on the micro:bit, the missile should fire. To
do this, you can move it to the location of the rocket, tell it to show and then move
forwards until it hits either the UFO or the edge of the screen. It will need to carry
on turning as it flies, otherwise the missile’s rotation will become out of sync with
the rocket.

Test out your rocket’s new ability to shoot missiles.


Raspberry Pi Learning Resources rpf.io/learn 12

What next?

The game has plenty of potential to be improved.

Can you add in some obstacles for the rocket and UFO to avoid?

You still have to find a use for the b button. Maybe this could start a turbo mode
where the rocket flies faster?

Could the UFO shoot a laser beam at the rocket every now and then?

You can learn more about the micro:bit by visiting rpf.io/microbit.

This learning resource is provided for free by the Raspberry Pi Foundation under a Creative
Commons licence.Find more at raspberrypi.org/resources and github.com/raspberrypilearning.
Raspberry Pi Learning Resources rpf.io/learn
Teacher's guide

The micro:bit game controller teacher's guide

Objectives
• Understand that two devices can send data to each other using
serial communication
• Understand that Python and Scratch can communicate with each other
• Implement a MicroPython program to read the accelerometer data from
the micro:bit
• Relate accelerometer data to the motion of sprites in Scratch
• Experiment with physical inputs, to implement new sprite behaviours

Software installation
For this lesson, your students will need access to the mu IDE. To install mu, open
the terminal on the Raspberry Pi and type:

sudo apt-get update && sudo apt-get install mu -y

They will also need the Python Scratch library. You can install this by typing
the following into the terminal:

sudo pip3 install --pre scratch

Hardware requirements
Hardware
As well as a Raspberry Pi with an SD card and the usual peripherals, you’ll need:

x1 x1 x1

This learning resource is provided for free by the Raspberry Pi Foundation under a Creative
Commons licence. See more at raspberrypi.org/resources and github.com/raspberrypilearning.

Вам также может понравиться