Help with shooter game

@killer.jo - can you please describe what you want from jump and jump2. Is it direction like left or right ? Or is it the platforms ?

The direction is right, the player is basically supposed to jump from platform to platform.

@Killer.Jo - OK, one step at a time, let’s start with movement - you basically want to move left or right so I would use a variable say direction which has a integer number which could be -1 for left and +1 for right (the value can be increased or decreased to alter the speed of movement). Then there is a variable for up or down let’s call that vertical (again + or - for same reasons). We’ll introduce jump later and leave physics to last.

We need code for movement so we’ll introduce that initially get the movement sorted for the first step.

I’ll put something together for that post it and we’ll move on later.

Heres the code i coded i tried to make movement function (it works) It has CurrentTouch included because i think its easier to use (for the player). I would like to hear your opinion on the code.

-- Modern

function setup()
    direction = 100
    vertical = 200
end

function draw()
    background(255)
    sprite(asset.builtin.Planet_Cute.Character_Boy, direction, vertical)
    
    if CurrentTouch.state == MOVING then
        if CurrentTouch.x >= 200-50 and CurrentTouch.x <= 200+50 and CurrentTouch.y >= 100-50 and CurrentTouch.y <= 100+50 then
            direction = direction + 5
        end
    end
    sprite(asset.Bild2, 200, 100, 75)
    
    if CurrentTouch.state == MOVING then
        if CurrentTouch.x >= 50-75 and CurrentTouch.x <= 50+75 and CurrentTouch.y >= 100-75 and CurrentTouch.y <= 100+75 then
            direction = direction - 5
        end
    end
    sprite(asset.Button1, 50, 100, 75)
    
    if CurrentTouch.state ==BEGAN then
        if CurrentTouch.x >= 50-75 and CurrentTouch.x <= 50+75 and CurrentTouch.y >= 100-75 and CurrentTouch.y <= 100+75 then
            direction = direction - 5
        end
    end
    
    if CurrentTouch.state == BEGAN then
        if CurrentTouch.x >= 200-50 and CurrentTouch.x <= 200+50 and CurrentTouch.y >= 100-50 and CurrentTouch.y <= 100+50 then
            direction = direction + 5
        end
    end
end

TOUCHED

function touched(t)
    if t.state == MOVING then
        if t.x >= 100-50 and t.x <= 100+50 and t.y >= 100-50 and t.y <= 100+50 then
            direction = direction + 5
        end
    end
    
    if t.state == MOVING then
        if t.x >= 50-50 and t.x <= 500+50 and t.y >= 100-50 and t.y <= 100+50 then
            direction = direction - 5
        end
    end
end

@Killer.Jo - sorry in the delay in replying to you, I have been digging around in my archives trying to find code that already meets your need, but so far without success.

Your code works well but relies a lot on the CurrentTouch call which puts more demand on the draw() function. - on simple projects this approach is easy to apply and should not cause any issues. But, as your project grows you put a lot of demand on the draw function to capture touch information. Capturing touches in the touched() function removes that load from draw(). Also, try to minimise your calls for touch() - by nesting loops you can minimise the calls to check the state of touch

So, I modified your code to simplify it and hopefully help you to meet your needs. A few notes on that:

The code has been simplified to enable you to move your character left or right (and up or down with a few mods) using the variables direction and position. The variable vertical can be added and the touch - you’ve made 4 calls in draw() and two in touched(). I’d look at the code published here from other members and try to see what their approach is.


-- KJcode02

function setup()
    --
    midX, midY = WIDTH//2, HEIGHT//2
    position = vec2(midX, midY)
    direction = 0
    vertical = 0
    goLeft = readImage(asset.builtin.UI.Blue_Slider_Left)
    goRight = readImage(asset.builtin.UI.Blue_Slider_Right)
    buttonLeftPos = vec2(midX-100, midY-100)
    buttonRightPos = vec2(midX-100, midY-100)
end

function draw()
    --
    background(255)
    spriteMode(CENTER)
    sprite(asset.builtin.Planet_Cute.Character_Boy, position.x+direction, position.y+vertical)
    sprite(goRight,midX+100,midY-100,80)
    sprite(goLeft,midX-100, midY-100, 80)
end

function touched(t)
    --
    if t.state == BEGAN or t.state == MOVING then
        if t.x >= midX-150 and t.x <= midX-50 and t.y >= midY-140 and t.y <= midY-40 then
            direction = direction - 5
        elseif t.x >= midX+50 and t.x <= midX+150 and t.y >= midY-140 and t.y <= midY-40 then
            direction = direction + 5
        end
    end
end


p.s. My code neads a little adjustment as you need to keep your finger moving on the buttons to get constant motion - see if you can figure out how to do that. Also I added different UI buttons for control - there’s lots of assets provided with Codea you just need to check them out.

Thanks!

Can we now do the Jump function?

Nice code (2) but I had a question… (for a shooter game) it can be a little bit hard or too easy… this is the question:
How can i make with this code:
x = x - anotherX * 0.1
y = y - anotherY * 0.1
That the x and y coordinates to be simply further differentiated but not, so to speak, the “anotherX” and “anotherY” variable to be tracked (the code is for one sprite tracking another sprite.)

BUT i dont know how i explain it so right

Edit ( a better explanation) : But the sprite should not track the other sprite all the time but simply “aim” to the other sprite so I mean that the sprite flies in one direction but not the other sprite tracked and tracked all the time

@Killer.Jo - nearly finished your jump demo - needs a little bit more work before posting. On your tracking query I’m not sure what you need. Could you explain what the game involves.

