Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. module Main(main) where
  2.  
  3. import Graphics.Gloss
  4. import Graphics.Gloss.Data.ViewPort
  5.  
  6. width, height, offset :: Int
  7. width = 300
  8. height = 300
  9. offset = 100
  10.  
  11. window :: Display
  12. window = InWindow "very fun game i promise" (width, height) (offset, offset)
  13.  
  14. background :: Color
  15. background = black
  16.  
  17. -- använd translate-funktionen för att flytta.
  18. render :: Game -> Picture -- ^ The game state to render -> Picture of game state
  19. render game = pictures [player, monster, walls]
  20. where
  21. player = uncurry translate (playLoc game) $ color playerColor $ rectangleSolid 10 10
  22. playerColor = red
  23.  
  24. monster = uncurry translate (monsterLoc game) $ color monsterColor $ circleSolid 10
  25. monsterColor = blue
  26.  
  27. wall :: Float -> Float -> Float -> Float -> Picture
  28. wall offhorizontal offvertical x y =
  29. translate offhorizontal offvertical $
  30. color wallColor $
  31. rectangleSolid x y
  32.  
  33. wallColor = greyN 0.5
  34. walls = pictures [wall 0 150 300 10, wall 0 (-150) 300 10,
  35. wall 150 0 10 300, wall (-150) (0) 10 300]
  36.  
  37.  
  38. data Game = Game { playLoc :: (Float, Float) -- player location
  39. , monsterLoc :: (Float, Float) -- monster (x, y) location
  40. , monsterVel :: (Float, Float) -- monster (x, y) velocity
  41. }
  42.  
  43.  
  44. -- | Detect a collision with one of the side walls. Upon collisions,
  45. -- update the velocity of the ball to bounce it off the wall.
  46. type Radius = Float
  47. type Position = (Float, Float)
  48.  
  49. -- | Given position and radius of the ball, return whether a collision occurred.
  50. wallCollision :: Position -> Radius -> Bool
  51. wallCollision (_, y) radius = topCollision || bottomCollision
  52. where
  53. topCollision = y - radius <= -fromIntegral width / 2
  54. bottomCollision = y + radius >= fromIntegral width / 2
  55.  
  56. wallBounce :: Game -> Game
  57. wallBounce game = game { monsterVel = (vx, vy') }
  58. where
  59. -- Radius. Use the same thing as in `render`.
  60. radius = 10
  61.  
  62. -- The old velocities.
  63. (vx, vy) = monsterVel game
  64.  
  65. vy' = if wallCollision (monsterLoc game) radius
  66. then
  67. -- Update the velocity.
  68. -vy
  69. else
  70. -- Do nothing. Return the old velocity.
  71. vy
  72.  
  73.  
  74.  
  75. -- Seconds since last update -> initial game state -> updated game state
  76. moveMonster :: Float -> Game -> Game
  77. moveMonster seconds game = game { monsterLoc = (x', y') }
  78. where
  79. -- Old locations and velocities.
  80. (x, y) = monsterLoc game
  81. (vx, vy) = monsterVel game
  82.  
  83. -- New locations.
  84. x' = x + vx * seconds * 10
  85. y' = y + vy * seconds * 10
  86.  
  87.  
  88. initialState :: Game
  89. initialState = Game
  90. { playLoc = 0 --playLoc = players location
  91. , monsterLoc = (100, 100)
  92. , monsterVel = (0, 1)
  93. } -- deriving Show
  94.  
  95. -- | Number of frames to show per second.
  96. fps :: Int
  97. fps = 60
  98.  
  99. update :: ViewPort -> Float -> Game -> Game
  100. update _ seconds = wallBounce . moveMonster seconds
  101.  
  102. main :: IO ()
  103. main = simulate window background fps initialState render update
  104.  
  105. {-
  106. main :: IO ()
  107. main = animate window background frame
  108. where
  109. frame :: Float -> Picture
  110. frame seconds = render $ moveMonster seconds initialState
  111. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement