Created
July 13, 2013 08:05
-
-
Save dermotbalson/5989931 to your computer and use it in GitHub Desktop.
pool2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
--Main | |
function setup() | |
V={P1,P2,P3,P4,P5,P6,P7,P8,P9,P10} | |
parameter.integer('Version',1,10,2,function() setVersion() end) | |
end | |
function setVersion() | |
ver=V[Version] | |
ver.setup() | |
end | |
function draw() | |
ver.draw() | |
end | |
function touched(touch) | |
ver.touched(touch) | |
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 | |
--# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment