Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {- | First moves the cars, generates new cars
  2. -- and then resolves collision.
  3. -- Split up in a lot of functions for clarity.
  4. -}
  5. step :: Float -> Frogger -> Frogger
  6. step _ frogger@(Frogger _ _ _ _ _ time _)  -- time is 'amount of steps'
  7.     = resolveCollisions $ move (frogger { time = time + 1 })
  8.  
  9. -- | Calls the function that moves cars in lanes
  10. -- and generates new cars with the correct args.
  11. move :: Frogger -> Frogger
  12. move frogger@(Frogger width _ lanes _ gen time _)
  13.     = let (newLanes, gen1) = moveLanes lanes (length lanes - 1)
  14.                                        gen width (round time)
  15.        in frogger { lanes = newLanes, rg = gen1 }
  16.  
  17. ...
  18.  
  19. -- Checks for a collision. Only checks if frog is alive.
  20. resolveCollisions :: Frogger -> Frogger
  21. resolveCollisions frogger@(Frogger _ frog lanes alive _ _ _)
  22.     | alive && collision frog lanes = frogger { alive = False }
  23.     | otherwise                     = frogger
  24.  
  25. -- | Detects a collision.
  26. collision :: Cell -> [Lane] -> Bool
  27. collision (row, col) lanes
  28.     | laneIndex < 0 || laneIndex >= length lanes = False  -- Frog is on grass.
  29.     | otherwise = or [ col >= column car && col < column car+cLength car
  30.                         | car <- cars $ lanes!!laneIndex ]
  31.   where
  32.     laneIndex = row - grassAmount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement