-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
168 lines (139 loc) · 4.58 KB
/
game.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
local Object = require "libraries/classic"
local Bunny = require "bunny"
local CarrotManager = require "carrot_control/carrotmanager"
local CloudManager = require "cloud_control/cloudmanager"
local GameOverScreen = require "gui/gameoverscreen"
local Game = Object:extend()
Game.floor = love.graphics.getHeight()/5*4
function Game:new(mainMenu)
self.cloudManager = CloudManager(self)
self.carrotManager = CarrotManager(self)
self.bunny = Bunny(self)
self.mainMenu = mainMenu
self.gameOver = false
self.speed = 100
self.spawnTimer = 5
self.spawnFrequency = (100/self.speed)*5
self.addScore = false
self.score = 0
self.highScore = 0
if love.filesystem.getInfo("highScoreFile.txt") ~= nil then
content, size = love.filesystem.read("highScoreFile.txt")
self.highScore = tonumber(content)
end
self.gameOverScreen = GameOverScreen(self)
end
function Game:update(dt)
if self.gameOver == false then
self.cloudManager:update(dt)
self.carrotManager:update(dt)
self:handleAddScore()
end
love.mouse.setVisible(self.gameOver)
self.bunny:update(dt)
self.gameOver = self:checkCollision()
if self.gameOver then
self.spawnTimer = 0
self:saveHighScore()
self.gameOverScreen:update()
end
self:handleSpawnTimer(dt)
end
function Game:draw()
love.graphics.setBackgroundColor(135/255, 206/255, 235/255, 1)
self:drawFloor()
self.cloudManager:draw()
self.carrotManager:draw()
self.bunny:draw()
local score_y = love.graphics.getHeight()/25
love.graphics.printf(
self.score,
font,
-love.graphics.getWidth()/25,
score_y,
love.graphics.getWidth(),
"right"
)
if self.gameOver then
self.gameOverScreen:draw()
end
end
function Game:checkCollision()
local bunnyLeft = self.bunny.x
local bunnyRight = self.bunny.x + self.bunny.width
local bunnyTop = self.bunny.y
local bunnyBottom = self.bunny.y + self.bunny.height
for i=1, #self.carrotManager.carrotTable do
local carrotLeft = self.carrotManager.carrotTable[i].x
local carrotRight = self.carrotManager.carrotTable[i].x + self.carrotManager.carrotTable[i].width
local carrotTop = self.carrotManager.carrotTable[i].y
local carrotBottom = self.carrotManager.carrotTable[i].y + self.carrotManager.carrotTable[i].height
--early returns when bunny collides with carrot
if bunnyRight >= carrotLeft
and bunnyLeft <= carrotRight
and bunnyBottom >= carrotTop
and bunnyTop <= carrotBottom then
return true
end
--otherwise early returns if this carrot is the first carrot to the
--right of bunny
if carrotLeft > bunnyRight then
return false
end
end
return false
end
function Game:handleSpawnTimer(dt)
if self.spawnTimer >= self.spawnFrequency then
self.spawnTimer = 0
self.carrotManager:spawnCarrot()
end
self.spawnTimer = self.spawnTimer + dt
self.speed = math.min(self.speed + 2 * dt, 350)
self.spawnFrequency = (100/self.speed)*5
end
function Game:saveHighScore()
if self.score > self.highScore then
self.highScore = self.score
if love.filesystem.getInfo("highScoreFile.txt") == nil then
love.filesystem.newFile("highScoreFile.txt")
love.filesystem.write("highScoreFile.txt", tostring(self.highScore))
else
love.filesystem.write("highScoreFile.txt", tostring(self.highScore))
end
end
end
function Game:handleAddScore()
for i = 1, #self.carrotManager.carrotTable do
local bunnyLeft = self.bunny.x
local bunnyRight = self.bunny.x + self.bunny.width
local carrotRight = self.carrotManager.carrotTable[i].x + self.carrotManager.carrotTable[i].width
if bunnyLeft <= carrotRight and bunnyRight >= carrotRight then
self.addScore = true
end
if bunnyLeft > carrotRight and self.addScore then
self.score = self.score + 1
self.addScore = false
end
end
end
function Game:drawFloor()
love.graphics.setColor(72/255, 111/255, 56/255, 1)
love.graphics.rectangle(
"fill",
0,
self.floor,
love.graphics.getWidth(),
self.floor
)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setColor(0, 0, 0, 1)
love.graphics.line(
0,
self.floor,
love.graphics.getWidth(),
self.floor
)
love.graphics.setColor(1, 1, 1, 1)
end
return Game