首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Haskell Gloss不是动画效果

Haskell Gloss不是动画效果
EN

Stack Overflow用户
提问于 2013-04-08 08:44:02
回答 1查看 657关注 0票数 3

我有一个程序,可以模拟社区中许多智能体的交互。我使用Gloss library对交互进行动画处理,智能体的图片渲染正确,只是动画不正确。我通过生成一个模拟来对其进行动画处理,这是一个交互列表,然后我将获取与第二个动画相对应的列表,然后在其中呈现该交互。用于模拟的代码在输出到终端时工作正常。代码:

代码语言:javascript
运行
复制
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)

当我执行此命令时,它将显示模拟正在运行,但只渲染第一个交互。

代码语言:javascript
运行
复制
  $ ghc -O2 -threaded main.hs && ./main
  [6 of 8] Compiling Prisoners        ( Prisoners.hs, Prisoners.o )
  Linking main ...
  simulation
  simulation
  simulation

它将像这样继续下去,直到我停止它,每次渲染相同的图片。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-04-09 15:03:07

(所以注释框不会让我粘贴代码)

你的代码不能为我编译。您使用的是哪个版本的Gloss,API已从v1.0更改为v1.7.x。什么版本的GHC?什么操作系统?

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

代码语言:javascript
运行
复制
{- 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15869617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档