The Matrix Revisited
The code editors all have statements that allow you to display text, numbers and patterns on the LED Matrix. They also have statements that allow you to use the LED Matrix as the basic output for a game. This page will explore some of those statements.
In computer game design, the term sprite is used to describe the image used to represent a game object, like the player character, enemies or items that the player must interact with in some way. The code editors all have some way of doing this with the micro:bit.
Notice that we are using a variable and an assignment statement here. We aren't just drawing a dot in the centre of the screen. The variable name 'player' (you can choose your own name for the variable) allows us to keep track of the position of the sprite. The following code takes advantage of that and allows the sprite to be moved when the A button is pressed.
Add a little more code and we can make a way for the user to move the sprite around the matrix,
Test this code and you should be able to use the buttons to move the sprite all over the place. The problem is that it isn't so easy to control this way. The A button means move forward a step, the B button is rotating the direction of travel by 90° in the clockwise direction. Another approach we could take is to use both buttons for movement horizontally, one to move left, one to move right.
We can also make the sprite move around in a random fashion,
- If you change the order of the blocks inside the while structure, there are subtle changes to the way that the sprite moves. Experiment with this and, thinking carefully about what each block does, explain why the movement is different when you change the order.
- Go back to the page on tilting. Work out how to control the movement of the sprite by tilting the micro:bit. If you use pitch and roll, you can give the user 4 different inputs that you can sense on the board. Small changes in the way you make this happen can have quite a large impact on how easy it is for the user to control the position of the sprite. For example, you can have it so that the sprite will be in the centre of the matrix when the board is held pretty much level and always be placed off centre to represent the orientation of the board. This is not so easy to control but might make for a fun way to move the sprite around for a game or to make a digital spirit level. Another approach, with a few extra blocks, is to require the user to return the board to level before leaning it in a direction causes the sprite to move another place. There are lots of ways to achieve this - experiment until you have an approach that works.