class (Show m) => Widget m where
update :: Integer -> m -> m
instance (Num a,Num b) => Num (a,b) where
(x1,y1) + (x2,y2) = (x1+x2,y1+y2)
(x1,y1) * (x2,y2) = (x1*x2,y1*y2)
(x1,y1) - (x2,y2) = (x1-x2,y1-y2)
negate (x,y) = (negate x,negate y)
abs (x,y) = (abs x,abs y)
signum (x,y) = (signum x,signum y)
fromInteger i = (fromInteger i,fromInteger i)
data Sqare = Sqare{pos::(Integer,Integer),speed::(Integer,Integer)}
deriving(Show)
instance Widget Sqare where
update t w@(Sqare p s) = w{pos=p+s*(t,t)}
data World a = World [a]
deriving(Show)
instance (Widget a) => Widget (World a) where
update t (World children) = World (map (update t) children)
run::(Widget m)=>Integer->m->IO()
run 0 world = do
putStrLn (show world)
run (-1) world = do
putStrLn (show world)
run (-1) (update 1 world)
run times world = do
putStrLn (show world)
run (times-1) (update 1 world)
main = do
let world = World [Sqare (1,1) (1,1),Sqare (100,100) (-1,1),Sqare
(100,100) (1,-1)]
run 10 world
--
您收到此邮件是因为您订阅了 Google 网上论坛的“haskell.cn”论坛。
要向此网上论坛发帖,请发送电子邮件至 hask...@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 haskellcn+...@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/haskellcn?hl=zh-CN 访问此网上论坛。
哈哈,是这样的,昨晚写得比较匆忙。
想了一下,从模型上来看,每次从旧的状态计算出新的状态,确实是正确的。
但从执行来看,World里面有大量物体的情况下,动画的每一帧都需要创建大量的对象,而且旧的对象直接被丢弃不再被使用,似乎有点浪费。