I was messing around with the touched function and decided to make a mini golf game. In total, this probably took 30 - 45 minutes, mainly working out small bugs. I thought I’d share it.
function setup()
distanceX = 0
distanceY = 0
tx = 0
ty = 0
x = WIDTH / 2
touchBegan = false
dx, dy = 0, 0
r = 30
scored = false
strokes = 0
barrierWidth = 250
barrierHeight = 50
holePos = vec2(math.random(r*1.5,WIDTH-r*1.5),math.random(r*1.5,HEIGHT-r*1.5))
if holePos.y > HEIGHT/2 then
y = 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(HEIGHT/2+barrierHeight,HEIGHT-barrierHeight))
else y = HEIGHT- 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(barrierHeight,HEIGHT/2-barrierHeight))
end
end
function draw()
background(49, 183, 89, 255)
if scored == false then
if holePos.y > barrier.y-barrierHeight-r and holePos.y < barrier.y+barrierHeight+r*1.5 and holePos.x < barrier.x+barrierWidth+r*1.5 and holePos.x > barrier.x-barrierWidth-r*1.5 then
if holePos.y > HEIGHT/2 then
y = 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(HEIGHT/2+barrierHeight,HEIGHT-barrierHeight))
else y = HEIGHT- 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(barrierHeight,HEIGHT/2-barrierHeight))
end
end
fill(0,0,0,255)
ellipse(holePos.x,holePos.y,r*1.5)
fill(255, 255, 255, 255)
ellipse(x, y, r)
fill(0,12,255,255)
rectMode(CENTER)
rect(barrier.x,barrier.y,barrierWidth,barrierHeight)
if touchBegan then
else
x, y = x + dx, y + dy
dx,dy = dx*0.98,dy*0.98
if x < r then
dx = - dx
x = 2 * r - x
end
if x > WIDTH - r then
dx = - dx
x = 2 * (WIDTH - r) - x
end
if y < r then
dy = - dy
y = 2 * r - y
end
if y > HEIGHT - r then
dy = - dy
y = 2 * (HEIGHT - r) - y
end
if x > barrier.x-barrierWidth/2 and x < barrier.x+barrierWidth/2 and y > barrier.y-barrierHeight/2-r/2 and y< barrier.y+barrierHeight/2+r/2 then
dy = -dy
end
if x > barrier.x-barrierWidth/2-r/2 and x < barrier.x+barrierWidth/2+r/2 and y > barrier.y-barrierHeight/2 and y< barrier.y+barrierHeight/2 then
dx = -dx
end
end
if x < holePos.x+15 and x> holePos.x-15 and y < holePos.y+15 and y>holePos.y-15 then
scored = true
end
else
fontSize(50)
fill(255,255,255,255)
text("Nice putt!",WIDTH/2,HEIGHT-200)
fontSize(35)
text("Double tap to play again",WIDTH/2,HEIGHT-300)
fontSize(25)
text("Strokes: "..strokes,WIDTH/2,HEIGHT-400)
end
end
function touched(touch)
tx = touch.x
ty = touch.y
if touch.state == BEGAN then
touchBegan = true
velocity = {}
dx, dy = 0, 0
end
if touch.state == BEGAN and scored == false then
strokes = strokes + 1
end
if touch.state == MOVING then
local newVelocity = vec2(touch.deltaX, touch.deltaY)
table.insert(velocity, 1, newVelocity)
end
if touch.state == ENDED then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
dx = dx + velocity[i].x
dy = dy + velocity[i].y
end
end
if n > 0 then
dx, dy = dx / n, dy / n
end
touchBegan = false
end
if scored == true and touch.tapCount == 2 then
scored = false
holePos = vec2(math.random(r*1.5,WIDTH-r*1.5),math.random(r*1.5,HEIGHT-r*1.5))
if holePos.y > HEIGHT/2 then
y = 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(HEIGHT/2+barrierHeight,HEIGHT-barrierHeight))
else y = HEIGHT- 100
barrier = vec2(math.random(barrierWidth,WIDTH-barrierWidth),math.random(barrierHeight,HEIGHT/2-barrierHeight))
end
strokes = 0
end
end
Note: A lot of this game was based off the “handling touches” tutorial in the wiki.