-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
285 lines (231 loc) · 6.1 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- SUPER-AWESOME-GAME
local GS = require "hump.gamestate"
require "apple"
require "creep"
require "zombo"
-- SETTINGS
apple_popup_interval = 1 -- seconds
creep_popup_interval = 15 -- seconds
zombo_popup_interval = 10 -- seconds
creep_speed = 1
creep_speed_increment = 0.2
points_per_apple = 1
lives = 3
game_name = "Super Awesome Game"
-- GLOBALS
lives_left = lives
points = 0
creeps = {}
apples = {}
zombos = {}
-- hump stuff; declare different game states
menu = GS.new()
game = GS.new()
dead = GS.new()
over = GS.new()
paus = GS.new()
-- internal variables
start_time = love.timer.getTime()
last_apple_time = start_time
last_creep_time = start_time
last_zombo_time = start_time
-- MENU STATE
function menu:enter()
love.graphics.setCaption(game_name .. " - Menu")
love.graphics.setBackgroundColor(0, 0, 0)
love.audio.pause()
end
function menu:draw()
love.graphics.draw(steve_img, 100, 100)
love.graphics.print("This is you and you need these apples ->", 200, 120)
love.graphics.draw(apple_img, 630, 120)
love.graphics.draw(zombo_img, 100, 190)
love.graphics.print("These guys are BAD!", 200, 200)
love.graphics.draw(creep_img, 440, 195)
love.graphics.print("Use the mouse to move! \"P\" pauses the game!", 100, 300)
love.graphics.print("Press any key to start!", 100, 370)
end
function menu:keypressed(k)
if k == "q" or k == "escape" then
os.exit()
end
GS.switch(game)
end
-- GAME STATE
function game:enter()
love.graphics.setCaption(game_name .. " - Playing")
love.audio.play(music)
love.mouse.setVisible(false)
end
function game:draw()
draw_grass_background()
draw_info()
creeps_move_and_draw()
zombos_move_and_draw()
apples_draw()
-- draw the mouse cursor
love.graphics.draw(steve_img, mx, my)
end
function game:update()
mx = love.mouse.getX()
my = love.mouse.getY()
time = love.timer.getTime()
-- Add apple
if time - last_apple_time > apple_popup_interval then
last_apple_time = time
table.insert(apples, Apple.new())
end
-- Add creep
if time - last_creep_time > creep_popup_interval then
last_creep_time = time
table.insert(creeps, Creep.new(creep_speed))
creep_speed = creep_speed + creep_speed_increment
end
-- Add zombo
if time - last_zombo_time > zombo_popup_interval then
last_zombo_time = time
table.insert(zombos, Zombo.new())
end
if touching_apple() then
love.audio.stop(food)
love.audio.play(food)
points = points + points_per_apple
end
if touching_creep() or touching_zombo() then
love.audio.stop(music)
GS.switch(dead) -- switch to death screen
end
love.timer.sleep(10) -- sleep 10ms
end
function game:keypressed(k)
if k == "q" or k == "escape" then GS.switch(menu) end
if k == "p" then GS.switch(paus) end
end
-- PAUSE STATE
function paus:enter()
love.audio.setVolume(0.3)
love.graphics.setBackgroundColor(45, 45, 240)
end
function paus:draw()
love.graphics.setCaption("Super Awesome Game - Paused")
love.graphics.print("The Game is paused", 300, 270)
end
function paus:keypressed(k)
if k == "q" then GS.switch(menu)
love.audio.setVolume(1.0)
end
if k == "p" then GS.switch(game)
love.audio.setVolume(1.0)
end
end
function dead:enter()
love.audio.stop(pain)
love.audio.play(pain)
lives_left = lives_left - 1
if lives_left < 1 then
GS.switch(over)
end
-- clear the field
creeps = {}
apples = {}
zombos = {}
love.graphics.setBackgroundColor(240, 23, 23)
end
function dead:draw()
love.graphics.print("YOU HAVE DIED!.", 230, 200)
love.graphics.print("But you have " .. lives_left .. " lives left.", 230, 250)
love.graphics.print("Press any key to continue.", 230, 300)
end
function dead:keypressed(key)
GS.switch(game)
end
function over:enter()
love.graphics.setCaption(game_name .. " - Game over")
lives_left = lives
love.graphics.setBackgroundColor(0,0,0)
end
function over:draw()
love.graphics.print("GAME OVER!", 300, 300)
love.graphics.print("Score: " .. points, 300, 350)
end
function over:keypressed(key)
lives_left = lives
points = 0
creep_speed = 1
GS.switch(menu)
end
function love.load()
-- load background grass tile
bg = love.graphics.newImage("img/grass.png")
-- load images
steve_img = love.graphics.newImage("img/steve.png")
creep_img = love.graphics.newImage("img/creep.jpg")
zombo_img = love.graphics.newImage("img/zombo.png")
apple_img = love.graphics.newImage("img/apple.png")
-- load audio sources
pain = love.audio.newSource("audio/death.mp3", "static")
food = love.audio.newSource("audio/crunch.wav", "static")
music = love.audio.newSource("audio/bummer.mp3")
music:setLooping(true)
w = love.graphics.getWidth()
h = love.graphics.getHeight()
love.mouse.setPosition(math.floor(w/2)-32, math.floor(h/2)-32)
local f = love.graphics.newFont("font/font.ttf", 32)
love.graphics.setFont(f)
GS.registerEvents()
GS.switch(menu) -- go into "menu" first
end
-- HELPER FUNCTIONS
function draw_grass_background()
love.graphics.draw(bg, 0, 0)
love.graphics.draw(bg, 600, 0)
love.graphics.draw(bg, 0, 427)
love.graphics.draw(bg, 600, 427)
end
function draw_info()
love.graphics.print("FPS: " .. love.timer.getFPS(), 10, 10)
love.graphics.print("Lives: " .. lives_left, 350, 10)
love.graphics.print("Points: " .. points, 650, 10)
end
function touching_apple()
for i,v in ipairs(apples) do
if (math.abs(v.x()-(mx+32)) < 32) and (math.abs(v.y()-(my+32)) < 32) then
table.remove(apples, i)
return true
end
end
return false
end
function touching_creep()
for i,c in ipairs(creeps) do
if math.abs(c.x()-mx) < 25 and math.abs(c.y()-my) < 25 then
return true
end
end
return false
end
function touching_zombo()
for i,z in ipairs(zombos) do
if math.abs(z.x()-mx) < 32 and math.abs(z.y()-my) < 32 then
return true
end
end
return false
end
function zombos_move_and_draw()
for i, z in ipairs(zombos) do
z.draw()
z.move()
end
end
function creeps_move_and_draw()
for i,c in ipairs(creeps) do
c.draw()
c.move()
end
end
function apples_draw()
for i,a in ipairs(apples) do
a.draw()
end
end