Java Tetris Attack

tetrisAttack_screenshot

To download the application or source code, view this post.

So between the summer and fall terms during my time at SFU, I had two weeks to sit around my house waiting for classes to start back up. What better way to spend that time than to do some programming?!

For years one of my favourite games has been the Super Nintendo puzzle classic Tetris Attack. The SNES version isn’t necessarily the best; the computer AI in Pokemon Puzzle League for the Nintendo 64 is devilish and the stylus in Planet Puzzle League for the Nintendo DS make everyone feel like a pro, but there’s just something about having a controller in your hand with Yoshi and his pals that kept me busy shuffling blocks for a good part of my childhood. It also felt like a good basis for one of my first ventures into graphics programming. I’ve worked on games before, but primarily on menu systems, and certainly never on what goes on in the background of it all. To cover what I learned in a nutshell:

 

Planning is everything and you should plan for absolutely everything

Now I’m fairly certain I’m not alone when I say that LayoutManagers in Java is are a mess: you never get out what you think you’re putting in. So for this project that had to be the first thing to go; besides, isn’t absolute pixel positioning exactly how they did it back in the 90’s anyway? I love the disconnect between our idea of the x and y axis in 2D space on paper and on a computer. Of course this goes back to how monitors originally drew scanlines and characters and is just one of those things that stuck around, and I’m not complaining, but until you work in it for a while, it can leave you scratching your head once in while. Maybe not when you’re just placing graphics on the screen, but when you start trying to write dynamic animations and keeping track of everything, a negative sign that looks fine on paper can look completely backwards on-screen! This got even more fun when I started making math-based applications, but that’s for a future post!

axis

So when you plan on doing things with absolute positioning, a good idea is to quickly whip together a system that deals with this disconnect, by applying a vertical flip to everything before its painted, for instance. I didn’t do it in this project and only kind of did it in my next one, but this is how we learn!

Before you start making a game, its good to know how all the different elements of your game will work together, and what relies on who to get the cogs all turning properly. My system originally had a Game class, that had two Grid classes, and each of those Grids took care of themselves, and worked independent of one another. What happens when one grid needs to send garbage blocks or information to the other? They had no way of talking to each other and this poor planning step resulted in a terrible series of quick fixes, that ultimately had itself swapped out for a brand new GridController class. This class took care of communication between the grids and took input from the Game class, effectively working as a delegate for game information.

I’m still a little unsure on how I feel about delegation as a whole, but its easy enough to sum up.

Pros: Code is nice, small and really gives your program a sense of structure.
Cons: If you don’t know the structure, it can feel like a never ending hallway of winding turns. When you finally get to the block of code you were looking for, you could be scratching your head over why it is where you found it and what it isn’t up closer to the main structure.

With proper planning, a lot of a oversimplification can be avoided and sloppy decisions can be weeded out before they have a chance to surprise you down the road. UML diagrams sound like a lot of work but payoff in spades if you have a big project to tackle. I’m sure with experience I’ll be at the point where my off-the-cuff programming has all these smart decisions subconsciously woven into it.

 

Programmers talk about “Composition over Inheritance” for a reason

I’ll talk more about this another time.

Now, here is 30-second clip of the game in action!

Leave a Reply