Help with shooter game

Can anyone help me with the sprite guard shooting at the player??? It would help me a lot. I’ve always wanted to play a shooter game. I know everything I should happen at that and at that point. I am writing this with a translator. I come from Germany. If someone doesn’t understand me, it’s not bad because I can describe things very badly. Just write to me!


if Training001 then
    background()
        sprite(Wache, 850, 500)
        sprite(Spieler, x, y) 
        sprite(asset.builtin.Blocks.Cactus_Inside, 200, 250, 250, 400)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 420)
    sprite(asset.builtin.UI.Blue_Button_07, 300, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 100, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 100)
       
    end 

I think you will need to tell us a bit more than that.

What sort of shooter game? Can you share some pictures of the type of game you want to make?

@Killer.Jo - just looking at your code we can’t see how it fits into the rest of your code without seeing it. Does this relate to the code you posted in another thread?

@Bri_G @sim I just need a small code where the sprite (“WACHE”) shoots at the sprite called “SPIELER” with small distances. It is simply to be “thrown” a sprite in the direction of “SPIELER”. Is that enough?

@Killer.Jo - I’ll just post this little piece of code to try to show how it should work. It’s untested as I don’t know what the rest of your code does. So here’s how I think it would work.

You take the position of SPIELER and the position of WACHE then calculate the x an y distance between the two. Assuming the x an y distances are different, you increment on the smallest distance as an integer (effectively) and for each of the other x/y distances you multiply out in the loop and floor the answer to try to keep the line of travel as straight as possible.

Then place your sprite based on that calling the drawing routine from your draw() function.

Hope that makes sense.

Now here is roughly how I expect the code to work.
Call the routine to calculate the distance first.

Note - you may need to adjust the signs of the distance depending on where WACHE and SPIELER are relative to each other.

Edit: realised later that loops for sprite movement shouldn’t be used here, modified the code to increment on screen refresh by using count as a variable incrementing each screen cycle.


function findDistance(v1, v2)
    -- v1 is WACHE, v2 is SPIELER, Calc distance
    dist = math.sqrt(math.abs(v1.x-v2.x)^2+math.abs(v1.y-v2.y)^2)
    count = 1
    return dist
end
    
function shoot(dist)
    -- 
    
    if (v1.x - v2.x) > (v1.y - v2.y) then
        -- loop on height
        incY = 1
        incX = (v1.x - v2.x)/(v1.y - v2.y)
        sprite(bullit, math.floor(v1.x+count*incX), v1.y+count*incY)
    else
        -- loop on width
        incX = 1
        incY = (v1.x - v2.x)/(v1.y - v2.y
        sprite(bullit, (v1.x+count*incX), math.floor(v1.y+count*incY))
    end
    If count > dist then count = 0 end
end


function draw()
    
    
    if CurrentTouch.state == ENDED then        
        if CurrentTouch.x >= 850-120 and CurrentTouch.x <= 850+120 and 
            CurrentTouch.y >= 100-49 and CurrentTouch.y <= 100+49 then   
            shooting()
            
            
           tween.delay(0.5, function()
       nicht = true 
            sprite(asset.documents.Nachladen, 150, 715, 100)
    end) 
              
        end 
        end  
    fill(255, 14, 0)
    font("Arial-ItalicMT")
    if A then
        WachenX = WachenX - WachenX_sgk
        B = false
        tween.delay(1, function()
        A = false
            B = true
                
        end)
    end
    
    if B then
        WachenX = WachenX + WachenX_sgk
        A = false
        tween.delay(1, function()         
      B = true
            A = true          
        end)
    end
    
    -- ((8.8.2023)) -- 
  -- Spfa
    if ops == "Start" then
        
        background(46)
        sprite(asset.documents.Start_buttonKJ, 800, 100, Startbtnscale)
        sprite(asset.documents.Skineditor, 800, 300, Skinbtnscale) -- Telefonnieren vorbei
        

    end
    
    if ops == "Start11" then   
        background(91, 155, 64)   
        if CurrentTouch.state == ENDED then        
        if CurrentTouch.x >= 800-120 and CurrentTouch.x <= 900+120 and 
            CurrentTouch.y >= 50-49 and CurrentTouch.y <= 150+49 then   
            shooting()
            
            
           tween.delay(0.5, function()
       nicht = true 
            sprite(asset.documents.Nachladen, 150, 715, 100)
    end) 
              
        end 
        end  
        if CurrentTouch.state == MOVING then        
        if CurrentTouch.x >= 200-120 and CurrentTouch.x <= 200+120 and 
            CurrentTouch.y >= 420-49 and CurrentTouch.y <= 420+49 then   
            y = y + 5
            
        end 
        end     
        
     if CurrentTouch.state == MOVING  then        
        if CurrentTouch.x >= 200-120 and CurrentTouch.x <= 200+120 and 
            CurrentTouch.y >= 100-49 and CurrentTouch.y <= 100+49 then   
            y = y - 5
            
        end 
    end 
    
    if CurrentTouch.state == MOVING  then        
        if CurrentTouch.x >= 300-120 and CurrentTouch.x <= 300+120 and 
            CurrentTouch.y >= 250-49 and CurrentTouch.y <= 250+49 then   
            x = x + 5
            
        end 
    end 
    
    if CurrentTouch.state == MOVING   then        
        if CurrentTouch.x >= 100-120 and CurrentTouch.x <= 100+120 and 
            CurrentTouch.y >= 250-49 and CurrentTouch.y <= 250+49 then   
            x = x - 5
            
        end 
    end 


if CurrentTouch.state == BEGAN then        
        if CurrentTouch.x >= 200-120 and CurrentTouch.x <= 200+120 and 
            CurrentTouch.y >= 420-49 and CurrentTouch.y <= 420+49 then   
            y = y + 5
            
        end 
        end     
        
     if CurrentTouch.state == BEGAN  then        
        if CurrentTouch.x >= 200-120 and CurrentTouch.x <= 200+120 and 
            CurrentTouch.y >= 100-49 and CurrentTouch.y <= 100+49 then   
            y = y - 5
            
        end 
    end 
    
    if CurrentTouch.state == BEGAN  then        
        if CurrentTouch.x >= 300-120 and CurrentTouch.x <= 300+120 and 
            CurrentTouch.y >= 250-49 and CurrentTouch.y <= 250+49 then   
            x = x + 5
            
        end 
    end 
    
    if CurrentTouch.state == BEGAN   then        
        if CurrentTouch.x >= 100-120 and CurrentTouch.x <= 100+120 and 
            CurrentTouch.y >= 250-49 and CurrentTouch.y <= 250+49 then   
            x = x - 5
            
        end 
    end 

    if nicht then
    shootX = x
    shootY = y
        end
        
        
        background(57, 61, 69)
    sprite(asset.documents.munio, shootX, shootY, 50)
   
        if shootX > WachenX then
    ServicePAD = true
        end
    
        
             
    sprite(Spieler,x,y)    
    sprite(asset.builtin.Blocks.Cactus_Inside, 200, 250, 250, 400)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 420)
    sprite(asset.builtin.UI.Blue_Button_07, 300, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 100, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 100)
    
sprite(asset.documents.Wache, WachenX, WachenY, 100, 100)
    sprite(asset.documents.Angreifen, 850, 100, 100)
    
    sprite(asset.documents.Nachladen, 150, 715, 100)
    text("Nachladen", 150, 715)
        --
        --
    if CurrentTouch.state == ENDED then        
        if CurrentTouch.x >= 800-120 and CurrentTouch.x <= 900+120 and 
            CurrentTouch.y >= 50-49 and CurrentTouch.y <= 150+49 then   
            shooting()
            
            
           tween.delay(0.5, function()
       nicht = true 
            sprite(asset.documents.Nachladen, 150, 715, 100)
    end) 
              
        end 
        end  
    
    if CurrentTouch.state == BEGAN then        
        if CurrentTouch.x >= 800-120 and CurrentTouch.x <= 900+120 and 
            CurrentTouch.y >= 50-49 and CurrentTouch.y <= 150+49 then   
    
            sprite(asset.documents.angreifen_touch, 850, 100, 100)
            end end

    
    
    
    if CurrentTouch.state == BEGAN then
        if CurrentTouch.x >= 75-120 and CurrentTouch.x <= 200+120 and
        CurrentTouch.y >= 650-49 and CurrentTouch.y <= 750+49 then
     
            sprite(asset.documents.Nachladen_touch, 150, 715, 100)
    end
end  
    end
    
    
    if not nicht then 
        shootX = shootX + 30
    end 


    
    if ops == "Tuto01" then
        
    end

   
    -- \28:18/ Mittwoch 9.Aug. (9.8.2023)] Sofa/hh NE SPAß IM BEZT NATÜRLICH CODE 500 MAL(Xx) GÄSCHMENERT
    -- Lolp)hPAPAWar da=l(Eig. wol lte cuh machen Verlosung
    if ServicePAD then
        
        if SpielerLeben < Mtest then
        background(0)
    end
        background(128)
       fill(7, 0, 255) 
        font("Arial-BoldMT")
        text("Akku: " .. Akku, 150, 750)
        sprite(asset.builtin.Small_World.Dialog_Icon, 900, 700, 200)
        -- Eigggg p0 730900
        sprite(Spieler, 900, 731)
        Counter = Counter + 1
    if Counter > math.random(60, 100) then
        yourFunction()
        Counter = 0
            
            
        end
        fill(255, 14, 0)
        text("Hier siehst du, wie es am Pad aussieht, wenn du auf ein Land zugreifen willst", 500, 100)
        
        sprite(asset.builtin.UI.Blue_Button_05, 525, 150)
        
        fill(0)
        font("Arial-BoldMT")
    
        text("Land Zugreifen", 525, 150)
        --
        sprite(asset.builtin.Platformer_Art.Block_Brick, 525, 450, 300)
        ---
        
        sprite(asset.Yeah_bildschirm, 525, 450,300)
        -- Alle Länder
     sprite(option, 525, 450, 200)
        -- Mittwoch 26. Augusr 
        text(Brueckenburg_insel_name, 525, 725)
            ----- code leider : di hanze zeit :  geändert leider ::: 16:35 Sonntag 13. Aug. 13.8.2034
            -- ;3 (ne eigg 13.8.2023  16:37 zeit vergeeeht 
    end
    
    
    if ops == "Box" then


    end
    
    if Training001 then
    background()
        sprite(Wache, 850, 500, 100)
        sprite(Spieler, x, y) 
        sprite(asset.builtin.Blocks.Cactus_Inside, 200, 250, 250, 400)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 420)
    sprite(asset.builtin.UI.Blue_Button_07, 300, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 100, 250)
    sprite(asset.builtin.UI.Blue_Button_07, 200, 100)
      -- --
        
        ----------
        function findDistance(v1, v2)
    -- v1 is WACHE, v2 is SPIELER, Calc distance
    dist = math.sqrt(math.abs(v1.x-v2.x)^2+math.abs(v1.y-v2.y)^2)
    count = 1
    return dist
end
    
function shoot(dist)
    -- 
    
    if (v1.x - v2.x) > (v1.y - v2.y) then
        -- loop on height
        incY = 1
        incX = (v1.x - v2.x)/(v1.y - v2.y)
        sprite(asset.builtin.UI.Blue_Box_Cross, math.floor(v1.x+count*incX), v1.y+count*incY)
    else
        -- loop on width
        incX = 1
        incY = (v1.x - v2.x)/(v1.y - v2.y)
        sprite(asset.builtin.UI.Blue_Box_Cross, (v1.x+count*incX), math.floor(v1.y+count*incY))
    end
    if count > dist then count = 0 end
end
        
    end end


function shooting()
    shootX = shootX + 10
    nicht = false
end

ATTENTION :warning:: This code has partly included CurrentToch because there is no other way.

This is the whole code. I don’t know why it doesn’t work so I want the Sprite GUARD to shoot a small “button” (on the sprite PLAYER) so that a small button is “thrown” to the sprite “PLAYER”. It’s not meant to be mean now : ) I just wanted to explain to them briefly ; )

Hello??

I must this write

Sorry in slow response KJ but - I’m a little busy at the moment, part the way working through your project but having to re-write a lot of it as Currenttouch in particular makes very messy code to read and interpret.

You would do better to use the touched(touch) function and keep most of the touching interactions there. Using Currenttouch is handy occasionally to capture the odd event.

Note if you use function touched(touch) all aspects of the touch are saved in the touch variable array so you can check many conditions by interrogating the array.

Codea runs setup() once then loops through the draw() function and the touched() function with each pass, which makes it better to use the touched(touch) function for that reason.

Hopefully I’ll finish the coding for you today and post it later.

It’s not bad. But I had to use currenttouch because if you used toched, it didn’t work properly