我有一个程序可以模拟社区中许多代理的交互。我正在使用Gloss library动画交互,代理的图片正确呈现,而不是动画。我通过生成一个模拟来为它设置动画,模拟是一个交互列表的列表,然后我将获取与动画的第二个相对应的模拟,然后我在那里渲染该交互。模拟代码在输出到终端时工作正常。代码:
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
它将继续这样,直到我停止它,每次渲染相同的图片。我做错了什么?
答案 0 :(得分:1)
(SO评论框不允许我粘贴代码)
你不为我编译。您使用的是什么版本的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