A Tetris Game in Scala



The Why?

My brother is working on a small game project during this summer, so I wanted to test out Slick2D to help him a little. I decided to do a small Tetris game and ended up having 5 essential classes, some medium sized, and some small. Here I will share a few of the tricks used in the game.

Instead of explaining the boring logic of the game, I will present to you some handy Scala functionality that I was using for the code. These are things I deem to be highly relevant for Scala programmers.

Accessors and Mutators

Actually, access modifiers in Scala is not something you need to worry as much about as you would in Java. Why is that? Well, you can easily change a class member to be private and add accessors and mutators without modifying any other piece of the program. I guess it is example time:

Now this class has a publicly available member, namely x. We can change this to be a private member and use it transparently:

I am quite ambivalent about the syntax for this. Using the underscore for the member variable is a nice convention I picked up around the Internet, but the 2nd function uses the underscore to achieve some compiler magic. The function

is actually being called in the line

so we get a bit of language help here, but this helps us make everything transparent.

Notice that we won’t be having to clutter our software with “get” and “set” methods everywhere, plus we can continue using the class transparently. What are the cons? Well, for starters, we have added overhead with the accessor and mutator calls, but this would be the same in Java for example. Other than that, since the function calls are transparent, we don’t see that a function is being called and thus we wouldn’t be aware of any side effects. Of course, side effects for accessors and mutators would be rather unwelcome in the first place.

As a side note, I didn’t put any private members in the first place when writing the game, and still it took only 5 minutes to make everything private and nicely encapsulated.

Accessing Columns in a Matrix

A nice thing which is somehow trivial, but didn’t hit me until I spoke to my friend (a real Haskell fan), is that you can get the column of a matrix in a very elegant way. I usually implemented a yield which would iterate over the column and retrieve each element, however, this was very bulky and made the code ugly. Using map one can do the following:

For some reason this never occurred to me, so I thought others might find it interesting 🙂 I guess this is one of those things that you have to stumble upon in order for it to become obvious.

Update and Apply

In the game, I have a Grid class which deals with the rendering and logic of the grid. Since it was needed to access the grid content during the game logic, I was looking for a nice way to make the grid appear as though it was a matrix. Using two methods called apply and update I achieved total transparency. A small example code would be as follows:

Syntactic sugar FTW!

Links

Full Source Code
Slick2D Game Engine

Full Class Listing

Bring your coffee!

class TetriScala

class Piece

class Blocks

class Grid

class RowKillFX

CategoriesUncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *