microbit Tetris in python

microbit Tetris in python

I’ve finally got round to updating the online microbit python simulator in create.withcode.uk to catch up with some of the amazing new micropython features (such as speech and radio).

To celebrate, here are three different versions of the classic game tetris that you can simulate online in a virtual microbit or download to your device and play.

Microbit tetris in python
microbit tetris in python

First version

The first two tetris games weren’t made by me: the first was made by VivianePons and donated into the public domain after making it at PyCon2016.

It uses the microbit’s accelerometer to sense how much you tilt the device in order to move the blocks left and right. You can play it in the simulator by clicking on the accelerometer tab and changing the X slider left and right.

Second version

The second version of tetris comes from the excellent tutorial at 101 Computing. It uses buttons A and B to move the blocks and you get different shapes that you can rotate by pressing both buttons rather than a single dot:

Which version do you prefer? Can you combine the best bits of both programs? I think it’d be great it you could use the accelerometer like the first version but have the more complex blocks that you can rotate like the second one. Perhaps button A would rotate counter clockwise and button B clockwise. Post a comment with a link to your code if you’re able to improve on either of these projects and thanks to the original authors for sharing their code online.

 

Update 28/3/2017:

It seems that the second version doesn’t work any more with the current version of micropython for a micro:bit.

Please see below for a version of Tetris I’ve put together that works on both the simulator and an actual device.

There are four different types of blocks that fall down. You can move them by pressing A or B or rotate them by tilting the microbit left or right.

Have fun!

Doodle jump: microbit python game tutorial

Doodle jump: microbit python game tutorial

This tutorial will talk you through how to write, test, debug and improve the python code for a doodle jump style game on a BBC micro:bit.

Challenging
Warning: this isn’t easy

This microbit python game tutorial is possibly a little too advanced for beginners so have a look at some of the other tutorials if you’re just getting started or jump straight in here if you’re feeling confident.

Doodle jump is a brilliantly simple but infuriatingly addictive game where you have to control a green alien by moving it left and right to jump up to the next platform, as the world falls away beneath it. The original game won an Apple design award and it quickly took the gaming world by storm. You can play it here (it’s likely to be blocked if you’re viewing this tutorial page in school). The challenge of this tutorial is to attempt to scale down the game so it still works on a 5×5 LED screen on the BBC micro:bit whilst still being fun to play.

 

Try it with code
Try the code

Below is a sample of the doodle jump game that we’ll create using this tutorial.

Press Ctrl + Enter to run the code, or click on the run button that appears when you click on the {+} button in the bottom right corner of the code editor.

Press button A to move left and button B to move right.

If you want to test the game on an actual micro:bit, plug it into your computer, run the code and click on Download HEX and save that file onto your micro:bit.

microbit python tutorial: shake ‘n’ burn fire simulator

microbit python tutorial: shake ‘n’ burn fire simulator

This microbit python tutorial talks you through how to write python code that simulates fire on your micro:bit. It burns down gradually until you shake it to stoke the fire back to life.

It’s written for beginners so you don’t need any experience writing python code. It covers:

Concepts you'll understand
Concepts you’ll understand

Concepts:

  • Loops: repeating code with while and for loops
  • Selection: only running certain parts of the code if a condition is met
  • Images: how images can be represented as data in memory before being displayed on a screen
  • Operators: how the multiplication operator (*) works with strings, numbers and images

 

 

Skills covered in this tutorial
Skills covered in this tutorial

Skills:

  • How to change each pixel of an image on the micro:bit screen
  • How to use the accelerometer to check if the micro:bit has been shaken
  • How to choose a random number
  • How to fade and shift an image on the micro:bit screen

The next page shows a simulation of the code we’ll create in this tutorial.