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.