# Very simple snake game for beginners

I made this snake game (its free roaming so you can turn in all directions not cell based) for a codea user and I thought it the ideas behind it were so simple it would be good for beginners.
Start moving your finger over the screen in different directions and you’ll soon know how to make it turn. Feel free to use this however you please.

``````-- snake by Luatee

-- Use this function to perform your initial setup
function setup()
-- We set up a table of the snake parts (1. Head, 2+. Body) 3 to begin with the first being the head
snaketbl = {vec2(WIDTH/2,100),vec2(WIDTH/2,80),vec2(WIDTH/2,60)}
snakespeed = 3
-- Give the snake some food as a position
snakefood = vec2(100,HEIGHT-200)
-- Direction the snake travels
snakedir = vec2(0,1)
score = 0
started = false
fontSize(20)
parameter.watch("math.floor(snakespeed*10)/10")
end

function touched(t)
-- Start the program when first tapped
if started == false and t.state == BEGAN then
started = true
end
-- Move snake according to touch direction
snakedir = snakedir + vec2(t.deltaX/75,t.deltaY/75)
snakedir = snakedir:normalize()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(71, 71, 90, 255)

-- This sets the line thickness
strokeWidth(2)
-- This creates a scope in which different styles can be set (i.e. fill,stroke,blendMode)
pushStyle()
for i=1,#snaketbl do
if i>1 then
-- This makes the previous part of the snakes body link to the one in front
snaketbl[i] = snaketbl[i-1]+(snaketbl[i]-snaketbl[i-1]):normalize()*20
end
fill(0,200,30)
-- Draw snake body using ellipse
ellipse(snaketbl[i].x,snaketbl[i].y,20)
end
if started then
fill(40, 110, 130, 255)
ellipse(snakefood.x,snakefood.y,30)
popStyle()
-- Move the snakes head (snaketbl[1]) using the snakespeed and direction of travel
snaketbl[1] = snaketbl[1] + snakedir*snakespeed
-- Slowly increase the snakespeed to add difficulty
snakespeed = snakespeed + 0.0005
else
text([[
Tap the screen to begin!
Rules:
Drag your finger in the direction you want
the snake to turn to. If you leave the screen
you will die. Eat the food to become bigger!
Warning the snake speeds up over time!]],WIDTH/2,HEIGHT/2)
end
-- Kill the snake if it hits the wall and restart program
if snaketbl[1].x<10 or snaketbl[1].x>WIDTH-10 or snaketbl[1].y<10 or snaketbl[1].y>HEIGHT-10 then
setup()
end
-- Check if the snake head can reach its food
if snaketbl[1]:dist(snakefood)<20 then
-- Move snakefood once eaten
snakefood = vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50))
score = score + 1
-- Add new part to snake body
table.insert(snaketbl,snaketbl[#snaketbl])
end
text("Score: "..score,50,50)
end
``````

Enjoy, hope you guys can use this.

@luatee very cool! I’d like the snake to die if it touches itself too!

@luatee very nice. Here’s my take on snake aimed at beginners - https://gist.github.com/Westenburg/6487451

@Jmv38 sure I will probably have to slow down direction change to avoid the head hitting the third or fourth if the turn is too quick, thanks! Is it simple enough do you think?

@West that’s very in depth I think that will help a lot of people, with a different approach too.