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

它会继续这样,直到我停止它,每次都渲染相同的图片。我究竟做错了什么?

4

1 回答 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
于 2013-04-09T07:03:06.537 回答