Haskell with Gloss を使用して単純なサイモン ゲームを実装しています。現時点では、たとえば、最初の 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
、プログラムが短いアニメーションを表示し、入力を待ってそれに応じて動作するようにする方法はありますか?