I made a polygon transforming thing that slowly adds sides to itself. Instead of using the default Löve polygons, I had to make my owns. Here are the controls:
SPACE or "R": Randomizes colors
ENTER: Toggles inner lines
Middle click: Toggles auto-running polygon changes
Mouse wheel: Increases/Decreases the polygon's sides
And the code:
Code: Select all
-- LÖVE Code Doodle #5
-- by HugoBDesigner
function love.load()
shapet = 0
shapetb = 0
shapemaxt = 10
shaped = "f"
shapemin = 3
shapemax = 15
recordingdata = {}
isRecording = false
timerecorded = 0 --Just so users can see how long they've been recording
framesrecorded = 0 --Just so users can see how long they've been recording
recordingtimer = 0
recordingmaxtimer = .025
canrun = true
inlines = true
rcol = {}
rcol[1] = {0, 0, 0, 255}
rcol[2] = {255, 255, 255, 255}
rcol[3] = {155, 155, 155, 255}
love.graphics.setBackgroundColor(unpack(rcol[1]))
end
function love.update(dt)
local dt = math.min(dt, 1/60)
if canrun then
if shaped == "f" then
shapet = shapet + dt
if shapet >= shapemaxt then
shaped = "b"
end
else
shapet = shapet - dt
if shapet <= 0 then
shaped = "f"
end
end
shapetb = shapet
end
if isRecording then
if recordingtimer >= recordingmaxtimer then
local a = love.graphics.newScreenshot()
recordingdata[#recordingdata+1] = a
framesrecorded = framesrecorded + dt
timerecorded = round(framesrecorded)
love.graphics.setCaption( "Spacing Out [RECORDING] - " .. tostring(timerecorded) .. " seconds - " .. tostring(#recordingdata) .. "frames")
recordingtimer = 0
else
recordingtimer = recordingtimer + dt
end
end
end
function love.draw()
local rd = 100
local sides = (shapet/shapemaxt)*(shapemax-shapemin)+shapemin
local points = love.graphics.shape("line", 400, 300, rd, math.limit(sides, shapemin, shapemax))
love.graphics.clear()
love.graphics.print("sides = " .. round(sides), 5, 15)
love.graphics.setLineWidth(2)
love.graphics.setColor(unpack(rcol[3]))
if inlines then
for i = 1, #points, 2 do
local px = points[i]
local py = points[i+1]
for a = 1, #points, 2 do
if a ~= i then
local npx = points[a]
local npy = points[a+1]
love.graphics.line(px, py, npx, npy)
end
end
end
end
love.graphics.setLineWidth(4)
love.graphics.setColor(unpack(rcol[2]))
local points = love.graphics.shape("line", 400, 300, rd, math.limit(sides, shapemin, shapemax))
end
function love.graphics.shape(mode, x, y, rd, sides)
local points = {}
for i = 1, math.ceil(sides) do
local px = x
local py = y
local a = math.pi*2/sides*i
px = px + math.cos(a)*rd
py = py + math.sin(a)*rd
table.insert(points, px)
table.insert(points, py)
end
love.graphics.polygon(mode, points)
return points
end
function love.keypressed(key, unicode)
if key == "escape" then
love.event.quit()
end
if key == "f12" then
love.mouse.setGrab(not love.mouse.isGrabbed())
end
if key == "f3" then
-- PART 1: Get the screenshot itself and make it as an image
local number = 1
local lnumber = "0001"
if not love.filesystem.exists("screenshots") then --make one
love.filesystem.mkdir("screenshots")
end
local screenshot = love.graphics.newScreenshot()
while love.filesystem.exists("screenshots/screenshot_" .. lnumber .. ".png") do
number = number + 1
if number >= 1 and number < 10 then
lnumber = "000" .. tostring(number)
elseif number >= 10 and number < 100 then
lnumber = "00" .. tostring(number)
elseif number >= 100 and number < 1000 then
lnumber = "0" .. tostring(number)
else
lnumber = tostring(number)
end
end
--screenshot:encode("screenshots/screenshot_" .. lnumber .. ".png")
--PART 2: Remove transparecy from screenshots. I noticed that they make transparent pixels
local image = screenshot
local imagewidth = image:getWidth()
local imageheight = image:getHeight()
for y = 0, imageheight-1 do
for x = 0, imagewidth-1 do
local r, g, b, a = image:getPixel( x, y )
if a < 255 then
image:setPixel( x, y, r, g, b, 255)
end
end
end
image:encode("screenshots/screenshot_" .. lnumber .. ".png")
end
if key == "f9" then
if isRecording then
stoprecording()
else
startrecording()
end
end
if key == "f10" then
if isRecording then
stoprecording(true)
end
end
if key == "f11" then
if isRecording then
isRecording = false
recordingdata = {}
framesrecorded = 0
timerecorded = 0
end
end
if key == "enter" or key == "return" or key == "kpenter" then
inlines = not inlines
end
if key == " " or key == "r" then
rcol[1] = {math.random(55), math.random(55), math.random(55), rcol[1][4]}
rcol[2] = {math.random(155, 255), math.random(155, 255), math.random(155, 255), rcol[2][4]}
rcol[3] = {math.random(55, 205), math.random(55, 205), math.random(55, 205), rcol[3][4]}
love.graphics.setBackgroundColor(unpack(rcol[1]))
end
end
function love.keyreleased(key, unicode)
end
function love.mousepressed(x, y, button)
local mult = 50
local add = shapemaxt/(mult*(shapemax-shapemin))
if button == "m" then
canrun = not canrun
shapet = math.floor(shapet*mult/2)/mult*2
end
if canrun == false then
if button == "wu" then
shapet = math.limit(shapet + add, 0, shapemaxt)
elseif button == "wd" then
shapet = math.limit(shapet - add, 0, shapemaxt)
end
end
end
function love.mousereleased(x, y, button)
end
function dist(x1, y1, x2, y2, ab)
local ab = ab or true
local w, h = x1-x2, y1-y2
if ab then
w = math.abs(w)
h = math.abs(h)
end
return math.sqrt(w^2+h^2)
end
function speedX(angle, speed)
return -math.sin(angle)*speed
end
function speedY(angle, speed)
return math.cos(angle)*speed
end
function math.limit(n, mn, mx)
return math.max(mn, math.min(mx, n))
end
function math.offset(n, mn, mx, f)
local n = n
local f = f or mx
if math.abs(mn-mx) > f then f = mx end
while n >= mx do
n = n - f
end
while n < mn do
n = n + f
end
return n
end
function round(n, d)
local d = d or 2
return math.floor(10^d*n)/(10^d)
end
function fade(currenttime, maxtime, c1, c2)
local tp = currenttime/maxtime
local ret = {} --return color
for i = 1, #c1 do
ret[i] = c1[i]+(c2[i]-c1[i])*tp
ret[i] = math.max(ret[i], 0)
ret[i] = math.min(ret[i], 255)
end
return unpack(ret)
end
function startrecording()
recordingdata = {}
isRecording = true
end
function stoprecording(transparency)
local transparency = transparency or false
isRecording = false
recordingtimer = 0
if not love.filesystem.exists( "records" ) then
love.filesystem.mkdir( "records" )
end
local a = "0001"
local n = 1
if love.filesystem.exists("records/record_" .. a) then
while love.filesystem.exists("records/record_" .. a) do
local zeros = ""
n = n + 1
if n >= 1 and n < 10 then
zeros = "000"
elseif n >= 10 and n < 100 then
zeros = "00"
elseif n >= 100 and n < 1000 then
zeros = "0"
end
a = zeros .. tostring(n)
end
end
n = 0
love.filesystem.mkdir("records/record_" .. a)
------------------
for i = 1, #recordingdata do
print("Recording saving at " .. tostring(round(100/#recordingdata*i, 2)) .. "%")
local b = "0001"
if love.filesystem.exists("records/record_" .. a .. "/" .. b .. ".png") then
while love.filesystem.exists("records/record_" .. a .. "/" .. b .. ".png") do
local zeros = ""
n = n + 1
if n >= 1 and n < 10 then
zeros = "000"
elseif n >= 10 and n < 100 then
zeros = "00"
elseif n >= 100 and n < 1000 then
zeros = "0"
end
b = zeros .. tostring(n)
end
end
if transparency then
for x = 0, recordingdata[i]:getWidth()-1 do
for y = 0, recordingdata[i]:getHeight()-1 do
local r, g, b, a = recordingdata[i]:getPixel(x, y)
a = 255
recordingdata[i]:setPixel(x, y, r, g, b, a)
end
end
end
recordingdata[i]:encode("records/record_" .. a .. "/" .. b .. ".png")
end
recordingdata = {}
framesrecorded = 0
timerecorded = 0
end
function table.contains(t, e)
for i, v in pairs(t) do
if v == e then
return i
end
end
return false
end
It also includes unneeded things and my screenshots/in-game recording systems because I was lazy to remove them and there is no need to not publish them...
- screenshot_0001 (1).png (59.11 KiB) Viewed 4828 times
EDIT: Made another one as a test. Here comes the warning:
DO NOT TRY IT IF YOU HAVE PROBLEMS WITH FLASHING IMAGES!!!
This one was a small test. When you enter it (or reload by pressing Ctrl+L), it'll ask you the file name. Just throw a few images in the game folder, type them in and hit enter. Use the mouse wheel to increase/decrease the amount of frames per image. What it does is take your image, divide it in 3 (red-only, green-only, blue-only) and show them on the screen one at a time. If you set a low amount of frames per image (1 is the best one), it looks just like if your image was normal colored. If you set it to a high amount (30 is the best one), it'll show slowly each color map of your picture.
Since it maps the pixels of your image to make this, this may take a while to load, but not much...
Code: Select all
-- LÖVE Code Doodle #6
-- by HugoBDesigner
function love.load()
imgs = {}
imgname = ""
state = "type"
currentimg = 0
frames = 3
exists = true
end
function loadimage(name)
imgdt = love.image.newImageData(name .. ".png")
for i = 1, 3 do
local newdt = love.image.newImageData(name .. ".png")
newdt:mapPixel(function(x, y, r, g, b, a)
local newr, newg, newb = r, g, b
if i == 2 or i == 3 then
newr = 0
end
if i == 1 or i == 3 then
newg = 0
end
if i == 1 or i == 2 then
newb = 0
end
return newr, newg, newb, a
end)
table.insert(imgs, love.graphics.newImage(newdt))
end
end
function love.update(dt)
currentimg = currentimg + 1
if currentimg > 3*frames then currentimg = 1 end
end
function love.draw()
if state == "draw" then
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(imgs[math.ceil(currentimg/frames)], 0, 0)
love.graphics.setColor(0, 0, 0, 255)
local txt, x, y = "Frames per image: " .. frames, 5, 15
love.graphics.print(txt, x-1, y-1)
love.graphics.print(txt, x, y-1)
love.graphics.print(txt, x+1, y-1)
love.graphics.print(txt, x-1, y)
love.graphics.print(txt, x+1, y)
love.graphics.print(txt, x-1, y+1)
love.graphics.print(txt, x, y+1)
love.graphics.print(txt, x+1, y+1)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(txt, x, y)
else
love.graphics.setColor(255, 255, 255, 255)
local font = love.graphics.newFont(12)
local txt1 = "Type your image name:"
love.graphics.print(txt1, 400-font:getWidth(txt1)/2, 50)
love.graphics.print(imgname, 400-font:getWidth(imgname)/2, 80)
if exists == false then
love.graphics.setColor(255, 155, 155, 255)
local txt2 = "Type a name that DOES exists as a .png"
love.graphics.print(txt2, 400-font:getWidth(txt2)/2, 110)
end
end
end
function love.mousepressed(x, y, button)
if button == "wu" then
frames = math.min(frames+1, 30)
elseif button == "wd" then
frames = math.max(frames-1, 1)
end
end
function love.keypressed(key, unicode)
if key == "escape" then
love.event.quit()
end
if state == "type" then
forget = {"rshift", "lshift", "lalt", "ralt", "rctrl", "lctrl"}
if key == "enter" or key == "return" or key == "kpenter" then
if love.filesystem.exists(imgname .. ".png") then
loadimage(imgname)
state = "draw"
else
exists = false
end
elseif key == "backspace" then
if string.len(imgname) <= 1 then
imgname = ""
else
imgname = string.sub(imgname, 1, -2)
end
elseif table.contains(forget, key) == false then
imgname = imgname .. key
end
end
if key == "l" and (love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")) then
love.load()
end
end
function table.contains(t, e)
for i, v in pairs(t) do
if v == e then
return i
end
end
return false
end