Friday 5 February 2016

Let's play. Making Tesseract (Days 1-3)

I have a devlog, now I should start writing in it, shouldn't I?

My first project? Tetris.

Why? Because of this post, which I like and find very sensible.

Why start with a game? Because why not? I like the idea and have lurked around game development sites, blogs and tutorials for a looong time.

My main objective here is to complete a project from start to finish. Possibly for the first time in my programming life. I have been using Python for years to test some of my mathematical hypotheses and it is by far the language I know best (I do know a couple of others, though), but I never made a full project, simply because I never had to.

You will find the code of this project on this repository. Feel free to have a look around.

Here are the (admittedly arbitrary) rules I set for myself:


  • The project will be complete when the game is finished and playable. Not just the base game logic (which at the time I'm writing this post is already written and working). For the moment it doesn't matter whether the game is ugly, it must work in all its parts.
  • I will write the code from scratch, not using other people's code written explicitly for Tetris. I know how to follow instructions and copy paste code. I'm actually really good at it, I'm doing something different here.
  • I will try to adhere as much as possible to the Tetris Guidelines because this gives me at least a full set of requirements written by someone else.
  • The game will be written in Python 3. I will use the SDL library thanks via the PySDL2 wrapper. I never used it and I want to try the library. These are somewhat softer rules; I decided to use Python because the focus this time is not on learning a new language and although I have mainly used Python 2.7 the switch to Python 3 is not a big deal since I have never written anything complex.
Let me make it clear that this will not be a tutorial (there's an immense number of Tetris coding tutorials on the internet): I will just be posting my progress and the interesting things and challenges I meet along the way.

The story so far:

This is how the game looks at the moment

It is clear that as I'm writing this post I have already been working on the game for a while. Three days, to be precise. I have stopped today because I decided to setup this blog in order to write a bit about my experience.

Here the features that I have implemented so far:
  • All the Tetriminoes (that is the official name, although I prefer the more mathematically correct tetrominoes) are implemented, have the correct colors (according to the Tetris guidelines) and can be moved, hard dropped and rotated clockwise (still have to think about the a suitable key mapping, but the counter-clockwise rotation is basically already in the code).
  • Lines are correctly eliminated and the board is updated
  • The SRS rotation system is fully implemented. This has required quite a bit of refactoring because I had to slightly change the way I had implemented the pieces in order to allow the correct wall kicks.
  • The preview box showing the next piece to fall is there.
  • The correct Random Generator has been implemented.
Still a lot of features to add before I even think to start polishing and adding stuff like music and menus, but the basic gameplay is there and I can already waste time with this guy. So far is the first sensible graphic program I made, so yay me!

A few things I have stumbled with:
  • The PySDL2 documentation is scarce and poor. After following the Pong tutorial I had to do quite a bit of trial and error to use the extensions to the library and work around some obscure errors I get from time to time. A word of warning: if you want to use the Applicator class you'd better have at least two elements in the componentsets tuple. Otherwise just use the System class. In fact it is still not clear to me the difference between the two classes.
  • PySDL2, on the other hand makes it very easy to initialize the the windowing system and to manage the keyboard input.
  • I don't know whether this is documented anywhere (it may be implicit from the underlying class in SDL), but if you subsurface with PySDL2 the area of the surface is represented by a 4-tuple of int (as per documentation). The first two are the coordinates of the top-left corner, followed by the width and height of the area (at first I thought the 4 int were the coordinates of the top left and bottom right corner).
  • The PySDL2 extensions are quite COP (component oriented programming) oriented. I barely know enough Object Oriented Design concepts - which, once again, I never had to use - so I ended up with a bit of a messy code to make it work. I've refactored a few times the code in order to make the class more granular and the system as decoupled as possible so that adding features doesn't break anything. It has worked so far and for a small project like this I am still able to manage the code. My objective is to keep the parts of the code that are really dependent on SDL as much isolated as possible, so that I can easily change library if I need to.
  • The main game loop looks horrible. I will soon have to brace myself and put some order in the chaos.
  • I have almost no documentation in the code. I usually manage to write Python code that is self-explanatory, but in this case (mostly because I am in a completely new developing territory) I keep changing things and, as I said, I ended up with messy code. I fear that as soon as I stop working on this I won't be able to understand my own code. We'll see.
I wish I had managed to have a better design, but I am not worried about it at the moment. I once to complete this as fast as possible: finishing is my top priority and I know that if I start overthinking it I will not be able to do it. Also, it's better to practice only a few things at a time. After I am done with this, I will have a nice toy project to test new technologies, design approaches and so on.

I also wished that I had managed to adopt a Test Driven Development approach seriously and basically for the first time, but with this kind of project I really have no idea where to start, even more so considering that I am designing as I go (which I should not, I know). Furthermore I have never done any unit testing in Python and did not want to learn yet another library and technology.

As I am repeating like a mantra: my objective this time is to complete the project.

I think it is all for now. Let's see what happens next.

A presto,

M.


No comments:

Post a Comment