在 运行 Gloss 代码 Haskell 时发布消息

Posting a message while running Gloss code in Haskell

Haskell playIO 具有以下类型:

 playIO:: Display   
-> Color    background color
-> Int  
-> world    --initial world 
-> (world -> IO Picture)  -- function to change world into a picture    
-> (Event -> world -> IO world) --event handler function
-> (Float -> world -> IO world) -- The function to update the world after the given time 
-> IO ()

一旦您在 main 中调用 playIO,它就会持续更新由 world 建模的 GUI。如果处理事件的代码(请参阅代码注释)或更新世界的函数内部发生某些事情,并且您想输出一条消息(不一定是错误),那么可以使用什么方法不违反类型?是否必须突破 playIO 函数才能显示我的消息?如果是这样,该怎么做?

例如,如果您想根据事件发出消息,则将该操作放入事件处理程序中。例如:

main :: IO ()
main = playIO black 100 world0 renderWorld handleEvent updateWorld

handleEvent evt w =
    do print event -- Right here, you are emitting a message!
       updateWorldWithEvent evt w
       putStrLn "I have updated the world, now time for breakfast."

请记住,handleEvent 操作可能会非常频繁地发生,因此 select 您的输出会相应地发生。