这是弹跳球代码。我试图让'appendFile'在更新功能上运行,所以当球从墙上反弹时,'appendFile'会将px和px值写入文件“log.txt”
import Graphics.Gloss
import Graphics.Gloss.Data.ViewPort (ViewPort)
main :: IO ()
main =
simulate
(InWindow "Bouncing ball" (width, height) (50, 50))
white
30
initial
view
update
但我遇到了麻烦,因为“appendFile”只在签名 IO 上运行。而且我不知道如何在这种情况下应用它
update :: ViewPort -> Float -> World -> World
update _ _ World {position = (px, py), velocity = (vx, vy)} =
let
appendFile "Log.txt" ("" ++ show px ++ " " + show py ++ "")
newPx = px + vx
newPy = py + vy
newVx = if newPx >= fromIntegral width || newPx <= 0 then - vx else vx
newVy = if newPy >= fromIntegral height || newPy <= 0 then - vy else vy
in World {position = (newPx, newPy), velocity = (newVx, newVy)}