Thanks to the CodingTrain for helping me reignite my gaming interest.
https://thecodingtrain.com/challenges/5-space-invaders
I've converted his P5JS code to DBPro code. It uses the BlueGui plugin canvas for drawing and works pretty well. Loads I/you could add/improve if you want.
Rem Project: Space_Invaders05_01
Rem Created: Friday, November 22, 2024
Rem ***** Main Source File *****
#constant grey rgb(51,51,51)
#constant white rgb(255,255,255)
#constant width screen width()
#constant height screen height()
#constant depth 32
`#constant up_arrow 200
`#constant down_arrow 208
#constant left_arrow 203
#constant right_arrow 205
#constant space 57
#constant true 1
#constant false 0
type ship_type
x as float
y as float
direct as float
endtype
type flower_type
x as float
y as float
r as float
xdir as float
right as boolean
edge as boolean
endtype
type drop_type
x as float
y as float
r as float
todelete as boolean
endtype
Setup()
do
Draw()
paintGadget canvid
sync
loop
end
function SetUp()
sync on : sync rate 30 : sync
set display mode width,height,depth
hide mouse
global canvid
canvid = createcanvas(0,0,width,height,0)
global edgeright as boolean
edgeright = false
global edgeleft as boolean
edgeleft = false
Dim Ship(0) as Ship_type
Dim Flowers(5) as flower_type
Dim Flowers(5) as flower_type
Dim Drops() as drop_type
Ship_init()
for f=0 to array count(Flowers())
Flower_Init(f,f*80+80,60)
next f
endfunction
function Draw()
cls grey
SetGadgetColor canvid, Grey, white
Ship_Show()
Ship_Setdir(Ship(0).direct)
Ship_Move(Ship(0).direct)
for i = 0 to array count(Drops())
Drop_Show(i)
Drop_Move(i)
for j=0 to array count(Flowers())
if drop_hits(i,j)
flower_grow(j)
drop_evaporate(i)
endif
next j
next i
right = false
for f=0 to array count(Flowers())
Flower_Show(f)
Flower_move(f)
if flowers(f).x > width - (flowers(f).r*2)
edgeright = true
endif
if flowers(f).x < 0
edgeleft = true
endif
if edgeright = true
flower_ShiftDown()
endif
if edgeleft = true
flower_ShiftRight()
endif
next f
for d=0 to array count(Drops())-1
if Drops(d).toDelete
drops_splice(d)
endif
next d
keyPressed()
drawText canvid , 0 , 0 , str$(scancode())
endfunction
function Ship_Init()
ship(0).x = width / 2
ship(0).direct = 0
endfunction
function Ship_Show()
sx as float
sy as float
sx = ship(0).x : sy = height-40
setDrawingcolor canvid, white, white, white
`rectmode(CENTER) // - interprets the first two parameters of rect() as the shape's center point,
// while the third and fourth parameters are its width and height.
drawRect canvid, sx, sy, sx+20, sy+40
endfunction
function Ship_Setdir(direct)
ship(0).direct = direct
endfunction
function Ship_Move(direct)
inc ship(0).x , direct*5
if ship(0).x > width-20
ship(0).x = width-20
endif
if ship(0).x <0
ship(0).x = 0
endif
endfunction
function Flower_Init(this,x as float ,y as float )
flowers(this).x = x
flowers(this).y = y
flowers(this).r = 30
flowers(this).xdir = 1
flowers(this).right = false
flowers(this).edge = false
endfunction
function Flower_Show(this)
sx as float
sy as float
sr as float
sx = flowers(this).x : sy = flowers(this).y : sr = flowers(this).r
setDrawingcolor canvid, rgb(255,0,200), rgb(255,0,200), white
drawEllipse canvid, sx, sy, sx+(sr*2), sy+(sr*2)
endfunction
function Flower_Move(this)
inc flowers(this).x , flowers(this).xdir
endfunction
function flower_ShiftDown()
for i = 0 to array count(flowers())
dec flowers(i).x, 1
flowers(i).xdir = -1
inc flowers(i).y, flowers(i).r
next i
edgeright = false
endfunction
function flower_ShiftRight()
for i = 0 to array count(flowers())
inc flowers(i).x, 1
flowers(i).xdir = 1
inc flowers(i).y, flowers(i).r
next i
edgeleft = false
endfunction
function Flower_grow(this)
inc flowers(this).r,2
endfunction
function Drop_Init(x as float ,y as float )
drops().x = x
drops().y = y
drops().r = 8
drops().todelete=false
endfunction
function Drop_Show(this)
sx as float
sy as float
sr as float
sx = drops(this).x : sy = drops(this).y : sr = drops(this).r
setDrawingcolor canvid, rgb(150,0,255), rgb(150,0,255), white
drawEllipse canvid, sx, sy, sx+(sr*2), sy+(sr*2)
endfunction
function Drop_Hits(this,that)
d as float
d = distance(drops(this).x,drops(this).y,flowers(that).x,flowers(that).y)
if d < drops(this).r + flowers(i).r
exitfunction true
else
exitfunction false
endif
endfunction false
function Drop_Move(this)
dec drops(this).y , 5
endfunction
function Drop_evaporate(this)
drops(this).todelete=true
endfunction
function drops_splice(this)
array delete element drops(0),this
endfunction
function drops_delete()
ac=array count(drops)
for x=ac to 0 step -1
if drops(x).todelete=true
array delete element drops(0),x
endif
next x
endfunction
function KeyPressed()
if scancode() = space and spacePressed = false
spacePressed = true
array insert at bottom(drops())
Drop_Init(ship(0).x,height-40)
else
`Ship_Setdir(0)
endif
if scancode() = 0 and spacePressed = true
spacePressed = false
endif
if scancode() = up_arrow
else
if scancode() = down_arrow
else
if scancode() = right_arrow
Ship_Setdir(1)
`Ship_Move(1)
else
if scancode() = left_arrow
Ship_Setdir(-1)
`Ship_Move(-1)
endif
endif
endif
endif
endfunction
function Distance(x1 as float , y1 as float , x2 as float , y2 as float )
r = sqrt((x2-x1)^2 + (y2-y1)^2 )
endfunction r