It sounds like you have one character jumping across gaps to land on platforms at different heights and in several directions. Now you seem to have introduced another character who could be chasing the first character. Is that what you intend ? If so what is the objective of both characters ?

I would like an example (another project) of a sprite that pursues another sprite but the sprite that follows the other sprite should only follow in one direction here is a video how the sprite should fly:

Do you yet know what I mean i think you yet know it but its ok

I mean photo

I must write THIS<<<

Edit: in the photo, the points are the sprite that not follow another sprite
And the line is how the sprite that follow the other sprite move

@Killer.Jo - sorry about this but I’m still a little confused.

On my last post I asked if you could explain the idea you have - what the sprites are, what they are doing and why one sprite follows another. Could you please do that as it is not obvious from your post.

Also you labelled the two lines in the image as False and Right. There is also some circular lines involved - how do they fit in?

I want to make a shooter game with the sprites by the sprite that follows the other sprite is the bullet

@Killer.Jo - aaah, now I know what you would like. The best way to do this, considering your previous posts, is - when you fire the bullet at a moving target an angle will be involved. Calculate the angle and from that calculate where it would hit the edge of the screen if it misses the target. Then in your calculations for the bullet movement check for a collision with the target. If it hits then your bullet disappears and it’s movement stops and your target suffers some damage. If that bullet misses then it continues on it’s trajectory until it’s off screen.

You are better to calculate when the bullet goes off screen then you can stop the bullet movement routine.

Does that fit your idea ?

Yes, i mean this, yeah

@Killer.Jo - this has started to sound like your old posts. Below is some code that @dave1707 posted ages ago. If you are trying to finish that post your Codea as far as you managed to get it to work.


-- Template V3

viewer.mode=FULLSCREEN

function setup()
    --
    vel=1
    shipBullet={}
    orbBullet={}
    hit=false
    msg=""
    shipX,shipY=WIDTH/2,60
    orbX,orbY=WIDTH/2,HEIGHT-50
end

function draw()
    --
    background(255, 185, 0)
    if not hit then 
        shipMove()
        orbMove()
        shipShooting()
        orbShooting()
        orbHit()
        shipHit()
        text("Double tap to shoot",WIDTH/2,80)
    else
        fontSize(100)
        fill(255,0,0)
        text(msg,WIDTH/2,HEIGHT/2)
        fontSize(40)
        fill(255)
        text("Double tap to restart",WIDTH/2,HEIGHT/2-80)
    end
end

function shipMove()
    --
    sprite(asset.builtin.Tyrian_Remastered.Ship_B, shipX, shipY, 100, 100) 
end

function orbMove()
    --
    orbX=orbX+vel
    if orbX>WIDTH or orbX<0 then
        vel=-vel
    end
    sprite(asset.builtin.Tyrian_Remastered.Energy_Orb_2, orbX, orbY,50)
end

function shipShooting()
    --
    offset = 20
    if shoot then
        shoot=false
        if #shipBullet<5 then  -- only allow 5 bullets at a time
            table.insert(shipBullet,vec2(shipX,shipY+offset))
        end
    end
    for a,b in pairs(shipBullet) do    -- show and increment bullet
        sprite(asset.builtin.Tyrian_Remastered.Energy_Orb_1, shipBullet[a].x, shipBullet[a].y+offset)
        shipBullet[a].y=shipBullet[a].y+5
    end
    for a,b in pairs(shipBullet) do
        if shipBullet[a].y+offset>HEIGHT then
            table.remove(shipBullet,a)
        end
    end
end

function orbShooting()
    --
    if #orbBullet<1 then    -- change to 2 for 2 bullets
        table.insert(orbBullet,vec2(orbX,orbY)) 
    end
    for a,b in pairs(orbBullet) do    -- show and increment bullet
        sprite(asset.builtin.Tyrian_Remastered.Mine_Spiked_Huge, orbBullet[a].x, orbBullet[a].y)
        orbBullet[a].y=orbBullet[a].y-5
        attack()
    end
    for a,b in pairs(orbBullet) do
        if orbBullet[a].y<0 then
            table.remove(orbBullet,a)
        end
    end
end

function attack()
    --
    speed=6
    if orbBullet[1].y>shipY then
        v1=vec2(orbBullet[1].x-shipX,orbBullet[1].y-shipY)
        v1=v1:normalize()
    end
    orbBullet[1].x=orbBullet[1].x-v1.x*speed
    orbBullet[1].y=orbBullet[1].y-v1.y
end

function shipHit()
    --
    for a,b in pairs(orbBullet) do
        if orbBullet[a].x>shipX-30 and orbBullet[a].x<shipX+30 and orbBullet[a].y>shipY-30 and orbBullet[a].y<shipY+30 then
            msg="You lose"
            hit=true
        end
    end   
end

function orbHit()
    --
    for a,b in pairs(shipBullet) do
        if shipBullet[a].x>orbX-30 and shipBullet[a].x<orbX+30 and shipBullet[a].y>orbY-30 and shipBullet[a].y<orbY+30 then
            msg="You win"
            hit=true
        end
    end   
end

function touched(t)
    --
    if t.state==BEGAN and t.tapCount==2 then
        shoot=true
        if hit then
            viewer.restart()
        end
    end
    if t.state==CHANGED then
        shipX=shipX+t.deltaX*2
        shipY=shipY+t.deltaY*2
    end
end

Ok, thanks! It helped me very lot!!! :+1:t3: