war game

Thanks for the help! im making another game like the other one but this one is a 1v1 battle and I cant get the top guy in the game to shoot right.

--# Main
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
   msl={}
  
 msl2={}
    
     x=400
    y=100
    
    xx=400
    yy=900
    end
    function draw()
        background(62, 152, 50, 255)
        fill(0, 0, 0, 255)
        ellipse(x,y,20)
        
        fill(255, 255, 255, 255)
        ellipse(xx,yy,20)
       
        
        for a,b in pairs(msl) do
        ellipse(b.x,b.y,5,5)  --green
        b.y=b.y+15
        if b.y>HEIGHT then
            table.remove(msl,a)
        


      
        
           end
        for c,d in pairs(msl2) do
        ellipse( d.x,d.y,6,6)  --fire
        d.y=d.y-15
        if d.y<0 then
            table.remove(msl2,c)
        end
      end
  
    end
end 
        
            
                

                   
                        
     function touched(t)      
      if t.state==BEGAN then 
        if t.y<HEIGHT/2 then
        -- insert a new missle for main object
        table.insert(msl,vec2(x,y+10))  

    end
    end
      if t.state==BEGAN then 
        if t.y>HEIGHT/2 then
         table.insert(msl2,vec2(xx,yy+10))    
            end
               
                    
            
            end   
           if t.state==MOVING then   
        if t.y<HEIGHT/2 then    
            x=x+t.deltaX
            y=y+t.deltaY  
        
        else
          xx=xx+t.deltaX
          yy=yy+t.deltaY    
             
    

    end
    end
    end

@dave1707

@Andrewsimpson4 Compare this to yours to see what I changed.


--# Main 

displayMode(FULLSCREEN) 
supportedOrientations(PORTRAIT_ANY) 

function setup() 
    rectMode(CENTER)    -- center rect on x,y values
    msl={}
    msl2={}
    x=400
    y=100
    xx=400
    yy=900
    rx=100
    ry=100
    rectTab={}    -- table of rectangles
    mtab={}       -- table of which rectangle is moving
    for z=1,8 do
        table.insert(rectTab,vec4(100,100,120,80)) -- starting rect x,y,w,h
        table.insert(mtab,false) -- false = rect isn't moving
    end
end

function draw()
    background(62, 152, 50, 255)
    fill(0, 0, 0, 255)
    ellipse(x,y,20)
    fill(255, 255, 255, 255)
    ellipse(xx,yy,20)
    for a,b in pairs(msl) do
        ellipse(b.x,b.y,5,5)  --green
        b.y=b.y+15
        if b.y>HEIGHT then
            table.remove(msl,a)
        end
    end
    for c,d in pairs(msl2) do
        ellipse( d.x,d.y,6,6)  --fire
        d.y=d.y-15
        if d.y<0 then
            table.remove(msl2,c)
        end
    end
    for a,b in pairs(rectTab) do
        rect(b.x,b.y,b.z,b.w)
    end
end

function touched(t) 
    -- is the rectangle being touched, set to 1
    if t.state==BEGAN then
        for a,b in pairs(rectTab) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then
                    mtab[a]=true
                    return
            end
        end
        return
    end 
    --  move the rectangle 
    if t.state==MOVING then
        for a,b in pairs(mtab) do
            if b then
                rectTab[a].x=t.x
                rectTab[a].y=t.y
            end
        end
        return
    end
    -- done moving rectangle, set to 0
    if t.state==ENDED then
        for z=1,#mtab do
            mtab[z]=false
        end
        return
    end 
    
    if t.state==BEGAN then 
        if t.y<HEIGHT/2 then
            table.insert(msl,vec2(x,y+10))  
        end
    end
    if t.state==BEGAN then 
        if t.y>HEIGHT/2 then
            table.insert(msl2,vec2(xx,yy+10))    
        end
    end   
    if t.state==MOVING then   
        if t.y<HEIGHT/2 then    
            x=x+t.deltaX
            y=y+t.deltaY  
        else
            xx=xx+t.deltaX
            yy=yy+t.deltaY    
        end
    end
end

@Andrewsimpson4 To put code on the forum, put a line before and after with three tildes: ~~~.

~~~
Some amazing code
~~~

thanks!!! @andrew_stacey

@dave1707 thanks I got it all figured out now. I want to have blocks in the game that you can go behind and hide but dont know how to make it so it only moves the blocks when you have you finger on the block its self.

@Andrewsimpson4 I changed the above code for moving a rectangle (block).

EDIT: @Andrewsimpson4 I added code for a 2nd rectangle. If you’re going to add a lot of rectangles, you can use a table of rectangles.

@Andrewsimpson4 I added a table for the rectangles in the above code. Set for 8 rectangles.

How could I make it into a table? @dave1707

thanks. Im making a little game game and I need want it so all of the blocks start at the same point on the screen and you can just touch the top block and the top bloke will move and so on. @dave1707

@Andrewsimpson4 Changed. Try the above code.

I tryed to add another box to the other side of the screen but i cant get it to work


--# Main

--# Main 

displayMode(FULLSCREEN) 
supportedOrientations(LANDSCAPE_ANY) 

function setup() 
    rectMode(CENTER) 
    rectMode(CENTER)   -- center rect on x,y valu
    
    
    
    

    rectTab={}    -- table of rectangles
    mtab={} 
    rectTab2={}    -- table of rectangles
    mtab2={}         -- table of which rectangle is moving
   
       for z=1,30 do
        table.insert(rectTab,vec4(800,700,120,80)) -- starting rect x,y,w,h
        table.insert(mtab,false) 
        end      
            
     for z1=1,30 do
        table.insert(rectTab2,vec4(100,700,120,80)) -- starting rect x,y,w,h
        table.insert(mtab2,false) -- false = rect isn't moving
    end
   

end

function draw()
    background(62, 152, 50, 255)
    fill(0, 0, 0, 255)
    fill(255, 255, 255, 255)
                                       
 for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,600,100,100) --grass
      end   
                
    
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,550,100,100) --grass
      end   
    
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,500,100,100) --grass
      end   

for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,450,100,100) --grass
      end       
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,400,100,100) --grass
      end   
    
        
            
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,350,100,100) --grass
      end   
    
        
            
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,300,100,100) --grass
      end       
        
            
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,250,100,100) --grass
      end   
    
        
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,200,100,100) --grass
      end   
    
for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,150,100,100) --grass
      end    
    
        
 for i = 0,25 do
        sprite("Planet Cute:Grass Block", 100*i-100,100,100,100) --grass
      end     

    
        
            
     for i = 0,25 do
        sprite("Planet Cute:Grass Block",  100*i-100,50,100,100) --grass
      end   
    for a,b in pairs(rectTab2) do
        --sprite("Documents:archer1",b.x,b.y,b.z,b.w)
     rect(b.x,b.y,b.z,b.w)
    end
    for a,b in pairs(rectTab) do
       -- sprite("Documents:arch2",b.x1,b.y1,b.z1,b.w)
        rect(b.x1,b.y1,b.z1,b.w)
    end
  end 
 


function touched(t) 
    -- is the rectangle being touched, set to 1
    if t.state==BEGAN then
        for a,b in pairs(rectTab2) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then
                    mtab2[a]=true
                    return
            end
        end
        return
    end 
    --  move the rectangle 
    if t.state==MOVING then
        for a,b in pairs(mtab2) do
            if b then
                rectTab2[a].x=t.x
                rectTab2[a].y=t.y
            end
        end
        return
    end
    -- done moving rectangle, set to 0
    if t.state==ENDED then
        for z=1,5   do --#mtab 
            mtab2[z]=false
      end  
        return
    end 
    
  
    if t.state==BEGAN then
        for a,b in pairs(rectTab) do
            if t.x>b.x-b.z/2 and t.x1<b.x+b.z1/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then 
                    mtab2[a]=true
                    return
            end
        end
        return
    end 
    --  move the rectangle 
    if t.state==MOVING then
        for a,b in pairs(mtab) do
            if b then
                rectTab[a].x=t.x
                rectTab[a].y=t.y
            end
        end
        return
    end
    -- done moving rectangle, set to 0
    if t.state==ENDED then
        for z1=1,5   do --#mtab 
            mtab[z1]=false
        end
        return
    end 
  
    
    
 
    
    
    
  end
    

@dave1707

@Andrewsimpson4 Try this.


--# Main

displayMode(FULLSCREEN) 
supportedOrientations(LANDSCAPE_ANY) 

function setup() 
    shootT=true
    rectMode(CENTER)   -- center rect on x,y value
    rectTab={}    -- table of rectangles
    mtab={} 
    rectTab2={}    -- table of rectangles
    mtab2={}         -- table of which rectangle is moving

    for z=1,30 do
        table.insert(rectTab,vec4(800,700,80,80)) -- starting rect x,y,w,h
        table.insert(mtab,false) 
        
        table.insert(rectTab2,vec4(100,700,80,80)) -- starting rect x,y,w,h
        table.insert(mtab2,false) -- false = rect isn't moving
    end
    
    arrows={}    -- vec4 x,y, x direction, y direction
end

function draw()
    background(62, 152, 50, 255)
    
    fill(255, 255, 255, 255)
    for i=0,25 do
        for j=1,12 do
            sprite("Planet Cute:Grass Block", 100*i-100,650-j*50,100,100) --grass
        end
    end
 
    for a,b in pairs(rectTab2) do
        --sprite("Documents:archer1",b.x,b.y,b.z,b.w)
        rect(b.x,b.y,b.z,b.w)
    end
    
    for a,b in pairs(rectTab) do
        -- sprite("Documents:arch2",b.x1,b.y1,b.z1,b.w)
        rect(b.x,b.y,b.z,b.w)
    end
    shootArrows()    
    drawArrows()
end 

function drawArrows()
    for a,b in pairs(arrows) do
        sprite("Tyrian Remastered:Bullet Fire B",b.x,b.y)
        v2=vec2(b.z,b.w)
        v1=v2:normalize()
        b.x=b.x+v1.x*8
        b.y=b.y+v1.y*8
        -- removearrows when they go off screen
        if b.x<0 or b.x>WIDTH or b.y<0 or b.y>HEIGHT then
            table.remove(arrows,a)
        end
    end
end

function shootArrows()
    if shootT then
        tween.delay(1,insertArrows)    -- delay 1 second
        shootT=false
    end
end

function insertArrows()
    for a,b in pairs(rectTab) do
        -- checked if rectangle(archer) was moved
        if b.x~=800 and b.y~=700 and not mtab[a] then    -- add arrow to table
            table.insert(arrows,vec4(b.x,b.y,math.random(-5,5),math.random(-5,5)))
        end
    end
    for a,b in pairs(rectTab2) do
        -- checked if rectangle(archer) was moved
        if b.x~=100 and b.y~=700 and not mtab2[a] then    -- add arrow to table
            table.insert(arrows,vec4(b.x,b.y,math.random(-5,5),math.random(-5,5)))
        end
    end
    shootT=true
end

function touched(t) 
    -- is the rectangle being touched, set to true
    if t.state==BEGAN then
        for a,b in pairs(rectTab) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then 
                    mtab[a]=true
                    return
            end
        end
        for a,b in pairs(rectTab2) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then
                    mtab2[a]=true
                    return
            end
        end
        return
    end 

    --  move the rectangle 
    if t.state==MOVING then
        for a,b in pairs(mtab) do
            if b then
                rectTab[a].x=t.x
                rectTab[a].y=t.y
                return
            end
        end
        for a,b in pairs(mtab2) do
            if b then
                rectTab2[a].x=t.x
                rectTab2[a].y=t.y
                return
            end
        end
        return
    end

    -- done moving rectangle, set to false
    if t.state==ENDED then
        for z=1,#mtab do --#mtab 
            mtab[z]=false
        end
        for z=1,#mtab2 do
            mtab2[z]=false
        end
    end
end

I tried to make it so they all shoot when your finger touches the sreen but it keeps coming up with a error.



--# Main

--# Main

displayMode(FULLSCREEN) 
supportedOrientations(LANDSCAPE_ANY) 

function setup() 
    rectMode(CENTER)   -- center rect on x,y value
    rectTab={}    -- table of rectangles
    mtab={} 
    rectTab2={}    -- table of rectangles
    mtab2={}  
    msl={}  
    
         -- table of which rectangle is moving

    for z=1,30 do
        table.insert(rectTab,vec4(800,700,120,80)) -- starting rect x,y,w,h
        table.insert(mtab,false) 
        
        table.insert(rectTab2,vec4(100,700,120,80)) -- starting rect x,y,w,h
        table.insert(mtab2,false) -- false = rect isn't moving
    end
end

function draw()
    background(62, 152, 50, 255)

    fill(255, 255, 255, 255)
    for i=0,25 do
        for j=1,12 do
            sprite("Planet Cute:Grass Block", 100*i-100,650-j*50,100,100) --grass
        end
    end
 
    for a,b in pairs(rectTab2) do
        sprite("Documents:archer1",b.x,b.y,b.z,b.w)
        --rect(b.x,b.y,b.z,b.w)
    end
    
    for a,b in pairs(rectTab) do
         sprite("Documents:arch2",b.x1,b.y1,b.z1,b.w)
        --rect(b.x,b.y,b.z,b.w)
    end

       for a,b in pairs(msl) do
        ellipse(b.x,b.y,8,8)  
        b.y=b.y+15
    

        
   
     end
    
end 

function touched(t) 
    -- is the rectangle being touched, set to true
    if t.state==BEGAN then
               if t.x>WIDTH/2 then  
        for a,b in pairs(rectTab) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then 
                    mtab[a]=true
                    return
            end
        end
        end
        
       
           
         for a,b in pairs(rectTab2) do
            if t.x>b.x-b.z/2 and t.x<b.x+b.z/2 and
                t.y>b.y-b.w/2 and t.y<b.y+b.w/2 then
                    mtab2[a]=true
                    return
            end
        end
        return
    end
    
    

    --  move the rectangle 
    if t.state==MOVING then
                if t.x>WIDTH/2 then 
        for a,b in pairs(mtab) do
            if b then
                rectTab[a].x=t.x
                rectTab[a].y=t.y
                return
            end
        end
        end
             if t.x<WIDTH/2 then  
             for a,b in pairs(mtab2) do
            if b then
                rectTab2[a].x=t.x
                rectTab2[a].y=t.y
                return
            end
        end
        return
        end

end
    -- done moving rectangle, set to false
    if t.state==ENDED then
                if t.x>WIDTH/2 then 
        for z=1,5 do --#mtab do --#mtab 
            mtab[z]=false
        end
        
        end
                
        end
    
        for z=1,4 do --#mtab2 do
            mtab2[z]=false
        end
                
            table.insert(msl,vec2(rectTab,10))
    end
    



@dave1707

@Andrewsimpson4 Could you tell me more on how you want this to happen. After you move the rectangles(archers) onto the green area, you want them to shoot. How do you want them to shoot. One at a time as you touch them, all at once when you touch the screen. Are they going to aim at something. I need a little more info.

Well I just put it into the touched end because I was just trying to see if i could get it to fire. Then once I got it to fire I was going to put it into a timer so ever 1 or 2 seconds every one on the archers would fire. @dave1707

@Andrewsimpson4 I added to the above code I posted. I’m not sure how you want to do the arrows, “direction”, so I added code so each rectangle(archer) that was moved onto the green area, shoots an arrow in a random direction when you tap the text “shoot” at the top of the screen. You can change how often they shoot, the direction they shoot, etc.

@ignatz has a ballistics tutorial for archer games