我正在使用Haskell with Gloss实现一个简单的Simon游戏。我希望,此刻,例如,在前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
结合起来,这样我的程序会显示一个简短的动画,然后然后等待输入并采取相应的行动?
答案 0 :(得分:1)
比较animate
和play
(以及simulate
)的类型。
docs/Graphics-Gloss。
animate
是play
的简化版本,您可以忽略任何输入,并忽略动画的任何先前状态。因此,让您的输入处理程序检查您是否在前4秒内完成,并让您的步骤功能执行相同操作。