-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
133 lines (121 loc) · 4.44 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
GameState = {}
function GameState.makeNewFallingObject (x, y, size, color, fallingVelocity)
local obj = {}
obj.x = x
obj.y = y
obj.size = size or 10
obj.color = color or {0, 240, 240}
obj.fallingVelocity = fallingVelocity or 5
obj.fallingRate = 0.25
table.insert(GameState.fallingObjects, obj)
end
function love.load()
GameState.catcher = {}
GameState.catcher.x = 20
GameState.catcher.y = 550
GameState.catcher.minx = 20
GameState.catcher.maxx = 590
GameState.catcher.width = 180
GameState.catcher.xvelocity = 0
GameState.score = 0
GameState.deltaAccumulator = 0
GameState.fallingObjects = {}
GameState.generateObjectInterval = 1.2
GameState.untilNextGeneratedObject = GameState.generateObjectInterval
love.keyboard.setKeyRepeat(true)
end
function love.draw()
love.graphics.setBackgroundColor(0,0,0)
love.graphics.clear()
local prevLineWidth = love.graphics.getLineWidth()
-- draw score
love.graphics.setNewFont(20)
love.graphics.setColor(240, 0, 240) -- magenta but not at highest brightness
love.graphics.print("Score: " .. tostring(GameState.score), 650, 25)
love.graphics.setLineWidth(3)
-- draw
love.graphics.line(GameState.catcher.maxx + 1, 0, GameState.catcher.maxx + 1, 600)
-- draw falling objects
for i in ipairs(GameState.fallingObjects) do
local obj = GameState.fallingObjects[i]
love.graphics.setColor(obj.color)
love.graphics.rectangle("line", obj.x, obj.y, obj.size, obj.size)
end
-- draw catcher
love.graphics.setColor(0, 240, 0) -- lime but not at highest brightness
local x, y, width = GameState.catcher.x, GameState.catcher.y, GameState.catcher.width
love.graphics.line(x, y, x, y + 20, x + width, y + 20, x + width, y)
-- draw Game Paused
if GameState.paused then
love.graphics.setColor(240, 0, 240) -- magenta but not at highest brightness
love.graphics.setNewFont(60)
love.graphics.print("Game is paused!", 150, 250)
end
love.graphics.setLineWidth(prevLineWidth)
end
function love.update(deltatime)
GameState.deltaAccumulator = GameState.deltaAccumulator + deltatime
-- print("love.update(): deltatime = " .. tostring(deltatime))
if not GameState.paused then
GameState.catcher.x = GameState.catcher.x + GameState.catcher.xvelocity
if GameState.catcher.x < GameState.catcher.minx then
GameState.catcher.x = GameState.catcher.minx
end
if GameState.catcher.x > (GameState.catcher.maxx - GameState.catcher.width) then
GameState.catcher.x = (GameState.catcher.maxx - GameState.catcher.width)
end
for i in ipairs(GameState.fallingObjects) do
local obj = GameState.fallingObjects[i]
if math.modf(math.fmod(GameState.deltaAccumulator, obj.fallingRate)) == 0 then
obj.y = obj.y + obj.fallingVelocity
local middlex = obj.x + (obj.size / 2)
if obj.y > GameState.catcher.y and middlex > GameState.catcher.x and middlex < (GameState.catcher.x + GameState.catcher.width) then
GameState.score = GameState.score + obj.size
end
if obj.y > 600 then
table.remove(GameState.fallingObjects, i)
end
end
end
if GameState.untilNextGeneratedObject <= 0 then
local size = math.random(10, 100)
local x = math.random(GameState.catcher.minx, (GameState.catcher.maxx - size))
local y = 10
local color = { math.random(60, 240), math.random(60, 240), math.random(60, 240) }
GameState.makeNewFallingObject(x, y, size, color, math.random(1, 10))
GameState.untilNextGeneratedObject = GameState.generateObjectInterval
else
GameState.untilNextGeneratedObject = GameState.untilNextGeneratedObject - deltatime
end
end
if GameState.deltaAccumulator >= 10 then
print("love.update(): deltaAccumlator tick")
GameState.deltaAccumulator = 0
end
end
function love.mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
end
function love.keypressed(key)
if love.keyboard.isDown("left") then
GameState.catcher.xvelocity = -5
end
if love.keyboard.isDown("right") then
GameState.catcher.xvelocity = 5
end
end
function love.keyreleased(key)
end
function love.focus(f)
if f then
GameState.paused = false
print("love.focus(): gained focus")
else
GameState.paused = true
print("love.focus(): lost focus")
end
end
function love.quit()
print("love.quit(): goodbye!")
end