Friday, April 22, 2011

Tetris challenge - reloaded

Few days ago I’ve blogged about a Tetris Challenge, where I’ve been asked how long does it take to implement a Tetris game. The technology of my choice was Windows.Forms and it took me about 100 minutes, starting from scratch.

Today I’ve decided to spent yet another hour, this time to convert the implementation to XNA. I’ve downloaded the XNA Game Studio, created a new project and started to play with the XNA Api which turns out to be really straightforward. To my surprize, the most tricky part was to convert the user input. As you’ll learn, XNA processing pipeline is split into Update / Draw routines which fire independently for each frame. The problem is that this is way too fast to read user input if you want to precisely control left-right movements. Updating block position every frame makes it moving just too fast.

I’ve decided then to limit the left-right movement to a single move so that when you want to move the block n squares, you have to press left/right n times. This does not feel natural, however I haven’t found any other satisfying solution. You can experiment on your own.

The source code and the compiled binary are here. You have to first make sure that you have following installed:

If you ever build your own XNA Game, please pay attention to the “Game profile” setting in Project Properties. By default it’s set to “HiDef” which makes your code unplayable on older machines. Switching to “Reach” can then solve nasty compatibility issues.

1 comment:

Anonymous said...

Although I don't know XNA at all, from my little gamedev xp I think that natural solution to your 'key capturing' troubles is to remember the time during specified key was pressed. It's feature that input is being read fastly, not a bug :-)
If we assume that only one key can be pressed at a time, it is matter of two variables, one remembering which key is currently pressed (and have null otherwise), and second that will store timestamp of first 'key-pressed' event/state whatsoever. So now, if the duration is multiplicity of a chosen value, you fire up another 'move-left' event.