Skip to content

Instantly share code, notes, and snippets.

@dermotbalson dermotbalson/gist:5466313
Last active Dec 16, 2015

Embed
What would you like to do?
bullets 4
function setup()
physics.gravity(0,0)
bullets={}
ball = {} --NEW
for i = 1,5 do --NEW
ball[i] = Ballons()
end
end
function touched(touch)
x = touch.x
y = touch.y
if touch.state == BEGAN then --create bullet
local bullet = physics.body(CIRCLE,10)
bullet.x,bullet.y = x,y
bullet.linearVelocity = vec2(1000,0)
bullet.restitution = 0
bullet.type = DYNAMIC
table.insert(bullets,bullet)
end
end
function draw()
background(255)
pushStyle()
fill(255, 255, 255, 255)
if collision~=nil then -----------NEW
ball[collision[1]].ballon:destroy()
table.remove(ball,collision[1])
bullets[collision[2]]:destroy()
table.remove(bullets,collision[2])
collision=nil
end
fill(0)
for i,b in pairs(bullets) do
if b.x<1 or b.x>WIDTH or b.y<1 or b.y>HEIGHT then
b:destroy()
table.remove(bullets,i)
else
ellipse(b.x,b.y,10)
end
end
popStyle()
for i = 1,#ball do --drawing the ballons
pushStyle()
fill(ball[i].ballon.colr)
ellipse(ball[i].ballon.x, ball[i].ballon.y, ball[i].radius*2)
popStyle()
end
pushStyle()
popStyle()
end
function collide(contact) --NEW
for i,b in pairs(ball) do
if b.ballon==contact.bodyA or b.ballon==contact.bodyB then
for j,bb in pairs(bullets) do
if bb==contact.bodyA or bb==contact.bodyB then
sound(DATA, "ZgNAXS4wQEBAQEBA/9iePiN3mD64nKs+cgBAf0BAQEBAb0BA ")
collision={i,j}
end
end
end
end
end
Ballons = class() --NEW
function Ballons:init(x,y,r,c,lv)
self.radius = r or math.random(25,50)
self.ballon = physics.body(CIRCLE,self.radius)
self.ballon.x = x or math.random(WIDTH/2,WIDTH)
self.ballon.y = y or math.random(-HEIGHT/2,-self.radius)
self.ballon.linearVelocity = lv or vec2(0,math.random(100,150))
self.ballon.colr = c or color(math.random(255),math.random(255),math.random(255),math.random(100,150))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.