Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Attempting to code cannon

edited November 2017 Posts: 30

As you can see here I already have the cannon turning but I’m attempting to have red ellipses fly out of it. For some reason the if than statement to turn the cannon it connecting to the ellipse. Also I want to ellipses to come out of the cannon somehow. If you are holding down they keep flying, when you let go they stop.. Seeking help

``````displayMode(FULLSCREEN)

function setup()
x,y=WIDTH*1/5,HEIGHT/2
ang=0

a,b=0,0
end
function touched(touch)

if touch.state==BEGAN or touch.state==MOVING then
ang=math.deg(math.atan(touch.y-y,touch.x-x))
end

end
function draw()
background(2, 2, 2, 255)
translate(x,y)
rotate(ang-90)
sprite("Tyrian Remastered:Boss D", 0,0,100,100)

fill(217, 38, 38, 255)
ellipse(a, b, 30)

if touch.state==BEGAN or touch.state==MOVING then
a=a+5
b=b+5

end
end
``````

• Posts: 3,295

Please put "~~~" before and after your code you will get a nice:

``````my code here
``````
• Posts: 7,554

@NG_2000 Don’t start different discussions for the same subject. Also, when you post code put ~~~ on a line before and after your code as @Jmv38 said. I added them to your code above.

• Posts: 30

Ok

• Posts: 30

Thank you guys for the help

• Posts: 30

I will do so

• Posts: 30

Can you guys help with the code?

• edited January 2018 Posts: 7,554
``````Removed code, see updated code below.
``````
• Posts: 30

Wow

• Posts: 30

I just wish I understood what all of it means. I get most of it of it, but not all. Learning in school

• edited January 2018 Posts: 30

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.
``````
• Posts: 30

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

• edited January 2018 Posts: 7,554

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.
``````
• edited January 2018 Posts: 30

Hi, just a few things wrong with the code. First of all, the shooting and blowing up sounds are very badly timed and long, also you have to let go of the screen for a new fire ball to come in. Wondering if there is a way to make more then one come out at once. Could you tell me which line the if then statement is that is when the fire ball is a 0 x so if you aren't able to hit the fireball I can add features to that without help.

``````Removed code.
``````
• edited January 2018 Posts: 7,554

@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.
``````
• Posts: 30

Wow bro thanks

• Posts: 30

How do i make it if a fire rock hits ship game=over?

• edited December 2017 Posts: 7,554

Create another for loop using the rocks table and check if a rock and the ship are less than 30 pixels apart. If they are, game over.

• Posts: 30

Sorry to be annoying, but I don't know how to do that. I'm kind of beginner and am trying to go farther that what I am learning at school. I know what you mean by checking but I see you did that in the near beginning and can't seem to recreate it. Tried for about an hour. Also something is wrong with the code and when ever I touch the screen it freezes up. I realized I copied the code down twice and tried to fix it, but messed up something in the code. Thanks

• edited January 2018 Posts: 30
``````Removed code
``````
• Posts: 30

For some reason ~~~ isn't working?

• edited January 2018 Posts: 7,554

There can’t be any spaces before the ~~~. Here’s a version for game over. I don’t think you’re learning anything if I keep writing the code. I’ll give you suggestions, but it’s time for you to start learning how to do things. It’s going to take awhile, but it will be worth it.

`````` Removed code, updated code below.
``````
• Posts: 30

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

• edited January 2018 Posts: 30

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.
``````
• Posts: 30

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

• Posts: 7,554

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.

• Posts: 7,554

@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
``````
• edited January 2018 Posts: 30

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.
``````
• edited January 2018 Posts: 7,554

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.
``````
• Posts: 30

There will be power ups

• edited February 2018 Posts: 30

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.
``````
• edited February 2018 Posts: 7,554

Here’s an updated version.

``````Removed code, see updated code below.
``````
• Posts: 30

What did you change? Looks the same

• Posts: 7,554

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

• edited February 2018 Posts: 30

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

``````
• edited February 2018 Posts: 7,554

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
``````
• Posts: 30

Good, cause I should do that myself

• edited February 2018 Posts: 30

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
``````
• edited February 2018 Posts: 7,554

@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.

• Posts: 30

Thanks that makes sense

• edited February 2018 Posts: 30

It’s not working, I Think because since it’s the first game the code doesn’t know what the hiscore is yet.

``````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

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 Score>hiscore, and gameOver==true, then

Score=saveLocalData("hiscore")
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
``````
• Posts: 7,554

When you get highscore in setup, if it’s nil, set it to 0. The first time you read it, it’s going to be nil.

• edited February 2018 Posts: 30

I did it but it still says it is nil

``````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

hiscore=math.floor(hiscore)
else
hiscore=0

end
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)
text(hiscore, WIDTH*19/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

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
if Score>hiscore then

Score=saveLocalData("hiscore")
end
``````
• Posts: 7,554

@NG_2000 Add this in setup `hiscore=readLocalData("hiscore",0)`. It will set hiscore to 0 if hiscore isn’t there. Add this `saveLocalData(“hiscore”,hiscore)` to save your high score at gameover. Try looking at the built in reference to see the format of the commands when something doesn’t work.