-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·1117 lines (981 loc) · 29.8 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function love.load()
--requires--
require "gameB"
require "gameBmulti"
require "gameA"
require "menu"
require "failed"
require "rocket"
vsync = true
love.graphics.setDefaultFilter("nearest", "nearest", 1)
autosize()
suggestedscale = math.min(math.floor((desktopheight - 50) / 144), math.floor((desktopwidth - 10) / 160))
if suggestedscale > 5 then
suggestedscale = 5
end
loadoptions()
maxscale = math.min(math.floor(desktopheight / 144), math.floor(desktopwidth / 160))
maxmpscale = math.min(math.floor(desktopheight / 144), math.floor(desktopwidth / 274))
if fullscreen == false then
if scale ~= 5 then
love.graphics.setMode(160 * scale, 144 * scale, false, vsync, 16)
end
else
love.graphics.setMode(0, 0, true, vsync, 16)
love.mouse.setVisible(false)
desktopwidth, desktopheight = love.graphics.getWidth(), love.graphics.getHeight()
saveoptions()
suggestedscale = math.floor((desktopheight - 50) / 144)
if suggestedscale > 5 then
suggestedscale = 5
end
maxscale = math.min(math.floor(desktopheight / 144), math.floor(desktopwidth / 160))
scale = maxscale
fullscreenoffsetX = (desktopwidth - 160 * scale) / 2
fullscreenoffsetY = (desktopheight - 144 * scale) / 2
end
physicsscale = scale / 4
--pieces--
tetriimages = {}
tetriimagedata = {}
--SOUND--
music = {}
music[1] = love.audio.newSource("sounds/themeA.ogg", "stream")
music[1]:setVolume(0.6)
music[1]:setLooping(true)
music[2] = love.audio.newSource("sounds/themeB.ogg", "stream")
music[2]:setVolume(0.6)
music[2]:setLooping(true)
music[3] = love.audio.newSource("sounds/themeC.ogg", "stream")
music[3]:setVolume(0.6)
music[3]:setLooping(true)
musictitle = love.audio.newSource("sounds/titlemusic.ogg", "stream")
musictitle:setVolume(0.6)
musictitle:setLooping(true)
musichighscore = love.audio.newSource("sounds/highscoremusic.ogg", "stream")
musichighscore:setVolume(0.6)
musichighscore:setLooping(true)
musicrocket4 = love.audio.newSource("sounds/rocket4.ogg", "stream")
musicrocket4:setVolume(0.6)
musicrocket4:setLooping(false)
musicrocket1to3 = love.audio.newSource("sounds/rocket1to3.ogg", "stream")
musicrocket1to3:setVolume(0.6)
musicrocket1to3:setLooping(false)
musicresults = love.audio.newSource("sounds/resultsmusic.ogg", "stream")
musicresults:setVolume(1)
musicresults:setLooping(false)
highscoreintro = love.audio.newSource("sounds/highscoreintro.ogg", "stream")
highscoreintro:setVolume(0.6)
highscoreintro:setLooping(false)
musicoptions = love.audio.newSource("sounds/musicoptions.ogg", "stream")
musicoptions:setVolume(1)
musicoptions:setLooping(true)
boot = love.audio.newSource("sounds/boot.ogg", "static")
blockfall = love.audio.newSource("sounds/blockfall.ogg", "stream")
blockturn = love.audio.newSource("sounds/turn.ogg", "stream")
blockmove = love.audio.newSource("sounds/move.ogg", "stream")
lineclear = love.audio.newSource("sounds/lineclear.ogg", "stream")
fourlineclear = love.audio.newSource("sounds/4lineclear.ogg", "stream")
gameover1 = love.audio.newSource("sounds/gameover1.ogg", "stream")
gameover2 = love.audio.newSource("sounds/gameover2.ogg", "stream")
pausesound = love.audio.newSource("sounds/pause.ogg", "stream")
highscorebeep = love.audio.newSource("sounds/highscorebeep.ogg", "stream")
newlevel = love.audio.newSource("sounds/newlevel.ogg", "stream")
newlevel:setVolume(0.6)
changevolume(volume)
--IMAGES THAT WON'T CHANGE HUE:
rainbowgradient = love.graphics.newImage("graphics/rainbow.png")
--Whitelist for highscorenames--
whitelist = {}
for i = 48, 57 do -- 0 - 9
whitelist[i] = true
end
for i = 65, 90 do -- A - Z
whitelist[i] = true
end
for i = 97, 122 do --a - z
whitelist[i] = true
end
whitelist[32] = true -- space
whitelist[44] = true -- ,
whitelist[45] = true -- -
whitelist[46] = true -- .
whitelist[95] = true -- _
-----------------------------
math.randomseed(os.time())
math.random(); math.random(); math.random() --discarding some as they seem to tend to unrandomness.
love.graphics.setBackgroundColor(255, 255, 255)
p1wins = 0
p2wins = 0
skipupdate = true
soundenabled = true
startdelay = 0 -- changed from 1, fixes oldtime = nil error
logoduration = 1.5
logodelay = 1
creditsdelay = 2
selectblinkrate = 0.29
cursorblinkrate = 0.14
selectblink = true
cursorblink = true
playerselection = 1
musicno = 1 --
gameno = 1 --
selection = 1 --
colorizeduration = 3 --seconds
lineclearduration = 1.2 --seconds
lineclearblinks = 7 --i
linecleartreshold = 8.1 --in blocks
densityupdateinterval = 1 / 30 --in seconds
nextpiecerotspeed = 1 --rad per seconnd
minfps = 1 / 50 --dt doesn't go higher than this
scoreaddtime = 0.5
startdelaytime = 0
blockstartY = -64 --where new blocks are created
losingY = 0 --lose if block 1 collides above this line
blockmass = 5 --probably obsolete because body:setMassFromShapes()
blockrot = 10
blockrestitution = 0.1
minmass = 1
blocktorque = 70000
blocklatforce = 2000
optionschoices = { "volume", "color", "scale", "fullscrn" }
piececenter = {}
piececenter[1] = { 17, 5 }
piececenter[2] = { 13, 9 }
piececenter[3] = { 13, 9 }
piececenter[4] = { 9, 9 }
piececenter[5] = { 13, 9 }
piececenter[6] = { 13, 9 }
piececenter[7] = { 13, 9 }
piececenterpreview = {}
piececenterpreview[1] = { 17, 5 }
piececenterpreview[2] = { 15, 7 }
piececenterpreview[3] = { 11, 7 }
piececenterpreview[4] = { 9, 9 }
piececenterpreview[5] = { 13, 9 }
piececenterpreview[6] = { 13, 7 }
piececenterpreview[7] = { 13, 9 }
loadhighscores()
loadimages()
--all done!
if startdelay == 0 then
menu_load()
end
end
function start()
menu_load()
end
function loadimages()
--IMAGES--
--menu--
stabyourselflogo = newPaddedImage("graphics/stabyourselflogo.png")
logo = newPaddedImage("graphics/logo.png")
title = newPaddedImage("graphics/title.png")
gametype = newPaddedImage("graphics/gametype.png")
mpmenu = newPaddedImage("graphics/mpmenu.png")
optionsmenu = newPaddedImage("graphics/options.png")
volumeslider = newPaddedImage("graphics/volumeslider.png")
--game--
gamebackground = newPaddedImage("graphics/gamebackground.png")
gamebackgroundcutoff = newPaddedImage("graphics/gamebackgroundgamea.png")
gamebackgroundmulti = newPaddedImage("graphics/gamebackgroundmulti.png")
multiresults = newPaddedImage("graphics/multiresults.png")
number1 = newPaddedImage("graphics/versus/number1.png")
number2 = newPaddedImage("graphics/versus/number2.png")
number3 = newPaddedImage("graphics/versus/number3.png")
gameover = newPaddedImage("graphics/gameover.png")
gameovercutoff = newPaddedImage("graphics/gameovercutoff.png")
pausegraphic = newPaddedImage("graphics/pause.png")
pausegraphiccutoff = newPaddedImage("graphics/pausecutoff.png")
--figures--
marioidle = newPaddedImage("graphics/versus/marioidle.png")
mariojump = newPaddedImage("graphics/versus/mariojump.png")
mariocry1 = newPaddedImage("graphics/versus/mariocry1.png")
mariocry2 = newPaddedImage("graphics/versus/mariocry2.png")
luigiidle = newPaddedImage("graphics/versus/luigiidle.png")
luigijump = newPaddedImage("graphics/versus/luigijump.png")
luigicry1 = newPaddedImage("graphics/versus/luigicry1.png")
luigicry2 = newPaddedImage("graphics/versus/luigicry2.png")
--rockets--
rocket1 = newPaddedImage("graphics/rocket1.png");
rocket2 = newPaddedImage("graphics/rocket2.png")
rocket3 = newPaddedImage("graphics/rocket3.png")
spaceshuttle = newPaddedImage("graphics/spaceshuttle.png")
rocketbackground = newPaddedImage("graphics/rocketbackground.png")
bigrocketbackground = newPaddedImage("graphics/bigrocketbackground.png")
bigrockettakeoffbackground = newPaddedImage("graphics/bigrockettakeoffbackground.png")
smoke1left = newPaddedImage("graphics/smoke1left.png")
smoke1right = newPaddedImage("graphics/smoke1right.png")
smoke2left = newPaddedImage("graphics/smoke2left.png")
smoke2right = newPaddedImage("graphics/smoke2right.png")
fire1 = newPaddedImage("graphics/fire1.png")
fire2 = newPaddedImage("graphics/fire2.png")
firebig1 = newPaddedImage("graphics/firebig1.png")
firebig2 = newPaddedImage("graphics/firebig2.png")
congratsline = newPaddedImage("graphics/congratsline.png")
--nextpiece
nextpieceimg = {}
for i = 1, 7 do
nextpieceimg[i] = newPaddedImage("graphics/pieces/" .. i .. ".png", scale)
end
--font--
tetrisfont = newPaddedImageFont("graphics/font.png", "0123456789abcdefghijklmnopqrstTuvwxyz.,'C-#_>:<! ")
whitefont = newPaddedImageFont("graphics/fontwhite.png", "0123456789abcdefghijklmnopqrstTuvwxyz.,'C-#_>:<!+ ")
love.graphics.setFont(tetrisfont)
end
function love.update(dt)
if gamestate == nil then
startdelaytime = startdelaytime + dt
if startdelaytime >= startdelay then
start()
end
end
if skipupdate then
skipupdate = false
return
end
if cuttingtimer ~= 0 then
dt = math.min(dt, minfps)
end
if gamestate == "logo" or gamestate == "credits" or gamestate == "title" or gamestate == "menu" or gamestate == "multimenu" or gamestate == "highscoreentry" or gamestate == "options" then
menu_update(dt)
elseif gamestate == "gameA" or gamestate == "failingA" then
if pause == false then
gameA_update(dt)
end
elseif gamestate == "gameB" or gamestate == "failingB" then
if pause == false then
gameB_update(dt)
end
elseif gamestate == "gameBmulti" or gamestate == "failingBmulti" or gamestate == "failedBmulti" or gamestate == "gameBmulti_results" then
gameBmulti_update(dt)
elseif gamestate == "rocket1" or gamestate == "rocket2" or gamestate == "rocket3" or gamestate == "rocket4" then
rocket_update()
end
end
function love.draw()
if gamestate == "logo" or gamestate == "credits" or gamestate == "title" or gamestate == "menu" or gamestate == "multimenu" or gamestate == "highscoreentry" or gamestate == "options" then
menu_draw()
elseif gamestate == "gameA" or gamestate == "failingA" then
gameA_draw()
elseif gamestate == "gameB" or gamestate == "failingB" then
gameB_draw()
elseif gamestate == "gameBmulti" or gamestate == "failingBmulti" or gamestate == "failedBmulti" or gamestate == "gameBmulti_results" then
gameBmulti_draw()
elseif gamestate == "failed" then
failed_draw()
elseif gamestate == "rocket1" or gamestate == "rocket2" or gamestate == "rocket3" or gamestate == "rocket4" then
rocket_draw()
end
end
function newImageData(path, s)
local imagedata = love.image.newImageData(path)
if s then
imagedata = scaleImagedata(imagedata, s)
end
local width, height = imagedata:getWidth(), imagedata:getHeight()
local rr, rg, rb = unpack(getrainbowcolor(hue))
for y = 0, height - 1 do
for x = 0, width - 1 do
local oldr, oldg, oldb, olda = imagedata:getPixel(x, y)
if olda ~= 0 then
if oldr > 203 and oldr < 213 then --lightgrey
local r = 145 + rr * 64
local g = 145 + rg * 64
local b = 145 + rb * 64
imagedata:setPixel(x, y, r, g, b, olda)
elseif oldr > 107 and oldr < 117 then --darkgrey
local r = 73 + rr * 43
local g = 73 + rg * 43
local b = 73 + rb * 43
imagedata:setPixel(x, y, r, g, b, olda)
end
end
end
end
return imagedata
end
function newPaddedImage(filename, s)
local source = newImageData(filename)
if s then
source = scaleImagedata(source, s)
end
local w, h = source:getWidth(), source:getHeight()
-- Find closest power-of-two.
local wp = math.pow(2, math.ceil(math.log(w) / math.log(2)))
local hp = math.pow(2, math.ceil(math.log(h) / math.log(2)))
-- Only pad if needed:
if wp ~= w or hp ~= h then
local padded = love.image.newImageData(wp, hp)
padded:paste(source, 0, 0)
return love.graphics.newImage(padded)
end
return love.graphics.newImage(source)
end
function padImagedata(source) --returns image, not imagedata!
local w, h = source:getWidth(), source:getHeight()
-- Find closest power-of-two.
local wp = math.pow(2, math.ceil(math.log(w) / math.log(2)))
local hp = math.pow(2, math.ceil(math.log(h) / math.log(2)))
-- Only pad if needed:
if wp ~= w or hp ~= h then
local padded = love.image.newImageData(wp, hp)
padded:paste(source, 0, 0)
return love.graphics.newImage(padded)
end
return love.graphics.newImage(source)
end
function newPaddedImageFont(filename, glyphs)
local source = newImageData(filename)
local w, h = source:getWidth(), source:getHeight()
-- Find closest power-of-two.
local wp = math.pow(2, math.ceil(math.log(w) / math.log(2)))
local hp = math.pow(2, math.ceil(math.log(h) / math.log(2)))
-- Only pad if needed:
if wp ~= w or hp ~= h then
local padded = love.image.newImageData(wp, hp)
padded:paste(source, 0, 0)
-- local image = love.graphics.newImage(padded)
-- image:setFilter("nearest", "nearest")
-- replaced by setting default image padding in inittioalization
return love.graphics.newImageFont(padded, glyphs)
end
return love.graphics.newImageFont(source, glyphs)
end
function scaleImagedata(imagedata, i)
local width, height = imagedata:getWidth(), imagedata:getHeight()
local scaled = love.image.newImageData(width * i, height * i)
for y = 0, height * i - 1 do
for x = 0, width * i - 1 do
local r, g, b, a = imagedata:getPixel(math.floor(x / i), math.floor(y / i))
scaled:setPixel(x, y, r, g, b, a)
end
end
return scaled
end
function changevolume(i)
music[1]:setVolume(0.6 * i)
music[2]:setVolume(0.6 * i)
music[3]:setVolume(0.6 * i)
musictitle:setVolume(0.6 * i)
musichighscore:setVolume(0.6 * i)
musicrocket4:setVolume(0.6 * i)
musicrocket1to3:setVolume(0.6 * i)
musicresults:setVolume(i)
highscoreintro:setVolume(0.6 * i)
musicoptions:setVolume(i)
boot:setVolume(i)
blockfall:setVolume(i)
blockturn:setVolume(i)
blockmove:setVolume(i)
lineclear:setVolume(i)
fourlineclear:setVolume(i)
gameover1:setVolume(i)
gameover2:setVolume(i)
pausesound:setVolume(i)
highscorebeep:setVolume(i)
newlevel:setVolume(0.6 * i)
end
function loadoptions()
if not love.filesystem.getInfo("options.txt", "file") == nil then
local s = love.filesystem.read("options.txt")
local split1 = s:split("\n")
for i = 1, #split1 do
local split2 = split1[i]:split("=")
if split2[1] == "volume" then
local v = tonumber(split2[2])
--clamp and round
if v < 0 then
v = 0
elseif v > 1 then
v = 1
end
v = math.floor(v * 10) / 10
volume = v
elseif split2[1] == "hue" then
hue = tonumber(split2[2])
elseif split2[1] == "scale" then
scale = tonumber(split2[2])
elseif split2[1] == "fullscreen" then
if split2[2] == "true" then
fullscreen = true
else
fullscreen = false
end
end
end
if volume == nil then
volume = 1
end
if hue == nil then
hue = 0.08
end
if fullscreen == nil then
fullscreen = false
end
if scale == nil then
scale = suggestedscale
end
else
volume = 1
hue = 0.08
autosize()
scale = suggestedscale
fullscreen = false
end
saveoptions()
end
function saveoptions()
local s = ""
s = s .. "volume=" .. volume .. "\n"
s = s .. "hue=" .. hue .. "\n"
s = s .. "scale=" .. scale .. "\n"
s = s .. "fullscreen=" .. tostring(fullscreen) .. "\n"
love.filesystem.write("options.txt", s)
end
function autosize()
local modes = love.window.getFullscreenModes()
desktopwidth, desktopheight = modes[1]["width"], modes[1]["height"]
end
function togglefullscreen(fullscr)
fullscreen = fullscr
love.mouse.setVisible(not fullscreen)
if fullscr == false then
scale = suggestedscale
physicsscale = scale / 4
love.graphics.setMode(160 * scale, 144 * scale, false, vsync, 16)
else
love.graphics.setMode(0, 0, true, vsync, 16)
desktopwidth, desktopheight = love.graphics.getWidth(), love.graphics.getHeight()
suggestedscale = math.min(math.floor((desktopheight - 50) / 144), math.floor((desktopwidth - 10) / 160))
suggestedscale = math.min(math.floor((desktopheight - 50) / 144), math.floor((desktopwidth - 10) / 160))
if suggestedscale > 5 then
suggestedscale = 5
end
maxscale = math.min(math.floor(desktopheight / 144), math.floor(desktopwidth / 160))
scale = maxscale
physicsscale = scale / 4
fullscreenoffsetX = (desktopwidth - 160 * scale) / 2
fullscreenoffsetY = (desktopheight - 144 * scale) / 2
end
end
function loadhighscores()
if gameno == 1 then
fileloc = "highscoresA.txt"
else
fileloc = "highscoresB.txt"
end
if not love.filesystem.getInfo(fileloc, "file") == nil then
highdata = love.filesystem.read(fileloc)
highdata = highdata:split(";")
highscore = {}
highscorename = {}
for i = 1, 3 do
highscore[i] = tonumber(highdata[i * 2])
highscorename[i] = string.lower(highdata[i * 2 - 1])
end
else
highscore = {}
highscorename = {}
highscore[1] = 0
highscorename[1] = ""
highscore[2] = 0
highscorename[2] = ""
highscore[3] = 0
highscorename[3] = ""
savehighscores()
end
end
function newhighscores()
highscore = {}
highscorename = {}
highscore[1] = 0
highscorename[1] = ""
highscore[2] = 0
highscorename[2] = ""
highscore[3] = 0
highscorename[3] = ""
savehighscores()
end
function savehighscores()
if gameno == 1 then
fileloc = "highscoresA.txt"
else
fileloc = "highscoresB.txt"
end
highdata = ""
for i = 1, 3 do
highdata = highdata .. highscorename[i] .. ";" .. highscore[i] .. ";"
end
love.filesystem.write(fileloc, highdata .. "\n")
end
function changescale(i)
love.graphics.setMode(160 * i, 144 * i, false, vsync, 16)
nextpieceimg = {}
for j = 1, 7 do
nextpieceimg[j] = newPaddedImage("graphics/pieces/" .. j .. ".png", i)
end
physicsscale = i / 4
end
function string:split(delimiter)
local result = {}
local from = 1
local delim_from, delim_to = string.find(self, delimiter, from)
while delim_from do
table.insert(result, string.sub(self, from, delim_from - 1))
from = delim_to + 1
delim_from, delim_to = string.find(self, delimiter, from)
end
table.insert(result, string.sub(self, from))
return result
end
function pythagoras(a, b)
c = math.sqrt(a ^ 2 + b ^ 2)
if a < 0 or b < 0 then
c = -c
end
return c
end
function round(num, idp)
local mult = 10 ^ (idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function table2string(mytable)
output = {}
for i, v in pairs(mytable) do
output[i] = mytable[i]
end
return output
end
function getPoints2table(shape)
x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8 = shape:getPoints()
if x4 == nil then
return { x1, y1, x2, y2, x3, y3 }
end
if x5 == nil then
return { x1, y1, x2, y2, x3, y3, x4, y4 }
end
if x6 == nil then
return { x1, y1, x2, y2, x3, y3, x4, y4, x5, y5 }
end
if x7 == nil then
return { x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6 }
end
if x8 == nil then
return { x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7 }
end
return { x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8 }
end
function getrainbowcolor(i)
local r, g, b
if i < 1 / 6 then
r = 1
g = i * 6
b = 0
elseif i >= 1 / 6 and i < 2 / 6 then
r = (1 / 6 - (i - 1 / 6)) * 6
g = 1
b = 0
elseif i >= 2 / 6 and i < 3 / 6 then
r = 0
g = 1
b = (i - 2 / 6) * 6
elseif i >= 3 / 6 and i < 4 / 6 then
r = 0
g = (1 / 6 - (i - 3 / 6)) * 6
b = 1
elseif i >= 4 / 6 and i < 5 / 6 then
r = (i - 4 / 6) * 6
g = 0
b = 1
else
r = 1
g = 0
b = (1 / 6 - (i - 5 / 6)) * 6
end
return { r, g, b }
end
function love.keypressed(key, unicode)
if gamestate == nil then
if key == "return" then
gamestate = "title"
love.graphics.setBackgroundColor(0, 0, 0)
love.audio.play(musictitle)
end
elseif gamestate == "logo" then
if key == "return" then
gamestate = "title"
love.graphics.setBackgroundColor(0, 0, 0)
love.audio.play(musictitle)
end
elseif gamestate == "credits" then
if key == "return" then
gamestate = "title"
love.graphics.setBackgroundColor(0, 0, 0)
love.audio.play(musictitle)
end
elseif gamestate == "title" then
if key == "return" then
if playerselection ~= 3 then
if soundenabled then
love.audio.stop(musictitle)
if musicno < 4 then
love.audio.play(music[musicno])
end
end
end
if playerselection == 1 then
gamestate = "menu"
elseif playerselection == 2 then
gamestate = "multimenu"
else
gamestate = "options"
if soundenabled then
love.audio.stop(musictitle)
love.audio.play(musicoptions)
end
optionsselection = 1
end
elseif key == "escape" then
love.event.push("quit")
elseif key == "left" and playerselection > 1 then
playerselection = playerselection - 1
elseif key == "right" and playerselection < 3 then
playerselection = playerselection + 1
end
elseif gamestate == "menu" then
oldmusicno = musicno
if key == "escape" then
if musicno < 4 then
love.audio.stop(music[musicno])
end
gamestate = "title"
if soundenabled then
love.audio.stop(musictitle)
love.audio.play(musictitle)
end
elseif key == "backspace" then
newhighscores()
elseif key == "return" then
if gameno == 1 then
gameA_load()
else
gameB_load()
end
elseif key == "left" then
if selection == 2 or selection == 4 or selection == 6 then
selection = selection - 1
selectblink = true
oldtime = love.timer.getTime()
end
elseif key == "right" then
if selection == 1 or selection == 3 or selection == 5 then
selection = selection + 1
selectblink = true
oldtime = love.timer.getTime()
end
elseif key == "up" then
if selection == 3 or selection == 4 or selection == 5 or selection == 6 then
selection = selection - 2
selectblink = true
oldtime = love.timer.getTime()
if selection < 3 then
selection = gameno
selectblink = false
oldtime = love.timer.getTime()
end
elseif selection == 1 or selection == 2 then
selection = musicno + 2
selectblink = false
oldtime = love.timer.getTime()
end
elseif key == "down" then
if selection == 1 or selection == 2 or selection == 3 or selection == 4 then
selection = selection + 2
selectblink = true
oldtime = love.timer.getTime()
if selection > 2 and selection < 5 then
selection = musicno + 2
selectblink = false
oldtime = love.timer.getTime()
end
elseif selection == 5 or selection == 6 then
selection = gameno
selectblink = false
oldtime = love.timer.getTime()
end
end
if selection > 2 and key ~= "escape" then
musicno = selection - 2
if oldmusicno ~= musicno and oldmusicno ~= 4 then
love.audio.stop(music[oldmusicno])
end
if musicno < 4 then
love.audio.play(music[musicno])
end
elseif key ~= "escape" then
gameno = selection
loadhighscores()
end
elseif gamestate == "options" then
if key == "escape" then
if soundenabled then
love.audio.stop(musicoptions)
love.audio.stop(musictitle)
love.audio.play(musictitle)
end
saveoptions()
loadimages()
gamestate = "title"
elseif key == "down" then
optionsselection = optionsselection + 1
if optionsselection > #optionschoices then
optionsselection = 1
end
selectblink = true
oldtime = love.timer.getTime()
elseif key == "up" then
optionsselection = optionsselection - 1
if optionsselection == 0 then
optionsselection = #optionschoices
end
selectblink = true
oldtime = love.timer.getTime()
elseif key == "left" then
if optionsselection == 1 then
if volume >= 0.1 then
volume = volume - 0.1
if volume < 0.1 then
volume = 0
end
changevolume(volume)
end
elseif optionsselection == 3 then
if fullscreen == false then
if scale > 1 then
scale = scale - 1
changescale(scale)
end
end
elseif optionsselection == 4 then
if fullscreen == false then
togglefullscreen(true)
end
end
elseif key == "right" then
if optionsselection == 1 then
if volume <= 0.9 then
volume = volume + 0.1
changevolume(volume)
end
elseif optionsselection == 3 then
if fullscreen == false then
if scale < maxscale then
scale = scale + 1
changescale(scale)
end
end
elseif optionsselection == 4 then
if fullscreen == true then
togglefullscreen(false)
end
end
elseif key == "return" then
if optionsselection == 1 then
volume = 1
changevolume(volume)
elseif optionsselection == 2 then
hue = 0.08
optionsmenu = newPaddedImage("graphics/options.png")
volumeslider = newPaddedImage("graphics/volumeslider.png")
elseif optionsselection == 3 then
if fullscreen == false then
if scale ~= suggestedscale then
scale = suggestedscale
changescale(scale)
end
end
elseif optionsselection == 4 then
if fullscreen == true then
togglefullscreen(false)
end
end
end
elseif gamestate == "multimenu" then
oldmusicno = musicno
if key == "escape" then
if musicno < 4 then
love.audio.stop(music[musicno])
end
gamestate = "title"
love.audio.stop(musictitle)
love.audio.play(musictitle)
elseif key == "return" then
gameBmulti_load()
elseif key == "left" or key == "a" then
if selection == 2 or selection == 4 or selection == 6 then
selection = selection - 1
selectblink = true
oldtime = love.timer.getTime()
end
elseif key == "right" or key == "d" then
if selection == 1 or selection == 3 or selection == 5 then
selection = selection + 1
selectblink = true
oldtime = love.timer.getTime()
end
elseif key == "up" or key == "w" then
if selection == 3 or selection == 4 or selection == 5 or selection == 6 then
selection = selection - 2
selectblink = true
oldtime = love.timer.getTime()
if selection < 3 then
selection = gameno
selectblink = false
oldtime = love.timer.getTime()
end
elseif selection == 1 or selection == 2 then
selection = musicno + 2
selectblink = false
oldtime = love.timer.getTime()
end
elseif key == "down" or key == "s" then
if selection == 1 or selection == 2 or selection == 3 or selection == 4 then
selection = selection + 2
selectblink = true
oldtime = love.timer.getTime()
if selection > 2 and selection < 5 then
selection = musicno + 2
selectblink = false
oldtime = love.timer.getTime()
end
elseif selection == 5 or selection == 6 then
selection = gameno
selectblink = false
oldtime = love.timer.getTime()
end
end
if selection > 2 and key ~= "return" and key ~= "escape" then
musicno = selection - 2
if oldmusicno ~= musicno and oldmusicno ~= 4 then
love.audio.stop(music[oldmusicno])
end
if musicno < 4 then
love.audio.play(music[musicno])
end
elseif key ~= "return" and key ~= "escape" then
gameno = selection
loadhighscores()
end
elseif gamestate == "gameA" or gamestate == "gameB" or gamestate == "failingA" or gamestate == "failingB" then