Caterpillar program

I’m trying to make a caterpillar program where the user moves around a caterpillar with their finger and they score when they touch the other sprite. Here is my code:

displayMode(FULLSCREEN)
    
function setup()
    supportedOrientations(CurrentOrientation)
    theSprite=readImage("Planet Cute:Character Boy")
    tx = CurrentTouch.x
    ty = CurrentTouch.y
    score = 0
    cat = {}
    x= math.random(0, WIDTH)
    y= math.random(0, HEIGHT)
    w= CurrentTouch.x
    z= CurrentTouch.y
end

function draw()

    background(0)
        for i=#cat,1,-1 do
            sprite("Planet Cute:Character Boy",cat[i].x,cat[i].y)
        end
        nex()
        sprite("Planet Cute:Character Boy",tx,ty)
        sprite("Planet Cute:Character Princess Girl", x, y)
        
        if CollisionDetected(w, z, 30, 30, x, y, 30, 30) then
            score = score + 1
            next()
        end
end

function nex() 
    table.insert(cat,vec2(w,z))
end

function next()
      x = math.random(0, WIDTH)
      y= math.random(0, HEIGHT) 
end

function touched(t)
    if t.state==MOVING then
        w= t.prevX
        z= t.prevY
        tx=tx+t.deltaX
        ty=ty+t.deltaY
    end
end

function CollisionDetected(x1,  y1,  w1,  h1,  x2,  y2,  w2,  h2) 
 return not ((y1+h1 < y2) or (y1 > y2+h2) or (x1 > x2+w2) or (x1+w1 < x2))
    end

Well If you run the program you’ll see that the “caterpillar” gets really messed up and sprites appear in weird places and you can touch far away from the caterpillar and it will become disconnected, etc. I suspect it has something to do with the prevX and prevY values. But I’m not really sure what to do about it

@RedCoder1 I’m not sure what you’re trying to do here, but you’re executing the nex() function from draw() 60 times per second. That means you’re inserting values into the table “cat” 60 times per second. Since you’re not resetting the table at any time, the screen is just filling up with the sprite images of the “Character Boy”.

@RedCoder1 Here is some code I have that I think might be something like what you’re after. Drag your finger anywhere around the screen.


displayMode(FULLSCREEN)

function setup()
    tab={}
    x=WIDTH/2
    y=HEIGHT/2
    sx,sy=0,0
end

function draw()
    background(40,40,50)     
    fill(255,0,0)
    x=x+sx
    y=y+sy
    if #tab<150 then
        table.insert(tab,1,vec2(x,y))
    else
        table.insert(tab,1,vec2(x,y))
        table.remove(tab,#tab)
    end
    for z=#tab,1,-1 do
        sprite("Planet Cute:Character Boy",tab[z].x,tab[z].y)
    end
end

function getDirection()
    dx=CurrentTouch.x-x
    dy=CurrentTouch.y-y
    h=math.sqrt(dx*dx+dy*dy)
    sx=dx*3/h
    sy=dy*3/h
end

function touched(t)
    getDirection()   
end

Yea but I want it to be sprites so it looks like a caterpillar

@RedCoder1 I changed the above program to use a sprite. Is that more like it.

Well thanks I tried incorporating that into my code but I’ve come up with a new error:

displayMode(FULLSCREEN)
    
function setup()
    supportedOrientations(CurrentOrientation)
    theSprite=readImage("Planet Cute:Character Boy")
    gameOver = true
    score = 0
    w= math.random(0, WIDTH)
    z= math.random(0, HEIGHT)
    tab={}
    x=WIDTH/2
    y=HEIGHT/2
    sx,sy=0,0
end

function draw()

    background(0)
    if gameOver then
         font("MarkerFelt-Thin")
        fontSize(72)
        fill(50, 196, 21, 255)
        text("Caterpillar", WIDTH / 2, HEIGHT - 80)
        text("double tap to begin", WIDTH / 2, HEIGHT / 2)
        
    else
        x=x+sx
        y=y+sy
        if x<0 then
            x=0
        end
        if x>WIDTH then
            x=WIDTH
        end
        if y < 0 then
            y = 0
        end
        if y > HEIGHT then
            y = HEIGHT
        end
        if #tab<150 then
            table.insert(tab,1,vec2(x,y))
            else
            table.insert(tab,1,vec2(x,y))
            table.remove(tab,#tab)
        end
        for z=#tab,1,-1 do
            sprite("Planet Cute:Character Boy",tab[z].x,tab[z].y)
            if CollisionDetected(tab[z].x,tab[z].y, 50, 50, tab[1].x, tab[1].y, 50, 50) and #tab == 150 then
                gameOver = true
            end
        end
        sprite("Planet Cute:Character Princess Girl", w, z)
        if CollisionDetected(w, z, 30, 30, x, y, 30, 30) then
            score = score + 1
            next()
        end
    end
end

function getDirection()
    dx=CurrentTouch.x-x
    dy=CurrentTouch.y-y
    h=math.sqrt(dx*dx+dy*dy)
    sx=dx*3/h
    sy=dy*3/h
end

function next()
    w = math.random(0, WIDTH)
    z= math.random(0, HEIGHT) 
end

function touched(t)
    if t.state==BEGAN and t.tapCount == 2 then
        gameOver = false
    end
    getDirection()   
end

function CollisionDetected(x1,  y1,  w1,  h1,  x2,  y2,  w2,  h2) 
 return not ((y1+h1 < y2) or (y1 > y2+h2) or (x1 > x2+w2) or (x1+w1 < x2))
    end

Well I’m trying to make the head part of the caterpillar test for a collision with the rest of its body and end the game if it touches itself.

check fro all the segments except the one directly after the head

@RedCoder1 Change the “for” loop that you have in draw to this.

        for z=#tab,2,-1 do
            sprite("Planet Cute:Character Boy",tab[z].x,tab[z].y)
            if CollisionDetected(tab[z].x,tab[z].y,1,1,tab[1].x,tab[1].y,1,1) and #tab == 150 then
                gameOver = true
            end
        end