-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
120 lines (84 loc) · 2.59 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
require 'player.lua'
require 'building.lua'
require 'background.lua'
require 'image.lua'
local g = love.graphics
local a = love.audio
game = {}
kills = 0
function game.addObject(o)
gameObjects.size = gameObjects.size + 1
gameObjects[gameObjects.size] = o
end
function game.load()
gameObjects = {}
gameObjects.size = 0
kills = 0
font = font or g.newFont('assets/Alien-Encounters-Italic.ttf', 36)
player = Player:new(200, g.getHeight() -30, 500)
buildingA = Building:new(player, -400)
buildingB = Building:new(player, buildingA.x + buildingA.w)
buildingA:setOther(buildingB)
buildingB:setOther(buildingA)
bgSky = bgSky or Image:new('assets/bg_gradient.jpg')
bgSkyQuad = bgSkyQuad or g.newQuad(0,0, g.getWidth(), g.getHeight(), bgSky:getWidth(), bgSky:getHeight())
bgImage1 = bgImage1 or Image:new("assets/bg01_y=525.png")
bgImage2 = bgImage2 or Image:new("assets/bg02_y=425.png")
local bg1 = Background:new(player, bgImage1, 525, { 123, 122, 122, 255}, 0.2, 0.1)
local bg2 = Background:new(player, bgImage2, 425, { 175, 175, 175, 255}, 0.1, 0.1)
game.addObject(bg2)
game.addObject(bg1)
game.addObject(buildingA)
game.addObject(buildingB)
game.addObject(player)
g.setBackgroundColor(239, 147, 49, 255)
time = 0
end
function game.keypressed(key)
if key == "escape" then
menu.load()
state = menu
else
for i,obj in ipairs(gameObjects) do
if obj.keypressed then obj:keypressed(key) end
end
end
end
function game.keyreleased(key)
for i,obj in ipairs(gameObjects) do
if obj.keypressed then obj:keyreleased(key) end
end
end
function game.update(dt)
player:checkCollision(buildingA, buildingB)
player:checkEnemies(buildingA.enemies, buildingB.enemies)
for i,obj in ipairs(gameObjects) do
if obj.update then obj:update(dt) end
end
if player:isDead() then
if time > bestTime then
local int, frac = math.modf(time)
local string = "bestTime = " .. int .. "." .. math.floor(frac * 100)
love.filesystem.write('highscore.lua', string, string:len())
bestTime = time
end
game.load()
end
time = time + dt
end
function game.draw()
g.drawq(bgSky:getImage(), bgSkyQuad, 0, 0)
for i,obj in ipairs(gameObjects) do
obj:draw()
end
for i,obj in ipairs(gameObjects) do
if obj.postDraw then obj:postDraw() end
end
g.setColor(255,255,255, 255)
-- g.print("KILLS: " .. kills, g.getWidth() - 200, 10)
g.setFont(font)
local int, frac = math.modf(bestTime)
g.print("HIGHSCORE: " .. int .. "." .. math.floor(frac * 100), 20, 10)
local int, frac = math.modf(time)
g.print("TIME: " .. int .. "." .. math.floor(frac * 100), g.getWidth()-300, 10)
end