11-07-2024, 05:38 PM
I'm on a one hour train trip so I started writing something in the style of galaga. I won't conitnue working on it, but I'm still posting the code.
Code:
constant ECOLS = 12, EROWS = 8
visible plyImg, enm1Img, enm2Img, enm3Img, enm4Img
set window "Attackers", 256, 224, false, 3
set redraw off
CreateAssets()
randomize 2
enms = dim(ECOLS, EROWS)
for y = 1 to 5 for x = 0 to ECOLS/2 - 1
if rnd(4) > 0
select rnd(4)
case 0
img = enm1Img
stm = 1
case 1
img = enm2Img
stm = 2
case 2
img = enm3Img
stm = 3
default
img = enm4Img
stm = 4
endsel
e = [
img: img,
a: 0,
stm: stm,
pos: Point(0, 0),
size: Point(14, 14),
c: fill(Point(0, 0), 4),
p: unset]
else
e = unset
endif
enms[x][y] = e
enms[ECOLS - 1 - x][y] = copy(e)
next
enmsa = 0
divet = 120
tmp = []
enmbullets = []
enmshoott = 180
ply = []
ply.pos = Point(120, 200)
ply.size = Point(16, 16)
ply.spd = 1
plybullets = []
while not keydown(KEY_ESCAPE, true)
i = 0
while i < sizeof(plybullets)
b = plybullets[i]
b.pos.x = b.pos.x + b.spd.x
b.pos.y = b.pos.y + b.spd.y
if b.pos.x < -b.size.x or b.pos.x >= 256 or b.pos.y < -b.size.y or b.pos.y >= 224
free key plybullets, i
else
hit = false
for y = EROWS - 1 to 0
for x = 0 to ECOLS - 1
e = enms[x][y]
if e and Collides(b, e)
e.stm = e.stm - 1
if e.stm <= 0 enms[x][y] = unset
hit = true
break
endif
next
if hit break
next
if hit free key plybullets, i
else i = i + 1
endif
wend
i = 0
while i < sizeof(enmbullets)
b = enmbullets[i]
b.pos.x = b.pos.x + b.spd.x
b.pos.y = b.pos.y + b.spd.y
if b.pos.x < -b.size.x or b.pos.x >= 256 or b.pos.y < -b.size.y or b.pos.y >= 224
free key enmbullets, i
else
i = i + 1
endif
wend
if keydown(KEY_LEFT) ply.pos.x = max(ply.pos.x - ply.spd, 0)
if keydown(KEY_RIGHT) ply.pos.x = min(ply.pos.x + ply.spd, 240)
if keydown(KEY_SPACE, true)
plybullets[sizeof(plybullets)] = [
pos: Point(ply.pos.x + ply.size.x/2 - 1, ply.pos.y - 4),
size: Point(2, 4),
spd: Point(0, -4)]
endif
divet = divet - 1
if divet <= 0
clear tmp
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
if enms[x][y] and enms[x][y].p = unset tmp[sizeof(tmp)] = enms[x][y]
next
if sizeof(tmp)
e = tmp[rnd(sizeof(tmp))]
e.p = 0
e.c[0].x = e.pos.x; e.c[0].y = e.pos.y
e.c[1].x = rnd(242); e.c[1].y = 224
e.c[2].x = rnd(242); e.c[2].y = 224
e.c[3].x = e.pos.x; e.c[3].y = e.pos.y
divet = 120 + rnd(3)*60
else
divet = 60
endif
endif
enmshoott = enmshoott - 1
if enmshoott < 0
enmshoott = 180 + rnd(3)*60
clear tmp
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
if enms[x][y] for i = 0 to enms[x][y].p <> unset tmp[sizeof(tmp)] = enms[x][y]
next
if sizeof(tmp)
e = tmp[rnd(sizeof(tmp))]
dx = ply.pos.x - e.pos.x
dy = ply.pos.y - e.pos.y
k = sqr(dx*dx + dy*dy)
if k > 0
dx = 2*dx/k
dy = 2*dy/k
enmbullets[sizeof(enmbullets)] = [
pos: Point(e.pos.x + e.size.x/2 - 2, e.pos.y + e.size.y/2 - 2),
size: Point(4, 4),
spd: Point(dx, dy)]
endif
endif
endif
enmsa = (enmsa + 1)%360
for y = 0 to EROWS - 1
offsx = 128 - ECOLS*8 + sin(rad(enmsa + y*22.5))*24
offsy = 24
for x = 0 to ECOLS - 1
e = enms[x][y]
if e
gx = x*16 + offsx + 1; gy = y*16 + offsy + 1
if typeof(e.p)
e.p = e.p + 0.005
if e.p >= 1
e.p = unset
e.pos.x = gx; e.pos.y = gy
else
e.c[3].x = gx; e.c[3].y = gy
EvalCBCD(e.pos, e.c, e.p)
e.a = atan2(e.pos.y, e.pos.x) - 0.5*PI
if e.p < 0.2 e.a = e.a*5*e.p
elseif e.p >= 0.8 e.a = e.a*(1 - e.p)/0.2
EvalCBC(e.pos, e.c, e.p)
endif
else
e.pos.x = gx
e.pos.y = gy
endif
endif
next
next
set color 0, 0, 0
cls
set color 255, 255, 255
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
e = enms[x][y]
if e and e.p = unset draw image e.img, e.pos.x, e.pos.y
next
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
e = enms[x][y]
if e and typeof(e.p)
draw image xform e.img, e.pos.x + 7, e.pos.y + 7, 1, 1, e.a, 7, 7
endif
next
draw image plyImg, ply.pos.x, ply.pos.y
set color 255, 255, 255
foreach b in plybullets draw rect b.pos.x, b.pos.y, b.size.x, b.size.y, true
set color 0, 255, 255
foreach b in enmbullets draw rect b.pos.x, b.pos.y, b.size.x, b.size.y, true
set color 255, 255, 255
set caret 0, 0
wln sizeof(plybullets)
wln sizeof(enmbullets)
redraw
fwait 60
wend
function Point(x, y)
return [x: x, y: y]
endfunc
function EvalCBC(dst, curve, param)
b0 = (1 - param)^3
b1 = 3*(1 - param)^2*param
b2 = 3*(1 - param)*param^2
b3 = param^3
dst.x = b0*curve[0].x + b1*curve[1].x + b2*curve[2].x + b3*curve[3].x
dst.y = b0*curve[0].y + b1*curve[1].y + b2*curve[2].y + b3*curve[3].y
endfunc
function EvalCBCD(dst, curve, param)
b0 = 3*(1 - param)^2
b1 = 6*(1 - param)*param
b2 = 3*param^2
dst.x = b0*(curve[1].x - curve[0].x) + b1*(curve[2].x - curve[1].x) + b2*(curve[3].x - curve[2].x)
dst.y = b0*(curve[1].y - curve[0].y) + b1*(curve[2].y - curve[1].y) + b2*(curve[3].y - curve[2].y)
endfunc
function Collides(a, b)
return a.pos.x + a.size.x > b.pos.x and a.pos.x < b.pos.x + b.size.x and
a.pos.y + a.size.y > b.pos.y and a.pos.y < b.pos.y + b.size.y
endfunc
function CreateAssets()
plyImg = CreateBitmap(
[[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,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 1, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0],
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0]],
128, 0, 255)
endfunc
function CreateBitmap(data, r, g, b)
img = createimage(sizeof(data[0]), sizeof(data))
set image img
for y = 0 to sizeof(data) - 1 for x = 0 to sizeof(data[0]) - 1
set color data[y][x]*r, data[y][x]*g, data[y][x]*b
set pixel x, y
next
set image primary
set image colorkey img, 0, 0, 0
return img
endfunc