Views
No views yet
1Filename: src/Game.hs
2Status: modified
3Additions: 21
4Deletions: 8
5Total changes: 29
6Code changes:
7@@ -24,7 +24,8 @@ data Man =
8 , speedY :: Float
9 , posX :: Float
10 , posY :: Float
11- } deriving (Show)
12+ }
13+ deriving (Show)
14
15 -- We need to store game state
16 data Game =
17@@ -51,6 +52,9 @@ windowSizeY = 700
18 manSize :: Float
19 manSize = 16
20
21+topGrassSize :: Float
22+topGrassSize = 22
23+
24 grassSize :: Float
25 grassSize = 150
26
27@@ -61,7 +65,7 @@ skyWidth :: Float
28 skyWidth = 2048
29
30 bottomBorder :: Float
31-bottomBorder = - windowSizeY / 2 + grassSize
32+bottomBorder = -windowSizeY / 2 + grassSize
33
34 obstacleWidth :: Float
35 obstacleWidth = 30
36@@ -70,7 +74,7 @@ obstacleHeight :: Float
37 obstacleHeight = 100
38
39 obstacleY :: Float
40-obstacleY = bottomBorder + obstacleHeight / 2 - 22 -- | 22 accounts for top-most layer of grass
41+obstacleY = bottomBorder + obstacleHeight / 2 - topGrassSize -- | 22 accounts for top-most layer of grass
42
43 obstaclePic :: Picture
44 obstaclePic = color red $ rectangleSolid obstacleWidth obstacleHeight
45@@ -82,7 +86,7 @@ initTranslateGrassX :: Float
46 initTranslateGrassX = grassWidth / 2 - windowSizeX / 2 -- starting from the left border of the picture
47
48 initTranslateGrassY :: Float
49-initTranslateGrassY = - windowSizeY / 2 + grassSize / 2
50+initTranslateGrassY = -windowSizeY / 2 + grassSize / 2
51
52 initTranslateSky :: Float
53 initTranslateSky = skyWidth / 2 - windowSizeX / 2 -- starting from the left border of the picture
54@@ -99,10 +103,14 @@ getSprite name = "sprites/" ++ name ++ ".bmp"
55 -- manPic :: Picture
56 manPic :: Picture
57 manPic = unsafePerformIO . loadBMP . getSprite $ "man"
58--- manPic = color black $ rectangleSolid manSize manSize
59
60+-- manPic = color black $ rectangleSolid manSize manSize
61 grassPic :: Picture
62-grassPic = translate 0 initTranslateGrassY (unsafePerformIO . loadBMP . getSprite $ "grass")
63+grassPic =
64+ translate
65+ 0
66+ initTranslateGrassY
67+ (unsafePerformIO . loadBMP . getSprite $ "grass")
68
69 skyPic :: Picture
70 skyPic = unsafePerformIO . loadBMP . getSprite $ "sky"
71@@ -152,14 +160,18 @@ checkCrush game =
72 obstaclePosX = head (obstacles game) + obstaclesTranslation game
73 in (playerPosX + manSize / 2) >= (obstaclePosX - obstacleWidth / 2) &&
74 playerPosX <= (obstaclePosX + obstacleWidth / 2) &&
75- playerPosY <= (obstacleY + obstacleHeight - 22) -- | See obstacleY to understand what 22 is
76+ playerPosY <= (obstacleY + obstacleHeight / 2 + topGrassSize)
77
78 updateGameSate :: Game -> GameState
79 updateGameSate game
80 | gameState game == Over = Over
81 | checkCrush game = Over
82 | otherwise = gameState game
83
84+
85+accelerate :: Float
86+accelerate = 0.001
87+
88 updateGame :: Float -> Game -> Game
89 updateGame seconds game =
90 game
91@@ -169,6 +181,7 @@ updateGame seconds game =
92 , obstacles = nextObstacles
93 , obstaclesTranslation = nextObstaclesTranslation
94 , gameState = updateGameSate game
95+ , gameSpeed = gameSpeed game + accelerate
96 }
97 where
98 nextManPosY
99@@ -185,7 +198,7 @@ updateGame seconds game =
100 | otherwise = backgrounds game
101 nextObstaclesTranslation
102 | obstaclesTranslation game <
103- -windowSizeX / 2 - 100 - head (obstacles game) = 0
104+ -windowSizeX / 2 - head (obstacles game) = 0
105 | otherwise = obstaclesTranslation game - gameSpeed game
106 nextObstacles
107 | nextObstaclesTranslation == 0 = drop 1 $ obstacles game