Snake Game

Do any one have a idea for a snake game?Im new here im a not soooo good programmer.i switsched from Swift to Lua.So do any one have a idea for a snake game or a code or a zipfile?then i look on the cood and i try to understand.
Kind regrads Programmer_23

@Programmer_23 - just search the forum, you’ll probably find a few. I’ll check and see if I can find any old posts - also load up and check on WebRepo.

ok thx

Here’s a simple one I wrote as a tutorial ages ago https://gist.github.com/Westenburg/6487451

@Programmer_23 - a quick search found a few hits. I’ll list a few below starting with :

Jordan

Dwins

Alex812a

Edit: Ooops sorry, links not added properly - now working correctly.

@Programmer_23 Here’s some code. Tap near an edge to move snake in that direction. Each tap increase snake length by 2. Snake dies if it runs into itself.

viewer.mode=FULLSCREEN

function setup() 
    rectMode(CENTER) 
    len,cnt=0,0
    size=8 -- size of snake square
    speed=20  -- speed of snake, larger value = slower speed
    dir=vec2(0,0)   -- direction to move
    snake={vec2(WIDTH//2,HEIGHT//2)}    -- starting position
end

function draw()
    background(0)
    fill(255)
    text("tap near an edge to move snake in that direction",WIDTH/2,HEIGHT-50)
    text("each tap increases snake size by 2",WIDTH/2,HEIGHT-75)
    cnt=cnt+1
    -- update snake table
    if cnt>speed then
        table.insert(snake,1,snake[1]+dir)  -- update table to move snake in dir
        if #snake>len then  -- remove end of snake from table
            table.remove(snake,#snake)
        end
        cnt=0
    end 
    if hit then  -- snake ran into itself
        dir=vec2(0,0)
        text("Snake ran into itself",WIDTH/2,HEIGHT/2)
    else
        for z=1,#snake do       -- draw snake 
            fill(255)
            if z==1 then
                fill(0,255,0)   -- color head of snake
            elseif snake[z]==snake[1] then  -- snake ran into itself
                hit=true
            end
            rect(snake[z].x,snake[z].y,size)   -- draw snake
        end
    end    
end

function touched(t)
    if t.state==BEGAN then
        len=len+2   -- add 2 to length of snake
        if t.x<150 then
            dir=vec2(-size,0)
        elseif t.x>WIDTH-150 then
            dir=vec2(size,0)
        elseif t.y<150 then
            dir=vec2(0,-size)
        elseif t.y>HEIGHT-150 then
            dir=vec2(0,size)
        end
    end
end

@dave1707 that’s some pretty great code, damn. I made snake in 3D a few days ago and it was like 600 lines in length, not including the dependencies 0.0

I took my above code and created a simple 2 player game. To change a snake direction, swipe up/down/right/left. Swipe in the upper half for the red snake, the lower half for the green snake. Each snake gets longer each direction change. Try not to run into yourself or the other snake. I don’t know how well this works because I didn’t play with it that much.

viewer.mode=FULLSCREEN

function setup()
    stroke(255)
    msg=""
    size=20
    w=WIDTH//size
    h=HEIGHT//size    
    h1=(h*.8)//1
    h2=(h*.2)//1
    strokeWidth(1)
    s1=snakeCl(vec2((w*size)//2,h1*size),color(255,0,0))
    s2=snakeCl(vec2((w*size)//2,h2*size),color(0,255,0))
end

function draw()
    background()
    line(0,HEIGHT/2,WIDTH,HEIGHT/2)
    s1:draw()
    s2:draw()
    hitOther()
end

function touched(t)
    if t.y>HEIGHT/2 then
        s1:touched(t)
    else
        s2:touched(t)
    end
end

function hitOther()
    for z=1,#s1.snake do
        if z>1 and s1.snake[1]==s1.snake[z] then
            gameOver=true
            msg="Red ran into itself"
        end
        if s2.snake[1]==s1.snake[z] then
            gameOver=true
            msg="Green ran into Red"
        end
    end
    for z=1,#s2.snake do
        if z>1 and s2.snake[1]==s2.snake[z] then
            gameOver=true
            msg="Green ran into itself"
        end
        if s1.snake[1]==s2.snake[z] then
            gameOver=true
            msg="Red ran into Green"
        end
    end
end


snakeCl=class()

function snakeCl:init(pos,col)
    self.snake={pos}
    self.size=size
    self.speed=20
    self.dir=vec2(0,0)
    self.len=0
    self.cnt=0
    self.hit=false
    self.col=col
end

function snakeCl:draw()
    self:update()
    for z=1,#self.snake do       
        fill(self.col)  
        rect(self.snake[z].x,self.snake[z].y,self.size)   
    end
end

function snakeCl:touched(t)
    if t.state==BEGAN then
        self.tx=t.x
        self.ty=t.y
    end
    if t.state==ENDED then
        self.len=self.len+2  -- add 2 to length of snake
        local tx=t.x-self.tx
        local ty=t.y-self.ty
        if math.abs(tx)>math.abs(ty) then
            if tx>0 then
                self.dir=vec2(self.size,0)
            else
                self.dir=vec2(-self.size,0)
            end
        else
            if ty>0 then
                self.dir=vec2(0,self.size)
            else
                self.dir=vec2(0,-self.size)
            end
        end   
    end
end

function snakeCl:update()
    if not gameOver then 
        self.cnt=self.cnt+1
        if self.cnt>self.speed then
            table.insert(self.snake,1,self.snake[1]+self.dir)
            if #self.snake>self.len then  
                table.remove(self.snake,#self.snake)
            end
            self.cnt=0
        end 
    else
        fill(255)
        text("Game over",WIDTH/2,HEIGHT/2+25)
        text(msg,WIDTH/2,HEIGHT/2-25)
    end
end