Search code examples
haskellfunctional-programminggame-loop

How do I write a game loop in Haskell?


I want to code a game in Haskell where every iteration of the loop computes the state of the world. I thought I should create a function:

gameLoop :: World -> World
-- ...

and have main :: IO () call it:

main = do
    gameLoop -- ...

But the problem is that I'm missing some fundamental understanding of how to wrap the gameLoop function so that it returns main's parameter value.

How would one go about creating a game loop in Haskell?


Solution

  • You'll probably want something like this

    import Control.Monad.Loops
    
    main = iterateM_ 
           (\w -> displayWorld w >> return (gameLoop w))
           initWorld
    -- iterateM_ ((>>) <$> displayWorld <*> return . gameLoop) initWorld
    

    Or if you don't want to use the whole monad-loops package (even though it rocks)

    main = loop initWorld
      where loop w = displayWorld w >> loop (gameLoop w)
    

    Basically you're just drawing the world, then looping again to with the next state.

    More likely you want something like this though

     -- False when the user wants to exit the game
     keepGoing :: World -> Bool
    
     main = iterateUntilM_ keepGoing displayLoop initWorld
       where displayLoop w = displayWorld w >> return (gameLoop w)
    

    Since otherwise you can't stop :)