Detect collide

-- construction dodge

function setup()
    parameter.watch("t")
    parameter.watch("Score")
    parameter.watch("gm")
    displayMode(FULLSCREEN)
    l= HEIGHT-50
    Helicopter:init()
    Ball:init()
    w={}
    table.insert(w,Wood())
    end
    gm=2
s=0
t=5
function draw()
    --gm 0 is debug
    --gm 1 is game
    --gm 2 is menu
    --gm 3 is gameover
    if gm==0 or 1 then
    s = s + 1
    end
    if gm==2 then
    background(78, 79, 59, 255)
    
    if CurrentTouch.tapCount==2 then
       gm=1
    end
    end
    if gm==1 then
    Score= math.floor(s)
    
    background(26, 164, 24, 255)
    sprite("Documents:bricks",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    text(Score,WIDTH/2,HEIGHT-20)
    for i,ww in pairs(w) do
        ww:draw()
        fill(198, 198, 198, 255)
        pushMatrix()
        pushStyle()
        popStyle()
        popMatrix()
            
            if CurrentTouch.state==BEGAN then
                Helicopter:touched()
            elseif CurrentTouch.state==MOVING then
                Helicopter.touched()
                elseif CurrentTouch.state==ENDED then
                    l = l - 5
                end
        Helicopter:draw()
        if l>= HEIGHT then
            l=HEIGHT
            end
        if l<=0 then
            l=0
            end
            end
        
    end
end

Helicopter = class()

function Helicopter:init()
    -- you can accept and set parameters here
    
    self.yx = l
end

function Helicopter:draw()
        
    fill(222, 222, 222, 255)  
    circle=physics.body(CIRCLE,30)
    circle.type=STATIC
    circle.x=WIDTH-50
    circle.y=l
    
           
    
    sprite("Dropbox:funny",WIDTH-50,l,20,20)
    -- Codea does not automatically call this method
end

function Helicopter:touched(touch)
            l = l + 5
    -- Codea does not automatically call this method
end

Wood = class()
q=0.001
function Wood:init()
    self.x = -200
    
    self.size = 50
    self.y = math.random(50,HEIGHT-500)
    self.y3= math.random(self.y,self.y+400)
    
end    

function Wood:draw()
 q = q + 0.01
self.y2 = self.y + 500
    t = t + .001
    if t>= 60 then
        t = 60
        end
    self.x = self.x + t
    self.y2 = self.y2 - 0.001
    a=self.x
    b=self.y
    if self.x>WIDTH+200 then
        table.remove(w)
        table.insert(w,Wood())
        
                
    end
    
    pushStyle()
    strokeWidth(2)
    stroke(255)
    line(self.x,0,self.x,self.y)
    line(self.x,HEIGHT,self.x,self.y2)
    line(self.x,self.y3,self.x,self.y3+200)
    popStyle()
    fmesh=mesh() 
    fmesh.vertices= {vec2(self.x-200,0),
                     vec2(self.x,self.y),
                     vec2(self.x+200,0)}
    fmesh.texCoords= {vec2(0,0),
                      vec2(1,0),
                      vec2(1,1)}
    fmesh.texture = "Documents:stone"
    fmesh:setColors(255,255,255,255)
    tmesh=mesh()
    tmesh.vertices= {vec2(self.x-200,HEIGHT),
                     vec2(self.x,self.y2),
                     vec2(self.x+200,HEIGHT)}
    tmesh.texCoords= {vec2(0,0),
                     vec2(1,0),
                     vec2(1,1)}
    tmesh.texture= "Documents:stone"
    tmesh:setColors(255,255,255,255)
    fmesh:draw()
    tmesh:draw()
end

My question is how can I make it so that its game over if the red dot touches eather the very top very bottom the triangle rocks and the line inbettween if anyone can do it that will be great

Hard to test your code without the complete code.
Create a function that takes two tables as parameters. where each table has an: x, y, width, height
then compare them to see if they are touching.

function setup()
   rectA = {x=0,y=0,width=50,height=50 }
    rectB = {x=WIDTH,y=HEIGHT,width=40,height=40}
    tween(3,rectA,{x=WIDTH,y=HEIGHT},{
    easing=tween.easing.linear,
    loop = tween.loop.pingpong})
    
    tween(5,rectB,{x=0,y=0},{
    easing=tween.easing.linear,
    loop = tween.loop.pingpong})
end

function draw()
    background(0, 0, 0, 255)
    fill(255, 9, 0, 255)
   -- rect(rectA.x,rectA.y,rectA.width,rectA.height)
    sprite("Platformer Art:Block Special",rectA.x,rectA.y,rectA.width,rectA.height)
    sprite("Platformer Art:Block Special Brick",rectB.x,rectB.y,rectB.width,rectB.height)
    --rect(rectB.x,rectB.y,rectB.width,rectB.height)
    if collision(rectA,rectB) then  print("Hit") else print("No Collision")end
end

--# Collision
--rects should hold: x,y,height,width
function collision(rectA, rectB)
    local ax, bx, ay, by = rectA.x + rectA.width, rectB.x + rectB.width, rectA.y + rectA.height, rectB.y + rectB.height

    return ax > rectB.x and bx > rectA.x and ay > rectB.y and by > rectA.y
 
end

. @Mnjk78lg You almost have the code for the top and bottom hits. I just added gm=3 where you check if l>= HEIGHT and if l<=0. I also added a simple “game over” routine if gm==3. You can expand on that. As for the other collisions, that gets more involved and I don’t have that yet.

EDIT: The collision for the triangle rocks and the line between them can be done by creating a physics.bode(EDGE for the leading edge of the triangle rocks and the line between them.


function draw()
    --gm 0 is debug
    --gm 1 is game
    --gm 2 is menu
    --gm 3 is gameover

    -- simple game over routine

    if gm==3 then
        background(40,40,50)
        fill(255)
        text("Game Over",WIDTH/2,HEIGHT/2)
        text("Double tap to play",WIDTH/2,HEIGHT/2-50)
        if CurrentTouch.state==BEGAN and CurrentTouch.tapCount==2 then
            gm=2
            l=HEIGHT-50
        end
    end

    -- this code is wrong

    if gm==0 or 1 then
    s = s + 1
    end

   -- it should be

    if gm==0 or gm==1 then
    s = s + 1
    end



    -- made changes to this code

            if l>= HEIGHT then
                l=HEIGHT
                gm=3   -- set to game over
            end
            if l<=0 then
                l=0
                gm=3  -- set to game over
            end

Woops I didn’t see that the code was in separate ~ tags. Sorry!

Add the lines of code I show below in the WOOD:draw routine. That will handle a hit with the line between the triangle rocks. That just leaves the triangle rocks. If you change the triangle rocks to rectangle rocks, that would make the hit a lot easier. It would be just like the line hit.

EDIT: I’m not sure what the Ball was used for in the first listing. I had to comment it out to run the program because the Ball class wasn’t included in the code.


    pushStyle()
    strokeWidth(2)
    stroke(255)
    line(self.x,0,self.x,self.y)
    line(self.x,HEIGHT,self.x,self.y2)
    line(self.x,self.y3,self.x,self.y3+200)  

    -- add these 3 lines of code for the hit with the line.
    if self.x>WIDTH-50 and self.x<WIDTH-30 and l>self.y3 and l<self.y3+200 then
        gm=3
    end

    popStyle()
    fmesh=mesh() 
    fmesh.vertices= {vec2(self.x-200,0),
                     vec2(self.x,self.y),
                     vec2(self.x+200,0)}
    fmesh.texCoords= {vec2(0,0),
                      vec2(1,0),
                      vec2(1,1)}

Sorry about that I should make the tabs more clear briarfox
Dave I thought I took out the class

Edit


-- construction dodge

function setup()
    parameter.watch("t")
    parameter.watch("Score")
    parameter.watch("gm")
    displayMode(FULLSCREEN)
    l= HEIGHT-50
    Helicopter:init()
    
    w={}
    table.insert(w,Wood())
    end
    gm=2
s=0
t=5
function draw()
    --gm 0 is debug
    --gm 1 is game
    --gm 2 is menu
    --gm 3 is gameover
    if gm==0 or gm==1 then
    s = s + 1
    end
    if gm==2 then
    background(78, 79, 59, 255)
    
    if CurrentTouch.tapCount==2 then
       gm=1
    end
    end
    if gm==1 then
    Score= math.floor(s)
    
    background(26, 164, 24, 255)
    sprite("Documents:bricks",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    text(Score,WIDTH/2,HEIGHT-20)
    for i,ww in pairs(w) do
        ww:draw()
        fill(198, 198, 198, 255)
        pushMatrix()
        pushStyle()
        popStyle()
        popMatrix()
            
            if CurrentTouch.state==BEGAN then
                Helicopter:touched()
            elseif CurrentTouch.state==MOVING then
                Helicopter.touched()
                elseif CurrentTouch.state==ENDED then
                    l = l - 5
                end
        Helicopter:draw()
    --gm 0 is debug
    --gm 1 is game
    --gm 2 is menu
    --gm 3 is gameover

    -- simple game over routine

    if gm==3 then
        background(40,40,50)
        fill(255)
        text("Game Over",WIDTH/2,HEIGHT/2)
        text("Double tap to play",WIDTH/2,HEIGHT/2-50)
            gm=2
            
        end
    end


    -- made changes to this code

            if l>= HEIGHT then
                gm=3   -- set to game over
            end
            if l<=0 then
                gm=3  -- set to game over
            end

    end
end


It glitches at the top why does it

Anyone

I guess I didn’t make it clear enough about adding the code. I didn’t mean that all of the lines had to be add. I showed existing code around the lines I added to give you an idea of where to add the new lines. Here is the way the draw routine should look like. Also, in the version I was working on, I changed the Helicopter up and down rate from 5 to 1. That made it smoother and more interesting to play.


function draw()
    --gm 0 is debug
    --gm 1 is game
    --gm 2 is menu
    --gm 3 is gameover
    if gm==3 then
        background(40,40,50)
        fill(255)
        text("Game Over",WIDTH/2,HEIGHT/2)
        text("Double tap to start",WIDTH/2,HEIGHT/2-50)
        if CurrentTouch.state==BEGAN and CurrentTouch.tapCount==2 then
            gm=2
            l=HEIGHT/2
        end
    end
    if gm==0 or gm==1 then
        s = s + 1
    end
    if gm==2 then
        background(78, 79, 59, 255)
        if CurrentTouch.tapCount==2 then
           gm=1
        end
    end
    if gm==1 then
        Score= math.floor(s)
        background(26, 164, 24, 255)
        sprite("Documents:bricks",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        text(Score,WIDTH/2,HEIGHT-20)
        for i,ww in pairs(w) do
            ww:draw()
            fill(198, 198, 198, 255)
            pushMatrix()
            pushStyle()
            popStyle()
            popMatrix()   
            if CurrentTouch.state==BEGAN then
                Helicopter:touched()
            elseif CurrentTouch.state==MOVING then
                Helicopter.touched()
            elseif CurrentTouch.state==ENDED then
                l = l - 5
            end
            Helicopter:draw()
            if l>= HEIGHT then
                l=HEIGHT
                gm=3
            end
            if l<=0 then
                l=0
                gm=3
            end
        end
    end
end