-
Notifications
You must be signed in to change notification settings - Fork 0
/
xwing.p8
247 lines (209 loc) · 5.35 KB
/
xwing.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
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
t=0
function _init()
ship = {
x=60,
y=100,
sp=7,
llaserx=-1,
rlaserx=6,
leftshot=true,
health=5,
box = {x1=0, y1=0, x2=7, y2=7}
}
lasers = {}
enemies = {}
add(enemies, rndmenemy())
explosions = {}
stars = {}
for i=1,32 do
add(stars,{
x=rnd(128),
y=rnd(128),
s=rnd(2)+1
})
end
end
function rndmenemy()
local e = {
sp=8,
x= 32 + rnd(64),
y=-32,
dx= rnd(4) - 2,
dy= 2,
llaserx=2,
rlaserx=4,
leftshot=true,
countdown=rnd(30) + 10,
box = {x1=0, y1=1, x2=7, y2=5}
}
return e
end
function abs_box(s)
local box = {}
box.x1 = s.box.x1 + s.x
box.y1 = s.box.y1 + s.y
box.x2 = s.box.x2 + s.x
box.y2 = s.box.y2 + s.y
return box
end
function coll(a,b)
-- todo
local box_a = abs_box(a)
local box_b = abs_box(b)
if box_a.x1 > box_b.x2 or
box_a.y1 > box_b.y2 or
box_b.x1 > box_a.x2 or
box_b.y1 > box_a.y2 then
return false
end
return true
end
function explode(x,y)
add(explosions,{x=x,y=y,t=0})
end
function fire(parent)
local enemy = true
if parent == ship then
enemy = false
end
local offset = parent.rlaserx
if parent.leftshot then
offset = parent.llaserx
end
local dy = -3
local yoffset = -2
if enemy then
dy = 3
yoffset = 6
end
local l = {
sp = 16,
x = parent.x + offset,
y = parent.y + yoffset,
spw = .5,
sph = .5,
dx = 0,
dy = dy,
box = {x1=1,y1=0,x2=1,y2=4}
}
parent.leftshot = not parent.leftshot
add(lasers, l)
end
function _draw()
cls()
for st in all(stars) do
pset(st.x,st.y,6)
end
for h=1, ship.health do
spr(4, h*8, 0)
end
if (ship.health > 0) then
spr(ship.sp, ship.x, ship.y)
end
for ex in all(explosions) do
circfill(ex.x,ex.y,ex.t/2,8+ex.t%3)
end
for l in all(lasers) do
spr(l.sp, l.x, l.y, l.spw, l.sph)
end
for e in all(enemies) do
spr(e.sp, e.x, e.y)
end
end
function livupdate()
if t % 15 == 0 then
add(enemies, rndmenemy())
end
if btn(0) then ship.x -=2 end
if btn(1) then ship.x +=2 end
if btn(2) then ship.y -=2 end
if btn(3) then ship.y +=2 end
if btnp(4) then fire(ship) end
end
function _update()
t=t+1
for st in all(stars) do
st.y += st.s
if st.y >= 128 then
st.y = 0
st.x=rnd(128)
end
end
for ex in all(explosions) do
ex.t+=1
if ex.t == 13 then
del(explosions, ex)
end
end
local shipdamaged = false;
for e in all(enemies) do
e.x = e.x + e.dx
e.y = e.y + e.dy
if e.x < -8 or e.x > 136 or
e.y < -50 or e.y > 136 then
del(enemies, e)
end
if coll(ship, e) then
explode(ship.x,ship.y)
explode(e.x, e.y)
del(enemies, e)
shipdamaged = true
end
e.countdown = e.countdown - 1
if e.countdown <= 0 then
fire(e)
e.countdown = rnd(30) + 10
end
end
for l in all(lasers) do
l.x += l.dx
l.y += l.dy
local deleted = false
if l.x < 0 or l.x > 128 or
l.y < 0 or l.y > 128 then
del(lasers, l)
deleted = true
end
local hit = false
for e in all (enemies) do
if deleted == false and coll(l, e) then
del(enemies, e)
del(lasers, l)
explode(e.x,e.y)
deleted = true
end
end
if deleted == false and coll(l, ship) then
del(lasers, l)
explode(ship.x,ship.y)
shipdamaged = true
deleted = true
end
end
if shipdamaged == true then
ship.health = ship.health - 1
end
if ship.health > 0 then
livupdate()
end
end
__gfx__
e0000000000000000000000007700770000770000000000000000000000660005000000500000000000000000000000000000000000000000000000000000000
00000000000000067000000078877887077777700500000000000050800660085005500500000000000000000000000000000000000000000000000000000000
0070070000000006600000007888888707bbbb700500000000000050600550065055550500000000000000000000000000000000000000000000000000000000
000770000000000660000000078888707bbbbbb70500000000000050600550065550055500000000000000000000000000000000000000000000000000000000
000770000000000660000000078888707bbbbbb70500000550000050600660065555555500000000000000000000000000000000000000000000000000000000
0070070000000066670000000078870007bbbb700500005555000050666cc66650b55b0500000000000000000000000000000000000000000000000000000000
00000000008000666600080000077000077777700550055555500050666cc6665006600500000000000000000000000000000000000000000000000000000000
0000000080d00065d6000d0800000000000770000555555555555550000660005000000500000000000000000000000000000000000000000000000000000000
0b00080060d0006556000d0600000000000000000550055665500550000000000000000000000000000000000000000000000000000000000000000000000000
0b00080060ddd065560ddd0600000000000000000500005555000050000000000000000000000000000000000000000000000000000000000000000000000000
0b000800600ddd6556ddd00600000000000000000500008558000050000000000000000000000000000000000000000000000000000000000000000000000000
0b000800666666666666666600000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
00000000666866666666666600000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
000000000666666cc666666000000000000000000500000000000050000000000000000000000000000000000000000000000000000000000000000000000000
000000000006666cc666600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000666600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000