Advertisement
Guest User

Tetris

a guest
Oct 8th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- | The Tetris game (main module)
  2. module Main where
  3. import ConsoleGUI       -- cabal install ansi-terminal
  4. --import CodeWorldGUI     -- cabal install codeworld-api
  5. import Shapes
  6.  
  7. --------------------------------------------------------------------------------
  8. -- * The code that puts all the piece together
  9.  
  10. main = runGame tetrisGame
  11.  
  12. tetrisGame = Game { startGame     = startTetris,
  13.                     stepGame      = stepTetris,
  14.                     drawGame      = drawTetris,
  15.                     gameInfo      = defaultGameInfo prop_Tetris,
  16.                     tickDelay     = defaultDelay,
  17.                     gameInvariant = prop_Tetris }
  18.  
  19. --------------------------------------------------------------------------------
  20. -- * The various parts of the Tetris game implementation
  21.  
  22.  
  23. -- | The state of the game
  24. data Tetris = Tetris (Vector,Shape) Shape [Shape]
  25. -- The state consists of three parts:
  26. --   * The position and shape of the falling piece
  27. --   * The well (the playing field), where the falling pieces pile up
  28. --   * An infinite supply of random shapes
  29.  
  30. -- ** Positions and sizes
  31.  
  32. type Vector = (Int,Int)
  33.  
  34. -- | The size of the well
  35. wellSize :: (Int,Int)
  36. wellSize = (wellWidth,wellHeight)
  37. wellWidth = 10
  38. wellHeight = 20
  39.  
  40. -- | Starting position for falling pieces
  41. startPosition :: Vector
  42. startPosition = (wellWidth `div` 2 - 1, 0)
  43.  
  44. -- | Vector addition
  45. vAdd :: Vector -> Vector -> Vector
  46. (x1,y1) `vAdd` (x2,y2) = (x1+x2,y1+y2)
  47.  
  48. -- | Move the falling piece into position
  49. place :: (Vector,Shape) -> Shape
  50. place (v,s) = shiftShape v s
  51.  
  52. -- | An invariant that startTetris and stepTetris should uphold
  53. prop_Tetris :: Tetris -> Bool
  54. prop_Tetris (Tetris (_,s) w _) = prop_Shape s && (shapeSize w == wellSize)
  55.  
  56. -- | Add black walls around a shape
  57. addWalls :: Shape -> Shape
  58. addWalls s = iterate appendLeft s !! 4
  59.   where appendLeft (S s') = rotateShape . S $ map (\r -> Just Black:r) s'
  60.  
  61. -- | Visualize the current game state. This is what the user will see
  62. -- when playing the game.
  63. drawTetris :: Tetris -> Shape
  64. drawTetris (Tetris (v,p) w _) = addWalls $ (shiftShape v p) `combine` w
  65.  
  66. move :: Vector -> Tetris -> Tetris
  67. move v (Tetris (v0,p) w ts) = Tetris (v `vAdd` v0,p) w ts
  68.  
  69. tick :: Tetris -> Maybe (Int,Tetris)
  70. tick t = Just (0, move (0,1) t)
  71.  
  72. -- | The initial game state
  73. startTetris :: [Double] -> Tetris
  74. startTetris rs = Tetris (startPosition,shape1) (emptyShape wellSize) supply
  75.   where
  76.     shape1:supply = repeat (allShapes!!1) -- incomplete !!!
  77.  
  78. -- | React to input. The function returns 'Nothing' when it's game over,
  79. -- and @'Just' (n,t)@, when the game continues in a new state @t@.
  80. stepTetris :: Action -> Tetris -> Maybe (Int,Tetris)
  81. stepTetris Tick t = tick t
  82. stepTetris MoveLeft t = Just (0, move (-1,0) t)
  83. stepTetris MoveRight t = Just (0, move (1,0) t)
  84.  
  85. collision :: Tetris -> Bool
  86. collision (Tetris ((x,y),p) w _)
  87.   | x < 0 || x + wShape p > wellWidth = True
  88.   | x + hShape p > wellHeight         = True
  89.   | place ((x,y),p) `overlaps` w      = True
  90.   | otherwise                         = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement