-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
404 lines (378 loc) · 11.3 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
game = {}
function game.load()
-- background init
game.clock = 0
game.gameover_time = 0
game.boss_dt = 0
game.boss_bullets = {}
-- enemy init
game.enemy_size = imgs["enemy"]:getWidth()
game.enemies = {}
game.enemy_dt = 0
game.enemy_rate = 2
game.enemy_bullets = {}
-- boss
game.num_bosses = 5
game.bosses = {}
-- player init
game.player_size = imgs["player"]:getWidth()
game.playerx = (160/2)*scale
game.playery = (144-12)*scale
-- bullet init
game.ammo = 10
game.recharge_dt = 0
game.recharge_rate = 1
game.bullet_size = imgs["bullet"]:getWidth()
game.bullets = {}
-- shields init
game.num_shields = 2
game.shield_dt = 0
game.shields = {}
game.player_has_shield = false
-- triple bullet powerup (tbp)
game.num_tbps = 2
game.tbp_dt = 0
game.tbps = {}
game.player_has_tbp = false
-- info init
game.score = 0
end
function game.draw()
-- Draw moving background
for i = 0,4 do
for j = -1,4 do
love.graphics.draw(imgs["background"],
i*32*scale,
(j+game.clock%1)*32*scale,
0,scale,scale)
end
end
-- Draw enemies
for _,v in ipairs(game.enemies) do
love.graphics.draw(imgs["enemy"],
v.x,v.y,
0,scale,scale,
game.enemy_size/2,game.enemy_size/2)
if debug then love.graphics.circle("line",v.x,v.y,(game.enemy_size/2)*scale) end
end
-- Draw bosses (just a triple enemy)
for i = -1,1 do
for _,v in ipairs(game.bosses) do
love.graphics.draw(imgs["enemy"],
v.x+(game.enemy_size*i),v.y,
0,scale,scale,
game.enemy_size/2,game.enemy_size/2)
if debug then love.graphics.circle("line",v.x,v.y,game.enemy_size/2*scale) end
end
end
-- Draw player
love.graphics.draw(imgs["player"],
game.playerx,game.playery,
0,scale,scale,
game.player_size/2,game.player_size/2)
if debug then love.graphics.circle("line",game.playerx,game.playery,game.player_size/2*scale) end
if game.player_has_shield then
love.graphics.setColor(255,0,0)
for i=0,1 do
love.graphics.circle("line",game.playerx,game.playery,(game.player_size/2 + i)*scale)
end
love.graphics.setColor(255,255,255)
end
-- Draw game.bullets
for _,v in ipairs(game.bullets) do
love.graphics.draw(imgs["bullet"],
v.x,v.y,
0,scale,scale,
game.bullet_size/2,game.bullet_size/2)
if debug then love.graphics.circle("line",v.x,v.y,game.bullet_size/2*scale) end
end
-- Draw enemy bullets
for _,v in ipairs(game.enemy_bullets) do
love.graphics.draw(imgs["bullet"],
v.x,v.y,
0,scale,scale,
game.bullet_size/2,game.bullet_size/2)
if debug then love.graphics.circle("line",v.x,v.y,game.bullet_size/2*scale) end
end
-- Draw boss bullets
for _,v in ipairs(game.boss_bullets) do
love.graphics.draw(imgs["bullet"],
v.x,v.y,
0,scale,scale,
game.bullet_size/2,game.bullet_size/2)
if debug then love.graphics.circle("line",v.x,v.y,game.bullet_size/2*scale) end
end
-- Draw shields
for _,v in ipairs(game.shields) do
love.graphics.setColor(0,0,0)
love.graphics.circle("fill",v.x,v.y,game.bullet_size/2*scale)
love.graphics.setColor(255,255,255)
end
-- Draw tbps
for _,v in ipairs(game.tbps) do
love.graphics.setColor(255,255,255)
love.graphics.circle("fill",v.x,v.y,game.bullet_size/2*scale)
love.graphics.setColor(255,255,255)
end
-- Draw game info
love.graphics.setColor(fontcolor.r,fontcolor.g,fontcolor.b)
love.graphics.printf(
"score:"..game.score..
" ammo:"..game.ammo,
0,0,love.graphics.getWidth(),"center")
if debug then love.graphics.print(
"enemies: "..#game.enemies..
"\nbullets:"..#game.bullets..
"\nenemy_rate:"..game.enemy_rate..
"\nFPS:"..love.timer.getFPS(),
0,14*scale) end
love.graphics.setColor(255,255,255)
end
-- Distance formula.
function game.dist(x1,y1,x2,y2)
return math.sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
end
function game.update(dt)
-- clock for background
game.clock = game.clock + dt
-- Update game.enemies
game.enemy_dt = game.enemy_dt + dt
-- Update game.bosses
game.boss_dt = game.boss_dt + dt
-- Update shields
game.shield_dt = game.shield_dt + dt
-- Update tbps
game.tbp_dt = game.tbp_dt + dt
-- Enemy spawn
if game.enemy_dt > game.enemy_rate then
game.enemy_dt = game.enemy_dt - game.enemy_rate
game.enemy_rate = game.enemy_rate - 0.01 * game.enemy_rate
local enemy = {}
enemy.x = math.random((8)*scale,(160-8)*scale)
enemy.y = -game.enemy_size
enemy.num_bullets = 1
table.insert(game.enemies,enemy)
end
-- Boss spawn
if game.boss_dt > 5 then
local boss = {}
boss.x = math.random((24)*scale,(160-24)*scale)
boss.y = -game.enemy_size
boss.num_bullets = 1
table.insert(game.bosses,boss)
game.num_bosses = game.num_bosses - 1
game.boss_dt = 0
end
-- Update enemy
for ei,ev in ipairs(game.enemies) do
ev.y = ev.y + 70*dt*scale
if ev.y > 20*scale and ev.num_bullets > 0 then
local ebullet = {}
ebullet.x = ev.x
ebullet.y = ev.y
table.insert(game.enemy_bullets,ebullet)
ev.num_bullets = ev.num_bullets - 1
end
if ev.y > 144*scale then
table.remove(game.enemies,ei)
end
-- If a player gets too close to enemy
if game.dist(game.playerx,game.playery,ev.x,ev.y) < (12+8)*scale then
if game.player_has_shield then
game.player_has_shield = false
table.remove(game.enemies,ei)
else
game.gameover_time = game.clock
gameover.load()
state = "gameover"
end
end
end
-- Update boss
for ei,ev in ipairs(game.bosses) do
ev.y = ev.y + 70*dt*scale
if ev.y > 20*scale and ev.num_bullets > 0 then
for i=-1,1 do
local bbullet = {}
bbullet.x = ev.x+(i*game.enemy_size)
bbullet.y = ev.y
table.insert(game.boss_bullets,bbullet)
end
ev.num_bullets = ev.num_bullets - 1
end
if ev.y > 144*scale then
table.remove(game.bosses,ei)
end
-- If a player gets too close to boss
if game.dist(game.playerx,game.playery,ev.x,ev.y) < (12+8)*scale then
if game.player_has_shield then
game.player_has_shield = false
table.remove(game.bosses,ei)
else
game.gameover_time = game.clock
gameover.load()
state = "gameover"
end
end
end
-- Spawn shields
if game.num_shields > 0 and game.shield_dt > 10 and not game.player_has_shield then
local shield = {}
shield.x = math.random((24)*scale,(160-24)*scale)
shield.y = -game.enemy_size
table.insert(game.shields,shield)
game.num_shields = game.num_shields - 1
game.shield_dt = 0
end
-- Update shields
for ei,ev in ipairs(game.shields) do
ev.y = ev.y + 70*dt*scale
if ev.y > 144*scale then
table.remove(game.shields,ei)
end
-- If a player gets too close to shield
if game.dist(game.playerx,game.playery,ev.x,ev.y) < (12+8)*scale then
game.player_has_shield = true
table.remove(game.shields,ei)
end
end
-- Spawn tbps
if game.num_tbps > 0 and game.tbp_dt > 15 and not game.player_has_tbp then
local tbp = {}
tbp.x = math.random((24)*scale,(160-24)*scale)
tbp.y = -game.enemy_size
table.insert(game.tbps,tbp)
game.num_tbps = game.num_tbps - 1
game.tbp_dt = 0
end
-- Update tbps
for ei,ev in ipairs(game.tbps) do
ev.y = ev.y + 70*dt*scale
if ev.y > 144*scale then
table.remove(game.tbps,ei)
end
-- If a player gets too close to tbp
if game.dist(game.playerx,game.playery,ev.x,ev.y) < (12+8)*scale then
game.player_has_tbp = true
table.remove(game.tbps,ei)
end
end
-- Update player movement
if love.keyboard.isDown("right") then
game.playerx = game.playerx + 100*dt*scale
end
if love.keyboard.isDown("left") then
game.playerx = game.playerx - 100*dt*scale
end
if love.keyboard.isDown("up") then
game.playery = game.playery - 100*dt*scale
end
if love.keyboard.isDown("down") then
game.playery = game.playery + 100*dt*scale
end
-- Keep the player on the map
if game.playerx > 160*scale then
game.playerx = 160*scale
end
if game.playerx < 0 then
game.playerx = 0
end
if game.playery < 0 then
game.playery = 0
end
if game.playery > 144*scale then
game.playery = 144*scale
end
-- Update bullets
for bi,bv in ipairs(game.bullets) do
if game.player_has_tbp then
bv.x = bv.x + 40*dt*bv.tbp_id
end
bv.y = bv.y - 100*dt*scale
if bv.y < 0 then
table.remove(game.bullets,bi)
end
-- Update bullets with game.enemies
for ei,ev in ipairs(game.enemies) do
if game.dist(bv.x,bv.y,ev.x,ev.y) < (2+8)*scale then
game.score = game.score + 1
table.remove(game.enemies,ei)
table.remove(game.bullets,bi)
end
end
-- Update bullets with game.bosses
for ei,ev in ipairs(game.bosses) do
if game.dist(bv.x,bv.y,ev.x,ev.y) < (2+8)*scale then
game.score = game.score + 3 -- extra points for boss
table.remove(game.bosses,ei)
table.remove(game.bullets,bi)
end
end
end
-- Update enemy bullets
for bi,bv in ipairs(game.enemy_bullets) do
bv.y = bv.y + 100*dt*scale
if bv.y > 144*scale then
table.remove(game.enemy_bullets,bi)
end
-- see if enemy bullet hit player
if game.dist(game.playerx,game.playery,bv.x,bv.y) < (12+8)*scale then
if game.player_has_shield then
game.player_has_shield = false
table.remove(game.enemy_bullets,bi)
else
game.gameover_time = game.clock
gameover.load()
state = "gameover"
end
end
end
-- Update boss bullets
for bi,bv in ipairs(game.boss_bullets) do
bv.y = bv.y + 120*dt*scale -- faster bullets for bosses
if bv.y > 144*scale then
table.remove(game.boss_bullets,bi)
end
-- see if boss bullet hit player
if game.dist(game.playerx,game.playery,bv.x,bv.y) < (12)*scale then
if game.player_has_shield then
game.player_has_shield = false
table.remove(game.boss_bullets,bi)
else
game.gameover_time = game.clock
gameover.load()
state = "gameover"
end
end
end
-- Update player ammunition
game.recharge_dt = game.recharge_dt + dt
if game.recharge_dt > game.recharge_rate then
game.recharge_dt = game.recharge_dt - game.recharge_rate
game.ammo = game.ammo + 1
if game.ammo > 10 then
game.ammo = 10
end
end
end
function game.keypressed(key)
-- Shoot a bullet
if key == " " and game.ammo > 0 then
love.audio.play(shoot)
game.ammo = game.ammo - 1
if game.player_has_tbp then
for i=-1,1 do
local bullet = {}
bullet.x = game.playerx
bullet.y = game.playery
bullet.tbp_id = i
table.insert(game.bullets,bullet)
end
else
local bullet = {}
bullet.x = game.playerx
bullet.y = game.playery
table.insert(game.bullets,bullet)
end
end
end