Thursday, November 25, 2010

A simple game in Haskell + SDL

I wanted to play a bit with SDL, and it's always good to step away from actually working on EclipseFP to use it for a "real" project. So I wrote a very primitive game using SDL and SDL_TTF. It's a typing speed game (hence the, ahem, clever pun on the name: TypeClass): you see letters scrolling down the screen and you need to type them, and of course they go faster and faster. Maybe my children will learn to type with it. Maybe not.


I had SDL installed earlier, but of course SDL_TTF proved challenging on my windows machine, but I think it's was all my fault, I probably didn't read the instructions properly or something. Anyway, I only forgotten to set the LIBS variable. When I ran export LIBS=/usr/local/lib/libSDL_ttf.dll.a in my MSYS environment then configure worked fine!


Afterwards it was all plain sailing, really. Just remember to add enableUnicode True in your initialization code so you can get the actual Unicode character typed (I'm on a French keyboard, so typing M gives me the SDL key ;). And of course you need a font file, so I include a GNU FreeFont file with the package. It needs to be in the same folder as the executable.


For some reason, after seeing code that was blocking for SDL events in a loop, I started building the game with two threads, one blocking for events and one looping and drawing, and a MVar in the middle. It worked, but rewriting it in a single normal game loop: process event, update game state, draw made things a lot easier.


This brilliant piece of software that has already made its mark in video game history can be found on Hackage here!


As usual, all feedback is welcome. Possible enhancements are a high score screen, difficulty levels, choosing letters by frequency in English, using Markov chains so that the flow of letters look more natural... The list is infinite!

5 comments:

sargon said...

Hi,

nice idea for a simple haskell game.
But I have to fix the cabal script bevor it starts to build for me. There are some
missing dependencies..

Additional to your extension ideas I would like to have the possibility to increase the character speed.

regards Daniel

JP Moresmau said...

Hi Daniel what did you have to change in the Cabal file?
The speed increases over time, what do you have in mind?

sargon said...

I had to add "base,random and containers" to the build-depends.

On the topic of speed increase: Would be nice if something like pressing the "+" Button would increase movement speed of the characters. When I tested the game the characters need about 5 minutes bevor they arrive in the centre of the screen.

JP Moresmau said...

OK, there seems to be something in EclipseFP that automatically adds these packages when you build the project. I've uploaded a new version on hackage that builds properly.
It shouldn't take 5 minutes for the first characters to reach the centre of the screen!! I mean, the first characters are a bit slow, but still...

sargon said...

Maybe I look at the code tonight. Maybe you could create a repo somewhere so I can clone and code exchange is simpler.