Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {- | First moves the cars, generates new cars
- -- and then resolves collision.
- -- Split up in a lot of functions for clarity.
- -}
- step :: Float -> Frogger -> Frogger
- step _ frogger@(Frogger _ _ _ _ _ time _) -- time is 'amount of steps'
- = resolveCollisions $ move (frogger { time = time + 1 })
- -- | Calls the function that moves cars in lanes
- -- and generates new cars with the correct args.
- move :: Frogger -> Frogger
- move frogger@(Frogger width _ lanes _ gen time _)
- = let (newLanes, gen1) = moveLanes lanes (length lanes - 1)
- gen width (round time)
- in frogger { lanes = newLanes, rg = gen1 }
- ...
- -- Checks for a collision. Only checks if frog is alive.
- resolveCollisions :: Frogger -> Frogger
- resolveCollisions frogger@(Frogger _ frog lanes alive _ _ _)
- | alive && collision frog lanes = frogger { alive = False }
- | otherwise = frogger
- -- | Detects a collision.
- collision :: Cell -> [Lane] -> Bool
- collision (row, col) lanes
- | laneIndex < 0 || laneIndex >= length lanes = False -- Frog is on grass.
- | otherwise = or [ col >= column car && col < column car+cLength car
- | car <- cars $ lanes!!laneIndex ]
- where
- laneIndex = row - grassAmount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement