war game

Im going to make a war little war game. How can I have a guys that just shoots straight but you can still move him around and he will still be shooting?

You can try it yourself first, post the result and then we can help. Or you can simply wait for Dave1707 to kindly do it for you…! :wink:

removed unneeded code to save space.

@Dave1707 Gotcha! I was 4 min faster, but without code! :slight_smile:

Thanks!!! @dave1707

@Jmv38 I’m watching TV, so I’m checking the forum every now and then to see what’s posted.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
    msl={}
    x=WIDTH/2
    y=200

    x1=400
    y1=500
end

function draw()
    background(0, 0, 0, 255)
   fill(255, 255, 255, 255)
     ellipse(x,y,20)
    for a,b in pairs(msl) do
        sprite("Tyrian Remastered:Bullet Big",b.x,b.y)
        b.y=b.y+8
        if b.y>HEIGHT then
            table.remove(msl,a)
        end
    end
  
    fill(255, 0, 45, 255)
    ellipse(x1,y1,20)

end

function collide(c)
    if c.state==BEGAN then
      x1=math.random(10,WIDTH-10)    
      y1=math.random(10,HEIGHT-10)   
    end   
end


function touched(t)
    if t.state==BEGAN then
        table.insert(msl,vec2(x,y+30))
    end
    if t.state==MOVING then
        x=x+t.deltaX
        y=y+t.deltaY
    end
end

Here is what I got.Sorry it got all messed up when I pasted it. I was trying to get is to spawn in a new place every time you hit it. @dave1707

removed unneeded code to save space.

Almost except I how could you make is to after one gets hit it spawns in a different place?

@Andrewsimpson4 I changed the above code. I set the number of targets to 5 and added a line of code in checkHit to create another target when one get hit.

perfect thank you @dave1707

@Andrewsimpson4 I made another change to the above code. The targets move down the screen.

Quick question. what does the “tar” and “msl” mean and what would i put for that if I wanted to add another one? @dave1707

@Andrewsimpson4 tar is a table for the targets. msl is a table for the missles (bullets). Add another what?

Never mind I got it but when it says like “b.x,b.y” what does the “b” mean? @dave1707

@Andrewsimpson4 When you do “for a,b in pairs(tab) do” you are stepping your way thru a table (tab). The variable “a” is the step number (1,2,3,4,etc) and “b” is the value at that table step. The variables “a” and “b” can be any name, I just use “a” and “b” cause I don’t want to think of some real names. In the case of your question, b.x and b.y, I did a table.insert of a vec2 variable (the x,y position of either the missle or target depending on the table). To get the first variable, use “.x”, to get the second, use “.y”. A vec3 would use .x, .y, .z . A vec4 would use .x, .y, .z, .w . Since “b” is the table value at some step, “b.x” is the 1st value, “b.y” is the 2nd value of the vec2 that was stored. So what I’m doing is getting the x,y values of either the missles or targets.

oh ok that makes sense now. Here what I have so far. I got the other guy to fire back when you shoot, but when you shoot and hit the other guy it explodes but doesn’t re-spawn in a random position it just stays in the same spot. I also cant get it to spawn the other 4 guys is only spawns the one of them. Heres the code. sorry I don’t know how to make the code where it shows up nicely in the form…

--# Main
displayMode(FULLSCREEN)
    function setup()
    msl={}
    tar={}
    msl2={}
    for z=1,4 do
        tar[z]=vec2(math.random(50,WIDTH-50),math.random(300,HEIGHT-50))
    end
    x=WIDTH/2
    y=100
    
    xx=(math.random(50,WIDTH-50))  
    yy=(math.random(300,HEIGHT-50))
    

end

function draw()
    background(50, 70, 50, 255)
    fill(255, 255, 255, 255)
  
    ellipse(x,y,10)
    fill(174, 35, 35, 255)
    for a,b in pairs(msl) do
        sprite("Tyrian Remastered:Bullet Simple B",b.x,b.y,5,5)
        b.y=b.y+5
        if b.y>HEIGHT then
            table.remove(msl,a)
        end
    end
    
    for a,b in pairs(msl2) do
        sprite("Tyrian Remastered:Bullet Fire C", b.x,b.y,5,5)
        b.y=b.y+-5
        if yy>HEIGHT then
            table.remove(mls2,b)
            end
            end
   

         for a,b in pairs(tar) do
        ellipse(xx,yy,15)
    end
    checkHit()
end

function touched(t)
    if t.state==BEGAN then
        table.insert(msl,vec2(x,y+30))
        table.insert(msl2,vec2(xx,yy+5))
    end
    if t.state==MOVING then
        x=x+t.deltaX
        y=y+t.deltaY
    end
end

function checkHit()
    for a,b in pairs(msl) do
        v1=vec2(b.x,b.y)   
        for c,d in pairs(tar) do
            if v1:dist(vec2(xx,yy)) < 9 then
                sprite("Tyrian Remastered:Explosion Huge",xx,yy,20)
                table.remove(msl,a)
                table.remove(tar,c)
                table.insert(tar,vec2(math.random(50,WIDTH-50),math.random(300,HEIGHT-50)))
               
                 break
            end
        end
    end
end

@dave1707

removed unneeded code to save space.