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

Hello? Can you reply?

I only will look maybe you reply i dont need help yet i only will look :slight_smile:

Sorry I am new here so I don’t know what is happening if you give me a brief explaination I may help.

Killer.jo - Hi, not heard from you for a while. Ran into problems with your last request, sorry.

What is it you need now?

@Killer.Jo - lost a lot of files when I bought a new pad after tidying up to use on the new pad. After digging around found a few of the projects I was playing around with to help you.

Can you just update me on what you was trying to achieve. Have not heard much from @dave1707 for a while who was also trying to help you.

at this moment i dont need help but i need an good Lua engine

@Killer.Jo - Codea uses the latest public release of the lua engine and has built a lot of enhancements to use to enable it to run well on Apple kit.

If you need to run on a PC you could look at Love2D, which can also run on a Mac or on Android tablets and , I think, can run on a browser. It would take quite a bit of learning though as the Love2D engine has to adapt to the OS and syntax is more demanding for learners. It is Open source though and has a large following.

Edit: - I have seen references to Love2D on the iPad but haven’t followed them up. If it is on the Apple Store it is early days and I would wait until it is a robust package.

Edit 2: After your prompt I looked up Love2D iPad app, yes it exists and I have had a Quick Look. Seems OK but I would raise an issue that might be a problem for you - the command structure is more demanding as it breaks the language structure into related groups. It makes it easier to read and understand but takes a while to absorb. It is however a good principal and would be expected by seasoned programmers. If you persevere with it - it would be a good alternative to Codea.

But - frankly I’d stick with Codea and get over the early learning curve.

Ok. Thanks but,

First, I have an Problem with an Jump n‘ Runner (Jump and run) because the 2. if at CheckCollide() Not work
Here is the Code

Main:

function setup()
    
    jumping = true
    jumping1 = true
    x = 100
    y = 600
    spieler = readImage(asset.builtin.Planet_Cute.Character_Boy)
    bg = color(0, 233, 255)
    hp = 100
    
    viewer.mode = FULLSCREEN
    
    
    PlattfandmRy = math.random(300,400)
    PlattfandmRx = math.random(800,900)
    fall = true
    jump2 = true
    
   counter = 0 
    
end 

function draw()
    checkCollide()
    print(counter)
    background(bg)
    sprite(spieler, x,y)
    fill(30, 255, 0)
    text(hp,x, y+30)
    
    x = x + 2
    
    sprite(asset.builtin.UI.Blue_Button13,300, 400)
    counter = 0
    
    if fall then
        y = y -2
    
        jump2 = false
    else
        jump2 = true
    end
    
    
    Zahl=75
    
    
    
    if jump then
        fall = false
        if jump1 then
            y = y + 5
            jump2 = false
        end
        tween.delay(0.5,function()
            jump1 = false
            y = y - 1 
            jump2 = false  
        end)  
        
        tween.delay(0.5, function()
            
            jump = false   
        end)
    end
    
    if not jump then
        fall = true
        jump2 = true
    end
    
    sprite(asset.builtin.UI.Blue_Button13, 500, 450)
    
    
end

Touched:

function touched(t)
    if jump2 then
    if t.state == ENDED then
 jump = true
   end     
    end

    
end

Collide:

function checkCollide()
    Zahl=75
    if x>500-Zahl and x<500+Zahl and y>450-Zahl and y<450+Zahl then
        fall = false
    else
        fall = true
    end
    
    if x>300-Zahl and x<300+Zahl and y>400-Zahl and y<400+Zahl then
        fall = false
    else
     fall = true   
    end
    
    
    
end

But thanks for the reply

@killer.jo - just looking at your code it would help if you could describe what you are trying to make with this project is your character supposed to jump from ledge to ledge ? If so you need to consider how the game progresses - that is vertically or horizontally. In either case you need to keep your character fairly central and move the background to simulate movement.

I think @dave1707 Ofsted a very early demo of a character jumping upwards from cloud to cloud - I’ll see if I can dig that one out.

You still seem to have a hangup on using the draw function for detecting change. The touched function is generally the best place for that - detecting change, updating a condition variable so that the draw function can respond to it.

Another way to detect change is if you are detecting an orientation change of your pad IE tilting it. Or, possibly joypad input - but that depends on you iOS in your pad.

Throw me a few hints at what you want to achieve and I’ll try to point you in the right direction. I’ll play around with your code in the meantime.

Oh one last thought - your game may also need to use physics.

I dont know how to do physics maybe you can explain it to me. And yes it is intended that the character moves from ledge to ledge. The code was actually only a test if i could do a jump and run… And how do i do a joypad input maybe you could explain that too, or how i could make the backround move too. And it is intended to be a casual jump and run. Maybe you could show me a code example of how i do a lose screen when you fall down.

and also why is the second if at touched () not working?