Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main(main) where
- import Graphics.Gloss
- import Graphics.Gloss.Data.ViewPort
- width, height, offset :: Int
- width = 300
- height = 300
- offset = 100
- window :: Display
- window = InWindow "very fun game i promise" (width, height) (offset, offset)
- background :: Color
- background = black
- -- använd translate-funktionen för att flytta.
- render :: Game -> Picture -- ^ The game state to render -> Picture of game state
- render game = pictures [player, monster, walls]
- where
- player = uncurry translate (playLoc game) $ color playerColor $ rectangleSolid 10 10
- playerColor = red
- monster = uncurry translate (monsterLoc game) $ color monsterColor $ circleSolid 10
- monsterColor = blue
- wall :: Float -> Float -> Float -> Float -> Picture
- wall offhorizontal offvertical x y =
- translate offhorizontal offvertical $
- color wallColor $
- rectangleSolid x y
- wallColor = greyN 0.5
- walls = pictures [wall 0 150 300 10, wall 0 (-150) 300 10,
- wall 150 0 10 300, wall (-150) (0) 10 300]
- data Game = Game { playLoc :: (Float, Float) -- player location
- , monsterLoc :: (Float, Float) -- monster (x, y) location
- , monsterVel :: (Float, Float) -- monster (x, y) velocity
- }
- -- | Detect a collision with one of the side walls. Upon collisions,
- -- update the velocity of the ball to bounce it off the wall.
- type Radius = Float
- type Position = (Float, Float)
- -- | Given position and radius of the ball, return whether a collision occurred.
- wallCollision :: Position -> Radius -> Bool
- wallCollision (_, y) radius = topCollision || bottomCollision
- where
- topCollision = y - radius <= -fromIntegral width / 2
- bottomCollision = y + radius >= fromIntegral width / 2
- wallBounce :: Game -> Game
- wallBounce game = game { monsterVel = (vx, vy') }
- where
- -- Radius. Use the same thing as in `render`.
- radius = 10
- -- The old velocities.
- (vx, vy) = monsterVel game
- vy' = if wallCollision (monsterLoc game) radius
- then
- -- Update the velocity.
- -vy
- else
- -- Do nothing. Return the old velocity.
- vy
- -- Seconds since last update -> initial game state -> updated game state
- moveMonster :: Float -> Game -> Game
- moveMonster seconds game = game { monsterLoc = (x', y') }
- where
- -- Old locations and velocities.
- (x, y) = monsterLoc game
- (vx, vy) = monsterVel game
- -- New locations.
- x' = x + vx * seconds * 10
- y' = y + vy * seconds * 10
- initialState :: Game
- initialState = Game
- { playLoc = 0 --playLoc = players location
- , monsterLoc = (100, 100)
- , monsterVel = (0, 1)
- } -- deriving Show
- -- | Number of frames to show per second.
- fps :: Int
- fps = 60
- update :: ViewPort -> Float -> Game -> Game
- update _ seconds = wallBounce . moveMonster seconds
- main :: IO ()
- main = simulate window background fps initialState render update
- {-
- main :: IO ()
- main = animate window background frame
- where
- frame :: Float -> Picture
- frame seconds = render $ moveMonster seconds initialState
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement