Attempting to code cannon

@NG_2000 Here’s an updated version. Compare this to the previous version to see what changed. I added some comments to help you understand what’s happening. If you want to do something if a rock isn’t hit and makes it off the left side of the screen, see the code near the bottom for `if b.x<-20 then`. I have code to remove the rock from the table and create another random rock.

``````Removed code, see updated code below.
``````

Now Trying to Have when the explosion ball hits fire rock there is an explosion and fire rock disappears. No idea what to do

``````Removed code.
``````

I’m not sure what happened but I was adding power ups and the ends got mixed up somewhere.

``````displayMode(FULLSCREEN)

function setup()
rTab={}     --table for multiple rocks
cTab={}     --table for multiple cubes
sTab={}
x,y=WIDTH*1/8,HEIGHT/2  -- position of ship
a,b=-100,WIDTH/2
ang=0       -- starting angle for ship
mTab={}     -- table for multiple missiles
mSpeed=10  -- speed of the missile
tx,ty=0,0
delay=10    -- used with limit
limit=10    -- delay between missile shots
maxRocks=3 -- max number of rocks to show
maxCubes=1  -- max number of cubes
maxSoldiers=1
createRock()
createCube()
createSoldier()
end

function createRock()   -- create up to maxRocks on the screen
while #rTab<maxRocks do
table.insert(rTab,vec3(WIDTH+20,math.random(760),math.random(-5,-1)))
end
end

function createCube()   -- create up to maxCubes on the screen
while #cTab<maxCubes do
table.insert(cTab,vec3(WIDTH+20,math.random(760),math.random(-6,-4)))
end
end

function createSoldier()
while #sTab<maxSoldiers do
table.insert(sTab,vec3(WIDTH+20,math.random(760),math.random(-10,-1)))
end
end

function touched(touch)
if touch.state==BEGAN or touch.state==MOVING then
if not gameOver then
ang=math.deg(math.atan(touch.y-y,touch.x-x))
shoot=true
tx=touch.x
ty=touch.y
end
end
if touch.state==ENDED then
shoot=false
end

end
function draw()

background()
--if not gameOver then

fill(255, 0, 0, 255)

sprite("Documents:battle ground", WIDTH/2, HEIGHT/2, 1700, 900)
pushMatrix()

fontSize(20)
text("Bullet Speed;", WIDTH*26/100, HEIGHT*9/10)

text(mSpeed, WIDTH/3, 9/10*HEIGHT)
text("Bullet Delay")
text(limit, WIDTH*3/4, 9/10*HEIGHT)

translate(x,y)
rotate(ang-270)
sprite("Tyrian Remastered:Boss B", 0,0,90,90)
translate()
popMatrix()
--end

-- loop thru missile table
for a,b in pairs(mTab) do
sprite("Tyrian Remastered:Explosion Ball",b.x,b.y)
b.x=b.x+b.z*mSpeed
b.y=b.y+b.w*mSpeed
if b.x>WIDTH or b.x<0 and b.y>HEIGHT or b.y<0 then
table.remove(mTab,a)
end

-- check if missile and rocks are less than 30 pixels apart
v1=vec2(b.x,b.y)    -- missle x,y values
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<30 then
table.remove(rTab,a1)   -- remove rock from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createRock()    -- create rocks
end
end

for a2, b2 in pairs(sTab) do
d=v1:dist(vec2(b2.x,b2.y))
if d<40 then
table.remove(sTab, a2)
sprite("Tyrian Remastered:Space Ice 3",b2.x,b2.y)
sound(SOUND_EXPLODE, 39131)
createSoldier()
limit=limit-1
end
end

-- check if missile and cubes are less than 10 pixels apart
for a1,b1 in pairs(cTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<10 then
table.remove(cTab,a1)   -- remove cube from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createCube()    -- create cubes
mSpeed=mSpeed+1
end
end

-- check if a rock hits the ship
v1=vec2(x,y)    -- position of ship
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and ship
if d<50 then
table.remove(rTab,a1)
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
gameOver=true
shoot=false -- dont allow any shooting
x,y=999,999 -- move ship off screen
end
end
fill(253, 4, 4, 255)

-- delay shooting
if shoot and delay>limit then
v1=vec2(tx-x,ty-y)
v1=v1:normalize()
table.insert(mTab,vec4(x,y,v1.x,v1.y))
delay=0
sound(SOUND_SHOOT, 15498)
end

-- loop thru rock table and draw them
for a,b in pairs(rTab) do
sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
b.x=b.x+b.z -- decrement rock position
if b.x<-20 then     -- rocks are off the left side of the screen
table.remove(rTab,a)    -- remove rock from table
createRock()    -- create another random rock
end
end

-- loop thru cube table and draw them
for c,d in pairs(cTab) do
sprite("Tyrian Remastered:Energy Cube",d.x,d.y)
d.x=d.x+d.z -- decrement cube position
if d.x<-20 then     -- cubes are off the left side of the screen
table.remove(cTab,c)    -- remove cube from table
createCube()
end
end

for e,f in pairs(sTab) do
sprite("Tyrian Remastered:Satellite",f.x,f.y, 50, 50)
f.x=f.x+f.z
if f.x<-20 then
table.remove(sTab,e)
createSoldier()
end
end

end
if gameOver == true then

background(212, 35, 35, 255)
text("GAME OVER", WIDTH/2, HEIGHT/2)
fontSize(50)
end

end

``````

Also for some reason the white rectangle in setup flashes up, then disappears.
Don’t know why.

Look over this code to see the changes I made. I removed the code that draws the white rectangle. You shouldn’t have rect in setup. Not sure what you were going to do with it.

EDIT: I also slowed down the number of bullets by increasing limit.

``````Removed code, see updated code below.
``````

There will be power ups

Tried to make cubes explode when they got shot, but they didn’t expolode. I tried to make the a1 and do the same. Also the cTab.

``````Removed code, see latest code below.
``````

Ok thanks. I’ll do as much as I can.

Hi, just wondering, I’m trying to make the cubes rarer then the rocks, sort of like power ups. I feel like it has somthing to do with the vec, but I can’t figure it out. It also make the rocks go extremely fast, so I changed the math.random to
(-2, -1) so it was slower. Just wondering.

``````Removed code.
``````

Also made a game over screen, and I’m trying to delay it so that the explosion happens to the ship

In your createRock function, create a variable that gets a random number between say 1 and 100. Depending on the ratio of rocks to cubes you want check that variable. If you want twice as many rocks as cubes, that’s 2 to 1, so if the random number is between 1 and 33, create a cube else create a rock. If you want 3 to 1 then it would be between 1 and 25. You should also create a different table for your rocks and cubes.

@NG_2000 Maybe before you try to add more code, you should learn how to format it correctly. Looking at the code you just posted, things are really screwed up. If you continue to add code to it, it’s going to turn into a mess that you might not get out of. Here’s your code `loop thru rock table and draw them` where I formatted it so you can see what’s really happening. You have the for c,d loop inside the for a,b loop. You have your gameOver code inside the for c,d loop. How you display the code in the editor doesn’t mean that’s how it’s going to run. You need to learn to match your end statements with the correct if or for statement. If you look at your code above, it might look how you want it to run, but Codea sees and runs it completely different.

``````    -- loop thru rock table and draw them
for a,b in pairs(rTab) do
sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
b.x=b.x+b.z -- decrement rock position
if b.x<-20 then     -- rocks are off the left side of the screen
table.remove(rTab,a)    -- remove rock from table
createRock()    -- create another random rock
end

for c,d in pairs(rTab) do
sprite("Tyrian Remastered:Energy Cube",d.x,d.y)
d.x=d.x+d.z -- decrement rock position
if d.x<-20 then     -- rocks are off the left side of the screen
table.remove(rTab,a)    -- remove rock from table
createRock()
end
sprite("Cargo Bot:Title Large Crate 1",-100, HEIGHT/2, 300, 800)
if gameOver == true then
background(212, 35, 35, 255)
text("GAME OVER", WIDTH/2, HEIGHT/2)
fontSize(50)
end
end
end
``````

Well, I must have done something wrong. Black screen. Also I’m very beginner, so you might want to be more basic so I get what you mean. For example loop through and draw them, I’m pretty stumped by that. I’m really sorry to be bothering you so much, I should try to study on my own

``````Removed code.
``````

What did you change? Looks the same

I fixed the `check if missile and cube are less than 10 pixels apart`.

Here’s a version that draws the cubes on the screen. I don’t know what you’re going to do with the cubes.

``````Removed the code, see updated version below.
``````

I just got it working, I didn’t fix everything.

``````displayMode(FULLSCREEN)

function setup()
rTab={}     --table for multiple rocks
cTab={}     --table for multiple cubes
sTab={}
x,y=WIDTH*1/8,HEIGHT/2  -- position of ship
a,b=-100,WIDTH/2
ang=0       -- starting angle for ship
mTab={}     -- table for multiple missiles
mSpeed=10  -- speed of the missile
tx,ty=0,0
delay=10    -- used with limit
limit=10    -- delay between missile shots
maxRocks=3 -- max number of rocks to show
maxCubes=1  -- max number of cubes
maxSoldiers=1
createRock()
createCube()
createSoldier()
end

function createRock()   -- create up to maxRocks on the screen
while #rTab<maxRocks do
table.insert(rTab,vec3(WIDTH+20,math.random(760),math.random(-5,-1)))
end
end

function createCube()   -- create up to maxCubes on the screen
while #cTab<maxCubes do
table.insert(cTab,vec3(WIDTH+20,math.random(760),math.random(-6,-4)))
end
end

function createSoldier()
while #sTab<maxSoldiers do
table.insert(sTab,vec3(WIDTH+20,math.random(760),math.random(-10,-1)))
end
end

function touched(touch)
if touch.state==BEGAN or touch.state==MOVING then
if not gameOver then
ang=math.deg(math.atan(touch.y-y,touch.x-x))
shoot=true
tx=touch.x
ty=touch.y
end
end
if touch.state==ENDED then
shoot=false
end

end

function draw()
background()
--if not gameOver then
fill(255, 0, 0, 255)
sprite("Documents:battle ground", WIDTH/2, HEIGHT/2, 1700, 900)
pushMatrix()
fontSize(20)
text("Bullet Speed;", WIDTH*26/100, HEIGHT*9/10)
text(mSpeed, WIDTH/3, 9/10*HEIGHT)
text("Bullet Delay")
text(limit, WIDTH*3/4, 9/10*HEIGHT)
translate(x,y)
rotate(ang-270)
sprite("Tyrian Remastered:Boss B", 0,0,90,90)
translate()
popMatrix()
--end

-- loop thru missile table
for a,b in pairs(mTab) do
sprite("Tyrian Remastered:Explosion Ball",b.x,b.y)
b.x=b.x+b.z*mSpeed
b.y=b.y+b.w*mSpeed
if b.x>WIDTH or b.x<0 and b.y>HEIGHT or b.y<0 then
table.remove(mTab,a)
end

-- check if missile and rocks are less than 30 pixels apart
v1=vec2(b.x,b.y)    -- missle x,y values
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<30 then
table.remove(rTab,a1)   -- remove rock from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createRock()    -- create rocks
end
end

for a2, b2 in pairs(sTab) do
d=v1:dist(vec2(b2.x,b2.y))
if d<40 then
table.remove(sTab, a2)
sprite("Tyrian Remastered:Space Ice 3",b2.x,b2.y)
sound(SOUND_EXPLODE, 39131)
createSoldier()
limit=limit-1
end
end

-- check if missile and cubes are less than 10 pixels apart
for a1,b1 in pairs(cTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<10 then
table.remove(cTab,a1)   -- remove cube from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createCube()    -- create cubes
mSpeed=mSpeed+1
end
end
end

-- check if a rock hits the ship
v1=vec2(x,y)    -- position of ship
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and ship
if d<50 then
table.remove(rTab,a1)
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
gameOver=true
shoot=false -- dont allow any shooting
x,y=999,999 -- move ship off screen
end
end

delay=delay+1
-- delay shooting
if shoot and delay>limit then
v1=vec2(tx-x,ty-y)
v1=v1:normalize()
table.insert(mTab,vec4(x,y,v1.x,v1.y))
delay=0
sound(SOUND_SHOOT, 15498)
end

-- loop thru rock table and draw them
for a,b in pairs(rTab) do
sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
b.x=b.x+b.z -- decrement rock position
if b.x<-20 then     -- rocks are off the left side of the screen
table.remove(rTab,a)    -- remove rock from table
createRock()    -- create another random rock
end
end

-- loop thru cube table and draw them
for c,d in pairs(cTab) do
sprite("Tyrian Remastered:Energy Cube",d.x,d.y)
d.x=d.x+d.z -- decrement cube position
if d.x<-20 then     -- cubes are off the left side of the screen
table.remove(cTab,c)    -- remove cube from table
createCube()
end
end

for e,f in pairs(sTab) do
sprite("Tyrian Remastered:Satellite",f.x,f.y, 50, 50)
f.x=f.x+f.z
if f.x<-20 then
table.remove(sTab,e)
createSoldier()
end
end

if gameOver == true then
background(212, 35, 35, 255)
text("GAME OVER", WIDTH/2, HEIGHT/2)
fontSize(50)
end
end
``````

Tried making high score, didn’t work

``````function setup()
supportedOrientations(LANDSCAPE_LEFT)
displayMode(FULLSCREEN)
rTab={}     --table for multiple rocks
cTab={}     --table for multiple cubes
sTab={}
x,y=WIDTH*1/8,HEIGHT/2  -- position of ship
a,b=-100,WIDTH/2
ang=0       -- starting angle for ship
mTab={}     -- table for multiple missiles
mSpeed=10  -- speed of the missile
tx,ty=0,0
delay=10    -- used with limit
limit=10    -- delay between missile shots
maxRocks=3 -- max number of rocks to show
maxCubes=1  -- max number of cubes
maxSoldiers=1
createRock()
createCube()
createSoldier()
Hearts=5
Score=0
end

hiscore=math.floor(hiscore)

else
hiscore=0
end

function createRock()   -- create up to maxRocks on the screen
while #rTab<maxRocks do
table.insert(rTab,vec3(WIDTH+20,math.random(760),math.random(-5,-1)))
end
end

function createCube()   -- create up to maxCubes on the screen
while #cTab<maxCubes do
table.insert(cTab,vec3(WIDTH+20,math.random(760),math.random(-6,-4)))
end
end

function createSoldier()
while #sTab<maxSoldiers do
table.insert(sTab,vec3(WIDTH+20,math.random(760),math.random(-10,-1)))
end
end

function touched(touch)
if touch.state==BEGAN or touch.state==MOVING then
if not gameOver then
ang=math.deg(math.atan(touch.y-y,touch.x-x))
shoot=true
tx=touch.x
ty=touch.y
end
end
if touch.state==ENDED then
shoot=false
end

end

function draw()
background()
--if not gameOver then
fill(255, 0, 0, 255)

sprite()
sprite("Documents:battle ground", WIDTH/2, HEIGHT/2, 1700, 900)
pushMatrix()
fontSize(20)
text("Bullet Speed;", WIDTH*26/100, HEIGHT*9/10)
text(mSpeed, WIDTH/3, 9/10*HEIGHT)
text("Bullet Delay;", WIDTH*68/100, HEIGHT*9/10)
text(limit, WIDTH*3/4, 9/10*HEIGHT)
text("Lives;",WIDTH*18/40, HEIGHT*29/30)
text(Hearts,WIDTH*39/80, HEIGHT*29/30)
text("Score;", WIDTH*1/20,HEIGHT*29/30)
text(Score, WIDTH*2/20, HEIGHT*29/30)
translate(x,y)
rotate(ang-270)
sprite("Tyrian Remastered:Boss B", 0,0,90,90)
translate()
popMatrix()
--end

-- loop thru missile table
for a,b in pairs(mTab) do
sprite("Tyrian Remastered:Explosion Ball",b.x,b.y)
b.x=b.x+b.z*mSpeed
b.y=b.y+b.w*mSpeed
if b.x>WIDTH or b.x<0 and b.y>HEIGHT or b.y<0 then
table.remove(mTab,a)
end

-- check if missile and rocks are less than 30 pixels apart
v1=vec2(b.x,b.y)    -- missle x,y values
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<30 then
table.remove(rTab,a1)   -- remove rock from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createRock()    -- create rocks
Score=Score+5
end
end

for a2, b2 in pairs(sTab) do
d=v1:dist(vec2(b2.x,b2.y))
if d<5 then
table.remove(sTab, a2)
sprite("Tyrian Remastered:Space Ice 3",b2.x,b2.y)
sound(SOUND_EXPLODE, 39131)
createSoldier()
limit=limit-0.1
end
end

-- check if missile and cubes are less than 10 pixels apart
for a1,b1 in pairs(cTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
if d<5 then
table.remove(cTab,a1)   -- remove cube from the table
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
createCube()    -- create cubes
mSpeed=mSpeed+1
end
end
end

-- check if a rock hits the ship
v1=vec2(x,y)    -- position of ship
for a1,b1 in pairs(rTab) do -- loop thru rock table
d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and ship
if d<50 then
table.remove(rTab,a1)
sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
sound(SOUND_EXPLODE, 39131)
gameOver=true
shoot=false -- dont allow any shooting
x,y=999,999 -- move ship off screen
end
end

delay=delay+1
-- delay shooting
if shoot and delay>limit then
v1=vec2(tx-x,ty-y)
v1=v1:normalize()
table.insert(mTab,vec4(x,y,v1.x,v1.y))
delay=0
sound(SOUND_SHOOT, 15498)
end

-- loop thru rock table and draw them
for a,b in pairs(rTab) do
sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
b.x=b.x+b.z -- decrement rock position
if b.x<-20 then     -- rocks are off the left side of the screen
table.remove(rTab,a)    -- remove rock from table
createRock()    -- create another random rock
Hearts=Hearts-1
end
end

-- loop thru cube table and draw them
for c,d in pairs(cTab) do
sprite("Tyrian Remastered:Powerup 19",d.x,d.y)
d.x=d.x+d.z -- decrement cube position
if d.x<-5 then     -- cubes are off the left side of the screen
table.remove(cTab,c)    -- remove cube from table
createCube()
end
end

for e,f in pairs(sTab) do
sprite("Tyrian Remastered:Powerup 18",f.x,f.y)
f.x=f.x+f.z
if f.x<-5then
table.remove(sTab,e)
createSoldier()
end
end

if Hearts == 0 then

gameOver=true
end

if gameOver == true then
background(212, 35, 35, 255)
font("ArialRoundedMTBold")
text("GAME OVER", WIDTH/2, HEIGHT/2)
fontSize(50)
text(Score, WIDTH/2, HEIGHT*18/40)
text(hiscore, WIDTH/2, HEIGHT*22/40)
end
end
``````

@NG_2000 When you add to score, compare that to hiscore. If score is higher, move score to hiscore. When the game is over, save the hiscore to local data.

Thanks that makes sense