-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
333 lines (271 loc) · 7.95 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
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
function love.load()
-- Oh hi hilsen werner
-- Cools hilsen Svein Even
-- Load images:
cloud = love.graphics.newImage("gfx/Clouds.png")
bgImage = love.graphics.newImage("gfx/bg.png")
particle = love.graphics.newImage("gfx/simpleparticle.png")
asteroid = love.graphics.newImage("gfx/asteroid.png")
-- Declare constants
sFactor = 1.0
meHeight = love.graphics.getHeight
meWidth = love.graphics.getWidth
speed = 100
systems = {} -- Table
questionsMultiplication =
{
{ "What is 1X2",2 },
{ "What is 2X2",4 },
{ "What is 4X1",4 },
{ "What is 3X2",6 },
{ "What is 1X1",1 },
{ "What is 3X1",3 },
{ "What is 4X2",8 }
} -- Table
-- Declare Variables
bgYoffset = -1200
scroller = 0
Wind = 0
answervalue = 0
correctanswer = 0
fuel = 100
velocity = 100
altitudeBonus = 1
question = ""
questionSolution = 0
bgMaxBounds = -1
rocket = love.graphics.newImage("gfx/rocket_1.png")
rocketId = 1
rocketXpos = 350
-- Score:
gAltitude = 0
-- Set Fonts:
font = love.graphics.newImageFont("gfx/Imagefont.png"," abcdefghijklmnopqrstuvwxyz" .."ABCDEFGHIJKLMNOPQRSTUVWXYZ0" .."123456789.,!?-+/():;%&`'*#=[]\"")
-- Game Settings:
gGameStarted = 0
-- The amazing music.
music = love.audio.newSource("sound/prondisk.xm")
love.audio.play(music, 9)
-- Test Variables:
direction = 0
num = 0
current = 1
-- Simple Particle Declaration (insert p into systems table)
local p = love.graphics.newParticleSystem(particle, 1000)
p:setEmissionRate(20)
p:setSpeed(300, 400)
p:setGravity(100)
p:setSizes(2, 1)
p:setColors(255, 255, 255, 255, 58, 128, 255, 0)
p:setPosition(400, 300)
p:setLifetime(1)
p:setParticleLife(1)
p:setDirection(180)
p:setSpread(180)
p:setRadialAcceleration(-2000)
p:setTangentialAcceleration(1000)
p:stop()
table.insert(systems, p)
-- Asteroids:
local p = love.graphics.newParticleSystem(asteroid, 1000)
p:setEmissionRate(6)
p:setSpeed(100, 250)
p:setGravity(10, 80)
p:setSizes(0.1, 1.3)
p:setColors(238, 233, 233, 155, 255, 250, 250, 255)
p:setPosition(400, 300)
p:setLifetime(1)
p:setParticleLife(20)
p:setDirection(130)
p:setSpread(70)
p:setSpin(1, 5)
p:stop()
table.insert(systems, p)
-- Rektangel (draggable), test med sprite
rect = {
rectx = 50,
recty = 50,
rectwidth = 50,
rectheight = 50,
dragging = { active = false, diffX = 0, diffY = 0 }
}
end
function love.draw()
drawBottom()
drawCloud()
drawMyRocket()
drawInfoText()
-- Print FPS in top left corner:
love.graphics.print("FPS: "..love.timer.getFPS(), 10, 30)
-- create draggable box:
love.graphics.rectangle("fill", rect.rectx, rect.recty, rect.rectwidth, rect.rectheight)
if correctanswer == 1 then
CorrectAnswer()
-- Wait a few seconds..
CreateQuestion()
answervalue = 0
correctanswer = 0
elseif correctanswer == 2 then
love.graphics.print("Nope try again ... " , 222, 333)
end
end
function drawInfoText()
love.graphics.setFont(font)
scoreBackground = love.graphics.setColor(255,255,0,200) -- Red font
scoreBackground = love.graphics.print("Altitude: "..round(gAltitude,1) , 10, 10, 0, 1.0, 1.0)
scoreBackground = love.graphics.setColor(255,0,0) -- Red font
if gGameStarted == 0 then
scoreForeground = love.graphics.print("Press S to Start" , 180, 60, 0, sx, sx)
scoreForeground = love.graphics.print("Press R to change rocket" , 180, 80, 0, sx, sx)
love.graphics.print("System: [" .. current .. "/"..table.getn(systems).."] - " .. systems[current]:count() .. " particles.", 180, 100);
scoreForeground = love.graphics.print("Solve the mathematical problems by adding numbers 1-5 together" , 180, 120, 0, sx, sx)
scoreForeground = love.graphics.print("Press space to test your answer, or backspace to start over" , 180, 140, 0, sx, sx)
scoreBackground = love.graphics.setColor(255,255,100) -- Red font
scoreForeground = love.graphics.print(" Game by Yell0w 2012 " , 280, 180, 0, sx, sx)
end
love.graphics.print(question , 180, 270)
if answervalue ~= 0 then
love.graphics.print("Answer: " .. answervalue , 180, 300)
end
-- Reset colours:
scoreForeground = love.graphics.setColor(255,255,255)
end
function drawBottom()
love.graphics.draw(bgImage, 0, bgYoffset)
end
function drawMyRocket()
love.graphics.draw(rocket, rocketXpos, 550)
-- Add Smoke:
love.graphics.setColorMode("modulate")
--love.graphics.setBlendMode("additive") WTF?!?
love.graphics.draw(systems[1], 0, 0)
systems[1]:setPosition(rocketXpos+50, 550+200)
systems[1]:start()
love.graphics.draw(systems[2], 0, 0)
systems[2]:setPosition(400,-600)
systems[2]:start()
end
function drawCloud()
mycloud = love.graphics.setColor(255,255,255, 100)
mycloud = love.graphics.draw(cloud, 570+Wind, scroller+80, 0,0.2)
mycloud = love.graphics.draw(cloud, 40+Wind, scroller+120, 0,0.15)
love.graphics.setColor(255,255,255,255)
end
function CreateQuestion()
if question == "" then
-- Select random row from the table:
tablerows = table.maxn(questionsMultiplication)
randomrow = math.random(1,tablerows )
question = questionsMultiplication[randomrow][1]
questionSolution = questionsMultiplication[randomrow][2]
end
end
function CorrectAnswer()
gAltitude = gAltitude + 100
question = ""
end
function love.update(dt)
-- Scroll bg or stop:
if gGameStarted == 1 then
scroller = scroller + (13 * dt) -- start scrolling slower (other stuffses)
if bgYoffset < bgMaxBounds then
bgYoffset = bgYoffset + (speed * dt)
-- else
end
end
Wind = Wind + (3 * dt)
-- Update Score:
if gGameStarted == 1 then
gAltitude = gAltitude + (dt*altitudeBonus)
end
if love.keyboard.isDown("right") then
rocketXpos = rocketXpos + (speed * dt)
elseif love.keyboard.isDown("left") then
rocketXpos = rocketXpos - (speed * dt)
end
if rocketXpos < 0 or rocketXpos > 710 then
rocketXpos = 350
end
-- Call forth sprites of doom!:
systems[1]:update(dt)
if gGameStarted == 1 then
systems[2]:update(dt)
end
if rect.dragging.active then
rect.rectx = love.mouse.getX() - rect.dragging.diffX
rect.recty = love.mouse.getY() - rect.dragging.diffY
end
end
function love.mousepressed(rectx, recty, button)
if button == "l"
and rectx > rect.rectx and rectx < rect.rectx + rect.rectwidth
and recty > rect.recty and recty < rect.recty + rect.rectheight
then
rect.dragging.active = true
rect.dragging.diffX = rectx - rect.rectx
rect.dragging.diffY = recty - rect.recty
end
end
function love.mousereleased(x, y, button)
if button == "l" then rect.dragging.active = false end
end
function love.focus(f)
if not f then
print("LOST FOCUS")
else
print("GAINED FOCUS")
end
end
function love.quit()
print("Thanks for playing! Come back soon!")
end
function love.keypressed(key)
if key == "s" then
gGameStarted = 1
CreateQuestion()
end
if key == "return" or key == " " then
if questionSolution == answervalue then
correctanswer = 1
else
correctanswer = 2
end
end
if key == "escape" then
love.event.push("q")
end
if key == "1" then
answervalue = answervalue+1
end
if key == "2" then
answervalue = answervalue+2
end
if key == "3" then
answervalue = answervalue+3
end
if key == "4" then
answervalue = answervalue+4
end
if key == "5" then
answervalue = answervalue+5
end
if key == "backspace" or key == "delete" then
answervalue = 0
end
if key == "r" then
rollthis = math.random(1,2)
if rollthis == 2 then
rocket = love.graphics.newImage("gfx/rocket_2.png")
else
rocket = love.graphics.newImage("gfx/rocket_1.png")
end
end
if key == "rctrl" then
debug.debug()
end
end
-- Hjelpefunksjoner
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end