Re: Code Doodles!
Posted: Fri Nov 24, 2023 9:37 am
Fairly new to löve but saw this thread and thought I'd try my hand at a quick doodle. Nothing too fancy, just some colourful lines around a square grid but I was quite happy with the result.
To add in an icon that draws in the middle and changes colour just uncomment the iconImg lines in load and draw.
To add in an icon that draws in the middle and changes colour just uncomment the iconImg lines in load and draw.
Code: Select all
function love.load()
windowHeight = love.graphics.getHeight()
windowWidth = love.graphics.getWidth()
frameSize = 400
frameX = (windowWidth / 2) - (frameSize / 2)
frameY = (windowHeight / 2) - (frameSize / 2)
linesPerSide = 40 -- lines per side (i.e. total / 4)
drawLines = math.floor(linesPerSide * 3.8) -- number of lines to draw each frame
firstLine = 1
delayTimer = 0
delayTime = 0.03 -- seconds per update
generateLines()
-- iconImg = love.graphics.newImage("Cham.png")
end
function HSV(h, s, v)
if s <= 0 then return v,v,v end
h = h*6
local c = v*s
local x = (1-math.abs((h%2)-1))*c
local m,r,g,b = (v-c), 0, 0, 0
if h < 1 then r, g, b = c, x, 0
elseif h < 2 then r, g, b = x, c, 0
elseif h < 3 then r, g, b = 0, c, x
elseif h < 4 then r, g, b = 0, x, c
elseif h < 5 then r, g, b = x, 0, c
else r, g, b = c, 0, x end
return r+m, g+m, b+m
end
function generateLines()
lines = {}
local delta = frameSize / linesPerSide
for i = 1, linesPerSide do
lines[i] = {frameX, frameY + i * delta, frameX + frameSize - i * delta, frameY} -- top left
lines[linesPerSide + i] = {frameX, frameY + i * delta, frameX + i * delta, frameY + frameSize} -- bottom left
lines[linesPerSide * 2 + i] = {frameX + i * delta, frameY + frameSize, frameX + frameSize, frameY + frameSize - i * delta} -- bottom right
lines[linesPerSide * 3 + i] = {frameX + frameSize, frameY + frameSize - i * delta, frameX + frameSize - i * delta, frameY} -- top right
end
end
function love.update(dt)
delayTimer = delayTimer + dt
if delayTimer > delayTime then
firstLine = firstLine + 1
if firstLine > #lines then
firstLine = 1
end
delayTimer = 0
end
end
function love.draw()
love.graphics.setLineWidth(1)
local j, r, g, b
-- draw the lines, rearmost first
for i = 1, drawLines do
j = (firstLine + i) % #lines + 1 -- set current line, starting at firstLine and wrapping
r, g, b = HSV(j/#lines, 1, 1)
love.graphics.setColor(r, g, b, i / drawLines) -- make early lines more transparent
love.graphics.line(lines[j])
end
-- draw the icon, colour should still be the same as the foremost line
-- love.graphics.draw(iconImg, windowWidth / 2, windowHeight / 2, 0, 1, 1, iconImg:getWidth() / 2, iconImg:getHeight() / 2)
-- draw the frame
love.graphics.setColor(1, 1, 1)
love.graphics.setLineWidth(4)
love.graphics.rectangle("line", frameX, frameY, frameSize, frameSize)
end