Haskell Gloss动画不播放

3

我有一个模拟社区中许多代理互动的程序。我正在使用Gloss库对交互进行动画处理,代理图片正确呈现,但动画没有呈现出来。我通过生成模拟来实现动画,这是一个相互作用的列表列表,我随后选择与第二个动画对应的列表,并渲染该交互。当将代码输出到终端时,模拟代码运行正常。 以下是代码:

render ::  Int -> History -> Picture -- assume window to be square
render size (History int agents) =  Pictures $ map (drawAgent (step`div`2) colors step) agents
    where step = size*6 `div` (length agents)
          --agents = nub $ concat $ map (\(Interaction a1 a2 _ ) -> [a1,a2]) int
          nubNames = nub $ map (getName . name) agents --ignore the first two letters of name
          colors = Map.fromList $ zipWith (\name color-> (name, color)) nubNames (cycle colorlist)
          colorlist = [red,green,blue,yellow,cyan,magenta,rose,violet,azure,aquamarine,chartreuse,orange]

drawAgent :: Int -> Map.Map String Color -> Int -> Agent -> Picture
drawAgent size colors step agent =
    color aColor (Polygon [(posA,posB),(posA,negB),(negA,negB),(negA,posB)])
    where aColor = fromMaybe black $ Map.lookup (getName $ name agent ) colors
          a = (fst $ position agent) * step
          b = (snd $ position agent) * step
          posA = fromIntegral $ a+size
          negA = fromIntegral $ a-size
          posB = fromIntegral $ b+size
          negB = fromIntegral $ b-size

simulate :: Int -> [Agent] -> [History]
simulate  len  agents = trace ("simulation"
                        (playRound agents len) : 
                         simulate len (reproduce (playRound agents len))

main =  do
    a <- getStdGen
          G.animate (G.InWindow "My Window" (400, 400) (0,0)) G.white 
          (\time ->(render 400) $ ((simulate 5 (agent a))!!(floor time)))

    where agent a = generate a 9
          sim a = simulate 40 (agent a)

当我执行这个命令时,它会显示模拟正在运行,但只会渲染第一个交互。
  $ ghc -O2 -threaded main.hs && ./main
  [6 of 8] Compiling Prisoners        ( Prisoners.hs, Prisoners.o )
  Linking main ...
  simulation
  simulation
  simulation

这将一直持续下去,直到我停止它,每次呈现相同的图片。我做错了什么?

你的代码在我这里编译不通过。你使用的是哪个版本的Gloss?API从v1.0变成了v1.7.x。这个简单的例子对你有效吗? - ja.
1个回答

1

我的评论框无法让我粘贴代码。

你的代码在我的电脑上无法编译。你使用的是哪个版本的Gloss?API从v1.0到v1.7.x已经发生了变化。你使用的是哪个版本的GHC?你使用的是哪个操作系统?

这个简单的例子对你有用吗?

{- left click to create a circle;  escape to quit  -}
import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game

initial _ = [(0.0,0.0) :: Point]

event (EventMotion (x,y)) world = world --(x,y)
event (EventKey (MouseButton LeftButton) Up mods (x,y)) world = (x,y):world
event _                   world = world

step time world = world 

draw pts = Pictures $ map f pts
  where f (x,y) = translate x y (circle 10)

m = play (InWindow "Hi" (600,600) (200,200)) white 1 (initial 0) draw event step

谢谢,我只是更新了词汇表并切换到游戏模式,就让它正常工作了。 - MichaelFine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接