-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastero.p8
1536 lines (1437 loc) · 53.1 KB
/
astero.p8
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
pico-8 cartridge // http://www.pico-8.com
version 8
__lua__
--astero
--alex morris
state=0
--0==menu
--1==gameplay
--2==game over
hi_score=0
new_hs=false
function _init()
if state==0 then
init_menu()
elseif state==1 then
init_game()
else
init_go()
end
end
function _update()
if state==0 then
update_menu()
elseif state==1 then
update_game()
else
update_go()
end
end
function _draw()
if state==0 then
draw_menu()
elseif state==1 then
draw_game()
else
draw_go()
end
end
----------------------------
--game state----------------
----------------------------
function init_game()
-- start music
music(0)
--game globals
clock=0
score=0
lives=5
curr_enemy=""
won=false
--init background
stars={}
for x=0,128 do
for y=0,128 do
if rnd(1)<0.003 then
local s={}
s.x=x
s.y=y
s.dy=flr(rnd(3))+2
--light cols faster for parallax
if s.dy==2 then s.col=1
elseif s.dy==3 then s.col=13
else s.col=6 end
add(stars,s)
end
end
end
--init player
pl={}
pl.i=1
pl.mi=1
pl.li=0
pl.ri=2
pl.w=8
pl.h=7
pl.x=64-4
pl.y=85
pl.dx=2
pl.dy=2
pl.ft=0
--player weapon
bullets={}
equip_single()
p_ups={}
--enemies
enemies={}
e_bullets={}
e_smoke={}
--waves
never=6000
w_dur=1800
wt=90--wv text var
wc=7--wv txt var
w1()
--init thrust particles
thrust={}
--sfx
scores={}
dust={}
cam_t=0
cam_x=-1
circs={}
end
function update_game()
--update clock
clock=(clock+1)%3000
--if end of wave
if clock>=w_dur then
clock=0
--kill all enemies
foreach(enemies,kill_e)
foreach(e_bullets,kill_eb)
--show wave text
wt=90
if wave.last_wave then
music(-1)
won=true
else
wave.nw()
end
end
--update wave text
if wt>0 then
wt-=1
wc=(wc+1)%5+7
elseif won then
game_over()
end
--camera shake
if cam_t>0 then cam_t-=1 end
--update bg
foreach(stars,upd_star)
--update player
upd_pl()
--spawn enemies
if not won then
if clock!=0 then
if clock%wave.w_clock==0 then
if wave.w_count then
local mx=128/wave.w_count
for i=0,wave.w_count-1 do
make_warden(flr(rnd(mx))+mx*i-18)
end
else
make_warden()
end
end
if clock%wave.s_clock==0 then
if wave.s_count then
local mx=128/wave.s_count
for i=0,wave.s_count-1 do
make_sentry(flr(rnd(mx))+mx*i-18)
end
else
make_sentry()
end
end
if clock%wave.j_clock==0 then
make_jackal()
end
if clock%wave.a_clock==0 then
make_alpha()
end
end
end
--update enemies
foreach(enemies,upd_e)
--update enemy bullets
foreach(e_bullets,upd_b)
--update player bullets
foreach(bullets,upd_b)
--update sfx
foreach(thrust,upd_thrust)
foreach(circs,upd_circ)
foreach(scores,upd_score)
foreach(dust,upd_dust)
foreach(e_smoke,e_upd_smoke)
end
function draw_game()
cls()
if cam_t>0 then
cam_x*=-1
camera(cam_x,0)
else camera() end
--draw bg
foreach(stars,dr_star)
--draw enemy smoke
foreach(e_smoke,e_dr_smoke)
--draw player bullets
foreach(bullets,dr_b)
--draw thrust
foreach(thrust,dr_thrust)
--draw player
dr_pl()
--draw enemies
foreach(enemies,dr_e)
--draw e bullets
foreach(e_bullets,dr_b)
--draw powerups
foreach(circs,dr_circ)
foreach(p_ups,dr_p_up)
--draw dust
foreach(dust,dr_dust)
--draw scores
foreach(scores,dr_score)
--ui
rectfill(0,119,128,128,1)
print_bold("score "..score,3,121,12)
print_lives()
--hud (enemies and weapon)
print(curr_enemy,125-#curr_enemy*4,2,8)
local w_text=pw.name
if pw.et then
w_text=w_text.." 00:0"..flr(pw.et/30)
end
print(w_text,2,2,pw.c)
--wave text
if wt>0 and not won then
print(wave.name,64-#wave.name*2,56,wc)
end
end
---------
--waves--
---------
function w1()
wave={}
wave.name="wave 1"
wave.nw=w2
wave.w_clock=150
wave.s_clock=55
wave.j_clock=never
wave.a_clock=never
end
function w2()
wave={}
wave.name="wave 2"
wave.nw=w3
wave.w_clock=never
wave.s_clock=85
wave.s_count=2
wave.j_clock=180
wave.a_clock=never
end
function w3()
wave={}
wave.name="wave 3"
wave.nw=w4
wave.w_clock=100
wave.w_count=2
wave.s_clock=never
wave.j_clock=50
wave.a_clock=never
end
function w4()
wave={}
wave.name="wave 4"
wave.nw=w5
wave.w_clock=150
wave.s_clock=90
wave.s_count=2
wave.j_clock=180
wave.a_clock=never
end
function w5()
wave={}
wave.name="wave 5"
wave.nw=w6
wave.w_clock=never
clock=100
wave.s_clock=140
wave.s_count=3
wave.j_clock=190
wave.a_clock=200
end
function w6()
wave={}
wave.name="final wave"
wave.last_wave=true
clock=90
wave.w_clock=190
wave.w_count=2
wave.s_clock=180
wave.s_count=3
wave.j_clock=350
wave.a_clock=200
end
----------------------
--game state helpers--
----------------------
--player functions
function kill_pl()
sfx(22)
sfx(23)
--explosion
make_circ(pl.x+pl.w/2,
pl.y+pl.h/2,8)
make_dust(pl.x+pl.w/2,
pl.y+pl.h/2,8)
lives-=1
if lives<0 then
music(-1)
pl.dead=true
end
pl.ft=60
equip_single()
end
function upd_pl()
--hurt and dying
if pl.ft>0 then
pl.ft-=1
if pl.ft==0 and pl.dead then
game_over()
return
end
end
if pl.dead then return end
--move
if btn(0) then
pl.x-=pl.dx
pl.i=pl.li
end
if btn(1) then
pl.x+=pl.dx
pl.i=pl.ri
end
if not btn(0) and not btn(1) or
btn(0) and btn(1) then
pl.i=pl.mi
end
if btn(2) then pl.y-=pl.dy end
if btn(3) then pl.y+=pl.dy end
if pl.x>128-pl.w then pl.x=128-pl.w end
if pl.x<0 then pl.x=0 end
if pl.y>119-pl.h then pl.y=119-pl.h end
if pl.y<0 then pl.y=0 end
--collide with p_ups
foreach(p_ups,upd_p_up)
foreach(p_ups, function(p)
if colliding(p,pl) then
--equip the right weapon
if p.i==50 then equip_laser()
elseif p.i==49 then equip_fan()
elseif p.i==51 then equip_double()
else equip_tri() end
sfx(21)
make_circ(p.x+p.w/2,
p.y+p.h/2,p.c)
add_score(p.x+p.w/2,
p.y+p.h/2,10)
del(p_ups,p)
end
end)
--update weapon and bullets
pw.t+=1
if pw.et then
if pw.et>0 then pw.et-=1 end
if pw.et<=0 then equip_single() end
end
if btn(4) or btn(5) then
pw.fire()
end
--player death
foreach(e_bullets, function(b)
if colliding(b,pl) and
pl.ft<=0 then
kill_pl()
kill_eb(b)
end
end)
foreach(enemies, function(e)
if colliding(e,pl) and
pl.ft<=0 then
kill_pl()
kill_e(e)
end
end)
--make thrust
if clock%2==0 then make_thrust() end
end
function dr_pl()
if pl.dead then return end
if clock%3==0 or pl.ft<=0 then
spr(pl.i,pl.x,pl.y)
end
end
--bg--
function upd_star(s)
s.y+=s.dy
if s.y>127 then s.y=0 end
end
function dr_star(s)
pset(s.x,s.y,s.col)
end
--collisions
function colliding(r1,r2)
if r1.x<r2.x+r2.w and
r1.x+r1.w>r2.x and
r1.y<r2.y+r2.h and
r1.y+r1.h>r2.y then
return true
end
return false
end
------------
--powerups--
------------
function make_circ(x,y,col)
local c={}
c.x=x
c.y=y
c.w=0
c.col=col
add(circs,c)
end
function upd_circ(c)
c.w+=3
if c.w>128 then del(circs,c) end
end
function dr_circ(c)
local col=c.col
if clock%3==0 then col=7 end
circ(c.x,c.y,c.w,col)
end
function make_p_up(x,y)
local p={}
local r=flr(rnd(4))
if r==0 then
p.i=50
p.c=11
elseif r==1 then
p.i=49
p.c=10
elseif r==2 then
p.i=51
p.c=12
else
p.i=48
p.c=14
end
p.w=8
p.h=8
p.x=x-p.w/2
p.y=y-p.h/2
p.dy=.25
add(p_ups,p)
end
function upd_p_up(p)
p.y+=p.dy
if p.y>128 then
del(p_ups,p)
return
end
end
function dr_p_up(p)
spr(p.i,p.x,p.y)
end
-----------
--weapons--
-----------
function kill_b(b)
--kill bullet
sfx(20)
make_dust(b.x+b.w/2,
b.y+b.h/2,4)
del(bullets,b)
end
function upd_b(b)
b.upd()
end
function dr_b(b)
b.dr()
end
--single shot
function equip_single()
pw={}
pw.name=""
pw.c=12
pw.t=0
pw.wt=7
pw.os=-1
pw.fire=function()
if pw.t<pw.wt then return end
pw.t=0
sfx(0)
local b={}
b.w=5
b.h=5
b.x=pl.x+pl.w/2-b.w/2+pw.os
pw.os*=-1
b.y=pl.y
b.dy=-4
b.upd=function()
b.y+=b.dy
if b.y<-b.h then del(bullets,b) end
end
b.dr=function()
spr(17,b.x,b.y)
end
add(bullets,b)
end
end
--laser
function equip_laser()
pw={}
pw.name="laser"
pw.c=11
pw.t=0
pw.wt=0
pw.et=150
pw.fire=function()
if pw.t<pw.wt then return end
pw.t=0
sfx(15)
local b={}
b.w=4
b.h=8
b.x=pl.x+pl.w/2-b.w/2
b.y=pl.y+1
b.dy=-6
b.upd=function()
b.y+=b.dy
if b.y<=-b.h then del(bullets,b) end
end
b.dr=function()
spr(3,b.x,b.y)
end
add(bullets,b)
end
end
--fan
function equip_fan()
pw={}
pw.name="fan shot"
pw.c=10
pw.t=0
pw.wt=6
pw.et=150
pw.os=-1
pw.fire=function()
if pw.t<pw.wt then return end
pw.t=0
sfx(16)
for i=-1,1 do
local b={}
b.w=5
b.h=5
b.x=pl.x+pl.w/2-b.w/2
b.y=pl.y+pl.h/2
b.dx=i
b.spd=3
if b.dx==0 then
b.dy=-3
b.x+=pw.os
pw.os*=-1
else b.dy=-2 end
b.upd=function()
b.x+=b.dx*b.spd
b.y+=b.dy*b.spd
if b.x<-b.w or b.x>128 or
b.y<-b.h or b.y>128 then
del(bullets,b)
end
end
b.dr=function()
spr(18,b.x,b.y)
end
add(bullets,b)
end
end
end
--tri
function equip_tri()
pw={}
pw.name="tri shot"
pw.c=14
pw.t=0
pw.wt=6
pw.et=180
pw.os=-1
pw.fire=function()
if pw.t<pw.wt then return end
pw.t=0
sfx(17)
for i=-1,1 do
local b={}
b.w=5
b.h=5
b.x=pl.x+pl.w/2-b.w/2
b.y=pl.y+pl.h-b.h
b.dx=i
b.spd=6
if b.dx==0 then
b.dy=-1
b.x+=pw.os
pw.os*=-1
else b.dy=0 end
b.upd=function()
b.x+=b.dx*b.spd
b.y+=b.dy*b.spd
if b.x<-b.w or b.x>128 or
b.y<-b.h or b.y>128 then
del(bullets,b)
end
end
b.dr=function()
spr(19,b.x,b.y)
end
add(bullets,b)
end
end
end
--double shot
function equip_double()
pw={}
pw.name="double shot"
pw.c=12
pw.t=0
pw.wt=6
pw.et=180
pw.fire=function()
if pw.t<pw.wt then return end
pw.t=0
sfx(24)
--make two bullets
for i=0,1 do
local b={}
b.w=3
b.h=8
b.x=pl.x+(pl.w-b.w)*i
b.y=pl.y-b.h/2
b.dy=-4
b.upd=function()
b.y+=b.dy
if b.y<-b.h then del(bullets,b) end
end
b.dr=function()
spr(33,b.x,b.y)
end
add(bullets,b)
end
end
end
-----------------
--enemy weapons--
-----------------
function kill_eb(b)
--kill bullet
sfx(20)
make_dust(b.x+b.w/2,
b.y+b.h/2,4)
del(e_bullets,b)
end
function e_make_single(x,y,dx,dy)
local b={}
b.w=4
b.h=4
b.x=x-b.w/2
b.y=y-b.h/2
b.dx=dx
b.dy=dy
b.upd=function()
b.x+=b.dx
b.y+=b.dy
if b.x<-b.w or b.x>127 or b.y>127 then
del(e_bullets,b)
end
end
b.dr=function()
spr(36,b.x,b.y)
end
add(e_bullets,b)
end
function e_make_smoke(x,y,down)
local s={}
s.x=x+flr(rnd(5))-2
s.y=y
s.dy=-3
if down then s.dy*=-1 end
s.w=-1
add(e_smoke,s)
end
function e_upd_smoke(s)
s.y+=s.dy
if s.y<-s.w or s.y>128+s.w then
del(e_smoke,s)
end
if clock%2==0 then
s.w+=1
end
if s.w>5 then s.w=5 end
end
function e_dr_smoke(s)
circfill(s.x,s.y,s.w+2,15)
circfill(s.x+1,s.y+1,s.w,7)
end
function e_make_laser(x,y)
sfx(15)
local b={}
b.w=4
b.h=8
b.x=x-b.w/2
b.y=y
b.dy=8
if clock%2==0 then
make_dust(b.x+b.w/2,y+8,4)
end
b.upd=function()
b.y+=b.dy
if b.y>=128 then del(e_bullets,b) end
end
b.dr=function()
spr(37,b.x,b.y)
end
add(e_bullets,b)
end
-----------
--enemies--
-----------
function dmg_e(e)
--coll w bullets
for b in all(bullets) do
if colliding(b,e) then
e.ft=5
e.health-=1
if e.health<=0 then
kill_e(e)
end
kill_b(b)
--print enemy name
curr_enemy=e.name
end
end
end
function kill_e(e)
add_score(e.x+e.w/2,
e.y+e.h/2,e.pts)
del(enemies,e)
make_dust(e.x+e.w/2,
e.y+e.h/2,8)
sfx(19)
cam_t=5
--make powerup
if rnd(1)<.1 then
make_p_up(e.x+e.w/2,e.y+e.h/2)
end
end
function upd_e(e)
dmg_e(e)
--flash timer
if e.ft>0 then e.ft-=1 end
e.upd()
end
function dr_e(e)
local fl=false
if e.ft>0 and clock%3==0 then
fl=true
pal(13,7)
pal(2,6)
end
spr(e.i,e.x,e.y,e.cols,e.rows)
if fl then pal() end
end
--warden
function make_warden()
local w={}
w.name="warden"
w.i=4
w.cols=2
w.rows=2
w.w=16
w.h=16
w.x=flr(rnd(128-w.w))
w.y=-w.h
w.dx=1
if flr(rnd(2))==1 then w.dx*=-1 end
w.dy=.5
w.health=4
w.pts=10
w.ft=0
w.st=flr(rnd(50))
w.upd=function()
--movement
w.x+=w.dx
w.y+=w.dy
--keep on screen
if w.x<0 then
w.x=0
w.dx*=-1
elseif w.x>127-w.w then
w.x=127-w.w
w.dx*=-1
end
if w.y>128 then del(enemies,w) end
--shoot bullets
w.st-=1
if w.st<1 then
w.st=50
e_make_single(w.x+w.w/2,
w.y+w.h,0,2)
end
end
add(enemies,w)
end
--sentries
function make_sentry(x)
local s={}
s.name="sentry"
s.i=8
s.cols=2
s.rows=2
s.w=s.cols*8
s.h=s.rows*8
s.x=x or flr(rnd(128-s.w))
s.y=-s.h
s.dy=0.5
s.health=3
s.pts=5
s.ft=0
s.st=flr(rnd(60))
s.upd=function()
--move
s.y+=s.dy
if s.y>128 then del(enemies,s) end
if s.x<0 then
s.x=0
elseif s.x>127-s.w then
s.x=127-s.w
end
--fire
s.st-=1
if s.st<1 then
s.st=80
for i=-1,1 do
if i!=0 then
e_make_single(s.x+s.w/2,
s.y+s.h,i,1)
end
end
end
end
add(enemies,s)
end
--jackals
function make_jackal()
local j={}
j.name="jackal"
j.cols=2
j.rows=1
j.w=j.cols*8
j.h=j.rows*8
j.x=-j.w
j.i=10
j.dx=3
j.st=flr(rnd(6))+5
if rnd(1)<.5 then
j.x=128
j.i=12
j.dx=-3
end
j.y=flr(rnd(20))
j.health=2
j.pts=15
j.ft=0
j.upd=function()
--move
j.x+=j.dx
if j.x<-j.w or j.x>128 then
del(enemies,j)
end
--make missiles
j.st-=1
if j.st<1 then
j.st=25
sfx(25)
make_missile(j.x+j.w/2,j.y+j.h)
end
end
add(enemies,j)
end
--missiles
function make_missile(x,y)
local m={}
m.name="missile"
m.cols=1
m.rows=1
m.w=m.cols*8
m.h=m.rows*8
m.x=x
m.y=y
m.dy=0
m.ddy=.25
m.i=14
m.os=-1
m.health=1
m.pts=15
m.ft=0
m.upd=function()
m.x+=m.os
m.os*=-1
m.y+=m.dy
m.dy+=m.ddy
if m.dy>2 then m.dy=2 end
if m.y<-m.h or m.y>128 then
del(enemies,m)
end
if clock%2==0 then
e_make_smoke(m.x+m.w/2,m.y-1,false)
end
end
add(enemies,m)
end
--alpha
function make_alpha()
local a={}
a.name="alpha"
a.cols=4
a.rows=1
a.w=a.cols*8
a.h=a.rows*8
a.x=-a.w
a.i=10
a.dx=1
if rnd(1)<.5 then
a.x=128
a.dx=-1
end
a.y=flr(rnd(9))+2
a.health=9
a.pts=20
a.ft=0
a.st=50