packages feed

breakout 0.0.1 → 0.0.2

raw patch · 2 files changed

+19/−16 lines, 2 files

Files

breakout.cabal view
@@ -1,5 +1,5 @@ Name:                breakout-Version:             0.0.1+Version:             0.0.2 Cabal-Version:       >= 1.2 Synopsis:            A simple Breakout game implementation. Category:            game
src/breakout.hs view
@@ -8,6 +8,7 @@ import Graphics.UI.HaskGame.Vector2(Vector2(..)) import Graphics.UI.HaskGame.Color(Color(..)) import qualified Graphics.UI.SDL as SDL+import qualified Graphics.UI.SDL.Video as SDL.Video import Graphics.UI.SDL(Rect(..)) import qualified System.IO as IO import qualified Control.Exception as Exception@@ -24,8 +25,7 @@  data QuitException = QuitException   deriving (Show, Typeable)-instance Exception QuitException where-  -- Nothing+instance Exception QuitException  displayWidth :: Int displayHeight :: Int@@ -61,8 +61,15 @@ initialPlayerWidth :: Int initialPlayerWidth = 80 initialBallSpeed :: Vector2 Double-initialBallSpeed = Vector2 1 (-2)+initialBallSpeed = Vector2 3 (-2) +data GameState = GameState {+          gsPlayerPos :: Int+        , gsPlayerWidth :: Int+        , gsBall :: Maybe (Vector2 Double, Vector2 Double)+        , gsBrickPositions :: [Vector2 Int]+        }+ playerRect :: GameState -> Rect playerRect GameState{gsPlayerPos=playerPos                     ,gsPlayerWidth=playerWidth} =@@ -75,13 +82,6 @@ capPlayerRange curPlayerWidth = capRange halfWidth (displayWidth - halfWidth)     where halfWidth = curPlayerWidth `div` 2 -data GameState = GameState {-          gsPlayerPos :: Int-        , gsPlayerWidth :: Int-        , gsBall :: Maybe (Vector2 Double, Vector2 Double)-        , gsBrickPositions :: [Vector2 Int]-        }- initGameState :: GameState initGameState = GameState 0 initialPlayerWidth Nothing initBrickPositions @@ -109,9 +109,10 @@  handleEvents :: [SDL.Event] -> IO () handleEvents events = do-  forM_ events $ \event -> case event of -                                   SDL.Quit -> Exception.throwIO QuitException-                                   _ -> return ()+  forM_ events $+    \event -> case event of+       SDL.Quit -> Exception.throwIO QuitException+       _ -> return ()  draw :: SDL.Surface -> GameState -> IO () draw display gs = do@@ -195,14 +196,16 @@     gameState <- get     liftIO $ sdlIteration display gameState     modify . atGsPlayerPos . const . capPlayerRange (gsPlayerWidth gameState) $ mouseX-    modify nextGameState-    when leftPressed $ do+    ball <- gsBall `fmap` get+    when (leftPressed && isNothing ball) $ do       ballPos <- ballPosition `fmap` get       modify . atGsBall . const $ Just (ballPos, initialBallSpeed)+    modify nextGameState  main :: IO () main = do   HaskGame.withInit $ do+    SDL.Video.showCursor False     display <- HaskGame.setVideoMode displaySize colordepth     mainLoop display       `Exception.catch`