-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
74 lines (58 loc) · 1.57 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
debug = true
local spaceship = require('spaceship')
local asteroids = require('asteroids')
local show_score = false
function love.load(arg)
spaceship.load(arg)
asteroids.load(arg)
asteroids.set_decrease_score_cb(spaceship.decrease_score)
end
function love.update(dt)
-- Exit the game
if love.keyboard.isDown('escape', 'q') then
love.event.push('quit')
end
if love.keyboard.isDown('left', 'a') then
spaceship.move_left(dt)
elseif love.keyboard.isDown('right', 'd') then
spaceship.move_right(dt)
end
spaceship.update_shooting_interval(dt)
if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') then
-- Shoot
spaceship.shoot(dt)
end
spaceship.move_lasers(dt)
asteroids.create(dt)
asteroids.move(dt)
local lasers_table = spaceship.get_lasers()
for i, laser in ipairs(lasers_table) do
if asteroids.check_collision(laser.x, laser.y, laser.img:getWidth(),
laser.img:getHeight()) then
spaceship.increase_score()
spaceship.remove_laser(i)
end
end
if spaceship.alive and
asteroids.check_collision(spaceship.x, spaceship.y,
spaceship.img:getWidth(),
spaceship.img:getHeight()) then
spaceship.killed()
end
if not spaceship.alive and love.keyboard.isDown('r') then
spaceship.revive()
asteroids.reset()
end
end
function love.keyreleased(key)
if key == 's' then
show_score = not show_score
end
end
function love.draw(dt)
spaceship.draw(dt)
asteroids.draw(dt)
if show_score then
love.graphics.print(spaceship.score, 0, 0)
end
end