Car*_*ong 5 haskell arrows game-engine frp netwire
三个月后更新
我在下面的回答中使用netwire-5.0.1+ sdl,在使用Arrows和Kleisli Arrows进行I/O的功能反应编程结构中.虽然太简单,不能被称为"游戏",但它应该是非常可组合的并且非常可扩展.
原版的
我正在学习Haskell,并尝试用它制作一个小游戏.但是,我想看看小型(规范)文本游戏的结构.我也尽量保持代码尽可能纯净.我现在正在努力了解如何实施:
State,在http://www.gamedev.net/page/resources/_/technical/game-programming/haskell-game-object-design-or-how-functions-can-get-you中试了一下-apples-r3204,但是虽然单个组件可以在有限的步骤中工作和更新,但我看不出如何在无限循环中使用它.如果可能的话,我想看一个基本上最小的例子:
我没有任何可用的代码,因为我无法获得最基本的东西.我在网上找到的任何其他材料/示例都使用其他一些库,比如SDL或GTK驱动事件.我发现在Haskell中完全写的唯一一个是http://jpmoresmau.blogspot.com/2006/11/my-first-haskell-adventure-game.html,但是那个在主循环中看起来不像是一个尾递归(同样,我不知道这是否重要).
或者,可能Haskell不打算做这样的事情?或者我可能应该放入mainC?
编辑1
所以我在https://wiki.haskell.org/Simple_StateT_use中修改了一个小例子,使它更简单(并且它不符合我的标准):
module Main where
import Control.Monad.State
main = do
putStrLn "I'm thinking of a number between 1 and 100, can you guess it?"
guesses <- execStateT (guessSession answer) 0
putStrLn $ "Success in " ++ (show guesses) ++ " tries."
where
answer = 10
guessSession :: Int -> StateT Int IO ()
guessSession answer =
do gs <- lift getLine -- get guess from user
let g = read gs -- convert to number
modify (+1) -- increment number of guesses
case g of
10 -> do lift $ putStrLn "Right"
_ -> do lift $ putStrLn "Continue"
guessSession answer
Run Code Online (Sandbox Code Playgroud)
但是,它最终会溢出内存.我测试过
bash prompt$ yes 1 | ./Test-Game
Run Code Online (Sandbox Code Playgroud)
并且内存使用率开始线性增长.
编辑2
好的,我发现了Haskell的递归和内存使用,并对"堆栈"有了一些了解......那么我的测试方法有什么问题吗?
经过 3 个月的挖掘无数网站并尝试一些小项目后,我终于以一种非常非常不同的方式实现了一个简约的游戏(或者是吗?)。这个例子的存在只是为了演示用 Haskell 编写的游戏的一种可能的结构,并且应该很容易扩展以处理更复杂的逻辑和游戏玩法。
完整代码和教程可在https://github.com/carldong/HMovePad-Tutorial上找到
这个游戏只有一个矩形,玩家可以通过左右键左右移动,这就是整个“游戏”。
该游戏是使用netwire-5.0.1和SDL处理图形来实现的。如果我理解正确的话,该架构是完全功能反应式的。几乎所有东西都是由 Arrow 组合实现的,只有一个函数在IO. 因此,我希望读者对 Haskell 的 Arrow 语法有基本的了解,因为它被广泛使用。
选择该游戏的实现顺序是为了使调试变得容易,并且选择实现本身是为了netwire尽可能演示不同的用法。
连续时间语义用于 I/O,但离散事件用于处理游戏逻辑内的游戏事件。
第一步是确保 SDL 正常工作。来源很简单:
module Main where
import qualified Graphics.UI.SDL as SDL
main :: IO ()
main = do
SDL.init [SDL.InitEverything]
w <- SDL.setVideoMode 800 600 32 [SDL.SWSurface]
s <- SDL.createRGBSurfaceEndian [SDL.SWSurface] 800 600 32
SDL.fillRect s (Just testRect) (SDL.Pixel 0xFFFFFFFF)
SDL.blitSurface s (Nothing) w (Nothing)
SDL.flip w
testLoop
SDL.quit
where
testLoop = testLoop
testRect = SDL.Rect 350 500 100 50
Run Code Online (Sandbox Code Playgroud)
如果一切正常,窗口底部应该会出现一个白色矩形。请注意,单击x不会关闭窗口。它必须通过Ctrl+C或kill来关闭。
由于我们不想一直执行到最后一步,发现屏幕上什么也画不出来,所以我们先做输出部分。
我们需要箭头语法:
{-# LANGUAGE Arrows #-}
Run Code Online (Sandbox Code Playgroud)
另外,我们需要导入一些东西:
import Prelude hiding ((.), id)
import Control.Wire
import Control.Arrow
import Control.Monad
import Data.Monoid
import qualified Graphics.UI.SDL as SDL
Run Code Online (Sandbox Code Playgroud)
我们需要了解如何构建 Kleisli Wires:Netwire 5 中的 Kleisli Arrow?。此示例显示了使用 Kleisli Wires 的交互式程序的基本结构:Netwire 中的控制台交互性?。要从任何类型 构造 Kleisli Wire a -> m b,我们需要:
mkKleisli :: (Monad m, Monoid e) => (a -> m b) -> Wire s e m a b
mkKleisli f = mkGen_ $ \a -> liftM Right $ f a
Run Code Online (Sandbox Code Playgroud)
然后,由于我没有trace在 Arrow 进程下工作,因此使用调试线将对象打印到控制台:
wDebug :: (Show a, Monoid e) => Wire s e IO a ()
wDebug = mkKleisli $ \a -> putStrLn $ show a
Run Code Online (Sandbox Code Playgroud)
现在是时候编写一些要提升到线路中的函数了。对于输出,我们需要一个函数,该函数返回一个SDL.Surface根据焊盘的 X 坐标绘制的正确矩形:
padSurf :: SDL.Surface
-> Int
-> IO SDL.Surface
padSurf surf x' = do
let rect' = SDL.Rect x' 500 100 50
clipRect <- SDL.getClipRect surf
SDL.fillRect surf (Just clipRect) (SDL.Pixel 0x00000000)
SDL.fillRect surf (Just rect') (SDL.Pixel 0xFFFFFFFF)
return surf
Run Code Online (Sandbox Code Playgroud)
请小心,此函数会进行破坏性更新。传入的表面稍后将被传输到窗口表面。
现在我们有了表面。输出线就很简单了:
wTestOutput :: SDL.Surface -> Wire s () IO () SDL.Surface
wTestOutput surf = mkKleisli $ \_ -> testPad
where
testPad = padSurf surf 350
Run Code Online (Sandbox Code Playgroud)
然后,我们将电线放在一起,并玩一下它们:
gameWire :: SDL.Surface
-> Wire s () IO () SDL.Surface
gameWire w = proc _ -> do
finalSurf <- wTestOutput w -< ()
wDebug -< "Try a debug message"
returnA -< finalSurf
Run Code Online (Sandbox Code Playgroud)
最后,我们main正确地改变并驱动电线:
main :: IO ()
main = do
SDL.init [SDL.InitEverything]
w <- SDL.setVideoMode 800 600 32 [SDL.SWSurface]
s <- SDL.createRGBSurfaceEndian [SDL.SWSurface] 800 600 32
run w (countSession_ 1) $ gameWire w
SDL.quit
run ::SDL.Surface -> Session IO s -> Wire s () IO () SDL.Surface -> IO ()
run mainSurf s w = do
(ds, s') <- stepSession s
(eSrcSurf, w') <- stepWire w ds (Right ())
case eSrcSurf of
Right srcSurf -> do
SDL.blitSurface srcSurf (Nothing) mainSurf (Nothing)
SDL.flip mainSurf
SDL.delay 30
run mainSurf s' w'
_ -> return ()
Run Code Online (Sandbox Code Playgroud)
请注意,如果您愿意,您也可以制作另一根电线来处理主窗口表面(并且它比我当前的实现更容易且更好),但我来得太晚且懒惰地添加它。查看我上面提到的交互式示例,看看run可以变得多么简单(如果使用抑制而不是使用抑制,它可以变得更加简单)quitWire在该示例中,它可以变得更加简单)。
当程序运行时,它的外观应该和以前一样。
这是完整的代码:
{-|
01-OutputWires.hs: This step, the output wires are constructed first for
easy debugging
-}
{-# LANGUAGE Arrows #-}
module Main where
import Prelude hiding ((.), id)
import Control.Wire
import Control.Arrow
import Control.Monad
import Data.Monoid
import qualified Graphics.UI.SDL as SDL
{- Wire Utilities -}
-- | Make a Kleisli wire
mkKleisli :: (Monad m, Monoid e) => (a -> m b) -> Wire s e m a b
mkKleisli f = mkGen_ $ \a -> liftM Right $ f a
-- | The debug wire
wDebug :: (Show a, Monoid e) => Wire s e IO a ()
wDebug = mkKleisli $ \a -> putStrLn $ show a
{- Functions to be lifted -}
padSurf :: SDL.Surface
-- ^ Previous state of surface
-> Int
-- ^ X'
-- | New state
-> IO SDL.Surface
padSurf surf x' = do
let rect' = SDL.Rect x' 500 100 50
clipRect <- SDL.getClipRect surf
SDL.fillRect surf (Just clipRect) (SDL.Pixel 0x00000000)
SDL.fillRect surf (Just rect') (SDL.Pixel 0xFFFFFFFF)
return surf
{- Wires -}
wTestOutput :: SDL.Surface -> Wire s () IO () SDL.Surface
wTestOutput surf = mkKleisli $ \_ -> testPad
where
testPad = padSurf surf 350
-- | This is the main game wire
gameWire :: SDL.Surface
-- ^ The main surface (i.e. the window)
-> Wire s () IO () SDL.Surface
gameWire w = proc _ -> do
finalSurf <- wTestOutput w -< ()
wDebug -< "Try a debug message"
returnA -< finalSurf
main :: IO ()
main = do
SDL.init [SDL.InitEverything]
w <- SDL.setVideoMode 800 600 32 [SDL.SWSurface]
s <- SDL.createRGBSurfaceEndian [SDL.SWSurface] 800 600 32
run w (countSession_ 1) $ gameWire w
SDL.quit
run ::SDL.Surface -> Session IO s -> Wire s () IO () SDL.Surface -> IO ()
run mainSurf s w = do
(ds, s') <- stepSession s
(eSrcSurf, w') <- stepWire w ds (Right ())
case eSrcSurf of
Right srcSurf -> do
SDL.blitSurface srcSurf (Nothing) mainSurf (Nothing)
SDL.flip mainSurf
SDL.delay 30
run mainSurf s' w'
_ -> return ()
Run Code Online (Sandbox Code Playgroud)
在本节中,我们将构建将玩家输入输入到程序中的连线。
由于我们将在逻辑部分使用离散事件,因此我们需要游戏事件的数据类型:
data GameEvent = MoveR
| MoveL
| NoEvent
deriving (Show, Eq)
-- | Make it Monoid so that game events can be combined
-- (Only applicable in this "game"!)
instance Monoid GameEvent where
mempty = NoEvent
-- | Simultaneously moving left and right is just nothing
MoveR `mappend` MoveL = NoEvent
MoveL `mappend` MoveR = NoEvent
-- | NoEvent is the identity
NoEvent `mappend` x = x
x `mappend` NoEvent = x
x `mappend` y
-- | Make sure identical events return same events
| x == y = x
-- | Otherwise, no event
| otherwise = NoEvent
Run Code Online (Sandbox Code Playgroud)
正如评论所建议的,该Monoid实例仅适用于这个特定的游戏,因为它只有两个相反的操作:左和右。
首先,我们将从 SDL 轮询事件:
pollEvents :: [SDL.Event] -> IO (Either () ([SDL.Event]))
pollEvents es = do
e <- SDL.pollEvent
case e of
SDL.NoEvent -> return $ Right es
SDL.Quit -> return $ Left ()
_ -> pollEvents $ e:es
Run Code Online (Sandbox Code Playgroud)
显然,该函数将来自 SDL 的事件作为列表进行轮询,并在Quit收到事件时进行抑制。
接下来,我们需要检查事件是否是键盘事件:
isKeyEvent :: SDL.Event -> Bool
isKeyEvent (SDL.KeyDown k) = True
isKeyEvent (SDL.KeyUp k) = True
isKeyEvent _ = False
Run Code Online (Sandbox Code Playgroud)
我们将有一个当前按下的键的列表,并且当键盘事件发生时它应该更新。简而言之,当某个键按下时,将该键插入到列表中,反之亦然:
keyStatus :: [SDL.Keysym] -> [SDL.Event] -> [SDL.Keysym]
keyStatus keysDown (e:es) =
case e of
-- | If a KeyDown is detected, add key to list
SDL.KeyDown k -> keyStatus (k:keysDown) es
-- | If a KeyUp is detected, remove key from list
SDL.KeyUp k -> keyStatus (filter (/= k) keysDown) es
_ -> keyStatus keysDown es
keyStatus keysDown [] = keysDown
Run Code Online (Sandbox Code Playgroud)
接下来,我们编写一个函数将键盘事件转换为游戏事件:
toGameEv :: SDL.Keysym -> GameEvent
toGameEv (SDL.Keysym SDL.SDLK_RIGHT _ _) = MoveR
toGameEv (SDL.Keysym SDL.SDLK_LEFT _ _) = MoveL
toGameEv _ = NoEvent
Run Code Online (Sandbox Code Playgroud)
我们折叠游戏事件并得到一个事件(真的,真的,特定于游戏!):
fireGameEv :: [SDL.Keysym] -> GameEvent
fireGameEv ks = foldl mappend NoEvent $ fmap toGameEv ks
Run Code Online (Sandbox Code Playgroud)
现在我们可以开始制作电线了。
首先,我们需要一条轮询事件的线路:
wPollEvents :: Wire s () IO () [SDL.Event]
wPollEvents = mkGen_ $ \_ -> pollEvents []
Run Code Online (Sandbox Code Playgroud)
请注意,这mkKleisli使得连线不会抑制,但我们希望在该连线中进行抑制,因为程序应该在应该退出的时候退出。因此,我们在这里使用mkGen_。
然后,我们需要过滤事件。首先,创建一个辅助函数来创建连续时间过滤器线:
mkFW_ :: (Monad m, Monoid e) => (a -> Bool) -> Wire s e m [a] [a]
mkFW_ f = mkSF_ $ filter f
Run Code Online (Sandbox Code Playgroud)
用于mkFW_制作过滤器:
wKeyEvents :: (Monad m, Monoid e) => Wire s e m [SDL.Event] [SDL.Event]
wKeyEvents = mkFW_ isKeyEvent
Run Code Online (Sandbox Code Playgroud)
然后,我们需要另一个方便的函数来从 类型的有状态函数创建有状态线路b -> a -> b:
mkSW_ :: (Monad m, Monoid e) => b -> (b->a->b) -> Wire s e m a b
mkSW_ b0 f = mkSFN $ g b0
where
g b0 a = let b1 = f b0 a in
(b1, mkSW_ b1 f)
Run Code Online (Sandbox Code Playgroud)
接下来,构建一条记住所有关键状态的有状态线路:
wKeyStatus :: (Monad m, Monoid e) => Wire s e m [SDL.Event] [SDL.Keysym]
wKeyStatus = mkSW_ empty keyStatus
Run Code Online (Sandbox Code Playgroud)
最后一段线段触发游戏事件:
wFireGameEv :: (Monad m, Monoid e) => Wire s e m [SDL.Keysym] (GameEvent)
wFireGameEv = arr fireGameEv
Run Code Online (Sandbox Code Playgroud)
要主动触发包含游戏事件的离散事件(netwire 事件),我们需要对 netwire 进行一些修改(我认为它仍然很不完整),因为它不提供始终触发事件的线路:
always :: (Monad m, Monoid e) => Wire s e m a (Event a)
always = mkSFN $ \x -> (WE.Event x, always)
Run Code Online (Sandbox Code Playgroud)
与 的实现相比now,唯一的区别是never和always。
最后,一条结合了上面所有输入线的大线:
wGameInput :: Wire s () IO () (Event GameEvent)
wGameInput = proc _ -> do
ge <- wFireGameEv <<< wKeyStatus
<<< wKeyEvents <<< wPollEvents -< ()
e <- always -< ge
-- Debug!
case e of
WE.NoEvent -> wDebug -< "No Event?!!"
WE.Event g -> wDebug -< "Game Event: " ++ show g
-- End Debug
returnA -< e
Run Code Online (Sandbox Code Playgroud)
该线路中还显示了调试示例。
要与主程序交互,请修改gameWire以使用输入:
gameWire w = proc _ -> do
ev <- wGameInput -< ()
finalSurf <- wTestOutput w -< ()
returnA -< finalSurf
Run Code Online (Sandbox Code Playgroud)
其他什么都不需要改变。嗯,很有趣,不是吗?
当程序运行时,控制台会给出大量输出,显示当前正在触发的游戏事件。尝试按左键、右键以及它们的组合,看看该行为是否符合预期。当然,矩形不会移动。
这是一大块代码:
{-|
02-InputWires.hs: This step, input wires are constructed and
debugged by using wDebug
-}
{-# LANGUAGE Arrows #-}
module Main where
import Prelude hiding ((.), id)
import Control.Wire
import Control.Arrow
import Control.Monad
import Data.Monoid
import qualified Graphics.UI.SDL as SDL
import qualified Control.Wire.Unsafe.Event as WE
{- Data types -}
-- | The unified datatype of game events
data GameEvent = MoveR
| MoveL
| NoEvent
deriving (Show, Eq)
-- | Make it Monoid so that game events can be combined
-- (Only applicable in this "game"!)
instance Monoid GameEvent where
mempty = NoEvent
-- | Simultaneously moving left and right is just nothing
MoveR `mappend` MoveL = NoEvent
MoveL `mappend` MoveR = NoEvent
-- | NoEvent is the identity
NoEvent `mappend` x = x
x `mappend` NoEvent = x
x `mappend` y
-- | Make sure identical events return same events
| x == y = x
-- | Otherwise, no event
| otherwise = NoEvent
{- Wire Utilities -}
-- | Make a stateless filter wire
mkFW_ :: (Monad m, Monoid e) => (a -> Bool) -> Wire s e m [a] [a]
mkFW_ f = mkSF_ $ filter f
-- -- | Make a stateful wire from a chained stateful function and initial value
-- -- The function (a -> b -> a) takes in an old state /a/, and returns state
-- -- transition function (b -> a).
mkSW_ :: (Monad m, Monoid e) => b -> (b->a->b) -> Wire s e m a b
mkSW_ b0 f = mkSFN $ g b0
where
g b0 a = let b1 = f b0 a in
(b1, mkSW_ b1 f)
-- | Make a Kleisli wire
mkKleisli :: (Monad m, Monoid e) => (a -> m b) -> Wire s e m a b
mkKleisli f = mkGen_ $ \a -> liftM Right $ f a
-- | The debug wire
wDebug :: (Show a, Monoid e) => Wire s e IO a ()
wDebug = mkKleisli $ \a -> putStrLn $ show a
-- | The "always" wire
always :: (Monad m, Monoid e) => Wire s e m a (Event a)
always = mkSFN $ \x -> (WE.Event x, always)
{- Functions to be lifted -}
-- | This is the pad surface whose X coordinate can be updated
padSurf :: SDL.Surface
-- ^ Previous state of surface
-> Int
-- ^ X'
-- | New state
-> IO SDL.Surface
padSurf surf x' = do
let rect' = SDL.Rect x' 500 100 50
clipRect <- SDL.getClipRect surf
SDL.fillRect surf (Just clipRect) (SDL.Pixel 0x00000000)
SDL.fillRect surf (Just rect') (SDL.Pixel 0xFFFFFFFF)
return surf
-- | The function to poll events and add to a list of events
pollEvents :: [SDL.Event] -> IO (Either () ([SDL.Event]))
pollEvents es = do
e <- SDL.pollEvent
case e of
SDL.NoEvent -> return $ Right es
SDL.Quit -> return $ Left ()
_ -> pollEvents $ e:es
-- | Checks whether one SDL.Event is a keyboard event
isKeyEvent :: SDL.Event -> Bool
isKeyEvent (SDL.KeyDown k) = True
isKeyEvent (SDL.KeyUp k) = True
isKeyEvent _ = False
-- | The raw function to process key status from events
keyStatus :: [SDL.Keysym] -> [SDL.Event] -> [SDL.Keysym]
keyStatus keysDown (e:es) =
case e of
-- | If a KeyDown is detected, add key to list
SDL.KeyDown k -> keyStatus (k:keysDown) es
-- | If a KeyUp is detected, remove key from list
SDL.KeyUp k -> keyStatus (filter (/= k) keysDown) es
_ -> keyStatus keysDown es
-- | If all events are processed, return
keyStatus keysDown [] = keysDown
-- | Convert a SDL Keysym into "standard" game events
toGameEv :: SDL.Keysym -> GameEvent
toGameEv (SDL.Keysym SDL.SDLK_RIGHT _ _) = MoveR
toGameEv (SDL.Keysym SDL.SDLK_LEFT _ _) = MoveL
toGameEv _ = NoEvent
-- | Combine all game events to get one single firing
fireGameEv :: [SDL.Keysym] -> GameEvent
fireGameEv ks = foldl mappend NoEvent $ fmap toGameEv ks
{- Wires -}
-- | The Kleisli wire to poll events
wPollEvents :: Wire s () IO () [SDL.Event]
wPollEvents = mkGen_ $ \_ -> pollEvents []
-- | A stateless wire that filters out keyboard events
wKeyEvents :: (Monad m, Monoid e) => Wire s e m [SDL.Event] [SDL.Event]
wKeyEvents = mkFW_ isKeyEvent
-- | A stateful wire to keep track of key status
wKeyStatus :: (Monad m, Monoid e) => Wire s e m [SDL.Event] [SDL.Keysym]
wKeyStatus = mkSW_ empty keyStatus
-- | A wire to fire game events from SDL events
wFireGameEv :: (Monad m, Monoid e) => Wire s e m [SDL.Keysym] (GameEvent)
wFireGameEv = arr fireGameEv
-- | This is the connected wire for the entire game input
wGameInput :: Wire s () IO () (Event GameEvent)
wGameInput = proc _ -> do
ge <- wFireGameEv <<< wKeyStatus
<<< wKeyEvents <<< wPollEvents -< ()
e <- always -< ge
-- Debug!
case e of
WE.NoEvent -> wDebug -< "No Event?!!"
WE.Event g -> wDebug -< "Game Event: " ++ show g
-- End Debug
returnA -< e
-- | The wire to test output
wTestOutput :: SDL.Surface -> Wire s () IO () SDL.Surface
wTestOutput surf = mkKleisli $ \_ -> testPad
where
testPad = padSurf surf 350
-- | This is the main game wire
gameWire :: SDL.Surface
-- ^ The main surface (i.e. the window)
-> Wire s () IO () SDL.Surface
gameWire w = proc _ -> do
ev <- wGameInput -< ()
finalSurf <- wTestOutput w -< ()
returnA -< finalSurf
main :: IO ()
main = do
SDL.init [SDL.InitEverything]
w <- SDL.setVideoMode 800 600 32 [SDL.SWSurface]
s <- SDL.createRGBSurfaceEndian [SDL.SWSurface] 800 600 32
run w (countSession_ 1) $ gameWire w
SDL.quit
run ::SDL.Surface -> Session IO s -> Wire s () IO () SDL.Surface -> IO ()
run mainSurf s w = do
(ds, s') <- stepSession s
(eSrcSurf, w') <- stepWire w ds (Right ())
case eSrcSurf of
Right srcSurf -> do
SDL.blitSurface srcSurf (Nothing) mainSurf (Nothing)
SDL.flip mainSurf
SDL.delay 30
run mainSurf s' w'
_ -> return ()
Run Code Online (Sandbox Code Playgroud)
首先,我们编写焊盘 X 位置的积分函数:
padDX :: Int -> GameEvent -> Int
padDX x0 e
| x > 700 = 700
| x < 0 = 0
| otherwise = x
where
x = x0 + go e
go MoveR = dx
go MoveL = -dx
go _ = 0
dx = 15
Run Code Online (Sandbox Code Playgroud)
我对所有内容进行了硬编码,但对于这个简约的示例来说这些并不重要。它应该很简单。
然后,我们创建代表焊盘当前位置的连线:
wPadX :: (Monad m, Monoid e) => Wire s e m (Event GameEvent) Int
wPadX = accumE padDX 400 >>> hold
Run Code Online (Sandbox Code Playgroud)
hold保存离散事件流的最新值。
接下来,我们将所有逻辑事物放在一条大逻辑线上:
wGameLogic :: Wire s () IO (Event GameEvent) Int
wGameLogic = proc ev -> do
x' <- wPadX -< ev
returnA -< x'
Run Code Online (Sandbox Code Playgroud)
由于我们有关于 X 坐标的一种状态,因此我们需要修改输出线:
wGameOutput :: SDL.Surface -> Wire s () IO Int SDL.Surface
wGameOutput surf = mkKleisli $ testPad
where
testPad = padSurf surf
Run Code Online (Sandbox Code Playgroud)
最后,我们将所有内容链接到gameWire:
gameWire w = proc _ -> do
ev <- wGameInput -< ()
x <- wGameLogic -< ev
finalSurf <- wGameOutput w -< x
returnA -< finalSurf
Run Code Online (Sandbox Code Playgroud)
main和中不需要进行任何更改run。哇!
就是这样!运行它,你应该能够左右移动矩形了!
一个巨大的代码块(我很好奇做同样事情的 C++ 程序需要多长时间):
{-|
03-GameLogic.hs: The final product!
-}
{-# LANGUAGE Arrows #-}
module Main where
import Prelude hiding ((.), id)
import Control.Wire
import Control.Arrow
import Control.Monad
import Data.Monoid
import qualified Graphics.UI.SDL as SDL
import qualified Control.Wire.Unsafe.Event as WE
{- Data types -}
-- | The unified datatype of game events
data GameEvent = MoveR
| MoveL
| NoEvent
deriving (Show, Eq)
-- | Make it Monoid so that game events can be combined
-- (Only applicable in this "game"!)
instance Monoid GameEvent where
mempty = NoEvent
-- | Simultaneously moving left and right is just nothing
MoveR `mappend` MoveL = NoEvent
MoveL `mappend` MoveR = NoEvent
-- | NoEvent is the identity
NoEvent `mappend` x = x
x `mappend` NoEvent = x
x `mappend` y
-- | Make sure identical events return same events
| x == y = x
-- | Otherwise, no event
| otherwise = NoEvent
{- Wire Utilities -}
-- | Make a stateless filter wire
mkFW_ :: (Monad m, Monoid e) => (a -> Bool) -> Wire s e m [a] [a]
mkFW_ f = mkSF_ $ filter f
-- -- | Make a stateful wire from a chained stateful function and initial value
-- -- The function (a -> b -> a) takes in an old state /a/, and returns state
-- -- transition function (b -> a).
mkSW_ :: (Monad m, Monoid e) => b -> (b->a->b) -> Wire s e m a b
mkSW_ b0 f = mkSFN $ g b0
where
g
| 归档时间: |
|
| 查看次数: |
1448 次 |
| 最近记录: |