Snake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
displayMode(FULLSCREEN) | |
function setup() | |
speed=20 --a bigger number makes it slower and easier | |
size=20 --size of squares in snake | |
start=vec2(WIDTH/2//size,HEIGHT/2//size)*size --start in the middle | |
S={start,start} --table holding snake squares | |
joyCentre=vec2(WIDTH-140,140) --centre of joystick paddle | |
joyWidth,joyLength=40,80 --length and width of paddle arms | |
joyArms={ | |
{joyCentre-vec2(joyLength,joyWidth)/2,joyLength,joyWidth,vec2(-1,0)}, | |
{joyCentre+vec2(joyLength,joyWidth)/2,joyLength,joyWidth,vec2(1,0)}, | |
{joyCentre-vec2(joyWidth,joyLength)/2,joyWidth,joyLength,vec2(0,-1)}, | |
{joyCentre+vec2(joyWidth,joyLength)/2,joyWidth,joyLength,vec2(0,1)} | |
} | |
AddFood() | |
direction=vec2(0,0) --start by not moving at all | |
counter=0 | |
end | |
function draw() | |
background(150, 192, 209, 255) | |
DrawSnake() | |
DrawFood() | |
DrawJoyStick() | |
end | |
function DrawJoyStick() | |
pushStyle() | |
stroke(86, 133, 199, 255) | |
strokeWidth(joyWidth) | |
line(joyCentre.x-joyLength,joyCentre.y,joyCentre.x+joyLength,joyCentre.y) | |
line(joyCentre.x,joyCentre.y-joyLength,joyCentre.x,joyCentre.y+joyLength) | |
popStyle() | |
end | |
function DrawSnake() | |
--update snake position | |
counter=counter+1 | |
if counter%speed==0 then | |
local p=S[1]+direction*size | |
if p.x<0 or p.x>WIDTH or p.y<0 or p.y>HEIGHT then | |
--LOSE GAME - NOT PROGRAMMED | |
end | |
table.insert(S,1,p) | |
--eat food | |
if S[1]:dist(food)<0.1 then | |
--UPDATE SCORE -- NOT PROGRAMMED | |
AddFood() --create new food somewhere else | |
else | |
table.remove(S,#S) | |
end | |
end | |
pushStyle() | |
fill(75, 140, 72, 255) | |
local f=(counter%speed)/speed | |
for i=1,#S-1 do | |
local x,y=S[i].x*f+S[i+1].x*(1-f),S[i].y*f+S[i+1].y*(1-f) | |
rect(x,y,size) | |
end | |
popStyle() | |
end | |
function DrawFood() | |
pushStyle() | |
fill(205, 83, 83, 255) | |
rect(food.x,food.y,size) | |
popStyle() | |
end | |
function touched(t) | |
if t.state==ENDED then | |
for i=1,4 do | |
local j=joyArms[i] | |
if math.abs(t.x-j[1].x)<j[2] and math.abs(t.y-j[1].y)<j[3] then | |
direction=j[4] | |
break | |
end | |
end | |
end | |
end | |
function AddFood() | |
while true do --keep looping until we get a position we want | |
--positions must be a multiple of the size, so first figure out how many we can fit into the screen | |
local w,h=WIDTH//size,HEIGHT//size | |
--choose one that isn't on the edge | |
food=vec2(math.random(2,w-1),math.random(2,h-1))*size | |
--make sure it is further than 50 pixels from our snake head | |
--and also that it is not inside our joystick | |
if food:dist(S[1])>50 and food:dist(joyCentre)>joyLength then break end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment