-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
92 lines (81 loc) · 1.92 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
_G.love = require("love")
Player = require("objects/Player")
Game = require("states/Game")
require("Globals")
math.randomseed(os.time())
function love.load()
love.mouse.setVisible(false)
_G.mouse_x, _G.mouse_y = 0, 0
_G.player = Player()
_G.game = Game()
game:startGame()
end
function love.keypressed(key)
if game.state.running then
if key == "up" or key == "w" then
player.thrusting = true
end
if key == "space" then
player:shootLasers()
end
end
if game.state.ended then
if key == "r" then
game:restartGame()
end
end
end
function love.keyreleased(key)
if game.state.running then
if key == "up" or key == "w" then
player.thrusting = false
end
if key == "escape" then
game:changeGameState("paused")
end
elseif game.state.paused then
if key == "escape" then
game:changeGameState("running")
end
end
end
function love.update(dt)
mouse_x, mouse_y = love.mouse.getPosition()
if game.state.running then
player:movePlayer()
for i = #game.asteroids, 1, -1 do
local asteroid = game.asteroids[i]
asteroid:move(dt)
if calculateDistance(player.x, player.y, asteroid.x, asteroid.y) <= asteroid.radius then
game:endGame()
end
for j = #player.lasers, 1, -1 do
local laser = player.lasers[j]
if
calculateDistance(laser.x, laser.y, asteroid.x, asteroid.y) <= asteroid.radius
and not laser.exploding
then
laser:explode()
game:increasePoints(10)
asteroid:breakAsteroid(game)
table.remove(game.asteroids, i)
break
end
end
end
for _, laser in pairs(player.lasers) do
laser:move()
end
end
end
function love.draw()
player:draw(game.state.paused)
game:draw(game.state.ended)
Text("Points " .. game.points, 0, 20, "h3", 1, false, true, love.graphics.getWidth(), "center"):draw()
for _, asteroid in pairs(game.asteroids) do
asteroid:draw(game.state.paused)
end
if game.state.paused then
game:draw(game.state.paused)
end
end