Skip to content

Instantly share code, notes, and snippets.

@kennycason
Last active December 25, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennycason/6895937 to your computer and use it in GitHub Desktop.
Save kennycason/6895937 to your computer and use it in GitHub Desktop.
metroid
import Graphics.UI.SDL as SDL
import Data.Maybe
import Control.Monad
getSpriteSheetOffset :: Int -> Maybe Rect
getSpriteSheetOffset n = Just (Rect offx offy 32 32)
where
offx = mod (n * 32) (32 * 5)
offy = quot (n * 32) (32 * 5) * 32
drawSprite :: SDL.Surface -> SDL.Surface -> Int -> Int -> Int -> IO Bool
drawSprite screen sprites n x y = SDL.blitSurface
sprites (getSpriteSheetOffset n)
screen dst
where dst = Just (Rect x y 32 32)
drawSamus :: SDL.Surface -> SDL.Surface -> Int -> Int -> IO Bool
drawSamus screen samus x y = SDL.blitSurface samus Nothing screen dst
where --src = Just (Rect 0 0 40 62)
dst = Just (Rect x y 40 62)
drawRoom :: SDL.Surface -> SDL.Surface -> [[Int]] -> IO()
drawRoom screen sprites room = mapM_ (\r -> drawRow screen sprites r) [0..14]
where drawRow screen sprites r = mapM_ (\(x, n) -> drawSprite screen sprites n (x * 32) (r * 32)) (coord (room !! r))
where coord row = map (\i -> (i, row !! i)) [0..19]
emptyWorld :: Int -> Int -> a -> [[a]]
emptyWorld x y = replicate y . replicate x
room = [[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
,[3,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,3,0,3]
,[3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,7,6,6,6,6,6,6,7,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,6,6,7,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,7,6,6,6,6,6,7,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
,[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,3,3]
,[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,3,3]
]
main :: IO()
main = do
-- init
SDL.init [SDL.InitEverything]
SDL.setVideoMode 640 480 32 []
SDL.setCaption "Metroid" "Metroid"
screen <- getVideoSurface
samus <- loadBMP "samus.bmp"
sprites <- loadBMP "spritesheet.bmp"
drawRoom screen sprites room
drawSamus screen samus 64 354
SDL.flip screen
eventLoop
SDL.quit
print "done"
where
eventLoop = SDL.waitEventBlocking >>= checkEvent
checkEvent (KeyUp _) = return ()
checkEvent _ = eventLoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment