Some of the new coders want to write games, but they don’t know where to start. Here is some code the anyone can take and make into something more interesting. It’s kind of barebones, but it’s a start for someone willing to improve on it. The object is to tilt the ipad and move the blue circle around the screen hitting the numbered white circles. If you hit the circles in the correct order they will turn red. If you complete the order, the next round will have 3 more circles to hit. If you hit a circle out of order, they all turn white and you start that round again. As I said, this is a starter, so what else you want to add to make it more interesting is up to you. Start the game with the ipad laying flat.
displayMode(FULLSCREEN)
function setup()
ball=vec2(WIDTH/2,HEIGHT/2)
count=5 -- number of circles to start with
nbr=1 -- circle number to start with
createTarget() -- create circles to hit
end
function createTarget()
target={} -- create table for targets
for z=1,count do -- create number of count circles
-- x,y,number,0=not hit 1=hit
table.insert(target,vec4(math.random(100,WIDTH-100),
math.random(100,HEIGHT-100),z,0))
end
nbr=1 -- starting circle number
match=0 -- number of circle being hit
end
function draw()
background(40,40,50) -- background color
fill(0, 52, 255, 255) -- ball color
ellipse(ball.x,ball.y,50) -- draw blue ball
ball.x = ball.x + Gravity.x*40 -- move in x direction
ball.y = ball.y + Gravity.y*40 -- move in y direction
for a,b in pairs(target) do -- draw circles
fill(255) -- start with white color
if b.w==1 then -- should it be red
fill(255,0,0) -- set red color
end
ellipse(b.x,b.y,50) -- draw circle
fill(0, 115, 255, 255) -- color for numbers on circles
text(a,b.x,b.y) -- draw numbers on circles
end
hit() -- check which circle is hit
end
function hit()
v1=ball -- vector info for distance calculation
overlap=false -- no circle is being hit
for a,b in pairs(target) do -- loop thru target table
d=v1:dist(vec2(b.x,b.y))-- distance between blue ball and circles
if d<50 then -- less than 50
overlap=true -- ball and circle are touching
match=b.z -- number of circle being touched
end
end
if not overlap then -- no overlap happening
if match>0 then -- was a circle being overlapped
nbr=nbr+1 -- add 1 for next circle
match=0 -- clear variable
end
end
if match>0 then -- circle being overlapped
if match==nbr then -- correct circle
target[match].w=1 -- set flag for red color
if match==#target then -- last circle being touched
count = count + 3 -- add 3 to circle count
createTarget() -- create new circles
end
else
createTarget() -- wrong circle touched, redraw circles
end
end
end