Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created April 19, 2013 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/5419806 to your computer and use it in GitHub Desktop.
Save dermotbalson/5419806 to your computer and use it in GitHub Desktop.
shoot1
-- Main
function setup()
numtouches=0 --number of current touches
aim={id=0,x=0,y=0} --details for touch which is aiming
createScope()
shots={} --table for shots fired
shotSize=7 --size of shot mark
shotColour=color(255)
print("Hold and move one finger on the screen to aim")
print("Tap with a second finger to shoot")
end
function createScope()
scopeBuffer=70 --draw scope this many pixels above finger (so user can see scope!)
scopeOuterRing=50 --pixels
scopeInnerRing=20
crossHairExtends=10 --crosshairs extend this far outside rings
local s=scopeOuterRing+crossHairExtends*2 --total width
--since we're drawing the same scope a lot, we create an image
imgScope=image(s,s)
setContext(imgScope) --start drawing on the image instead of the screen
ellipseMode(CENTER)
strokeWidth(.5) --lines one pixel wide
stroke(94, 89, 89, 255) --dark colour
fill(0,0,0,0) --transparent fill (the last item, alpha, is nil)
ellipse(s/2,s/2,scopeOuterRing) --draw rings
ellipse(s/2,s/2,scopeInnerRing)
line(s/2,0,s/2,s) --draw crosshairs
line(0,s/2,s,s/2)
setContext() --stop drawing on image
end
function touched(touch)
if touch.state==BEGAN then --user has just touched, hasn't let go yet
numtouches=numtouches+1 --keep count
if numtouches==1 then --if first touch, it is aiming, store details
aim.id=touch.id --so we know which touch is which when we shoot
aim.x=touch.x
aim.y=touch.y
end
elseif touch.state==MOVING then --finger is moving
if touch.id==aim.id then --if the aiming finger, store new x,y
aim.x=touch.x
aim.y=touch.y
end
elseif touch.state==ENDED then
if touch.id==aim.id then aim.id=0 --if aiming finger lifted, remove scope
elseif numtouches>1 then --if more than one finger, bang bang!
shots[#shots+1]={x=aim.x,y=aim.y+scopeBuffer} --add shot to table
end
numtouches=numtouches-1 --adjust touch count
end
end
function draw()
background(195, 211, 215, 255)
popStyle()
--draw target
stroke(82, 181, 199, 255) --outer circle colour
strokeWidth(25)
fill(120, 141, 186, 255) --inner circle colour
ellipse(WIDTH/2,HEIGHT/2,100)
if aim.id>0 then --if aiming, draw scope
sprite(imgScope,aim.x,aim.y+scopeBuffer)
end
--draw past shots on screen
for i,v in pairs(shots) do
fill(shotColour)
strokeWidth(0)
ellipse(v.x,v.y,shotSize)
end
pushStyle()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment