This is the first project I have worked on in Codea. It’s a simple project that shows balls and stuff. :>
--# Main
-- Ball Test 3 (complete)
function setup()
parameter.boolean("add", false) -- For adding balls
parameter.boolean("stat",true) -- Makes a newly made ball move
parameter.boolean("pchange",true) -- Set to false if you don't want to change the ball you want to move
parameter.boolean("psize",false) -- Set to true if you want to change the size your ball
parameter.integer("pspeed",0,10,1) -- The amount of force exerted on your ball is multiplied by this value + 1
parameter.integer("size",1,100,30) -- Sets the size of a newly made ball or your ball
parameter.action("Stop Player Movement", function() -- Stops the movement of your ball
for k,v in pairs(b) do
if v.player then
v.linearVelocity = vec2(0,0)
end
end
end)
parameter.action("Stop all ball movement", function() -- Stops the movement of all balls
for k,v in pairs(b) do
v.linearVelocity = vec2(0,0)
end
end)
b = {} -- Store all the bodies here
-- Walls
local floor = physics.body( EDGE, vec2(0, 0), vec2(WIDTH, 0))
floor.restitution = 1.1
local left = physics.body( EDGE, vec2(0, 0), vec2(0, HEIGHT))
left.restitution = 1.1
local right = physics.body( EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
right.restitution = 1.1
local ceiling = physics.body( EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
ceiling.restitution = 1.1
physics.gravity(0,0) -- Tabletop physics
physics.continuous = true -- Balls don't pass through walls if they get too fast
table.insert(b,floor)
table.insert(b,left)
table.insert(b,right)
table.insert(b,ceiling)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(2)
-- Do your drawing here
for k,v in pairs(b) do -- Draws the balls
drawBodies(v)
if v.player and psize then -- Changes the size of your ball if psize
v.radius = size
end
end
if explode then -- Shows the last contact / Nice effects
sprite("Space Art:Red Explosion", exp.x, exp.y, exps, exps) -- exps is the energy of the impact
end
end
function touched(touch)
t = vec2(touch.x, touch.y)
if touch.state == BEGAN then
if add then -- Makes a ball if add
makeCirc(t.x,t.y)
else
for k,v in pairs(b) do
if v.shapeType == CIRCLE and not pcheck(v) and v.player then
v:applyForce((t-v.position)*(pspeed+1)) -- Applies force
end
end
end
end
end
function collide(contact)
if contact.state == BEGAN then
exp = contact.position
exps = contact.normalImpulse
explode = true
tween.delay(0.1, function()
explode = false
end)
sound(DATA, "ZgNAFQBJQEBAQEBAAAAAAAAAAAC5ZIg+AABAf0BAQEBAQHJx")
end
end
function makeCirc(x,y)
local a = physics.body(CIRCLE,size)
a.x, a.y = x, y
a.restitution = 1
a.interpolate = true
a.player = false
if not stat then
a:applyForce(vec2(math.random(-1000,1000),math.random(-1000,1000)))
end
table.insert(b, a)
end
function drawBodies(obj)
pushStyle()
fill(255, 0, 0, 0)
stroke(25, 255, 0, 165)
if obj.shapeType == EDGE then
line(obj.points[1][1],obj.points[1][2],
obj.points[2][1],obj.points[2][2])
elseif obj.shapeType == CIRCLE and obj.player then
pushStyle()
noStroke()
fill(0, 255, 180, 255)
ellipse(obj.x,obj.y,obj.radius*2)
popStyle()
elseif obj.shapeType == CIRCLE then
ellipse(obj.x,obj.y,obj.radius*2)
end
popStyle()
end
function pcheck(obj) -- Checks if a tap is inside a ball
if pchange and t:dist(obj.position)<=obj.radius then
for k,v in pairs(b) do
v.player = false
end
obj.player = true
return true
else
return false
end
end