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 15 in Questions Posts: 18

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

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,271

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

    my code here
    
  • dave1707dave1707 Mod
    Posts: 6,392

    @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: 18

    Ok

  • Posts: 18

    Thank you guys for the help

  • Posts: 18

    I will do so

  • Posts: 18

    Can you guys help with the code?

  • dave1707dave1707 Mod
    Posts: 6,392
    displayMode(FULLSCREEN)
    
    function setup()
        x,y=WIDTH*1/5,HEIGHT/2
        ang=0
        mTab={}
        tx,ty=0,0
        delay=0
        mSpeed=20
        limit=15
    end
    
    function touched(touch)
        if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end
        if touch.state==ENDED then
            shoot=false
        end
    end
    
    function draw()
        background(2, 2, 2, 255)
        pushMatrix()
        translate(x,y)
        rotate(ang-90)
        sprite("Tyrian Remastered:Boss D", 0,0,100,100)
        translate()
        popMatrix()
        fill(255,0,0)
        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
        end
        delay=delay+1
        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
        end        
    end
    
  • Posts: 18

    Wow

  • Posts: 18

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

  • Posts: 18

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


    displayMode(FULLSCREEN) function setup() fill(255) rect(0*WIDTH, 0, 3/20*WIDTH, HEIGHT) x,y=WIDTH*1/5,HEIGHT/2 ang=0 mTab={} tx,ty=0,0 delay=10 mSpeed=40 limit=4 B=WIDTH+20 C=math.random(-10, -1) A=math.random(1,760) end function touched(touch) if touch.state==BEGAN or touch.state==MOVING then ang=math.deg(math.atan(touch.y-y,touch.x-x)) shoot=true tx=touch.x ty=touch.y end if touch.state==ENDED then shoot=false end end function draw() background(2, 2, 2, 255) pushMatrix() translate(x,y) rotate(ang-90) sprite("Tyrian Remastered:Boss C", 0,0,90,90) translate() popMatrix() fill(255,0,0) 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 end delay=delay+1 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 end sprite("Tyrian Remastered:Fire Rock",B,A, 50, 50) B = B + C function collide(contact) if contact.state == BEGAN then ? end end end
  • Posts: 18

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

  • dave1707dave1707 Mod
    edited November 26 Posts: 6,392

    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.

    displayMode(FULLSCREEN)
    
    function setup()
        fill(255)
        --rect(0*WIDTH, 0, 3/20*WIDTH, HEIGHT)       
        x,y=WIDTH*1/5,HEIGHT/2
        ang=0
        mTab={}
        tx,ty=0,0
        delay=10
        mSpeed=40
        limit=20
        createRock()
    end
    
    function createRock()
        B=WIDTH+20
        C=math.random(-10, -1)
        A=math.random(1,760)
    end
    
    function touched(touch)
        if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end
        if touch.state==ENDED then
            shoot=false
            if hit then
                createRock()
                hit=false
                B=WIDTH+20
            end
        end
    end
    
    function draw()
        background(2, 2, 2, 255)
        pushMatrix()    translate(x,y)
        rotate(ang-90)
        sprite("Tyrian Remastered:Boss C", 0,0,90,90)
        translate()
        popMatrix()
        fill(255,0,0)
        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
            v1=vec2(b.x,b.y)
            d=v1:dist(vec2(B,A))
            if d<30 then
                hit=true
            end
        end
        delay=delay+1
        if shoot and delay>limit and not hit then
            v1=vec2(tx-x,ty-y)
            v1=v1:normalize()           
            table.insert(mTab,vec4(x,y,v1.x,v1.y))
            delay=0
        end
        if not hit then
            sprite("Tyrian Remastered:Fire Rock",B,A, 50, 50)
            B = B + C
            if B<-20 then
                createRock()
            end
        else
            sprite("Tyrian Remastered:Explosion Huge",B,A)
        end
    end
    
  • edited November 28 Posts: 18

    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.

     displayMode(FULLSCREEN)
    
    function setup()
        fill(255)
        --rect(0*WIDTH, 0, 3/20*WIDTH, HEIGHT)       
        x,y=WIDTH*1/5,HEIGHT/2
        ang=0
        mTab={}
        tx,ty=0,0
        delay=10
        mSpeed=40
        limit=20
        createRock()
    
    end
    
    function createRock()
        B=WIDTH+20
        C=math.random(-10, -1)
        A=math.random(1,760)
    end
    
    function touched(touch)
    
               if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
            sound(SOUND_SHOOT, 15498)
         end 
        if touch.state==ENDED then
           shoot=false
            if hit then
                createRock()
                hit=false
                B=WIDTH+20
                sound(SOUND_EXPLODE, 39131)
               end
        end
    end
    
    function draw()
           background(130, 70, 82, 255)
            pushMatrix()    translate(x,y)
        rotate(ang-90)
        sprite("Tyrian Remastered:Boss C", 0,0,90,90)
        translate()
        popMatrix()
        fill(1, 0, 255, 255)
        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
            v1=vec2(b.x,b.y)
            d=v1:dist(vec2(B,A))
            if d<30 then
                hit=true
            end
        end
        delay=delay+1
        if shoot and delay>limit and not hit then
            v1=vec2(tx-x,ty-y)
            v1=v1:normalize()           
            table.insert(mTab,vec4(x,y,v1.x,v1.y))
            delay=0
        end
        if not hit then
    
           sprite("Tyrian Remastered:Fire Rock",B,A, 50, 50)
            B = B + C
            if B<-20 then
                createRock()
            end
        else
            sprite("Tyrian Remastered:Explosion Huge",B,A)
             font("ArialRoundedMTBold")
               text("Great Shot!", B, A)
    
          end
        sprite("Tyrian Remastered:Rock Boss Right",-100, 1/2*HEIGHT, 400, HEIGHT)
    
        end
    
  • dave1707dave1707 Mod
    Posts: 6,392

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

     displayMode(FULLSCREEN)
    
    function setup()
        rTab={}     --table for multiple rocks
        x,y=WIDTH*1/5,HEIGHT/2  -- position of ship
        ang=0       -- starting angle for ship
        mTab={}     -- table for multiple missiles
        mSpeed=40   -- speed of the missile
        tx,ty=0,0
        delay=0     -- used with limit
        limit=20    -- delay between missile shots
        maxRocks=5  -- max number of rocks to show
        createRock()
    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(-10,-1)))
        end
    end
    
    function touched(touch)
        if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end 
        if touch.state==ENDED then
            shoot=false
        end
    end
    
    function draw()
        background(130, 70, 82, 255)
        pushMatrix()    
        translate(x,y)
        rotate(ang-90)
        sprite("Tyrian Remastered:Boss C", 0,0,90,90)
        translate()
        popMatrix()
        -- 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
            v1=vec2(b.x,b.y)
            -- check if missile and rock are less than 30 pixels apart
            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)
                    sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                    sound(SOUND_EXPLODE, 39131)
                    createRock()
                end
            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     -- this is where the rocks are off the left side of the screen
                table.remove(rTab,a)    -- remove rock from table
                createRock()    -- create another random rock
            end        
        end
        sprite("Tyrian Remastered:Rock Boss Right",-100, 1/2*HEIGHT, 400, HEIGHT)
    end
    
  • Posts: 18

    Wow bro thanks

  • Posts: 18

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

  • dave1707dave1707 Mod
    edited December 1 Posts: 6,392

    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: 18

    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 December 1 Posts: 18
    displayMode(FULLSCREEN)
    
    
      displayMode(FULLSCREEN)
    function setup()
        rTab={}     --table for multiple rocks
        x,y=WIDTH*1/5,HEIGHT/2  -- position of ship
        ang=0       -- starting angle for ship
        mTab={}     -- table for multiple missiles
        mSpeed=40   -- speed of the missile
        tx,ty=0,0
        delay=0     -- used with limit
        limit=10  -- delay between missile shots
        maxRocks=5  -- max number of rocks to show
        createRock()
        A=19/20*WIDTH
        B=18/20*WIDTH
        C=17/20*WIDTH
        D=16/20*WIDTH
        E=15/20*WIDTH
            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(-3,-1)))
        end
    end
    
    function touched(touch)
        if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end 
        if touch.state==ENDED then
            shoot=false
        end
    end
    
    function draw()
        background(130, 70, 82, 255)
        pushMatrix()    
        translate(x,y)
        rotate(ang-90)
        sprite("Tyrian Remastered:Boss C", 0,0,90,90)
        translate()
        popMatrix()
        -- 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
            v1=vec2(b.x,b.y)
            -- check if missile and rock are less than 30 pixels apart
            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)
                    sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                    sound(SOUND_EXPLODE, 39131)
                    createRock()
                end
            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     -- this is where the rocks are off the left side of the screen
                table.remove(rTab,a)    -- remove rock from table
                createRock()    -- create another random rock
    
        end
    
                end
    function setup()
        rTab={}     --table for multiple rocks
        x,y=WIDTH*1/5,HEIGHT/2  -- position of ship
        ang=0       -- starting angle for ship
        mTab={}     -- table for multiple missiles
        mSpeed=40   -- speed of the missile
        tx,ty=0,0
        delay=0     -- used with limit
        limit=100  -- delay between missile shots
        maxRocks=5  -- max number of rocks to show
        createRock()
    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(-3,-1)))
        end
    end
    function touched(touch)
        if touch.state==BEGAN or touch.state==MOVING then       
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end 
        if touch.state==ENDED then
            shoot=false
        end
              -- 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.
    
    function draw()
        -- loop thru rock table and draw them
         sprite("SpaceCute:Health Heart", A, 19/20*HEIGHT, 50,50)
        sprite("SpaceCute:Health Heart", B, 19/20*HEIGHT, 50,50)         
          sprite("SpaceCute:Health Heart", C, 19/20*HEIGHT, 50,50)       
            sprite("SpaceCute:Health Heart", D, 19/20*HEIGHT, 50,50) 
              sprite("SpaceCute:Health Heart", E, 19/20*HEIGHT, 50,50)
    
    
     end
            end 
               end    
    
  • Posts: 18

    For some reason ~~~ isn't working?

  • dave1707dave1707 Mod
    Posts: 6,392

    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.

     displayMode(FULLSCREEN)
    
    function setup()
        rTab={}     --table for multiple rocks
        x,y=WIDTH*1/5,HEIGHT/2  -- position of ship
        ang=0       -- starting angle for ship
        mTab={}     -- table for multiple missiles
        mSpeed=40   -- speed of the missile
        tx,ty=0,0
        delay=0     -- used with limit
        limit=20    -- delay between missile shots
        maxRocks=5  -- max number of rocks to show
        createRock()
    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(-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(130, 70, 82, 255)
        --if not gameOver then
            pushMatrix()    
            translate(x,y)
            rotate(ang-90)
            sprite("Tyrian Remastered:Boss C", 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 rock are less than 30 pixels apart
            v1=vec2(b.x,b.y)
            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)
                    sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                    sound(SOUND_EXPLODE, 39131)
                    createRock()
                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     -- this is where the rocks are off the left side of the screen
                table.remove(rTab,a)    -- remove rock from table
                createRock()    -- create another random rock
            end        
        end
        sprite("Tyrian Remastered:Rock Boss Right",-100, 1/2*HEIGHT, 400, HEIGHT)
    end
    
  • Posts: 18

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

Sign In or Register to comment.