Skip to content

Instantly share code, notes, and snippets.

@sean-m
Last active April 3, 2020 00:14
Show Gist options
  • Save sean-m/3839c21c3638a1375117864e79daf38a to your computer and use it in GitHub Desktop.
Save sean-m/3839c21c3638a1375117864e79daf38a to your computer and use it in GitHub Desktop.
dinking around with lua
function lerp(a,b,t) return (1-t)*a + t*b end
function new_star()
local rx = love.math.random() * 20
local ry = love.math.random() * 12
if (love.math.random() < 0.5) then
rx = rx * -1.0
end
if (love.math.random() < 0.52) then
ry = ry * -1.0
end
local s = {x=(ox + rx), y=(oy + ry), dx=0.0, dy=0.0}
s.dx = lerp(0.0, rx, 1.0)
s.dy = lerp(0.0, ry, 1.0)
--print (s.x .. " " .. s.y .. " " .. s.dx .. " " .. s.dy)
return s
end
function love.load()
ox, oy = love.graphics.getWidth()/2, love.graphics.getHeight()/2
star_count = 250
stars = {}
for i=1,star_count do
star = new_star()
stars[i] = star
end
local width = love.graphics.getWidth();
local height = love.graphics.getHeight();
-- Simulate a bunch to make it look more interesting at the start
for p=1, 1500 do
for i=1,star_count do
local s = stars[i]
s.x = lerp(s.x, s.x + s.dx, 0.05)
s.y = lerp(s.y, s.y + s.dy, 0.05)
stars[i] = s
--print (stars[i].x .. " " .. stars[i].y .. " " .. stars[i].dx .. " " .. stars[i].dy)
end
for i=1,star_count do
if stars[i].x > width or stars[i].x < 0 then
stars[i] = new_star()
elseif stars[i].y > height or stars[i].y < 0 then
stars[i] = new_star()
end
end
end
end
function love.draw()
love.graphics.setColor(1, 1, 1)
for i=1,star_count do
love.graphics.rectangle("fill", stars[i].x, stars[i].y, 2, 2)
end
end
function love.update(dt)
local width = love.graphics.getWidth();
local height = love.graphics.getHeight();
for i=1,star_count do
local s = stars[i]
s.x = lerp(s.x, s.x + s.dx, 0.05)
s.y = lerp(s.y, s.y + s.dy, 0.05)
stars[i] = s
--print (stars[i].x .. " " .. stars[i].y .. " " .. stars[i].dx .. " " .. stars[i].dy)
end
for i=1,star_count do
if stars[i].x > width or stars[i].x < 0 then
stars[i] = new_star()
elseif stars[i].y > height or stars[i].y < 0 then
stars[i] = new_star()
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment