Paste this into a new project and play around with it.
Code: Select all
local pi = math.pi
local gr = love.graphics
local _sq = math.sqrt
math.randomseed(os.time())
function love.load()
gr.setFont(love._vera_ttf, 14)
gr.setBackgroundColor(0,0,0)
--Set up Missile and Bomb tables
missiles = {}
bombs = {}
--Add 10 random Bombs
for i=0,10,1 do
table.insert(bombs, {x = math.random(20,620), y = -10})
end
end
function love.update(dt)
--Move the Bombs (Enemy missiles)
for j, b in pairs(bombs) do
b.y = b.y + 10 * dt
b.x = b.x + 5 * dt
end
--Update the player missiles
for i, m in pairs(missiles) do
--Make them bigger and kill them when done
m.life = m.life - 100 * dt
if m.life <= 0 then
table.remove(missiles, i)
end
--Check for collision with enemy bombs
for j, b in pairs(bombs) do
if distanceFrom(b.x, b.y, m.x, m.y) < (m.start-m.life) then
--Set off another explosion to chain to the closer bombs
createMissile(b.x, b.y, 40)
table.remove(bombs, j)
end
end
end
end
function love.draw()
local fps = love.timer.getFPS()
--Draw the Enemy Bombs
for j, b in pairs(bombs) do
gr.setColor(255,0,0,255)
gr.circle("fill", b.x, b.y, 5, 32)
end
--Draw the Player Missiles
for i, m in pairs(missiles) do
gr.setColor(255,255,255,220)
gr.circle("fill", m.x, m.y, m.start-m.life, 32)
end
gr.setColor(255,255,255)
gr.print("FPS: " .. fps, 10, 14)
end
function love.mousepressed(x, y, button)
if button == "l" then
createMissile(x,y,50)
end
end
--Create a missile wherever you want
function createMissile(x,y,l)
table.insert(missiles, { x = x, y = y, life = l, start = l })
end
--Returns the distance between two points
function distanceFrom(x1,y1,x2,y2) return _sq((x2 - x1) ^ 2 + (y2 - y1) ^ 2) end
You could take it further and make each enemy bomb have a target that it flies towards like the real game does.
It even has chains where detonated bombs will detonate other bombs close by just like the real game.