2013-04-08 37 views
3

我有一個程序,它模擬社區中許多代理的交互。我使用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

您不會爲我編譯。您使用的是什麼版本的光澤,API已從v1.0更改爲v1.7.x.這個簡單的例子適合你嗎? – 2013-04-09 07:00:05

回答

1

(SO評論箱不會讓我粘貼代碼)

你不會爲我編譯。您使用的是什麼版本的光澤,API已從v1.0更改爲v1.7.x.什麼版本的GHC?什麼OS?

這個簡單的例子適合你嗎?

{- 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 
+0

謝謝,只是通過更新光澤和切換到遊戲,我得到了它的工作。 – MichaelFine 2013-04-09 20:40:24