Here is a little game I created awhile ago. It uses a flick of the finger to shoot white balls at a red one. The object is to push the red ball through the red line. I’m not sure how accurate the flick controls the white balls as far as speed, but it seems to work OK here with very little math involved.
supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)
lineCapMode(SQUARE)
function setup()
tab={}
rcount=0
scount=0
screated=false
rcreated=false
createEdges()
end
function draw()
background(40, 40, 50)
showLines()
showText()
drawEdge()
drawRandom()
drawShots()
end
function showLines()
stroke(0,255,0)
strokeWidth(4)
line(0,300,WIDTH,300)
stroke(255,0,0)
strokeWidth(20)
line(300,HEIGHT,450,HEIGHT)
end
function showText()
fontSize(40)
fill(255,0,0)
if not rcreated then
text("Double tap screen for red ball.",WIDTH/2,HEIGHT/2+300)
end
str=string.format("Goals %4d Shots %4d",rcount,scount)
fontSize(20)
fill(255)
text(str,WIDTH/2,HEIGHT/2-50)
str=string.format("Score %6.4f (Goals / shot)",rcount/scount)
text(str,WIDTH/2,HEIGHT/2-100)
text("Flick shots from below this line to push",WIDTH/2,320)
text("the red ball through the red line at the top.",WIDTH/2,280)
end
function createEdges()
e1=physics.body(EDGE,vec2(0,0),vec2(0,1200))
e1.type=STATIC
e2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,1200))
e2.type=STATIC
e3=physics.body(EDGE,vec2(0,HEIGHT),vec2(300,HEIGHT))
e3.type=STATIC
e4=physics.body(EDGE,vec2(450,HEIGHT),vec2(WIDTH,HEIGHT))
e4.type=STATIC
end
function drawEdge()
stroke(255)
strokeWidth(10)
line(0,0,0,HEIGHT)
line(WIDTH,0,WIDTH,HEIGHT)
line(0,HEIGHT,300,HEIGHT)
line(450,HEIGHT,WIDTH,HEIGHT)
end
function createRandom()
r1=physics.body(CIRCLE,30)
r1.gravityScale=.5
r1.x=math.random(WIDTH)
r1.y=HEIGHT-200
rcreated=true
end
function drawRandom()
if rcreated then
strokeWidth(0)
fill(255,0,0)
ellipse(r1.x,r1.y,60,60)
destroyRandom()
end
end
function destroyRandom()
if r1.y < 0 or r1.y>HEIGHT then
rcreated=false
tab={}
if r1.y>HEIGHT then
rcount = rcount + 1
end
r1:destroy()
end
end
function createShots()
scount = scount + 1
z=#tab+1
tab[z]=physics.body(CIRCLE,15)
tab[z].gravityScale=-.1
end
function drawShots()
fill(255)
for z=1,#tab do
ellipse(tab[z].x,tab[z].y,30,30)
end
destroyShots()
end
function destroyShots()
for z=1,#tab do
if tab[z].y > 1000 or tab[z].y < 0 then
tab[z]:destroy()
table.remove(tab,z)
return
end
end
end
function touched(t)
if not rcreated then
if t.tapCount==2 then
createRandom()
end
return
end
if t.state==BEGAN and t.y< 300 and rcreated then
createShots()
screated=true
z=#tab
tab[z].x=t.x
tab[z].y=t.y
x1=t.x
y1=t.y
end
if t.state==MOVING and screated then
if #tab>0 then
z=#tab
tab[z].x=t.x
tab[z].y=t.y
end
end
if t.state==ENDED and screated then
screated=false
if #tab>0 then
z=#tab
x2=t.x
y2=t.y
xd=x2-x1
yd=y2-y1
tab[z].linearVelocity=vec2(xd*5,yd*5)
end
end
end