1 pico-8 cartridge // http://www.pico-8.com
2 version 32
3 __lua__
4 --https://ztiromoritz.github.io/pico-8-shooter/
5 --https://github.com/ztiromoritz/pico-8-shooter/tree/master/p8
6 function fps()
7 f = stat(7)
8 print("fps: "..f, 0, 120, 1)
9 end
10
11 t=0
12 bullets={}
13
14 function init_stars()
15 stars={}
16 star_cols={1,2,5,6,7,12}
17 warp_f=3
18 for i=1,#star_cols do
19 for j=1,10 do
20 local s={
21 x=rnd(128),
22 y=rnd(128),
23 z=i,
24 c=star_cols[i]
25 }
26 add(stars,s)
27 end
28 end
29 end
30
31 function init_enemies()
32 enemies={}
33 for i=1,10 do
34 add(enemies, {
35 sp=4,
36 m_x=i*16,
37 m_y=60-i*8,
38 x=-32,
39 y=-32,
40 r=12,
41 })
42 end
43 end
44
45 function draw_enemies()
46 for e in all(enemies) do
47 spr(e.sp,e.x,e.y)
48 end
49 end
50
51 function update_enemies()
52 for e in all(enemies) do
53 e.x = e.r*sin(t/50) + e.m_x
54 e.y = e.r*cos(t/50) + e.m_y
55 end
56 end
57
58 function init_player()
59 player={
60 h=3,
61 sp=1,
62 x=64,
63 y=64,
64 }
65 end
66
67 function draw_health()
68 inc=10
69 for i=1,player.h do
70 spr(2,(inc*i),1)
71 end
72 end
73
74 function draw_player()
75 spr(player.sp,player.x,player.y)
76 end
77
78 function update_player()
79 if(t%6<3) then
80 player.sp=0
81 else
82 player.sp=1
83 end
84 end
85
86 function fire()
87 local b={
88 sp=3,
89 x=player.x,
90 y=player.y,
91 dx=0,
92 dy=-3,
93 }
94 add(bullets,b)
95 end
96
97 function draw_bullets()
98 for b in all(bullets) do
99 spr(b.sp,b.x,b.y)
100 end
101 end
102
103 function draw_stars()
104 for s in all(stars) do
105 pset(s.x,s.y,s.c)
106 end
107 end
108
109 function update_stars()
110 for s in all(stars) do
111 s.y+=s.z*warp_f/10
112 if s.y>128 then
113 s.y=0
114 s.x=rnd(128)
115 end
116 end
117 end
118
119 function _init()
120 player_xpos=64
121 player_ypos=64
122 init_stars()
123 init_player()
124 init_enemies()
125 end
126
127 function _update60()
128 t=t+1
129 if ( t > 1000) then t=0 end
130
131 for b in all(bullets) do
132 b.x+=b.dx
133 b.y+=b.dy
134 if b.x < 0 or b.x > 128 or
135 b.y < 0 or b.y > 128 then
136 del(bullets,b)
137 end
138 end
139
140 if (btn(0) and player.x > 0) player.x -= 2
141 if (btn(1) and player.x < 127) player.x += 2
142 if (btn(2) and player.y > 0) player.y -= 2
143 if (btn(3) and player.y < 127) player.y += 2
144 if btnp(4) then fire() end
145
146 update_stars()
147 update_player()
148 update_enemies()
149 end
150
151 function _draw()
152 cls()
153 fps()
154 draw_health()
155 draw_stars()
156 draw_player()
157 draw_bullets()
158 draw_enemies()
159 end
160
161
162
163
164 __gfx__
165 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
166 005dd500005dd500005dd500000000000b3333b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
167 0455554004555540045555400000000000b33b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
168 04466440044664400446644000088000000bb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
169 07466470074664700746647000088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
170 07898970079898700700007000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
171 57089075570980755700007500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
172 55008055550800555500005500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000