Help me improve my snake game :)

Hi, I am João, 16 years old and I am from Portugal… I have used codea for 1 year and now, without any programming lessons, just by using codea, I learned a lot, and because of that I made a lot of cool tools most of them calculators to help in school such as quadratic formula resolver (sry for my English xD) and yesterday I started making a snake game because my brother bet I couldn’t do it… So, this is what I done so far (remember I have few skills and I couldn’t learn until now how you use classes)


-- Snake

-- João Lopes
-- 07/12/2015

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
 
-- Innitial Score   
score = 0
    
-- Innitial Snake Position     
snake1X = WIDTH/2
snake1Y = HEIGHT/2  

-- Initial Speed And Difficulty
speed = 3

-- Initial State At The Game Start    
status = notColided

-- Spawn The Firts Ellipse           
ellipsex = math.random(WIDTH)    
ellipsey = math.random(HEIGHT)

-- Parameters To Change Difficulty    
parameter.action("Easy", function() speed = 1 alert("Dificulty Set To Easy") end)
parameter.action("Medium", function() speed = 2 alert("Dificulty Set To Medium") end)
parameter.action("Hard", function() speed = 3 alert("Dificulty Set To Hard") end)
parameter.action("Impossible", function() speed = 10 alert("Dificulty Set To Impossible") end)

-- Innitial Snake Size
    
snakeW = 20
snakeH = 20

end

function draw() 

-- Black BackGround
background(0)

-- Draw The Initial Snake 
pushStyle()


fill(0, 0, 255, 255)   
stroke(255, 255, 0, 255) 
rectMode(CENTER)      
strokeWidth(3)
rect(snake1X, snake1Y, snakeW, snakeH)
    
popStyle()

-- Make The Snake Grow As It Colides
    
    if createsnake == true then 
        
    snakeW = snakeW + 20
    snakeH = snakeH + 20
        
    createsnake = false    
        
    end
    
-- Draw The TouchPad
    pushStyle()
    
    rectMode(CENTER)
    noFill()
    stroke(255, 255, 0, 255)
    strokeWidth(3)
    rect(WIDTH*0.8, HEIGHT*0.1, 75,75)
    rect(WIDTH*0.8, HEIGHT*0.23, 75, 75)
    rect(WIDTH*0.9, HEIGHT*0.1, 75, 75)
    rect(WIDTH*0.7, HEIGHT*0.1, 75, 75)
    
    popStyle()

-- Set Up Of The TouchPad Commands
        
        if CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 + 37.5 then snake1Y = snake1Y - speed
        
        
        elseif CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.23 - 37.5 and CurrentTouch.y < HEIGHT*0.23 +37.5 then snake1Y = snake1Y + speed
        
        elseif CurrentTouch.x > WIDTH*0.7 - 37.5 and CurrentTouch.x < WIDTH*0.7 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 then snake1X = snake1X - speed
        
        elseif CurrentTouch.x > WIDTH*0.9 - 37.5 and CurrentTouch.x < WIDTH*0.9 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 then snake1X = snake1X + speed       
        
end
 
-- Alert "Game Over If Snake Colides With The Limits Of Your Display And Restart The Game   
    if snake1X + 10 >= WIDTH then 
        
        snake1X = WIDTH - 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snake1X - 10 <= 0 then 
        
        snake1X = 0 + 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snake1Y + 10 >= HEIGHT then 
        
        snake1Y = HEIGHT - 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snake1Y - 10 <= 0 then
        
         snake1Y = 0 + 10
        
         alert("GameOver", "Score ".. score)
        
         restart()

    end
    
-- Draw Score
    pushStyle()
   
fontSize(100)  
fill(255)
textMode(CORNER)
text(score, WIDTH*0.03, HEIGHT*0.85)
    
    popStyle()

-- Detect If Snake Colides With The Ellipse And If So, Score Raises By 10 And Another Ellipse Spawns
    if snake1X >= ellipsex - 10 and snake1X <= ellipsex + 10 and snake1Y >= ellipsey - 10 and snake1Y <= ellipsey + 10  then 
        
        sound(SOUND_PICKUP, 21838)

        status = Colided
        
        ellipsex = math.random(WIDTH)
        ellipsey = math.random(HEIGHT)
        
        score = score + 10
        
        createsnake = true
                  
    end  
-- Spawn The First Ellipse    
if status ==  notColided then   
        
    pushStyle()     
        
fill(0)
stroke(255)
strokeWidth(3)
ellipseMode(CENTER) 
ellipse(ellipsex, ellipsey, 20)

        popStyle()
   
     end  
        
        end 

So, my problem here is making the snake grow not in size but in squares … Also this is the prototype, I am going to improve a lot of stuff…

@Joao_Lopes Nice job on the game. When you post code, put 3~'s on a line befor and after your code so it displays properly. I added them to your code above. If you want the snake to grow in length, you need to use a table.

Parabéns @Joao_Lopes! Muito bom trabalho!

Obs: sou brasileiro!

@dave1707 thanks!

@erickyamato obrigado !

So, I don’t have a lof of time but this is my improved version but I don’t know how to make the snake grow in squares using tables …


-- Snake 1.1.0 Beta 3 xD

-- João Lopes
-- 07/12/2015

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
 
-- Table For Snakes
snakes = {}    
    
-- Innitial Score   
score = 0
    
-- Innitial Snake Position     
snakeX = WIDTH/2
snakeY = HEIGHT/2  

-- Initial Speed And Difficulty
speed = 3

-- Initial State At The Game Start    
status = notColided

-- Spawn The Firts Ellipse           
ellipsex = math.random(WIDTH - 10)    
ellipsey = math.random(HEIGHT - 10)

-- Parameters To Change Difficulty    
parameter.action("Easy", function() speed = 1 alert("Dificulty Set To Easy") end)
parameter.action("Medium", function() speed = 2 alert("Dificulty Set To Medium") end)
parameter.action("Hard", function() speed = 3 alert("Dificulty Set To Hard") end)
parameter.action("Impossible", function() speed = 10 alert("Dificulty Set To Impossible") end)
    
-- Fill Of The TouchPad While It Isn't Touched
    
    filloftouchpadup = 0,0,0,0
    filloftouchpaddown = 0,0,0,0
    filloftouchpadright = 0,0,0,0
    filloftouchpadleft = 0,0,0,0
    
end

function draw() 

-- Black BackGround
background(0)

-- Draw The Initial Snake 
pushStyle()


fill(0, 0, 255, 255)   
stroke(255, 255, 0, 255) 
rectMode(CENTER)      
strokeWidth(3)
rect(snakeX, snakeY, 20, 20)
    
popStyle()
    
-- Draw The TouchPad
    pushStyle()
    
    rectMode(CENTER)
    stroke(255, 255, 0, 255)
    strokeWidth(3)
 
-- TouchPad Down 
    pushStyle()
    fill(filloftouchpaddown)
    rect(WIDTH*0.8, HEIGHT*0.1, 75,75)
    popStyle()
  
-- TouchPad Up  
    pushStyle()
    fill(filloftouchpadup)
    rect(WIDTH*0.8, HEIGHT*0.23, 75, 75)
    popStyle()
 
-- TouchPad Right   
    pushStyle()
    fill(filloftouchpadright)
    rect(WIDTH*0.9, HEIGHT*0.1, 75, 75)
    popStyle()
    
-- TouchPad Left
    pushStyle()
    fill(filloftouchpadleft)
    rect(WIDTH*0.7, HEIGHT*0.1, 75, 75)
    popStyle()
    
    popStyle()

-- Set Up Of The TouchPad Commands

-- TouchPad Down        
        if CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 + 37.5 and CurrentTouch.state == BEGAN or CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 + 37.5 and CurrentTouch.state == MOVING then 
        
        snakeY = snakeY - speed
        
        filloftouchpaddown = 100
        
        elseif CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 + 37.5 and CurrentTouch.state == ENDED then
        
        filloftouchpaddown = 0, 0, 0, 0
        
        snakeY = snakeY - speed
        
        end      
        
-- TouchPad Up
        if CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.23 - 37.5 and CurrentTouch.y < HEIGHT*0.23 +37.5 and CurrentTouch.state == BEGAN or CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.23 - 37.5 and CurrentTouch.y < HEIGHT*0.23 +37.5 and CurrentTouch.state == BEGAN and CurrentTouch.state == MOVING then 
        
        snakeY = snakeY + speed
        
        filloftouchpadup = 100
        
        elseif CurrentTouch.x > WIDTH*0.8 - 37.5 and CurrentTouch.x < WIDTH*0.8 + 37.5 and CurrentTouch.y > HEIGHT*0.23 - 37.5 and CurrentTouch.y < HEIGHT*0.23 +37.5 and CurrentTouch.state == ENDED then
        
        filloftouchpadup = 0, 0, 0, 0
        
        snakeY = snakeY + speed        
        
        end 
    
-- TouchPad Left
        if CurrentTouch.x > WIDTH*0.7 - 37.5 and CurrentTouch.x < WIDTH*0.7 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == BEGAN or CurrentTouch.x > WIDTH*0.7 - 37.5 and CurrentTouch.x < WIDTH*0.7 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == MOVING then 
        
        snakeX = snakeX - speed
        
        filloftouchpadleft = 100
        
        elseif CurrentTouch.x > WIDTH*0.7 - 37.5 and CurrentTouch.x < WIDTH*0.7 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == ENDED then
        
        snakeX = snakeX - speed
        
        filloftouchpadleft = 0, 0, 0 ,0
        
        end
        
-- TouchPad Right
        if CurrentTouch.x > WIDTH*0.9 - 37.5 and CurrentTouch.x < WIDTH*0.9 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == BEGAN or CurrentTouch.x > WIDTH*0.9 - 37.5 and CurrentTouch.x < WIDTH*0.9 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == MOVING then 
        
        snakeX = snakeX + speed    
        
        filloftouchpadright = 100
        
        elseif CurrentTouch.x > WIDTH*0.9 - 37.5 and CurrentTouch.x < WIDTH*0.9 + 37.5 and CurrentTouch.y > HEIGHT*0.1 - 37.5 and CurrentTouch.y < HEIGHT*0.1 +37.5 and CurrentTouch.state == ENDED then 
        
        snakeX = snakeX + speed
        
        filloftouchpadright = 0, 0, 0, 0
      
        end
 
