I am following a shoot-em-up game tutorial,and I ran into this problem where I want the speed of the stars go faster when I press the up button while they keep their separate colors. Also, the stars should slow down after I release the button.Right now the game starts out fine, with all the stars having correct color, but once the stars start moving faster, they lose their color. ALso, I am unable to slow down the stars after I press the up button.
function _init()
--variables
x=63
y=110
xv=1.5
yv=1.5
bulx=63
buly=110
bulv=0
bullet_moving = 0
ship_sprite=2
exhaust_sprite=5
muzzle_flash=0
score=0
lives=3
bombs=3
starx={}
stary={}
starspd={}
for i=1,100 do
add(starx,rnd(127))
add(stary,rnd(127))
add(starspd,rnd(1.2))
end
starspd_init={}
for i=1,#starspd do
add(starspd_init,starspd[i])
end
end
function _update()
--player movement
if btn(0) and not btn(2) and not btn(3) then
ship_sprite=1
if x>40 then
x=x-xv
end
for i=1,#starx do
if starx[i]<128 then
starx[i]=starx[i]+starspd[i]
else
starx[i]=starx[i]-128
end
end
end
if btn(1) and not btn(2) and not btn(3) then
ship_sprite=3
if x<86 then
x=x+xv
end
for i=1,#starx do
if starx[i]>0 then
starx[i]=starx[i]-starspd[i]
else
starx[i]=starx[i]+128
end
end
end
if not btn(1) and not btn(0) then
ship_sprite=2
end
if btn(2) then
for i=1,#starspd do
starspd[i]=2
end
else
for i=1,#starspd do
if not starspd[i]==starspd_init[i] then
for i=1,#starspd do
starspd[i]=starspd_init[i]
end
end
end
end
if btn(3) then
print("debug")
for i=1,#starspd do
if not starspd[i]==starspd_init[i] then
starspd[i]=starspd_init[i]
end
end
end
--player loop
if x<-5 then
x=128
end
if x>132 then
x=-1
end
--shooting
if btnp(5) and bullet_moving==0 then
sfx (0)
muzzle_flash=4
end
if btnp(5) or bullet_moving==1 then
buly=buly-2
bullet_moving=1
else
bulx=x
buly=y+1
end
if buly<-5 then
bullet_moving=0
bulx=x
buly=y+1
end
if muzzle_flash>0 then
muzzle_flash=muzzle_flash-1
end
--ship_exhaust
if exhaust_sprite>8 then
exhaust_sprite=5
elseif btn(3) then
exhaust_sprite=4
else
exhaust_sprite=exhaust_sprite+1
end
end
--background
function background()
for i=1,#starx do
local star_opacity=6
if starspd[i]<0.4 then
star_opacity=1
elseif starspd[i]<0.7 then
star_opacity=13
elseif starspd[i]<0.73 then
star_opacity=10
end
pset(starx[i],stary[i],star_opacity)
end
end
function _draw()
cls(0)
background()
spr(16,bulx,buly)
spr(ship_sprite,x,y)
spr(exhaust_sprite, x, y+6)
if muzzle_flash>0 then
circfill(x+3,y+2,muzzle_flash,7)
end
print("score: "..score,46,3)
spr(13,1,1)
for i=1,3 do
if lives>=i then
spr(11,i*9-6,2)
else
spr(12,i*9-6,2)
end
end
for i=1,3 do
if bombs>=i then
spr(14,i*9+90,2)
else
spr(15,i*9+90,2)
end
end
for i=1,#stary do
if stary[i]<128 then
stary[i]=stary[i]+starspd[i]
else
stary[i]=stary[i]-128
end
end
end
In the way the
background()function is written, the stars change color depending on how fast they move. Thestar_opacityis the defined color the stars will get.So if you speed up the stars, they'll all move faster, likely all having the
star_opacity=1at the end.And in the section where it should revert the star speed back to it's initial value, I've noticed you're doing the for loop twice there (
for i=1,#starspd do). I think removing the second for-loop would work. like this:This is unrelated, but personally, I would put the stars in a seperate object in a new tab, so each star holds their own values and color. And it would also make the main script more readable. But for now, I'd suggest to keep sticking to the tutorial. Maybe they'll talk more about that later on.