Skip to content

Instantly share code, notes, and snippets.

@Fortyseven
Last active January 22, 2019 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fortyseven/a4e2d5d3105ed86e0916d162a1a4d466 to your computer and use it in GitHub Desktop.
Save Fortyseven/a4e2d5d3105ed86e0916d162a1a4d466 to your computer and use it in GitHub Desktop.
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
-- lines (classic)
-- by fortyseven
SPACING = 4
SPEED = 1
function buildPolyVerts()
local verts = {
x=rnd(128),
y=rnd(128),
xvel= SPEED,
yvel= SPEED
};
if (rnd(1) > 0.5) verts.xvel = -verts.xvel
if (rnd(1) > 0.5) verts.yvel = -verts.yvel
return verts;
end;
-------------------------------------------------------------
poly = {}
function poly:init(i, color)
self.color = color
self.verts = {}
-- ensure we use the same seed for each poly object, so it will always
-- create the same intial x/y/velocity directions
for i = 1, 4 do
verts = buildPolyVerts()
add(self.verts,verts)
end
-- run the update a couple times to space them out
for i = 1, i*SPACING do self:update() end
end
function poly:update()
foreach(self.verts, function(vert)
vert.x += vert.xvel
if ((vert.x >= 128) or (vert.x <= 0)) vert.xvel = -vert.xvel
vert.y += vert.yvel
if ((vert.y >= 128) or (vert.y <= 0)) vert.yvel = -vert.yvel
end)
end
function poly:draw()
for i = 1, 3 do
color(self.color);
line(self.verts[i].x, self.verts[i].y, self.verts[i+1].x, self.verts[i+1].y)
end
line(self.verts[4].x, self.verts[4].y, self.verts[1].x, self.verts[1].y)
end
function poly:new(o)
self.__index = self
return setmetatable(o or {}, self)
end
-------------------------------------------------------------
polyCluster = {}
function polyCluster:init(col_array)
self.objects = {}
seed = flr(rnd(1000))
-- generate three unique polys
for i = 1, 3 do
srand(seed)
local o = poly:new()
o:init(i, col_array[i])
add(self.objects, o )
end
end
function polyCluster:update()
foreach(self.objects, function(obj)
obj:update()
end)
end
function polyCluster:draw()
foreach(self.objects, function(obj)
obj:draw()
end)
end
function polyCluster:new(o)
self.__index = self
return setmetatable(o or {}, self)
end
-------------------------------------------------------------
clusters = {}
function _init()
local c = polyCluster:new()
c:init({2,8,14})
add(clusters, c)
c = polyCluster:new()
c:init({1,6,12})
add(clusters, c)
c = polyCluster:new()
c:init({13,6,7})
add(clusters, c)
end
function _draw()
foreach(clusters, function(c)
c:update()
end)
end
function _update()
cls()
foreach(clusters, function(c)
c:draw()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment