Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 19, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dermotbalson/5998716 to your computer and use it in GitHub Desktop.
Save dermotbalson/5998716 to your computer and use it in GitHub Desktop.
pool4
--# Main
--Main
function setup()
supportedOrientations(LANDSCAPE_RIGHT)
ver=P4
ver.setup()
end
function draw()
ver.draw()
end
function touched(touch)
ver.touched(touch)
end
function collide(contact)
ver.collide(contact)
end
--# P1
-- Pool1
P1={}
function P1.setup()
P1.CreateEdges()
P1.CreateBalls()
end
function P1.CreateEdges()
sides={}
sides[1]=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
sides[2]=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
sides[3]=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
sides[4]=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
for i=1,4 do
sides[i].restitution=.8
end
end
function P1.CreateBalls()
local d,f=40,14
physics.gravity(0,0)
pushStyle()
fontSize(f)
font('GillSans-Bold')
local ballCols={color(255,255,0),color(0,0,255),color(255,0,0),color(146, 21, 226, 255),
color(223, 99, 37, 255),color(0,255,0),color(95, 70, 49, 255),color(0)}
balls={}
for i=0,15 do
balls[i]={}
local p=physics.body(CIRCLE,d/2)
p.x,p.y=math.random(1,600),math.random(1,600)
p.linearVelocity=vec2(200,250)
p.linearDamping=0.2
p.angularDamping=.2
p.restitution=.8
balls[i].body=p
local img=image(d,d)
setContext(img)
stroke(130, 125, 125, 255)
strokeWidth(1)
if i==0 then
fill(255)
ellipse(d/2,d/2,d)
elseif i<=8 then
fill(ballCols[i])
ellipse(d/2,d/2,d)
else
fill(227, 227, 221, 255)
ellipse(d/2,d/2,d)
fill(ballCols[i-8])
local a=math.rad(30)
local x,y=d/2*math.cos(a)-1,d/2*math.sin(a)-1
rect(d/2-x,d/2-y,x*2,y*2)
end
if i>0 then
strokeWidth(0)
fill(255)
ellipse(d/2,d/2,d/2)
fill(0)
text(i,d/2,d/2)
end
setContext()
balls[i].img=img
end
popStyle()
end
function P1.draw()
background(111, 181, 143, 255)
for i,p in pairs(balls) do
pushMatrix()
translate(p.body.x,p.body.y)
rotate(-p.body.angle)
sprite(p.img,0,0)
popMatrix()
end
end
function P1.touched(touch)
end
function P1.collide(contact)
end
--# P2
--Pool2
P2={}
function P2.setup()
displayMode(FULLSCREEN)
P2.CreateEdges()
P2.CreateBalls()
aim=vec2(0,0)
end
function P2.CreateEdges()
sides={}
tableBorder=5
table=vec4(tableBorder,tableBorder,WIDTH-tableBorder,(WIDTH-tableBorder)/2)
sides[1]=physics.body(EDGE,vec2(table.x,table.y),vec2(table.z,table.y))
sides[2]=physics.body(EDGE,vec2(table.x,table.y),vec2(table.x,table.w))
sides[3]=physics.body(EDGE,vec2(table.x,table.w),vec2(table.z,table.w))
sides[4]=physics.body(EDGE,vec2(table.z,table.y),vec2(table.z,table.w))
for i=1,4 do
sides[i].restitution=.8
end
end
function P2.CreateBalls()
local d,f=40,14
physics.gravity(0,0)
pushStyle()
fontSize(f)
font('GillSans-Bold')
local ballCols={color(255,255,0),color(0,0,255),color(255,0,0),color(146, 21, 226, 255),
color(223, 99, 37, 255),color(0,255,0),color(95, 70, 49, 255),color(0)}
balls={}
for i=0,15 do
balls[i]={}
local p=physics.body(CIRCLE,d/2)
p.x,p.y=math.random(table.x+d/2,table.z-d/2),math.random(table.y+d/2,table.w-d/2)
p.linearVelocity=vec2(200,250)
p.friction=5
p.linearDamping=0.2
p.angularDamping=.2
p.restitution=.8
balls[i].body=p
local img=image(d,d)
setContext(img)
stroke(130, 125, 125, 255)
strokeWidth(1)
if i==0 then
fill(255)
ellipse(d/2,d/2,d)
elseif i<=8 then
fill(ballCols[i])
ellipse(d/2,d/2,d)
else
fill(227, 227, 221, 255)
ellipse(d/2,d/2,d)
fill(ballCols[i-8])
local a=math.rad(30)
local x,y=d/2*math.cos(a)-1,d/2*math.sin(a)-1
rect(d/2-x,d/2-y,x*2,y*2)
end
if i>0 then
strokeWidth(0)
fill(255)
ellipse(d/2,d/2,d/2)
fill(0)
text(i,d/2,d/2)
end
setContext()
balls[i].img=img
end
popStyle()
end
function P2.draw()
background(111, 181, 143, 255)
for i,p in pairs(balls) do
pushMatrix()
translate(p.body.x,p.body.y)
rotate(-p.body.angle)
sprite(p.img,0,0)
popMatrix()
stroke(50)
strokeWidth(10)
line(table.x-tableBorder,table.y-tableBorder,table.z,table.y-tableBorder)
line(table.x-tableBorder,table.y-tableBorder,table.x-5,table.w)
line(table.x-tableBorder,table.w,table.z,table.w)
line(table.z,table.y-tableBorder,table.z,table.w)
strokeWidth(1)
local b=balls[0].body
aim.x,aim.y=aim.x*.9+Gravity.x*.1,aim.y*.9+.1*Gravity.y
line(b.x, b.y, b.x-aim.x * 300000, b.y-aim.y * 300000)
end
end
function P2.touched(touch)
if touch.state==ENDED then
local b=balls[0].body
b:applyForce(-10000*vec2(aim.x,aim.y))
end
end
function P2.collide(contact)
end
--# P3
--Pool3
P3={}
function P3.setup()
displayMode(FULLSCREEN)
P3.CreateEdges()
P3.CreateSnookerBalls()
P3:CreateControls()
aim=vec2(0,0)
end
function P3.CreateEdges()
sides={}
tableBorder=5
tableWidth=WIDTH-tableBorder*2
table=vec4(tableBorder,tableBorder,WIDTH-tableBorder*2,(WIDTH-tableBorder*2)/2)
sides[1]=physics.body(EDGE,vec2(table.x,table.y),vec2(table.z,table.y))
sides[2]=physics.body(EDGE,vec2(table.x,table.y),vec2(table.x,table.w))
sides[3]=physics.body(EDGE,vec2(table.x,table.w),vec2(table.z,table.w))
sides[4]=physics.body(EDGE,vec2(table.z,table.y),vec2(table.z,table.w))
for i=1,4 do
sides[i].restitution=.8
end
end
function P3.CreateSnookerBalls()
local s=0.03 --diam of ball as fraction of tablewidth
local d=s*tableWidth --diam of balls
physics.gravity(0,0)
pushStyle()
local p=.3 --pink pos
local bx,by,pp=.9*s,s*2,p+s
local ballpos={
vec2(pp-by,.5), --rows of red balls
vec2(pp-by-bx,.5-by/2),vec2(pp-by-bx,.5+by/2),
vec2(pp-by-bx*2,.5),vec2(pp-by-bx*2,.5+by),vec2(pp-by-bx*2,.5-by),
vec2(pp-by-bx*3,.5-by/2),vec2(pp-by-bx*3,.5-by*3/2),vec2(pp-by-bx*3,.5+by/2),vec2(pp-by-bx*3,.5+by*3/2),
vec2(pp-by-bx*4,.5),vec2(pp-by-bx*4,.5-by),vec2(pp-by-bx*4,.5-by*2),vec2(pp-by-bx*4,.5+s),
vec2(pp-by-bx*4,.5+by*2),
vec2(.8,.667), --yellow
vec2(.8,.333), --green
vec2(.8,.5), --brown
vec2(.5,.5), --blue
vec2(p,.5), --pink
vec2(.09,.5) --black
}
ballpos[0]=vec2(.8,.4)--white
local ballcol={}
ballcol[0]=color(235) -- white
for i=1,15 do -- red
ballcol[i]=color(255,0,0)
end
ballcol[16]=color(255,255,0)--yellow
ballcol[17]=color(0,255,0) -- green
ballcol[18]=color(169, 127, 67, 255) --brown
ballcol[19]=color(0,0,255) -- blue
ballcol[20]=color(222, 107, 217) --pink
ballcol[21]=color(0) --black
balls={}
for i=0,21 do
balls[i]={}
local p=physics.body(CIRCLE,d/2)
p.x,p.y=ballpos[i].x*tableWidth,ballpos[i].y*tableWidth/2
p.linearVelocity=vec2(0,0)
p.friction=5
p.linearDamping=0.3
p.angularDamping=.3
p.restitution=.8
p.sleepingAllowed=false
p.bullet=true
p.info="b"
balls[i].body=p
balls[i].img=P3.drawBall(d,ballcol[i])
end
popStyle()
end
function P3.drawBall(s,c)
local r,g,b=c.r,c.g,c.b
local img=image(s,s)
setContext(img)
fill(r,g,b)
ellipse(s/2,s/2,s)
local t=s*.8
for i=t,2,-2 do
local z=(t-i)/(t-2)
local r1,g1,b1=r+(255-r)*z,g+(255-g)*z,b+(255-b)*z
fill(r1,g1,b1)
ellipse(s/2+2+(t-i)/(t-2)*2,s/2,i)
end
setContext()
return img
end
function P3:CreateControls()
--create power slider image
power={}
power.w,power.h=400,50
power.x,power.y=50,600
imgPower=image(power.w,power.h)
setContext(imgPower)
pushStyle()
stroke(255,0,0)
strokeWidth(power.h)
line(0,-power.h/2,power.w,power.h-power.h/2)
popStyle()
setContext()
end
function P3.draw()
background(111, 181, 143, 255)
pushStyle()
--table border
stroke(122, 88, 66, 255)
strokeWidth(10)
line(table.x-tableBorder,table.y-tableBorder,table.z+tableBorder,table.y-tableBorder)
line(table.x-tableBorder,table.y-tableBorder,table.x-5,table.w+tableBorder)
line(table.x-tableBorder,table.w+tableBorder,table.z+tableBorder,table.w+tableBorder)
line(table.z+tableBorder,table.y-tableBorder,table.z+tableBorder,table.w+tableBorder)
--draw balls
for i,p in pairs(balls) do
sprite(p.img,p.body.x,p.body.y)
end
--if white ball is nearly stationary, we can shoot
local ss=balls[0].body.linearVelocity.x*balls[0].body.linearVelocity.x+
balls[0].body.linearVelocity.y*balls[0].body.linearVelocity.y
shoot = (ss<10)
if shoot then --draw shooting controls
strokeWidth(1)
local b=balls[0].body
aim.x,aim.y=aim.x*.9+Gravity.x*.1,aim.y*.9+.1*Gravity.y
line(b.x, b.y, b.x-aim.x * 300000, b.y-aim.y * 300000)
--power control
spriteMode(CORNER)
sprite(imgPower,power.x,power.y)
end
strokeWidth(1)
fill(255)
textMode(CORNER)
text("Turn the iPad, using the line to aim",650,700)
text("Press on the red power bar at left, to shoot",650,670)
popStyle()
end
function P3.touched(touch)
if touch.state==ENDED then
--make the shot if we touched inside the power control and we are allowed to shoot
if touch.x>power.x and touch.x<power.x+power.w and touch.y>power.y and touch.y<=power.y+power.h
and shoot then
local f=(touch.x-power.x)/power.w --get power selection
local b=balls[0].body
--apply force based on the aim variable
--but standardise it so it doesnt depend on the vertical tilt
local v=(aim.x^2+aim.y^2)^.5
b:applyForce(-f*2000/v*vec2(aim.x,aim.y))
end
end
end
function P3.collide(contact)
if contact.state==BEGAN and ElapsedTime>1 then
if contact.bodyA.info==contact.bodyB.info then
sound(SOUND_HIT, 25505)
else
sound(SOUND_RANDOM, 14664)
end
end
end
--# P4
--Pool4
P4={}
function P4.setup()
displayMode(FULLSCREEN)
P4.CreateEdges()
P4.CreateSnookerBalls()
P4:CreateControls()
aim=vec2(0,0)
end
function P4.CreateEdges()
sides={}
size=0.03 --size of ball relative to width
tableBorder=5
tableWidth=WIDTH-tableBorder*2
tableHeight=tableWidth/2
hole=size*1.75*tableWidth
corner=hole/2^.5
table=vec4(tableBorder,tableBorder,WIDTH-tableBorder*2,(WIDTH-tableBorder*2)/2)
sides[1]=physics.body(EDGE,vec2(table.x+corner,table.y),vec2(table.x+tableWidth/2-hole/2,table.y))
sides[2]=physics.body(EDGE,vec2(table.x+tableWidth/2+hole/2,table.y),vec2(table.z-corner,table.y))
sides[3]=physics.body(EDGE,vec2(table.x,table.y+corner),vec2(table.x,table.w-corner))
sides[4]=physics.body(EDGE,vec2(table.x+corner,table.w),vec2(table.x+tableWidth/2-hole/2,table.w))
sides[5]=physics.body(EDGE,vec2(table.x+tableWidth/2+hole/2,table.w),vec2(table.z-corner,table.w))
sides[6]=physics.body(EDGE,vec2(table.z,table.y+corner),vec2(table.z,table.w-corner))
for i=1,#sides do
sides[i].restitution=.8
end
end
function P4.CreateSnookerBalls()
local d=size*tableWidth --diam of balls
physics.gravity(0,0)
pushStyle()
local p=.3 --pink pos
local bx,by,pp=.9*size,size*2,p+size
local ballpos={
vec2(pp-by,.5), --rows of red balls
vec2(pp-by-bx,.5-by/2),vec2(pp-by-bx,.5+by/2),
vec2(pp-by-bx*2,.5),vec2(pp-by-bx*2,.5+by),vec2(pp-by-bx*2,.5-by),
vec2(pp-by-bx*3,.5-by/2),vec2(pp-by-bx*3,.5-by*3/2),vec2(pp-by-bx*3,.5+by/2),vec2(pp-by-bx*3,.5+by*3/2),
vec2(pp-by-bx*4,.5),vec2(pp-by-bx*4,.5-by),vec2(pp-by-bx*4,.5-by*2),vec2(pp-by-bx*4,.5+by),
vec2(pp-by-bx*4,.5+by*2),
vec2(.8,.667), --yellow
vec2(.8,.333), --green
vec2(.8,.5), --brown
vec2(.5,.5), --blue
vec2(p,.5), --pink
vec2(.09,.5) --black
}
ballpos[0]=vec2(.8,.4)--white
local ballcol={}
ballcol[0]=color(235) -- white
for i=1,15 do -- red
ballcol[i]=color(255,0,0)
end
ballcol[16]=color(255,255,0)--yellow
ballcol[17]=color(0,255,0) -- green
ballcol[18]=color(169, 127, 67, 255) --brown
ballcol[19]=color(0,0,255) -- blue
ballcol[20]=color(222, 107, 217) --pink
ballcol[21]=color(0) --black
balls={}
for i=0,21 do
balls[i]={}
local p=physics.body(CIRCLE,d/2)
p.x,p.y=ballpos[i].x*tableWidth,ballpos[i].y*tableWidth/2
p.linearVelocity=vec2(0,0)
p.friction=5
p.linearDamping=0.3
p.angularDamping=.3
p.restitution=.8
p.sleepingAllowed=false
p.bullet=true
p.info="b"
balls[i].body=p
balls[i].img=P4.drawBall(d,ballcol[i])
end
popStyle()
end
function P4.drawBall(s,c)
local r,g,b=c.r,c.g,c.b
local img=image(s,s)
setContext(img)
fill(r,g,b)
ellipse(s/2,s/2,s)
local t=s*.8
for i=t,2,-2 do
local z=(t-i)/(t-2)
local r1,g1,b1=r+(255-r)*z,g+(255-g)*z,b+(255-b)*z
fill(r1,g1,b1)
ellipse(s/2+2+(t-i)/(t-2)*2,s/2,i)
end
setContext()
return img
end
function P4:CreateControls()
--create power slider image
power={}
power.w,power.h=400,50
power.x,power.y=50,600
imgPower=image(power.w,power.h)
setContext(imgPower)
pushStyle()
stroke(255,0,0)
strokeWidth(power.h)
line(0,-power.h/2,power.w,power.h-power.h/2)
popStyle()
setContext()
end
function P4.draw()
background(111, 181, 143, 255)
pushStyle()
--table border
stroke(122, 88, 66, 255)
strokeWidth(10)
line(table.x-tableBorder+corner,table.y-tableBorder,table.x+tableWidth/2-hole/2,table.y-tableBorder)
line(table.x+tableWidth/2+hole/2,table.y-tableBorder,table.z+tableBorder-corner,table.y-tableBorder)
line(table.x-tableBorder,table.y-tableBorder+corner,table.x-tableBorder,table.w+tableBorder-corner)
line(table.x-tableBorder+corner,table.w+tableBorder,table.x+tableWidth/2-hole/2,table.w+tableBorder)
line(table.x+tableWidth/2+hole/2,table.w+tableBorder,table.z+tableBorder-corner,table.w+tableBorder)
line(table.z+tableBorder,table.y-tableBorder+corner,table.z+tableBorder,table.w+tableBorder-corner)
--draw balls
for i,p in pairs(balls) do
sprite(p.img,p.body.x,p.body.y)
end
--if white ball is nearly stationary, we can shoot
local ss=balls[0].body.linearVelocity.x*balls[0].body.linearVelocity.x+
balls[0].body.linearVelocity.y*balls[0].body.linearVelocity.y
shoot = (ss<10)
if shoot then --draw shooting controls
strokeWidth(1)
local b=balls[0].body
aim.x,aim.y=aim.x*.9+Gravity.x*.1,aim.y*.9+.1*Gravity.y
line(b.x, b.y, b.x-aim.x * 300000, b.y-aim.y * 300000)
--power control
spriteMode(CORNER)
sprite(imgPower,power.x,power.y)
end
strokeWidth(1)
fill(255)
textMode(CORNER)
text("Turn the iPad, using the line to aim",650,700)
text("Press on the red power bar at left, to shoot",650,670)
popStyle()
end
function P4.touched(touch)
if touch.state==ENDED then
--make the shot if we touched inside the power control and we are allowed to shoot
if touch.x>power.x and touch.x<power.x+power.w and touch.y>power.y and touch.y<=power.y+power.h
and shoot then
local f=(touch.x-power.x)/power.w --get power selection
local b=balls[0].body
--apply force based on the aim variable
--but standardise it so it doesnt depend on the vertical tilt
local v=(aim.x^2+aim.y^2)^.5
b:applyForce(-f*2000/v*vec2(aim.x,aim.y))
end
end
end
function P4.collide(contact)
if contact.state==BEGAN and ElapsedTime>1 then
if contact.bodyA.info==contact.bodyB.info then
sound(SOUND_HIT, 25505)
else
sound(SOUND_RANDOM, 14664)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment