Space 8-bit shooter

This is the current attempt at an 8-bit shooter that I am working on and I’m currently having difficulties with the bullets. I’m hoping to get help on this so the bullet/bullets shoot in certain intervals and are based on touch. I also hope to get the bullets to continue drawing until it goes off the screen even when touching the other button.


-- Final App

-- Use this function to perform your initial setup
function setup()
    timeout=ElapsedTime+2
    arrowR=readImage("Documents:Arrow")
    arrowL=readImage("Documents:ArrowL")
    swordX1=WIDTH/2 
    swordY1=HEIGHT/2
    swordX2=WIDTH/2
    swordY2=HEIGHT/2
    characterX=WIDTH/2-30
    characterY=250
    characterW=60
    characterH=100
    weaponX1=characterX
    weaponY1=characterY+50
    weaponX2=characterX-50
    weaponY2=characterY+50
    bulletX1=WIDTH/2-70
    bulletY1=300
    bulletX2=WIDTH/2-90
    bulletY2=300
    bullet2X1=WIDTH/2-70
    bullet2X2=WIDTH/2-90
    bulletXR1=WIDTH/2+70
    bulletXR2=WIDTH/2+90
    bullet2XR1=WIDTH/2+70
    bullet2XR2=WIDTH/2+90
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(255, 255, 255, 255)
    
    touching()
    
    -- Do your drawing here
    stroke(0)
    
    --floor
    strokeWidth(15)
    line(0,250,WIDTH,250)
    strokeWidth(7)
    rect(-5,-5,WIDTH+10,250)
    
    --movement arrows
    rect(94,29,161,110)
    rect(494,29,161,110)
    sprite(arrowR,WIDTH/2+200,HEIGHT/2-300,150,100)
    sprite(arrowL,WIDTH/2-200,HEIGHT/2-300,150,100)
    
    --body
    strokeWidth(2)
    rect(characterX,characterY,characterW,characterH)
    
    --weapon
    line(weaponX1,weaponY1,weaponX2,weaponY2)
    
    
end

function touching()
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING or CurrentTouch.state == ENDED then
        if CurrentTouch.x>=94 and CurrentTouch.x<=255 and CurrentTouch.y>=29 and CurrentTouch.y<=139 then weaponX1=WIDTH/2-30 weaponX2=WIDTH/2-80 bullet1LFire()
            else if CurrentTouch.x>=494 and CurrentTouch.x<=655 and CurrentTouch.y>=29 and CurrentTouch.y<=139 then weaponX1=WIDTH/2+30 weaponX2=WIDTH/2+80 bullet1RFire()
            end
        end
    end
end

function bullet1LFire()
    bulletX1=bulletX1-2
    bulletX2=bulletX2-2
    line(bulletX1,bulletY1,bulletX2,bulletY2)
    if (ElapsedTime>timeout) then 
        if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then bullet2X1=bullet2X1-2 bullet2X2=bullet2X2-2 line(bullet2X1,bulletY1,bullet2X2,bulletY2) 
        end
    end
end

function bullet1RFire()
    bulletXR1=bulletXR1+2
    bulletXR2=bulletXR2+2
    line(bulletXR1,bulletY1,bulletXR2,bulletY2)
    if (ElapsedTime>timeout) then 
        if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then bullet2XR1=bullet2XR1+2 bullet2XR2=bullet2XR2+2 line(bullet2XR1,bulletY1,bullet2XR2,bulletY2) 
        end
    end
end

@ZacharyU When you post code, put 3~'s on a line before and after the code so it shows on the forum correctly. I added them to your code above.

Thank you @dave1707

@ZacharyU To have your bullets move even when touching other buttons, you need to learn about tables. You would add the bullets in a table and control their movement. When a bullet goes off the screen, you remove them from the table

In the table would I put in the “x” values of the bullets so, for the left side as an example, I would put in values of where it starts to where it ends?
Like:


for bulletX1=WIDTH/2-70, 0 do
mTable[bulletX1]=bulletX1
end

Here’s a simple example of multiple bullets.


function setup()
    bTab={}  -- table for bullets  
end

function draw()
    background(0)
    fill(255)
    text("tap screen to shoot multiple bullets",WIDTH/2,HEIGHT-100)
    text("bullets "..#bTab,WIDTH/2,HEIGHT-150)
    for nbr,bullet in pairs(bTab) do    -- draw bullets
        ellipse(bullet.x,bullet.y,20)
        bullet.y=bullet.y+5 -- move bullet
        if bullet.y>HEIGHT then
            table.remove(bTab,nbr)  -- remove from table
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        table.insert(bTab,vec2(200,200))    -- add bullet to table
    end
end

So I have this:

function setup()
       bTable={}
end

function draw()
    --bullets
    for nbr,bullet in pairs(bTable) do
        line(bullet.x,bulletY,bullet.x2,bulletY)
        bullet.x=bullet.x-2
        if bullet.x<0 then
            table.remove(bTable,nbr)
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        table.insert(bTable,vec2(200,200))
    end
end

Here’s an update. Touch the right or left side of the screen.


function setup()
       bTable={}
end

function draw()
    background(0)
    stroke(255)
    strokeWidth(4)
    for nbr,bullet in pairs(bTable) do
        line(bullet.x,bullet.y,bullet.x+20,bullet.y)
        bullet.x=bullet.x+bullet.z
        if bullet.x<0 or bullet.x>WIDTH then
            table.remove(bTable,nbr)
        end
    end
    rect(WIDTH/2-15,175,50,50)
end

function touched(t)
    if t.state==BEGAN then
        if t.x<WIDTH/2 then
            table.insert(bTable,vec3(WIDTH/2,200,-2))
        else
            table.insert(bTable,vec3(WIDTH/2,200,2))
        end
    end
end

Thank you @dave1707 ! This really helped! :smiley: