Creating a Handheld Console – Programming Pong (with Blocks)

Coding the Micro:Bit
Creating your own Handheld
Games Console
www.computerscienceuk.com
Learning Habits
Which learning habits will be important for this unit of work and why?
Adapting :
Reflecting and making changes
Noticing details
Empathising…
…with feelings and views
Effective use of time
Questioning:
Asking questions to get
below the surface
Imagining…
…how things could be and seeing a
range of possibilities
Listening…
…to understand
Collaboration:
Working effectively with
others
Independence:
Working effectively alone
Making links…
…and recognising
relevance
Reasoning:
Thinking rigorously,
methodically and giving
explanations.
Distilling…
…what you have learnt and
what you need to learn
Meta Learning:
Talking about how you have
been learning
Managing distractions…
…and sustaining concentration
Perseverance:
Overcoming frustration and difficulty
Imitation:
Picking up good habits
from others
Capitalising:
Using resources purposefully
Planning…
…your learning in advance
www.computerscienceuk.com
Lesson Objectives
Lesson Objectives
• To develop our skills in programming using the blocks
language
• To understand the purpose of variables
• To develop understanding of coordinates and see how
they are used in developing graphical programs.
• Success Criteria
• ALL: To develop a simple pong game turning the device
into a handheld console.
• MOST/SOME: To be able to develop the code to solve a
number of challenges.
Literacy – Key Words
Variables
A memory store in a program (think of it as a box which stores a piece of
data)
Coordinates
A system which enables us to locate the position of an object using a
horizontal (x) and vertical (y) . For example a pixel on a screen
www.computerscienceuk.com
Lets Create Pong!
To create a Pong we need to understand a few things.
1. Coordinates
Whenever we create computer games, we will need to draw items
on the screen and update their position over time.
The position of screen objects is always described with screen
coordinates and the Micro:Bit is no exception.
The LEDs on the Micro:Bit can be thought of as screen pixels and
each LED can be described using an X and Y coordinate.
X-Coordinates
Y-Coordinates
This LED has the
coordinate (3, 2)
www.computerscienceuk.com
Lets Create Pong!
2. X-Velocity / Y-Velocity
So we should now be OK with the idea of X and X positions of the
LED’s on the Micro:Bit.
The next thing to understand is that when we wish to update the
position of a screen object (lit up LED), we do so using X and Y
velocities. This is not as scary as it sounds. It simply stands for a
number which we use to add to an item’s coordinate so that it can
be repositioned on the screen and therefore appear to be moving.
For example, if the LED (3,2) had an X-Velocity of 1, it would move
to the right 1 place each time the screen was updated.
X-Coordinates
Y-Coordinates
This LED has the
coordinate (3, 2)
www.computerscienceuk.com
Lets Create Pong!
3. Variables
So that we can keep track of the item’s coordinates
and other data, variables are required.
Variables are simply stores of data (think of them like
boxes to store a stuff in).
To create a variable and store data in them, we will
need to use the following scripts:
This allows us to create new
variables and give them names
This allows us to set our variables
to particular values
(ie store data in the variable)
www.computerscienceuk.com
Step 1 - Coding Pong
• Go to www.microbit.co.uk/create-code
• Click on ‘New Project’
• Create the following script:
This scripts will create the start screen,
plotting the ball and paddle to start
the game.
1. Create 5 variables
2. Set each variable to the values you
see in the example script.
3. Use a ‘Show LEDs’ script and tick the
bottom row.
4. Plot the ball using the ball’s x and y
coordinates.
5. Plot the Paddle using the paddle’s y
coordinate (hardcode the x
coordinate to 0)
www.computerscienceuk.com
Step 1 - Coding Pong
• Next, create the following scripts:
These scripts move the paddle up and
down when the appropriate button is
pressed.
1. Create a ‘when the A/B button is pressed’
script
2. Un-plot the paddle from the screen
3. If the paddle is not already at the top /
bottom of the screen, change its y position
so that it moves up /down
4. Otherwise, redraw the paddle in the same
positon as before (ie. no movement).
www.computerscienceuk.com
Step 1 - Coding Pong
• Finally, create the following script:
This script moves the ball around the
screen, bouncing off walls and the paddle,
or shows an X if the paddle misses the ball.
1.
2.
3.
4.
5.
6.
In a forever script, begin by un-plotting the ball
Then, in an IF statement, code the condition that if the ball is at the
right hand side of the screen, change the x-velocity so that it
moves left (bounces back) – otherwise let the ball continue to
move left.
Then in another IF statement, code the condition that if the ball is
at the left side of the screen and level with the paddle, change the
x-velocity so that it bounces off the paddle, otherwise clear the
screen and show a cross (ie. game over).
Then in a final IF statement, code the condition that if the ball is at
the top or bottom of the screen, change the y-velocity so that it
bounces back into play.
Finally, update the position of the ball with the updated velocities
and re-plot the ball.
Pause for a short while and repeat.
www.computerscienceuk.com
Step 2 – Compiling and Flashing
If its working it is time to COMPILE and FLASH.
• Press the COMPILE button.
• Once the HEX file has downloaded, go into your
documents and find it.
• Now plug in your device to an available USB port.
• Open the device up as if it were a USB memory stick.
• Now drag the HEX file into the opened device folder (as if
you were transferring files to a memory stick).
• Now the FLASHING will be complete and you can try out
your software on your Micro:Bit.
www.computerscienceuk.com
Your Working Handheld Games’pong’sole
www.computerscienceuk.com
Class Tasks
Challenge 1
Speed up the game so that the ball moves faster.
Challenge 2
Add a variable called score and add a script so
that each time the paddle and ball meet, the
score variable is updated by 1 and this score is
shown at the end of the game.
Challenge 3
Add a script which will pause the game when
both the A and B buttons are pressed.
www.computerscienceuk.com
Class Tasks – Possible Solutions
Challenge 1
+
Challenge 2
Challenge 3
+
+
www.computerscienceuk.com