particle.lua
Code: Select all
local particle = {}
particle.__index = particle
function particle.new(x, y)
local self = setmetatable({
x = x,
y = y,
vx = math.random(-1, 1),
vy = math.random(-5, -1),
alpha = 255,
}, particle)
return self
end
function particle.update(self)
self.x = self.x + self.vx
self.y = self.y + self.vy
self.alpha = self.alpha - 10
end
function particle.draw(self)
love.graphics.setColor(255, 40, 40, self.alpha)
love.graphics.circle("fill", self.x, self.y, 10)
end
return particle
Code: Select all
local particle = require("particle")
local particles = {}
function love.update(dt)
table.insert(particles, particle.new(love.mouse.getPosition()))
for i, v in ipairs(particles) do
v:update()
if v.alpha < 0 then
table.remove(particles, i)
end
end
end
function love.draw()
for i, v in ipairs(particles) do
v:draw()
end
end