我正在学习如何使用 Gloss 库在 Haskell 中制作一些动画。
考虑下面的代码,它动画了一个随时间缩小和扩大其半径的圆。
import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game
type Radius = Float
type RealTime = Float
data World = World Radius RealTime
main :: IO ()
main
= do let initWorld = World 100.0 0
let windowSize = (800,800)
let windowPosition = (200,200)
putStrLn "Before play"
play (InWindow "Wobbling Circle" windowSize windowPosition)
white 40 initWorld renderWorld handleEvent stepWorld
putStrLn "After play"
renderWorld :: World -> Picture
renderWorld (World rad rtime ) = Pictures [ Circle rad ]
handleEvent :: Event -> World -> World
handleEvent _ = id
stepWorld :: Float -> World -> World -- wobbling circle
stepWorld _ (World _ rtime) = World (100 * sin rtime) (rtime+0.1)
ghc --make -O2 -threaded Main.hs
我在 Ubuntu 14.04 机器上编译了这段代码。
当我运行代码时,会打印出“播放前”语句,然后动画按预期开始。但是,当我关闭动画窗口时,代码会立即终止,而不会打印“播放后”语句。这是为什么?