Original Space Shooting Game

Hello,

I have been working on this original game for a while, and as a beginning programmer, my intent with this was to be able to create a functional and somewhat fun original game. One problem that I’m having is getting the lasers to go away when they hit the asteroids. Thanks in advance to anybody who can help with this. Also, any other suggestions for improvements would really be appreciated.

pastebin.com/KXiqTXBV

@bfisher - the reason is simple

``````table.remove(A,i)
score=score+1
table.remove(L,i)
``````

The variable i is the number of an asteroid. It is not the number of the laser that might have hit it, so you are removing the wrong laser.

@bfisher Probably an easy way is to have a loop that draws the asteroids and a loop to draw the lasers. Then have another loop for asteroids and inside that loop, loop thru the laser table and check for laser/asteroid hits. If there’s a hit, remove the current asteroid and current laser and exit the loop. Also in that loop, check for asteroid/ship hits.

@dave1707 This is what I tried to do, but it still doesn’t work properly:

``````for i,a in pairs(A) do
for i,l in pairs(L) do
if a.w==50 and x>=a.x-a.w/2 and y>=a.y-a.h/2 and y<=a.y+a.h/2 then
table.remove(L,i)
score=score+1
table.remove(A,i)
end
if a.w==35 and x>=a.x-a.w/2 and y>=a.y-a.h/2 and y<=a.y+a.h/2 then
table.remove(A,i)
table.remove(L,i)
score=score+2
end
end
if a.x-(a.w/2)<=rocketX+40 and a.x+(a.w/2)>=rocketX-40 and a.y-(a.h/2)<=rocketY+10 and a.y+(a.h/2)>=rocketY-10        then gameState=4
end
if a.x-a.w/2<=rocketX+10 and a.x+a.w/2>=rocketX-30 and a.y-a.h/2<=rocketY+40 and a.y+a.h/2>=rocketY-40
then gameState=4
end
end
``````

Thanks again for the help.

You have two local i variables in the nested loops; change one to a different letter so they don’t overwrite each other.

Also, please indent properly. Your code is very hard to read and difficult to debug, the way it is.

@bfisher indenting your code properly is very easy: select your code, tap on it to get the popup bar, then tap Re-indent button, and you get this:

``````    do
for i,l in pairs(L) do
if a.w==50 and x>=a.x-a.w/2 and y>=a.y-a.h/2 and y<=a.y+a.h/2 then
table.remove(L,i)
score=score+1
table.remove(A,i)
end
if a.w==35 and x>=a.x-a.w/2 and y>=a.y-a.h/2 and y<=a.y+a.h/2 then
table.remove(A,i)
table.remove(L,i)
score=score+2
end
end
if a.x-(a.w/2)<=rocketX+40 and a.x+(a.w/2)>=rocketX-40 and a.y-(a.h/2)<=rocketY+10 and a.y+(a.h/2)>=rocketY-10        then gameState=4
end
if a.x-a.w/2<=rocketX+10 and a.x+a.w/2>=rocketX-30 and a.y-a.h/2<=rocketY+40 and a.y+a.h/2>=rocketY-40
then gameState=4
end
end
``````

@dave1707 , @Ignatz , and @TheSolderKing I was able to get the collision to work properly so thanks for helping me with that. Now I want to make it so that you can shoot triple lasers if you get a powerup. I am able to draw the lasers on the screen properly, but I can’t control each laser separately when they either go off the screen or hit an asteroid. This is what I have so far:

``````    for i,l in pairs(L) do
laserX=l.x
l.x=l.x+5
if l.x>WIDTH then
table.remove(L,i)
end
stroke(255,0,9,255)
strokeWidth(2)
line(l.x,l.y,l.x+50,l.y)
if powerupState==1 then
l.ys=l.ys+2
for t,l in pairs(L) do
if l.x>WIDTH or l.y+l.ys>HEIGHT then
table.remove(L,t)
end
line(l.x,l.y+l.ys,l.x+50,l.y+(l.ys+20))
end
for b,l in pairs(L) do
if l.x>WIDTH or l.y-l.ys<0 then
table.remove(L,b)
end
line(l.x,l.y-l.ys,l.x+50,l.y+(-(l.ys+20)))
end
end
end
``````