-- Alert "Game Over If Snake Colides With The Limits Of Your Display And Restart The Game   
    if snakeX + 10 >= WIDTH then 
        
        snakeX = WIDTH - 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snakeX - 10 <= 0 then 
        
        snakeX = 0 + 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snakeY + 10 >= HEIGHT then 
        
        snakeY = HEIGHT - 10
        
        alert("GameOver", "Score ".. score)
        
        restart()
        
        elseif snakeY - 10 <= 0 then
        
         snakeY = 0 + 10
        
         alert("GameOver", "Score ".. score)
        
         restart()

    end
    
-- Draw Score
    pushStyle()
   
fontSize(100)  
fill(255)
textMode(CORNER)
text(score, WIDTH*0.03, HEIGHT*0.85)
    
    popStyle()

-- Detect If Snake Colides With The Ellipse And If So, Score Raises By 10 And Another Ellipse Spawns
    if snakeX >= ellipsex - 10 and snakeX <= ellipsex + 10 and snakeY >= ellipsey - 10 and snakeY <= ellipsey + 10  then 
        
        sound(SOUND_PICKUP, 21838)

        status = Colided
        
        ellipsex = math.random(WIDTH - 10)
        ellipsey = math.random(HEIGHT - 10)
        
        score = score + 10
                  
    end 
     
-- Spawn The First Ellipse    
if status ==  notColided then   
        
    pushStyle()     
        
fill(0)
stroke(255)
strokeWidth(3)
ellipseMode(CENTER) 
ellipse(ellipsex, ellipsey, 20)

        popStyle()
   
     end 
    
    end 

@Joao_Lopes - have a look at this

https://www.youtube.com/watch?v=1Xo9hMHW0Eo

I will post the code and explain it soon.

@Ignatz that’s what i want ! XD :slight_smile: thanks for the reply… I will be waiting for the code :smiley: !

@Joao_Lopes - here you are

https://coolcodea.wordpress.com/2015/12/11/252-snake/

@Ignatz thanks, i am now going to improve my snake game… Also, your code showed me how to make the snake move only in certain places, it’s like a grid… Thanks for your time :slight_smile:

If you have trouble with the tables, maybe try reading my ebook on Lua, which explains them. You’ll find it here

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

@Joao_Lopes Here’s a version of snake that I had just in case you want to see another version. Tap a blue square to move in that direction. If the snake eats his food (yellow square) the score increases by the length of the snake. Each time you tap a blue square, the length of the snake increases by 2. If the head of the snake runs into it’s body, the game is over. There aren’t any checks if the snake goes off screen, so you can go off screen and come back on.

displayMode(FULLSCREEN)

function setup() 
    fontSize(50) 
    rectMode(CENTER) 
    score,len,cnt=0,0,0
    size=20 -- size of snake square
    speed=20  -- speed of snake  
    dir=vec2(0,0)
    xLim=((WIDTH-100)/size)//1+1
    yLim=((HEIGHT-100)/size)//1+1
    food=vec2(math.random(xLim)*size,math.random(yLim)*size)
    snake={vec2(size*10,size*20)}
end

function draw()
    background(0)
    if not hit then
        cnt=cnt+1
        if cnt>speed then   -- move snake
            table.insert(snake,1,snake[1]+dir)
            if #snake>len then
                table.remove(snake,#snake)
            end
            cnt=0
        end  
        for z=1,#snake do
            fill(255)
            if z==1 then
                fill(0,255,0)   -- color head of snake
            elseif snake[z]==snake[1] then
                hit=true    -- snake ran into itself
            end
            rect(snake[z].x,snake[z].y,size,size)   -- draw snake
        end
        if snake[1]==food then    -- snake found food
            food=vec2(math.random(xLim)*size,math.random(yLim)*size)
            score=score+#snake  -- add length of snake to score
        end
    else
        fill(0,255,0)   -- show score
        text("Game over!",WIDTH/2,HEIGHT/2+200)
        text("You ran into yourself.",WIDTH/2,HEIGHT/2+100)
        text("Double tap to restart.",WIDTH/2,HEIGHT/2)
    end
    fill(0,0,255)   -- draw direction buttons
    rect(WIDTH/2,50,30,30)
    rect(WIDTH/2,HEIGHT-50,30,30)
    rect(50,HEIGHT/2,30)
    rect(WIDTH-50,HEIGHT/2,30,30)
    fill(255, 0, 0, 255)
    text("Score  "..score,WIDTH/2,HEIGHT-100)
    fill(252, 255, 0, 255)
    rect(food.x,food.y,size)    -- draw food
end

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

@dave1707 thank you. i will check it out :slight_smile: