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!

6 thoughts on “microbit Tetris in python

  • March 18, 2017 at 9:24 pm
    Permalink

    2nd version doesn’t work – it says:
    “Line 36
    Memory error
    Memory allocation failed
    allocating 712 bytes”
    is there any chance to shorten the script?

    Thanks

    Reply
    • March 19, 2017 at 9:42 am
      Permalink

      Yeah this is a bit of a problem at the moment. Since this tutorial was written there’s been a lot of new updates on the micropython runtime to add new features. Whilst these are fun to play with, they do eat up a significant amount of the very limited space on the microbit.

      Once thing I’d like to do is to add an option on the simulator to allow you to choose from the latest runtime, which will give you all of the features (such as speech and radio) or an older runtime which will allow you to write longer programs. It’s a bit of a bodge, but it’ll allow you to run programs like this without any problems.

      Watch this space – I’ll post here when I’ve worked out how to do that!

      Reply
  • May 8, 2017 at 6:03 pm
    Permalink

    I’ve made the second version work on a microbit running recent version of micropython (v1.7-9-gbe020eb) by replacing the line
    “from microbit import *” by
    “from microbit import display, sleep, button_a, button_b”.
    Thus enough memory space is available to avoid Memory Error exception raising.

    Reply

Leave a Reply