gpt4 book ai didi

animation - Haskell Gloss 没有动画

转载 作者:行者123 更新时间:2023-12-04 22:41:12 25 4
gpt4 key购买 nike

我有一个程序可以模拟社区中许多代理的交互。我正在使用 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

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

最佳答案

(所以评论框不允许我粘贴代码)

你的编译不适合我。您使用的是哪个版本的 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

关于animation - Haskell Gloss 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15869617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com