Haskell SDL - Boxxle (GameBoy version)

Posted on by Kenny Cason

Tagged as: haskell, sdl, gameboy, boxxle, functional programming, λ\=.

Haskell || Die

The main driver for me studying programming in the first place was because I love programming video games. After the discovery of SDL bindings for Haskell, along with an amazing tutorial, I couldn’t resist trying to make a simple game in Haskell. It took a lottttttt of reading and time in GHCI before I could start making any real progress, but after a couple days I began to wrap my head around things. This is a small remake of the GameBoy game Boxxle, a very simple but fun puzzle game.

The full code (with 40 levels) can be found on my GitHub

Below is the current version. I am still wrapping my head around the intricisies of Haskell, so feel free to make suggestions :)

Boxxle.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
{-# LANGUAGE FlexibleContexts #-} -- http://stackoverflow.com/questions/10865963/using-the-state-monad-to-hide-explicit-state

-- @author Kenny Cason
-- kennycason.com 2013

import Graphics.UI.SDL
import Graphics.UI.SDL.TTF as TTFG
import Graphics.UI.SDL.Mixer

import Data.List

import Control.Monad
import Control.Monad.State
import Control.Monad.Reader

import Timer

-- game data
-- levels from: http://www.gamefaqs.com/gameboy/585643-boxxle/faqs/52416

rooms = [
    roomBuilder 
        [[1,1,1,1,1,0,0,0,0]                    -- map tiles
        ,[1,0,0,0,1,0,0,0,0]
        ,[1,0,0,0,1,0,1,1,1]
        ,[1,0,0,0,1,0,1,4,1]
        ,[1,1,1,0,1,1,1,4,1]
        ,[0,1,1,0,0,0,0,4,1]
        ,[0,1,0,0,0,1,0,0,1]
        ,[0,1,0,0,0,1,1,1,1]
        ,[0,1,1,1,1,1,0,0,0]
        ]
        [(Coord 2 2), (Coord 3 2), (Coord 2 3)] -- boxes
        [(Coord 7 3), (Coord 7 4), (Coord 7 5)] -- targets
        (Coord 1 1)                                -- start pos
    ,roomBuilder
        [[1,1,1,1,1,1,1,1,1,1]
        ,[1,4,4,0,0,0,0,0,0,1]
        ,[1,4,4,0,0,0,1,0,0,1]
        ,[1,0,0,1,0,1,1,0,1,1]
        ,[1,0,0,0,0,0,0,0,1,0]
        ,[1,1,1,1,1,0,1,0,1,0]
        ,[0,0,1,0,0,0,0,0,1,0]
        ,[0,0,1,0,0,0,0,0,1,0]
        ,[0,0,1,1,1,1,1,1,1,0]
        ]
        [(Coord 3 2), (Coord 4 3), (Coord 2 4), (Coord 4 6)]
        [(Coord 1 1), (Coord 2 1), (Coord 1 2), (Coord 2 2)]
        (Coord 1 1)
    ,roomBuilder
        [[0,1,1,1,1,0]
        ,[1,1,0,0,1,0]
        ,[1,0,0,0,1,0]
        ,[1,1,0,0,1,1]
        ,[1,1,0,0,0,1]
        ,[1,4,0,0,0,1]
        ,[1,4,4,0,4,1]
        ,[1,1,1,1,1,1]
        ]
        [(Coord 2 2), (Coord 2 3), (Coord 3 4), (Coord 2 5)]
        [(Coord 1 5), (Coord 1 6), (Coord 2 6), (Coord 4 6)]
        (Coord 1 2)
    ,roomBuilder
        [[0,1,1,1,1,1,0,0]
        ,[0,1,0,0,1,1,1,0]
        ,[0,1,0,0,0,0,1,0]
        ,[1,1,1,0,1,0,1,1]
        ,[1,4,1,0,1,0,0,1]
        ,[1,4,0,0,0,1,0,1]
        ,[1,4,0,0,0,0,0,1]
        ,[1,1,1,1,1,1,1,1]
        ]
        [(Coord 3 2), (Coord 2 5), (Coord 5 6)]
        [(Coord 1 4), (Coord 1 5), (Coord 1 6)]
        (Coord 2 1)
    ,roomBuilder
        [[0,1,1,1,1,1,1,1,0,0]
        ,[0,1,0,0,0,0,0,1,1,1]
        ,[1,1,0,1,1,1,0,0,0,1]
        ,[1,0,0,0,0,0,0,0,0,1]
        ,[1,0,4,4,1,0,0,0,1,1]
        ,[1,1,4,4,1,0,0,0,1,0]
        ,[0,1,1,1,1,1,1,1,1,0]
        ]
        [(Coord 2 2), (Coord 4 3), (Coord 6 3), (Coord 5 4)]
        [(Coord 2 4), (Coord 3 4), (Coord 2 5), (Coord 3 5)]
        (Coord 2 1)
    ]

roomBuilder ::[[Int]] -> [Coord] -> [Coord] -> Coord -> Room
roomBuilder tiles boxes targets startPos = Room {
        tiles = tiles
        ,walls = foldTiles tiles
        ,boxes = boxes
        ,targets = targets
        ,startPos = startPos
    }


-- global constants
startLevel = 1

tEmpty     = 0
tBrick     = 1
tBox     = 2
tPlayer = 3
tTarget    = 4

textColor = Color 0x33 0x33 0x33


-- type defines
data Direction = UP | DOWN | LEFT | RIGHT deriving (Eq, Enum)

data Move = Move { 
    dir :: Direction
    ,dx :: Int
    ,dy :: Int 
}

data Coord = Coord { 
    x :: Int
    ,y :: Int 
} deriving (Eq)

data Room = Room { 
    tiles :: [[Int]]
    ,walls :: [Coord]
    ,boxes :: [Coord]
    ,targets :: [Coord]
    ,startPos :: Coord 
}

data GameData = GameData {
    timer :: Timer
    ,room :: Room
    ,player :: Coord
    ,level :: Int
}        

data GameConfig = GameConfig {
    screen :: Surface
    ,sprites :: Surface
    ,front :: Font
    ,music :: Music
}

type GameState = StateT GameData IO
type GameEnv = ReaderT GameConfig GameState


-- monad state get/put/modify 
getGameData :: MonadState GameData m => m GameData
getGameData = get

putGameData :: MonadState GameData m => GameData -> m ()
putGameData = put

modifyGameData :: MonadState GameData m => (GameData -> GameData) -> m ()
modifyGameData = modify

getPlayer :: MonadState GameData m => m Coord
getPlayer = gets player

getRoom :: MonadState GameData m => m Room
getRoom = gets room

getLevel :: MonadState GameData m => m Int
getLevel = gets level

getTimer :: MonadState GameData m => m Timer
getTimer = gets timer

putTimer :: MonadState GameData m => Timer -> m ()
putTimer t = modify $ \s -> s { timer = t }

modifyTimerM :: MonadState GameData m => (Timer -> m Timer) -> m ()
modifyTimerM act = getTimer >>= act >>= putTimer

getScreen :: MonadReader GameConfig m => m Surface
getScreen = liftM screen ask

getSprites :: MonadReader GameConfig m => m Surface
getSprites = liftM sprites ask

getFont :: MonadReader GameConfig m => m Font
getFont = liftM front ask


-- main functions
newGame :: Int -> IO (GameConfig, GameData)
newGame lvl = do
    -- setVideoMode 320 288 32 []
    setVideoMode 448 352 32 []
    setCaption "Boxxle - Haskell" []
    screen  <- getVideoSurface
    sprites <- loadBMP "img/boxxle.bmp"
    font    <- openFont "fonts/steelpla.ttf" 18
    timer   <- start defaultTimer

    openAudio 22050 AudioS16Sys 2 4096
    music   <- loadMUS "music/main.wav"
    playMusic music (-1)
    return (GameConfig screen sprites font music, GameData timer room (startPos room) lvl)
    where room = (rooms !! (lvl - 1))


levelUp :: GameData -> GameData
levelUp gd = gd { level = newLevel, room = nextRoom, player = (startPos nextRoom) }
            where 
                newLevel = (level gd) + 1
                nextRoom = rooms !! ((level gd) `mod` (length rooms))


resetLevel :: GameData -> GameData
resetLevel gd = gd { room = resetRoom, player = (startPos resetRoom) }
                where resetRoom = rooms !! ( ((level gd) - 1) `mod` (length rooms) )


handleWin :: GameData -> GameData
handleWin gd| isWin = levelUp gd
            | otherwise = gd
                where isWin = length (intersect 
                                        (boxes currentRoom) 
                                        (targets currentRoom)) == length (targets currentRoom)
                                            where currentRoom = (room gd)


foldTiles :: [[Int]] -> [Coord]
foldTiles tiles =
  [Coord x y                         -- generate a Coord pair
    | (y, row) <- enumerate tiles    -- for each row with its coordinate
    , (x, tile) <- enumerate row     -- for each tile in the row (with coordinate)
    , tile == 1]                     -- if the tile is 1
        where enumerate = zip [0..]


getSpriteSheetOffset :: Int -> Maybe Rect
getSpriteSheetOffset n = Just (Rect offx offy 32 32)
                            where 
                                offx = n * 32
                                offy = 0


applySurface :: Int -> Int -> Surface -> Surface -> Maybe Rect -> IO Bool
applySurface x y src dst clip = blitSurface src clip dst offset
    where offset = Just Rect { rectX = x, rectY = y, rectW = 0, rectH = 0 }


drawSprite :: Surface -> Surface -> Int -> Int -> Int -> IO Bool
drawSprite screen sprites n x y = blitSurface 
                                        sprites (getSpriteSheetOffset n) 
                                        screen dst
                                            where dst = Just (Rect x y 32 32)


drawPlayer :: Surface -> Surface -> Int -> Int -> IO Bool
drawPlayer screen sprites x y = blitSurface sprites src screen dst
                                where
                                    src = (getSpriteSheetOffset tPlayer)
                                    dst = Just (Rect (x * 32) (y * 32) 32 32)


drawTile :: Surface -> Surface -> Int -> [Coord] -> IO()
drawTile screen sprites tile coords = mapM_ (\c -> drawSprite screen sprites tile ((x c) * 32) ((y c) * 32) ) coords


drawBricks :: Surface -> Surface -> [Coord] -> IO()
drawBricks screen sprites bricks = drawTile screen sprites tBrick bricks


drawBoxes :: Surface -> Surface -> [Coord] -> IO()
drawBoxes screen sprites boxes = drawTile screen sprites tBox boxes


drawTargets :: Surface -> Surface -> [Coord] -> IO()
drawTargets screen sprites targets = drawTile screen sprites tTarget targets


collide :: Coord -> Coord -> Bool
collide c1 c2 = ((x c1) == (x c2)) && ((y c1) == (y c2))


offsetCoord :: Coord -> Move -> Coord
offsetCoord c move = c { x = (x c) + (dx move), y = (y c) + (dy move) } 


collideWithWorld :: Coord -> Room -> Bool
collideWithWorld c room = (foldr (||) False (map (collide c) (walls room)))
    

collideWithBoxes :: Coord -> Room -> Bool
collideWithBoxes c room = (foldr (||) False (map (collide c) (boxes room)))    


canBoxMove :: Coord -> Room -> Bool            
canBoxMove box room = not ((collideWithWorld box room) || (collideWithBoxes box room))


movePlayer :: Move -> GameData -> GameData
movePlayer move gd     | collideWithWorld newPlayerPos (room gd) = gd
                    | otherwise = gd { player = Coord { x = (x playerPos) + (dx move), y = (y playerPos) + (dy move)} }
                    where 
                        playerPos = (player gd)
                        newPlayerPos = (offsetCoord playerPos move)


moveBox :: Move -> Coord -> Coord
moveBox move box = box { x = (x box) + (dx move), y = (y box) + (dy move) }


checkBox :: Room -> Coord -> Move -> Coord -> Coord
checkBox room playerPos move box| collidedWithPlayer && (dir move) == UP && boxCanMove = moveBox move box
                                | collidedWithPlayer && (dir move) == DOWN && boxCanMove = moveBox move box
                                | collidedWithPlayer && (dir move) == LEFT && boxCanMove = moveBox move box
                                | collidedWithPlayer && (dir move) == RIGHT && boxCanMove = moveBox move box
                                | otherwise = box
                                    where 
                                        collidedWithPlayer = collide playerPos box -- player collided
                                        boxCanMove = canBoxMove newBoxPos room
                                            where newBoxPos = offsetCoord box move


checkBoxes :: Room -> Coord -> Move -> [Coord] -> [Coord]
checkBoxes room playerPos move boxes = map (checkBox room playerPos move) boxes


handleBoxes :: Move -> GameData -> GameData
handleBoxes move gd = gd { room = (room gd) { boxes = (checkBoxes (room gd) playerPos move (boxes (room gd)) ) } }
                        where playerPos = offsetCoord (player gd) move


-- if the resulting move yields the player standing on a box, undo it
undoPlayer :: Move -> GameData -> GameData
undoPlayer move gd     | collideWithBoxes playerPos (room gd) = gd { player = Coord { x = (x playerPos) - (dx move), y = (y playerPos) - (dy move)} }
                    | otherwise = gd
                    where 
                        playerPos = (player gd)


handleKeyboard :: Event -> GameData -> GameData
handleKeyboard (KeyDown (Keysym SDLK_UP _ _)) gd = ((handleWin.undoPlayer move).(movePlayer move).(handleBoxes move)) gd
                                                    where move = Move { dir = UP, dx = 0, dy = -1 }
handleKeyboard (KeyDown (Keysym SDLK_DOWN _ _)) gd = ((handleWin.undoPlayer move).(movePlayer move).(handleBoxes move)) gd
                                                    where move = Move { dir = DOWN, dx = 0, dy = 1 }
handleKeyboard (KeyDown (Keysym SDLK_LEFT _ _)) gd = ((handleWin.undoPlayer move).(movePlayer move).(handleBoxes move)) gd
                                                    where move = Move { dir = LEFT, dx = -1, dy = 0 }
handleKeyboard (KeyDown (Keysym SDLK_RIGHT _ _)) gd = ((handleWin.undoPlayer move).(movePlayer move).(handleBoxes move))  gd
                                                    where move = Move { dir = RIGHT, dx = 1, dy = 0 }
handleKeyboard (KeyDown (Keysym SDLK_r _ _)) gd = resetLevel gd
handleKeyboard (KeyDown (Keysym SDLK_s _ _)) gd = levelUp gd
handleKeyboard _ d = d


loop :: GameEnv ()
loop = do
    timer   <- getTimer
    screen  <- getScreen
    sprites <- getSprites
    pos     <- getPlayer
    room    <- getRoom
    font    <- getFont
    level   <- getLevel
    message <- liftIO $ renderTextSolid font ("level " ++ (show level)) textColor

    modifyTimerM $ liftIO . start
    quit <- whileEvents $ modifyGameData . handleKeyboard

    liftIO $ do
        bgRect    <- Just `liftM` getClipRect screen
        white     <- mapRGB' screen 0xff 0xff 0xff
        fillRect screen bgRect white

        drawBricks screen sprites (walls room)
        drawTargets screen sprites (targets room)
        drawBoxes screen sprites (boxes room)
        drawPlayer screen sprites (x pos) (y pos)

        applySurface 340 3 message screen Nothing

        Graphics.UI.SDL.flip screen

        ticks <- getTimerTicks timer
        when (ticks < secsPerFrame) $ do
            delay $ secsPerFrame - ticks
    unless quit loop
 where
    framesPerSecond = 30
    secsPerFrame = 1000 `div` framesPerSecond
    mapRGB' = mapRGB . surfaceGetPixelFormat


whileEvents :: MonadIO m => (Event -> m ()) -> m Bool
whileEvents act = do
    event <- liftIO pollEvent
    case event of
        Quit -> return True
        NoEvent -> return False
        _ -> do
            act event
            whileEvents act


runLoop :: GameConfig -> GameData -> IO ()
runLoop = evalStateT . runReaderT loop


main = withInit [InitEverything] $ do -- withInit calls quit
    result <- TTFG.init
    if not result
        then putStr "Failed to init ttf\n"
        else do
            (gc, gd) <- newGame startLevel
            runLoop gc gd
            haltMusic
            closeAudio
            TTFG.quit
comments powered by Disqus