Microbit
Advanced
50 mins
120 points
What you need:
  • Chromebook/Laptop/PC
  • Microbit
  • USB Cable

Microbit Pong Game - Part 2

This is the second part of Microbit Pong Game.

In this lesson, we will be adding new, more advanced features to the game such as a score and a bigger paddle!

1 - Complete Part 1

Before continuing, make sure you have fully completed Microbit Pong Game - Part 1.

Open your code from Part 1 by clicking and dragging the file into the editor.

2 - Add the score

We're going to add a score to the game. Every time the Paddle bounces the Ball successfully we will increase the score by 1.

When it's game over, we should display the score.

see the code

let Paddle: game.LedSprite = null
Paddle = game.createSprite(2, 4)
let YDirection = 1
game.setScore(0)
basic.pause(1000)
let Ball = game.createSprite(randint(0, 4), 0)
basic.pause(1000)
basic.forever(function () {
    Ball.change(LedSpriteProperty.Y, YDirection)
    if (Ball.get(LedSpriteProperty.Y) == 4) {
        if (!(Ball.isTouching(Paddle))) {
            basic.showNumber(game.score())
            basic.pause(2000)
            game.gameOver()
        }
        YDirection = -1
        game.addScore(1)
    } else if (Ball.get(LedSpriteProperty.Y) == 0) {
        YDirection = 1
        Ball.set(LedSpriteProperty.X, randint(0, 4))
        basic.pause(400)
    }
    basic.pause(300)
})

3 - Rename 'Paddle' variable to 'PaddleLeft'

Go to the block where you're setting the Paddle sprite variable (in 'On Start') and click the down arrow. You should see the option to 'Rename variable' near the bottom of the dropdown menu.



4 - Create a sprite variable called 'PaddleRight'

Now we will have two sprite variables for the Paddle (one for the left-side and one for the right-side).

5 - Use the Paddle sprite variables to draw a bigger Paddle

We're now going to use our new sprite variables to make the Paddle larger. So instead of the Paddle being one LED, it's now going to be two.

We'll start off by initializing our Paddle sprite variables.

let PaddleRight: game.LedSprite = null
let PaddleLeft: game.LedSprite = null
PaddleLeft = game.createSprite(randint(0, 3), 4)
PaddleRight = game.createSprite(PaddleLeft.get(LedSpriteProperty.X) + 1, 4)

We've changed the code where we were initializing the Paddle sprite variable before. So now, we're creating PaddleLeft within the first 4 LEDs at the bottom of the Microbit and PaddleRight will be offset by 1 LED to PaddleLeft.

This means that no matter where PaddleLeft is created, PaddleRight will always appear right beside it.


Join our club 😃

To view the remaining 3 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of . Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more