During the 2001 winter term I taught Haskell to our second-year undergraduates (see the course's home page). After going through the usual motions of typing, polymorphism, higher-order functions and quicksort in one line, the last exercise was to write a wee asteroids-type game. I thought this might be interesting (or at least amusing) to other students and teachers of Haskell, hence this web-page.
The game uses the Hugs Graphics Library, plus a small home-grown geometry library (Geometry.hs) on top of it for displaying and manipulationg geometric objects. During the lecture, the geometry library was developed, and a small program was demonstrated which allowed to navigate a space ship (Space.hs). The exercise was to extend this program to an asteroids-type game, i.e. to add asteroids, and have the space ship fire bullets.
If you read German, you can have a look at the lecture slides and the exercise sheet.
One group of students (exercises are solved in groups of up to four here in Bremen) calling themselves the Haskellies went to quite amazing lengths in their solution --- different ship shapes, background pictures, colourful explosions, and features such as the "starburst" are just the highlights. On the other hand, the reference implementation (by yours truly) is nice, clean and boring.
Here are compiled binaries for x86 Linux (SuSE 7.3, but should run on other Linuxen as well): haskelly, reference implementation.
Here is a tarball containing the sources. You will need the Hugs Graphics Library (HGL), Version 2.0.4, to compile or run the solutions:
To run them under Hugs, put the HGL somewhere where Hugs
can see it, and comment out the function
fromInt :: Num a=> Int-> a
To compile them with GHC, compile HGL first. You'll need GreenCard for that. Unpack the HGL sources, go to the subdirectory lib/x11, and do something along the following lines:
green-card --target=ghc X.gc green-card --target=ghc Xlib.gc green-card --target=ghc Xlib_StdDIS.gc gcc -c cbits/auxiliaries.c ghc --make GraphicsUtils.hs -package lang -package concurrent -O2 -fglasgow-exts -fvia-C -monly-3-regs ar -r graphics.a *.o
Now modify the Makefile in the sources such that the include and link paths point to the subdirectory lib/x11. You may also have to change the path to the system X11 libraries in the makefile.
The exercise was fairly popular with the students, as it demonstrated that Haskell can be used for things other than sorting lists, and to good effect. Using ghc, runtime performance was not an issue; under Windows even Hugs copes quite well.