here is another small arcade-like demogame: how many shots you need to kill all enemies.(LEFT/RIGHT/SPACE)
it is not commented, but splitted into small and easy-to-understand parts.
setvirtualresolution(800,600)
//***********************************
type Tenemy
alive as integer
x_pos as integer
y_pos as integer
x_speed as integer
sprite as integer
endtype
dim ENEMY[10] as Tenemy
type Tshot
alive as integer
x_pos as integer
y_pos as integer
y_speed as integer
sprite as integer
endtype
dim SHOT[10] as Tshot
//***********************************
global PLAYER_XPOS as integer = 400
global PLAYER_SPRITE as integer
PLAYER_SPRITE = createsprite(0)
SetSpriteColor(PLAYER_SPRITE,0,255,0,255)
setspritesize(PLAYER_SPRITE,20,50)
global ENEMY_COUNTER as integer = 0
global SHOT_COUNTER as integer = 0
//***********************************
ENEMY_ADD(10)
//***********************************
do
if GetRawKeyState(37) then dec PLAYER_XPOS, 5
if GetRawKeyState(39) then inc PLAYER_XPOS, 5
if GetRawKeyPressed(32) then SHOT_ADD()
ENEMY_MOVE()
SHOT_MOVE()
COLLISION_TEST()
ENEMY_DRAW()
SHOT_DRAW()
setspriteposition(PLAYER_SPRITE,PLAYER_XPOS,550)
print("enemies left: " + str(ENEMY_COUNTER))
print("shots used: " + str(SHOT_COUNTER))
Sync()
loop
//***********************************
function ENEMY_ADD(counter)
for z = 0 to counter-1
if ENEMY[z].alive = 0
ENEMY[z].alive = 1
ENEMY[z].x_pos = random(0, 800)
ENEMY[z].y_pos = random(0, 300)
ENEMY[z].x_speed = random(1,5)
ENEMY[z].sprite = createsprite(0)
SetSpriteColor(ENEMY[z].sprite,255,0,0,255)
setspritesize(ENEMY[z].sprite,30,30)
endif
next
endfunction
function ENEMY_MOVE()
for z = 0 to 9
if ENEMY[z].alive = 1
inc ENEMY[z].x_pos, ENEMY[z].x_speed
if ENEMY[z].x_pos < 0 then ENEMY[z].x_pos = 800
if ENEMY[z].x_pos > 800 then ENEMY[z].x_pos = 0
endif
next
endfunction
function ENEMY_DRAW()
ENEMY_COUNTER = 0
for z = 0 to 9
if ENEMY[z].alive = 1
inc ENEMY_COUNTER
setspriteposition(ENEMY[z].sprite,ENEMY[z].x_pos,ENEMY[z].y_pos)
elseif ENEMY[z].alive = -1
deletesprite(ENEMY[z].sprite)
ENEMY[z].alive = 0
endif
next
endfunction
//***********************************
function SHOT_ADD()
for z = 0 to 9
if SHOT[z].alive = 0
inc SHOT_COUNTER
SHOT[z].alive = 1
SHOT[z].x_pos = PLAYER_XPOS+5
SHOT[z].y_pos = 550
SHOT[z].y_speed = 8
SHOT[z].sprite = createsprite(0)
SetSpriteColor(SHOT[z].sprite,255,255,0,255)
exitfunction
endif
next
endfunction
function SHOT_MOVE()
for z = 0 to 9
if SHOT[z].alive = 1
dec SHOT[z].y_pos,SHOT[z].y_speed
setspriteposition(SHOT[z].sprite,SHOT[z].x_pos,SHOT[z].y_pos)
if SHOT[z].y_pos < 0 then SHOT[z].alive = -1
endif
next
endfunction
function SHOT_DRAW()
for z = 0 to 9
if SHOT[z].alive = 1
setspriteposition(SHOT[z].sprite,SHOT[z].x_pos,SHOT[z].y_pos)
elseif SHOT[z].alive = -1
deletesprite(SHOT[z].sprite)
SHOT[z].alive = 0
endif
next
endfunction
//***********************************
function COLLISION_TEST()
for z1 = 0 to 9
if SHOT[z1].alive = 1
for z2 = 0 to 9
if ENEMY[z2].alive = 1
if GetSpriteCollision(SHOT[z1].sprite,ENEMY[z2].sprite)
SHOT[z1].alive = -1
ENEMY[z2].alive = -1
endif
endif
next
endif
next
endfunction