Emma

Emma

Twenty years later I wrote a Tetris... again!

One night in May of 1996 I was bored and wanted to play Tetris very badly...

At the time I was an exchange student. I owned a broken laptop which wasn't of much use but I also had access to my host family's 486 desktop computer that came with MsDOS 6.22 and Windows 3.1. Not much else, not even a dial-up modem which wouldn't have been of much use anyway since StackOverflow hadn't been invented yet and most programming-related forums were quite hostile.
But I was determined to play Tetris, so I sat down at the computer and launched QBasic (which came bundled with DOS) and started coding.

Somewhere around 4am that night, after several hundred lines of Basic code (about 1500), I had myself a fully working Tetris-like. I was also too tired to actually play it, so I went to bed!

Twenty years later, I went to PyCon UK 2016.

PyCon UK felt very "playful" to me this year. There was of course the usual kid's day but every lucky attendee who got in line was also able to go home with a BBC micro:bit and there were several talks and workshops about the Raspberry Pi as well as introductions to libraries like PyGame Zero and GPIO Zero and a talk about hacking light-bulbs or one about winning at Blackjack.
On top of all that there were several stands in the hall that were in close relationship to single board computers like pi-top, Kitronik or even J.P. Morgan who were giving away micro:bit power banks (although they probably intended those to be cell-phone power banks, but most of us used them to power micro:bit's instead).

When we came back from Cardiff, I was intrigued about PGZero and also wanted to "play" with the micro:bit so, on a Sunday afternoon, I sat down at the computer ans started writing a Tetris again but this time controlled by 2 micro:bit's (using the built-in accelerometer and radio) and in Python instead of Basic.
That evening, I sat down on the couch and played Tetris.

It's only a few days later that I realized I had written the same kind of program, twice, twenty years apart and in two different but somewhat similar languages (both being interpreted and heavily used to teach kids how to code). So I went on to compare both experiences and drew a few conclusions.

  1. About myself: I'm aging. I used to do hobby coding at night, now I do it on Sunday's afternoon and 4am sounds more like an early morning rather than a late night.

  2. About the time it took: Surprisingly with improved (at least that's what I've been told) tools and an extra 20 years experience, it took me about the same time this time around as it did 20 years ago.
    Have they been lying to us all these years telling us that with improved tools we are able to write code faster? From this experience I would say "yes". Yes because with improved tools we are expected to do more things to achieve the same goals.
    A simple example is that 20 years ago, the squares composing the Tetris shapes were just programmatically built by plotting a main color and a border color on the screen; this time around I built those same squares in Gimp with a Gaussian blur effect.

  3. About the size of the code: the code I wrote this time is roughly one third of the size of my original code which probably means it has less bugs and is more maintainable (I remember very distinctly that the QBasic code I wrote wasn't very maintainable, I had the hardest time going back to that code to implement scores a few days later).
    I believe this difference in size is due to several things including experience and OOP, which IMHO favors more code re-use. But most importantly it is due to external libraries (I don't have to care about how I should put a shape on the screen or remove it as PGZero's Actor class does it for me)

  4. About the knowledge "required" to build such a game: In 1996, all I used aside from basic variables and control structures was, if I remember correctly, a pretty small set of instructions: SCREEN, LINE, PAINT and PGET; documentation for those instructions was directly available from inside the QBasic editor which was the only editor I had to use.
    In 2016 I used 2 different editors (Mu and Vim), a graphics editor (Gimp), 3 libraries (PGZero, pyserial and micropython). Basic understanding of OOP was a requirement as well (to use PGZero) and I also needed some sort of internet access (I am not ashamed to admit I wouldn't have been able to do this without the help of ReadTheDocs - please sponsor them).
    This is of course very subjective as I didn't use a micro:bit back in 1996 and if I hadn't used one this time around I wouldn't have needed 2 editors or 3 different libraries (only 1).
    I could also have written the micro:bit code in vim but it would still have required a tool to compile it.
    In any case I would still have needed basic knowledge of OOP and, due to the nature of PGZero, I can't access its docstrings directly in vim and need an external tool to view its documentation.
    I want to say that this is merely an observation about the evolution of the eco-system or of "the way of doing things" and this is not in any way, shape or form criticism directed towards the authors, contributors or maintainers of Mu, micropython or PGZero, they all did a wonderful job and we should be (as I am) grateful to have access to those tools.

  5. About the code produced and its re-usability: In 1996, after a night of coding Tetris, I ended-up with a Tetris...
    In 2016, after an afternoon of coding Tetris, I ended-up with:

    • a generic remote control for the micro:bit that transmits data over both radio and serial. It can be used to play Tetris but can also be used to control a robot or a car
    • a generic micro:bit receiver that re-transmits through serial what it received through radio
    • a "cellboard" library for PGZero that can be used as a base for Tetris but also a Game Of Life, a platform game, a "paintbrush" game, a Snake or a SimCity1-like and many more
    • a Game Of Life (originally used to test the cellboard library)
    • a Tetris

    In shorter terms, the code I ended-up with in 2016 is way more re-usable than the one from 1996. This is in part due to the use of several devices as well as experience. But I also believe that since one is "forced" to use OOP to use PGZero, re-usability also comes more naturally as a consequence.

Now that I've been rambling on and on about 2 different Tetris games coded 20 years apart, you probably want to see the code. Unfortunately, the 1996 version never left the computer it was created on and probably ended its life in a dumpster somewhere or was harshly terminated by a FORMAT C:.
The code for the 2016 version is available on github here (look for controller.py and receiver.py) and here (look for cellboard.py and tetris.py).
🗒 Those 2 repositories are not dedicated to Tetris, they are general purpose repositories with code respectively related to the micro:bit and PGZero.

Although it was the story I felt the most worthy of being told in details, the inspiration to write a Tetris is not the only thing I got out of PyCon UK this year. I'd like to make a special mention about 3 (series of) talks that have been useful to me in the month following my return:

I also wanted to thank the whole team who made PyCon UK possible this year, especially Daniele Procida for always making me feel welcome and personally approaching me about speaking at the conference, Nicholas Tollervey for giving away so many micro:bit's, Peter Inglesby for always being available (or most likely never giving you the impression he is actually already very busy) and Rae Knowler for taking up the organisation of TransCode this year (and also helping us escape Cardiff City Hall)

Have a nice day!

Photo Credit: Anton Novoselov (via Flickr) shared under CC-BY 2.0 License at the time of writing.