我正在使用带有 Gloss 的 Haskell 实现一个简单的西蒙游戏。目前,我希望,例如,在前 4 秒内,一些矩形会改变它们的颜色(或者简单地说显示一些动画),然后允许使用特定的键盘键输入改变颜色。
这是animate
用于动画的代码:
window :: Display
window = InWindow "Simon" (width, height) (offset, offset)
background :: Color
background = black
data SimonGame = Game {
rectangleGreen::Picture,
rectangleRed::Picture,
rectangleBlue::Picture,
rectangleYellow::Picture
} deriving Show
initialState :: SimonGame
initialState = Game
{ rectangleGreen = translate (-100) (0) $ color green $ rectangleSolid 60 60,
rectangleRed = translate (100) (0) $ color red $ rectangleSolid 60 60,
rectangleBlue = translate (0) (100) $ color blue $ rectangleSolid 60 60,
rectangleYellow = translate (0) (-100) $ color yellow $ rectangleSolid 60 60
}
render :: SimonGame -> Picture
render game = pictures
[ rectangleGreen game,
rectangleRed game,
rectangleBlue game,
rectangleYellow game
]
updateBoard :: Int-> SimonGame -> SimonGame
updateBoard seconds game
| sec_value == 1 = game {
rectangleGreen = translate (-100) (0) $ color white $ rectangleSolid 60 60,
rectangleRed = translate (100) (0) $ color red $ rectangleSolid 60 60,
rectangleBlue = translate (0) (100) $ color blue $ rectangleSolid 60 60,
rectangleYellow = translate (0) (-100) $ color yellow $ rectangleSolid 60 60
}
| sec_value == 2 = game {
rectangleGreen = translate (-100) (0) $ color green $ rectangleSolid 60 60,
rectangleRed = translate (100) (0) $ color white $ rectangleSolid 60 60,
rectangleBlue = translate (0) (100) $ color blue $ rectangleSolid 60 60,
rectangleYellow = translate (0) (-100) $ color yellow $ rectangleSolid 60 60
}
| sec_value == 3 = game {
rectangleGreen = translate (-100) (0) $ color green $ rectangleSolid 60 60,
rectangleRed = translate (100) (0) $ color red $ rectangleSolid 60 60,
rectangleBlue = translate (0) (100) $ color white $ rectangleSolid 60 60,
rectangleYellow = translate (0) (-100) $ color yellow $ rectangleSolid 60 60
}
| sec_value == 0 = game {
rectangleGreen = translate (-100) (0) $ color green $ rectangleSolid 60 60,
rectangleRed = translate (100) (0) $ color red $ rectangleSolid 60 60,
rectangleBlue = translate (0) (100) $ color blue $ rectangleSolid 60 60,
rectangleYellow = translate (0) (-100) $ color white $ rectangleSolid 60 60
}
| otherwise = game
where
sec_value = (seconds `mod` 4)
main :: IO ()
main = animate window background frame
where
frame :: Float -> Picture
frame seconds = render $ updateBoard (ceiling seconds) initialState
我知道使用这个函数play
我可以以某种方式实现一个逻辑,使用我实现的函数handleKeys
,它接受用户输入并改变游戏状态。
handleKeys :: Event -> SimonGame -> SimonGame
handleKeys (EventKey (Char 'a') _ _ _) game = game { rectangleGreen = translate (-100) (0) $ color white $ rectangleSolid 60 60 }
handleKeys (EventKey (Char 's') _ _ _) game = game { rectangleRed = translate (100) (0) $ color white $ rectangleSolid 60 60 }
handleKeys (EventKey (Char 'd') _ _ _) game = game { rectangleBlue = translate (0) (100) $ color white $ rectangleSolid 60 60 }
handleKeys (EventKey (Char 'f') _ _ _) game = game { rectangleYellow = translate (0) (-100) $ color white $ rectangleSolid 60 60 }
handleKeys _ game = game
有没有办法将函数play
与函数结合起来,animate
这样我的程序会显示一个简短的动画,然后等待输入并采取相应的行动?