# Beat the Spider (game)

I took my game, Circles of Confusion, and stripped out some code, added some sprites, and added some other code and came up with this game. The instructions are in the game or scroll down to read them at the end of the code. It’s a simple game and didn’t require much of a change to get it working. I also added more comments for code sharing which is the main reason for this.

``````
supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)

function setup()
level=20   -- starting number of brains, 20
speedTab={2,4,6}    -- speed of spider, 2=slow 4=medium 6=fast
instruct=true    -- show instructions
init()
end

function init()
tab1={}    -- table for brains
tab2={}    -- table for touched brains
tab3={}    -- table for reachec brains

tab4={225,375,525}    -- speed selection x value

mx=WIDTH/2
my=20
dx=0
dy=0
offset=1
touch=1
move=false

for z=1,level do    -- create random numbered brains
createEnd(z)
tab2[z]=0    -- 0 table
tab3[z]=0    -- 0 table
end
end

function draw()
local sqrt=math.sqrt
background(200,200,200)

-- show instruction screen
if instruct then
instructions()
ellipse(225,100,100,50)
ellipse(375,100,100,50)
ellipse(525,100,100,50)
fill(255)
text("Slow",225,100)
text("Medium",375,100)
text("Fast",525,100)
return
end

-- show level number in lower right corner
fill(0, 120, 255, 255)
fontSize(50)
text(level,WIDTH-50,30)
fontSize(17)

-- draw brains and numbers below them
for z=1,#tab1 do
-- if spider reached brain, show explosion
if tab3[z]==1 then
sprite(img3,tab1[z].x,tab1[z].y,35,35)
else
-- show green selection around brain
if tab2[z]==z then
fill(0,255,0)
ellipse(tab1[z].x,tab1[z].y,50)
end
-- show brain
sprite(img2,tab1[z].x,tab1[z].y,35,35)
-- show number below brain
fill(255,0,0)
text(z,tab1[z].x,tab1[z].y-25)
end
end

-- calculate dx,dy distance for constant speed
if move then
a=sqrt(dx*dx+dy*dy)
b=speed/a
mx = mx + dx*b
my = my + dy*b
else
fontSize(25)
text("Tap brain 1 to start.",WIDTH/2,100)
fontSize(17)
end

-- show spider
sprite(img1,mx,my,40,40)

moveCircle()
who()
end

function who()
fill(176, 173, 173, 255)
text("dave1707 (C)",WIDTH-200,25)
end

function touched(t)
if t.state==BEGAN then
-- instruction
if instruct then
for z=1,3 do
if (t.y-100)^2/25^2+(t.x-tab4[z])^2/50^2 <= 1 then
instruct=false
speed=speedTab[z]
holdSpeed=speed
init()
return
end
end
end
-- tap count of 1
if t.tapCount==1 then
-- spider not moving yet, start him moving
if not move then
speed=holdSpeed
dx=tab1[1].x-mx
dy=tab1[1].y-my
move=true
end
-- check which brain is being tapped
for z=1,#tab1 do
if (t.y-tab1[z].y)^2/40^2+(t.x-tab1[z].x)^2/40^2 <= 1 then
if z~=touch then
init()
return
end
tab2[z]=z
touch = touch + 1
-- all brains were tapped
if touch==level+1 then
holdSpeed=speed
speed=10 -- increase speed to end level
end
end
end
end
end
end

function moveCircle()
local a; local b; local ab
local sqrt=math.sqrt; local abs=math.abs
if move then
-- calc new x,y direction
a=abs(tab1[offset].x-mx)
b=abs(tab1[offset].y-my)
ab=sqrt(a*a+b*b)
-- within 5 pixels of brain
if ab<5 then
tab3[offset]=1
-- brain hasn't been touched yet
if tab2[offset]~=offset then
init()
return
end
offset = offset + 1
-- have all brains been reached
if offset==#tab1+1 then
level = level + 1
init()
return
end
dx=tab1[offset].x-mx
dy=tab1[offset].y-my
end
end
end

function createEnd(ep)
-- create table of random locations for each brain
count=0
if ep==1 then
tab1[ep]=vec2(math.random(50,WIDTH-50),math.random(150,HEIGHT-50))
else
-- create next location
while 1 do
xx=0
hx=math.random(50,WIDTH-50)
hy=math.random(150,HEIGHT-50)
-- keep brains at least 50 pixels apart
for z=1,ep-1 do
v1=vec2(hx,hy)
v2=tab1[z]
d=v1:dist(v2)
if d < 50 then
xx=1
end
end
if xx==0 then
tab1[ep]=vec2(hx,hy)
return
end
-- exit while loop if stuck
count = count + 1
if count>100 then
init()
return
end
end
end
end

function instructions()
w=WIDTH/2
fill(255,0,0)
fontSize(40)
text("Beat the Spider",w,900)
fill(255)
fontSize(25)
text("Object is to tap each brain in sequence before",w,800)
text("the spider beats you to it. If you tap a brain",w,750)
text("out of sequence or the spider beats you to it,",w,700)
text("the game is over and you re-play that level.",w,650)
text("If you beat the spider to all of the brains,",w,600)