# Game involving swiping gestures

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.

Nice game =D>

:-bd

This might seem weird on my own thread, but I was trying to change something in the code and I encountered a problem. When I try to draw a line, nothing shows up. Can anyone tell me what’s wrong? I tried to use

``````line(100,100,400,100)
``````

And put it under

``````rect(barrier.x,barrier.y,barrierWidth,barrierHeight)
``````

, but nothing happens.

@Staples “line” requires stroke(some value) and strokeWidth(some value). That information can be found in the build in reference.

@Staples Sometimes the hole is under the rectangle. Need to add some code to prevent that.

@dave1707 yeah , I tried to fix that. I thought it worked, but I guess it didn’t!

@dave1707 thanks, I think I fixed the code above. Here’s another version as well

``````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
pushStyle()
stroke(11, 0, 255, 255)
strokeWidth(4)
line(x,y,tx,ty)
popStyle()
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
distanceX =  (tx-x)/10
distanceY = (ty-y)/10
print(distanceX)
print(distanceY)
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
dx = dx + distanceX
dy = dy + distanceY
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
``````

@Staples The game is too easy now. I can always get a hole in one by having the line cross the hole.

@Staples You need to add “supportedOrientation(LANDSCAPE)”. If you try to run your code in PORTRAIT, there are errors on line 19 or 21.

@dave1707 oh ok thanks for the help

I have a suggestion, to make it harder, have the player drag the line backwards and have the ball “slingshot” in the opposite direction by replacing lines 129 and 130 with this:

``````dx = dx - distanceX
dy = dy - distanceY
``````

@Doge nice suggestion. I was going to limit the distance you could drag the line, but that sounds a lot cooler! Thanks

The user and all related content has been deleted.