Re: Code Doodles!
Posted: Tue Apr 15, 2014 10:21 pm
Good stuff, everybody. Here is mine.
Use arrow keys to generate waves in x- or y-direction (need to hold down for one or two seconds, until nice waves emerge).Code: Select all
clocks = {}
uselines = true
function makeClock(x,y,rate)
local c = {}
c.x = x
c.y = y
c.rate = rate
c.rot = 0
function c:draw()
love.graphics.setColor(140,140,140)
love.graphics.circle( "fill", x, y, 20, 50 )
love.graphics.setColor(60,60,60)
love.graphics.circle( "line", x, y, 20, 50 )
local dx,dy = math.cos(math.rad(self.rot))*13, math.sin(math.rad(self.rot))*13
if uselines then
love.graphics.setColor(60,60,60)
love.graphics.line( x,y,x+dx,y+dy )
else
love.graphics.setColor(220,220,220)
love.graphics.circle( "fill", x+dx,y+dy, 5, 20 )
end
end
function c:update(td)
self.rot = self.rot+ self.rate*td
end
return c
end
function love.load()
for x=0,love.graphics.getWidth()-40,50 do
for y=0,love.graphics.getHeight()-40,50 do
local c = makeClock(x+25,y+25,50+x+y)
table.insert(clocks,c)
end
end
love.graphics.setBackgroundColor( 220, 220, 220 )
end
function love.keypressed(key,isrepeat)
uselines = not uselines
end
function love.update(td)
for _,c in ipairs(clocks) do
c:update(td)
end
end
function love.draw()
for _,c in ipairs(clocks) do
c:draw()
end
end
That is so cool! 3D stuff in LÖVE is awesome.Ref wrote:Too bad we can't do 3D???
A 2 hour doodle.
(Don't know why it rotates faster the mode sides it has.)
Code: Select all
-- LÖVE Code Doodle #3
-- by HugoBDesigner
function love.load()
-- GOOD VARIABLE S TO MESS AROUND WITH:
recsize = 40 --Size, in pixels, of the blocks
maxheight = 12 --The maximum height of a pile of blocks
fakeblockmaxtime = 2 --The time it takes to remove a line after it surpasses the maximum height
gridw = math.floor(800/recsize)
gridh = math.floor(600/recsize)
maxheight = gridh-maxheight
fakeblocks = {}
fakeblocktimer = 0
simspeed = 1
grid = {}
gridwait = {}
minspawntime = .05
maxspawntime = .25
minspeed = 3
maxspeed = 12
spawntime = math.random(minspawntime, maxspawntime)
for x = 1, gridw do
grid[x] = {}
gridwait[x] = false
for y = 1, gridh do
grid[x][y] = false
end
end
economic = false
pyramid = true
fallingblocks = {}
love.graphics.setBackgroundColor(255, 255, 255, 255)
end
function love.update(ndt)
local dt = ndt*simspeed
if fakeblocktimer > 0 then
fakeblocktimer = fakeblocktimer - dt
if fakeblocktimer <= 0 then
fakeblocktimer = 0
removeline()
end
return
end
for i, v in pairs(fallingblocks) do
v.y = v.y + v.speed*dt
if v.y >= v.dy and not v.die then
grid[v.x][v.dy] = v.color
gridwait[v.x] = false
local rem = v.removes
fallingblocks[i] = nil
if rem then
fakeblocktimer = fakeblockmaxtime
end
elseif v.die and v.y >= gridh+1 then --offset, MURDER IT!!!
gridwait[v.x] = false
fallingblocks[i] = nil
end
end
spawntime = spawntime - dt
if spawntime <= 0 then
spawntime = math.random(minspawntime, maxspawntime)
spawnblock()
end
end
function love.draw()
if fakeblocktimer > 0 then
love.graphics.push()
love.graphics.translate(0, recsize-fakeblocktimer/fakeblockmaxtime*recsize)
end
for x = 1, gridw do
for y = 1, gridh do
if grid[x][y] then
love.graphics.setColor(unpack(grid[x][y]))
love.graphics.rectangle("fill", (x-1)*recsize, (y-1)*recsize, recsize, recsize)
if pyramid then --TOOK ME A FREAKIN LONG TIME TO MAKE!
local p1 = {(x-1)*recsize, (y-1)*recsize,
(x-1)*recsize+recsize, (y-1)*recsize+recsize}
local p2 = {(x-1)*recsize+recsize/4, (y-1)*recsize+recsize/4,
(x-1)*recsize+recsize-recsize/4, (y-1)*recsize+recsize-recsize/4}
love.graphics.setColor(255, 255, 255, 55) --left
love.graphics.polygon("fill", p1[1], p1[2], p1[1], p1[4], p2[1], p2[4], p2[1], p2[2])
love.graphics.setColor(255, 255, 255, 75) --top
love.graphics.polygon("fill", p1[1], p1[2], p1[3], p1[2], p2[3], p2[2], p2[1], p2[2])
love.graphics.setColor(0, 0, 0, 55) --right
love.graphics.polygon("fill", p1[3], p1[2], p1[3], p1[4], p2[3], p2[4], p2[3], p2[2])
love.graphics.setColor(0, 0, 0, 75) --bottom
love.graphics.polygon("fill", p1[1], p1[4], p1[3], p1[4], p2[3], p2[4], p2[1], p2[4])
end
if not economic then
love.graphics.setColor(dark(grid[x][y]))
love.graphics.rectangle("fill", (x-1)*recsize, (y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", x*recsize-2, (y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", (x-1)*recsize+2, (y-1)*recsize, recsize-4, 2)
love.graphics.rectangle("fill", (x-1)*recsize+2, y*recsize-2, recsize-4, 2)
end
end
end
end
for i, v in pairs(fallingblocks) do
love.graphics.setColor(v.color)
love.graphics.rectangle("fill", (v.x-1)*recsize, (v.y-1)*recsize, recsize, recsize)
if pyramid then --TOOK ME A FREAKIN LONG TIME TO MAKE!
local p1 = {(v.x-1)*recsize, (v.y-1)*recsize,
(v.x-1)*recsize+recsize, (v.y-1)*recsize+recsize}
local p2 = {(v.x-1)*recsize+recsize/4, (v.y-1)*recsize+recsize/4,
(v.x-1)*recsize+recsize-recsize/4, (v.y-1)*recsize+recsize-recsize/4}
love.graphics.setColor(255, 255, 255, 55) --left
love.graphics.polygon("fill", p1[1], p1[2], p1[1], p1[4], p2[1], p2[4], p2[1], p2[2])
love.graphics.setColor(255, 255, 255, 75) --top
love.graphics.polygon("fill", p1[1], p1[2], p1[3], p1[2], p2[3], p2[2], p2[1], p2[2])
love.graphics.setColor(0, 0, 0, 55) --right
love.graphics.polygon("fill", p1[3], p1[2], p1[3], p1[4], p2[3], p2[4], p2[3], p2[2])
love.graphics.setColor(0, 0, 0, 75) --bottom
love.graphics.polygon("fill", p1[1], p1[4], p1[3], p1[4], p2[3], p2[4], p2[1], p2[4])
end
if not economic then
love.graphics.setColor(dark(v.color))
love.graphics.rectangle("fill", (v.x-1)*recsize, (v.y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", v.x*recsize-2, (v.y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", (v.x-1)*recsize+2, (v.y-1)*recsize, recsize-4, 2)
love.graphics.rectangle("fill", (v.x-1)*recsize+2, v.y*recsize-2, recsize-4, 2)
end
end
if fakeblocktimer > 0 then
love.graphics.pop()
end
love.graphics.setColor(0, 205, 255, 25)
local x, y = love.mouse.getPosition()
x = math.max(1, math.min(gridw, math.floor(x/recsize)+1))
y = math.max(1, math.min(gridw, math.floor(y/recsize)+1))
love.graphics.rectangle("fill", (x-1)*recsize, (y-1)*recsize, recsize, recsize)
love.graphics.setColor(205, 255, 0, 55)
love.graphics.line(0, maxheight*recsize, gridw*recsize, maxheight*recsize)
end
function love.keypressed(key, u)
if key == "enter" or key == "return" or key == "kpenter" then
economic = not economic
elseif key == "p" then
pyramid = not pyramid
elseif key == " " then
spawnblock()
elseif key == "r" then
for x = 1, gridw do
for y = 1, gridh do
if grid[x][y] then
grid[x][y] = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
end
end
for i, v in pairs(fallingblocks) do
v.color = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
elseif key == "down" or key == "s" or key == "delete" then
if fakeblocktimer == 0 then
for x = 1, gridw do --check if it CAN remove line
if grid[x][gridh] then
fakeblocktimer = fakeblockmaxtime
break
end
end
end
elseif key == "escape" then
love.event.quit()
end
end
function love.mousepressed(x, y, button)
if button == "wu" then
if simspeed < 1 then
simspeed = math.min(1, simspeed+.1)
else
simspeed = math.min(5, simspeed+1)
end
elseif button == "wd" then
if simspeed > 1 then
simspeed = math.max(1, simspeed-1)
else
simspeed = math.max(.1, simspeed-.1)
end
elseif button == "m" then
simspeed = 1
elseif button == "l" then
spawnblock(math.max(1, math.min(gridw, math.floor(x/recsize)+1)))
elseif button == "r" then
local rem = 0
if fakeblocktimer > 0 then
rem = 1-fakeblocktimer/fakeblockmaxtime
end
local nx = math.max(1, math.min(gridw, math.floor(x/recsize)+1))
local found = false
for i, v in pairs(fallingblocks) do
if v.x == nx and (v.y-1+rem)*recsize <= y and (v.y-1+rem)*recsize+recsize > y then --It should be this one!
v.color = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
found = true
break
end
end
if not found then --check stacked blocks
local ny = math.max(1, math.min(gridh, math.floor((y-(rem*recsize))/recsize)+1))
if grid[nx][ny] then
grid[nx][ny] = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
end
end
end
function spawnblock(prex)
local prex = prex or false
local xtable = {}
for x = 1, gridw do
if grid[x][maxheight] == false and not gridwait[x] and (prex == false or x == prex) then
table.insert(xtable, x)
end
end
if #xtable == 0 then
return --mostly won't happen, but just in case...
end
local x = xtable[math.random(#xtable)]
local dy = gridh
local removes = false
for y = 1, gridh do
if grid[x][y] == false then
dy = y
end
end
if dy == maxheight then
removes = true --removes last line of blocks
end
gridwait[x] = true
local col = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
local ft = {x = x, y = -1, dy = dy, color = col, removes = removes, speed = math.random(minspeed, maxspeed), die = false}
table.insert(fallingblocks, ft)
end
function removeline()
for i, v in pairs(fallingblocks) do
if v.dy < gridh then
v.dy = v.dy + 1
end
if v.y >= gridh-.75 then -- Almost falling? Let it fall!
v.die = true
end
v.y = v.y + 1
end
for x = 1, gridw do
for y = gridh, 2, -1 do
grid[x][y] = grid[x][y-1]
end
grid[x][1] = false
end
end
function dark(c)
local r, g, b, a = unpack(c)
return math.max(r*.75, 0), math.max(g*.75, 0), math.max(b*.75, 0), a
end
Code: Select all
-- LÖVE Code Doodle #3
-- by HugoBDesigner
function love.load()
-- GOOD VARIABLE S TO MESS AROUND WITH:
recsize = 40 --Size, in pixels, of the blocks
maxheight = 12 --The maximum height of a pile of blocks
fakeblockmaxtime = 2 --The time it takes to remove a line after it surpasses the maximum height
gridw = math.floor(800/recsize)
gridh = math.floor(600/recsize)
maxheight = gridh-maxheight
fakeblocks = {}
fakeblocktimer = 0
simspeed = 1
grid = {}
gridwait = {}
minspawntime = .05
maxspawntime = .25
minspeed = 3
maxspeed = 12
spawntime = math.random(minspawntime, maxspawntime)
for x = 1, gridw do
grid[x] = {}
gridwait[x] = false
for y = 1, gridh do
grid[x][y] = false
end
end
economic = false
pyramid = true
fallingblocks = {}
forms = {"rectangle", "circle", "hexagon", "triangle"}
formn = 1
love.graphics.setBackgroundColor(255, 255, 255, 255)
end
function love.update(ndt)
local dt = ndt*simspeed
if fakeblocktimer > 0 then
fakeblocktimer = fakeblocktimer - dt
if fakeblocktimer <= 0 then
fakeblocktimer = 0
removeline()
end
return
end
for i, v in pairs(fallingblocks) do
v.y = v.y + v.speed*dt
if v.y >= v.dy and not v.die then
grid[v.x][v.dy] = v.color
gridwait[v.x] = false
local rem = v.removes
fallingblocks[i] = nil
if rem then
fakeblocktimer = fakeblockmaxtime
end
elseif v.die and v.y >= gridh+1 then --offset, MURDER IT!!!
gridwait[v.x] = false
fallingblocks[i] = nil
end
end
spawntime = spawntime - dt
if spawntime <= 0 then
spawntime = math.random(minspawntime, maxspawntime)
spawnblock()
end
end
function love.draw()
if fakeblocktimer > 0 then
love.graphics.push()
love.graphics.translate(0, recsize-fakeblocktimer/fakeblockmaxtime*recsize)
end
local drawt = {}
for x = 1, gridw do
for y = 1, gridh do
if grid[x][y] then
table.insert(drawt, {x = x, y = y, color = grid[x][y]})
end
end
end
for i, v in pairs(fallingblocks) do
table.insert(drawt, {x = v.x, y = v.y, color = v.color})
end
for i, v in pairs(drawt) do
love.graphics.setColor(unpack(v.color))
local f = forms[formn]
local px = {}
local ip = {}
if f == "hexagon" then
for i = 1, 6 do
table.insert(px, (v.x-1)*recsize+recsize/2+math.cos((math.pi*2)*((i-1)/6))*(recsize/2))
table.insert(px, (v.y-1)*recsize+recsize/2+math.sin((math.pi*2)*((i-1)/6))*(recsize/2))
table.insert(ip, (v.x-1)*recsize+recsize/2+math.cos((math.pi*2)*((i-1)/6))*(recsize/4))
table.insert(ip, (v.y-1)*recsize+recsize/2+math.sin((math.pi*2)*((i-1)/6))*(recsize/4))
end
end
if f == "rectangle" then
love.graphics.rectangle("fill", (v.x-1)*recsize, (v.y-1)*recsize, recsize, recsize)
if pyramid then --TOOK ME A FREAKIN LONG TIME TO MAKE!
local p1 = {(v.x-1)*recsize, (v.y-1)*recsize,
(v.x-1)*recsize+recsize, (v.y-1)*recsize+recsize}
local p2 = {(v.x-1)*recsize+recsize/4, (v.y-1)*recsize+recsize/4,
(v.x-1)*recsize+recsize-recsize/4, (v.y-1)*recsize+recsize-recsize/4}
love.graphics.setColor(255, 255, 255, 55) --left
love.graphics.polygon("fill", p1[1], p1[2], p1[1], p1[4], p2[1], p2[4], p2[1], p2[2])
love.graphics.setColor(255, 255, 255, 75) --top
love.graphics.polygon("fill", p1[1], p1[2], p1[3], p1[2], p2[3], p2[2], p2[1], p2[2])
love.graphics.setColor(0, 0, 0, 55) --right
love.graphics.polygon("fill", p1[3], p1[2], p1[3], p1[4], p2[3], p2[4], p2[3], p2[2])
love.graphics.setColor(0, 0, 0, 75) --bottom
love.graphics.polygon("fill", p1[1], p1[4], p1[3], p1[4], p2[3], p2[4], p2[1], p2[4])
end
elseif f == "circle" then
love.graphics.circle("fill", (v.x-1)*recsize+recsize/2, (v.y-1)*recsize+recsize/2, recsize/2, 32)
if pyramid then
love.graphics.setColor(255, 255, 255, 75)
love.graphics.circle("fill", (v.x-1)*recsize+recsize/2, (v.y-1)*recsize+recsize/2, recsize/2, 32)
love.graphics.setColor(unpack(v.color))
love.graphics.circle("fill", (v.x-1)*recsize+recsize/2, (v.y-1)*recsize+recsize/2, recsize/4, 32)
end
elseif f == "hexagon" then
love.graphics.polygon("fill", px)
if pyramid then
love.graphics.setColor(0, 0, 0, 55) --bottom
love.graphics.polygon("fill", px[5], px[6], px[3], px[4], ip[3], ip[4], ip[5], ip[6])
love.graphics.setColor(0, 0, 0, 75) --left-bottom
love.graphics.polygon("fill", px[3], px[4], px[1], px[2], ip[1], ip[2], ip[3], ip[4])
love.graphics.setColor(0, 0, 0, 55) --left-top
love.graphics.polygon("fill", px[1], px[2], px[11], px[12], ip[11], ip[12], ip[1], ip[2])
love.graphics.setColor(255, 255, 255, 55) --top
love.graphics.polygon("fill", px[9], px[10], px[11], px[12], ip[11], ip[12], ip[9], ip[10])
love.graphics.setColor(255, 255, 255, 75) --right-top
love.graphics.polygon("fill", px[7], px[8], px[9], px[10], ip[9], ip[10], ip[7], ip[8])
love.graphics.setColor(255, 255, 255, 55) --right-bottom
love.graphics.polygon("fill", px[7], px[8], px[5], px[6], ip[5], ip[6], ip[7], ip[8])
end
elseif f == "triangle" then
local px = {(v.x-1)*recsize+recsize/2, (v.x-1)*recsize, (v.x-1)*recsize+recsize}
local py = {(v.y-1)*recsize, (v.y-1)*recsize+recsize, (v.y-1)*recsize+recsize}
local cx = {(v.x-1)*recsize+recsize/2, (v.x-1)*recsize+recsize/3, (v.x-1)*recsize+recsize/3*2}
local cy = {(v.y-1)*recsize+recsize/3, (v.y-1)*recsize+recsize/4*3, (v.y-1)*recsize+recsize/4*3}
love.graphics.polygon("fill", px[1], py[1], px[2], py[2], px[3], py[3])
if pyramid then
love.graphics.setColor(255, 255, 255, 75) --top-left
love.graphics.polygon("fill", px[1], py[1], cx[1], cy[1], cx[2], cy[2], px[2], py[2])
love.graphics.setColor(0, 0, 0, 55) --top-right
love.graphics.polygon("fill", px[1], py[1], cx[1], cy[1], cx[3], cy[3], px[3], py[3])
love.graphics.setColor(0, 0, 0, 75) --bottom
love.graphics.polygon("fill", px[2], py[2], cx[2], cy[2], cx[3], cy[3], px[3], py[3])
end
end
if not economic then
love.graphics.setColor(dark(v.color))
if f == "rectangle" then
love.graphics.rectangle("fill", (v.x-1)*recsize, (v.y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", v.x*recsize-2, (v.y-1)*recsize, 2, recsize)
love.graphics.rectangle("fill", (v.x-1)*recsize+2, (v.y-1)*recsize, recsize-4, 2)
love.graphics.rectangle("fill", (v.x-1)*recsize+2, v.y*recsize-2, recsize-4, 2)
elseif f == "circle" then
love.graphics.circle("line", (v.x-1)*recsize+recsize/2, (v.y-1)*recsize+recsize/2, recsize/2, 32)
elseif f == "hexagon" then
love.graphics.polygon("line", px)
elseif f == "triangle" then
local px = {(v.x-1)*recsize+recsize/2, (v.x-1)*recsize, (v.x-1)*recsize+recsize}
local py = {(v.y-1)*recsize, (v.y-1)*recsize+recsize, (v.y-1)*recsize+recsize}
love.graphics.polygon("line", px[1], py[1], px[2], py[2], px[3], py[3])
end
end
end
if fakeblocktimer > 0 then
love.graphics.pop()
end
love.graphics.setColor(0, 205, 255, 25)
local x, y = love.mouse.getPosition()
x = math.max(1, math.min(gridw, math.floor(x/recsize)+1))
y = math.max(1, math.min(gridw, math.floor(y/recsize)+1))
love.graphics.rectangle("fill", (x-1)*recsize, (y-1)*recsize, recsize, recsize)
love.graphics.setColor(205, 255, 0, 55)
love.graphics.line(0, maxheight*recsize, gridw*recsize, maxheight*recsize)
end
function love.keypressed(key, u)
if key == "enter" or key == "return" or key == "kpenter" then
economic = not economic
elseif key == "p" then
pyramid = not pyramid
elseif key == " " then
spawnblock()
elseif key == "r" then
for x = 1, gridw do
for y = 1, gridh do
if grid[x][y] then
grid[x][y] = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
end
end
for i, v in pairs(fallingblocks) do
v.color = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
elseif key == "down" or key == "s" or key == "delete" then
if fakeblocktimer == 0 then
for x = 1, gridw do --check if it CAN remove line
if grid[x][gridh] then
fakeblocktimer = fakeblockmaxtime
break
end
end
end
elseif key == "escape" then
love.event.quit()
elseif key == "f" then
formn = formn+1
if formn > #forms then
formn = 1
end
end
end
function love.mousepressed(x, y, button)
if button == "wu" then
if simspeed < 1 then
simspeed = math.min(1, simspeed+.1)
else
simspeed = math.min(5, simspeed+1)
end
elseif button == "wd" then
if simspeed > 1 then
simspeed = math.max(1, simspeed-1)
else
simspeed = math.max(.1, simspeed-.1)
end
elseif button == "m" then
simspeed = 1
elseif button == "l" then
spawnblock(math.max(1, math.min(gridw, math.floor(x/recsize)+1)))
elseif button == "r" then
local rem = 0
if fakeblocktimer > 0 then
rem = 1-fakeblocktimer/fakeblockmaxtime
end
local nx = math.max(1, math.min(gridw, math.floor(x/recsize)+1))
local found = false
for i, v in pairs(fallingblocks) do
if v.x == nx and (v.y-1+rem)*recsize <= y and (v.y-1+rem)*recsize+recsize > y then --It should be this one!
v.color = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
found = true
break
end
end
if not found then --check stacked blocks
local ny = math.max(1, math.min(gridh, math.floor((y-(rem*recsize))/recsize)+1))
if grid[nx][ny] then
grid[nx][ny] = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
end
end
end
end
function spawnblock(prex)
local prex = prex or false
local xtable = {}
for x = 1, gridw do
if grid[x][maxheight] == false and not gridwait[x] and (prex == false or x == prex) then
table.insert(xtable, x)
end
end
if #xtable == 0 then
return --mostly won't happen, but just in case...
end
local x = xtable[math.random(#xtable)]
local dy = gridh
local removes = false
for y = 1, gridh do
if grid[x][y] == false then
dy = y
end
end
if dy == maxheight then
removes = true --removes last line of blocks
end
gridwait[x] = true
local col = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
local ft = {x = x, y = -1, dy = dy, color = col, removes = removes, speed = math.random(minspeed, maxspeed), die = false}
table.insert(fallingblocks, ft)
end
function removeline()
for i, v in pairs(fallingblocks) do
if v.dy < gridh then
v.dy = v.dy + 1
end
if v.y >= gridh-.75 then -- Almost falling? Let it fall!
v.die = true
end
v.y = v.y + 1
end
for x = 1, gridw do
for y = gridh, 2, -1 do
grid[x][y] = grid[x][y-1]
end
grid[x][1] = false
end
end
function dark(c)
local r, g, b, a = unpack(c)
return math.max(r*.75, 0), math.max(g*.75, 0), math.max(b*.75, 0), a
end
Code: Select all
-- LÖVE Code Doodle #4
-- by HugoBDesigner
function love.load()
love.mouse.setGrab = love.mouse.setGrab or love.mouse.setGrabbed
depth = 70
rec = 100
x1 = 0
y1 = 0
mult = 1
mode = true
love.graphics.setLineWidth(2)
cubecolor = {}
backcolor = {}
randomColors()
end
function love.update(dt)
x1, y1 = love.mouse.getPosition()
x1 = x1-400
y1 = y1-300
end
function love.draw()
local px1, py1 = x1-rec/2, y1-rec/2
local px2, py2 = x1-rec/2+rec, y1-rec/2+rec
local bx1, by1 = px1+rec/2-depth*(x1/800)*mult-rec/100*depth/2, py1+rec/2-depth*(y1/600)*mult-rec/100*depth/2
local bx2, by2 = bx1+rec/100*depth, by1+rec/100*depth
love.graphics.push()
love.graphics.translate(400, 300)
local r, g, b, a = unpack(cubecolor)
love.graphics.setColor(r*.5, g*.5, b*.5, a)
if mode then love.graphics.rectangle("fill", bx1, by1, rec/100*depth, rec/100*depth) end --back
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.rectangle("line", bx1, by1, rec/100*depth, rec/100*depth)
love.graphics.setColor(r*.75, g*.75, b*.75, a)
if by1 <= py1 or mode == false then --top
if mode then love.graphics.polygon("fill", px1, py1, bx1, by1, bx2, by1, px2, py1) end
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.polygon("line", px1, py1, bx1, by1, bx2, by1, px2, py1)
end
if by2 >= py2 or mode == false then --bottom
if mode then love.graphics.polygon("fill", px1, py2, bx1, by2, bx2, by2, px2, py2) end
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.polygon("line", px1, py2, bx1, by2, bx2, by2, px2, py2)
end
love.graphics.setColor(r*.5, g*.5, b*.5, a)
if bx1 <= px1 or mode == false then --left
if mode then love.graphics.polygon("fill", px1, py1, bx1, by1, bx1, by2, px1, py2) end
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.polygon("line", px1, py1, bx1, by1, bx1, by2, px1, py2)
end
if bx2 >= px2 or mode == false then --right
if mode then love.graphics.polygon("fill", px2, py1, bx2, by1, bx2, by2, px2, py2) end
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.polygon("line", px2, py1, bx2, by1, bx2, by2, px2, py2)
end
love.graphics.setColor(r, g, b, a)
if mode then love.graphics.rectangle("fill", px1, py1, rec, rec) end --front
love.graphics.setColor(r*.15, g*.15, b*.15, a)
love.graphics.rectangle("line", px1, py1, rec, rec)
love.graphics.pop()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print("FPS = "..love.timer.getFPS(), 4, 4)
love.graphics.print("(Mouse Wheel) Depth = "..mult, 4, 20)
love.graphics.print("(+/-) Cube size = "..rec, 4, 36)
love.graphics.print("(Enter) Wireframe mode = "..tostring(not mode), 4, 52)
love.graphics.print("(Space/R) Randomizes colors", 4, 68)
love.graphics.print("(F12) " .. (love.mouse.isGrabbed() and "Ungrab" or "Grab") .. " mouse", 4, 84)
end
function love.keypressed(k, u)
if k == "escape" then
love.event.quit()
elseif k == "enter" or k == "return" or k == "kpenter" then
mode = not mode
elseif k == "f12" then
love.mouse.setGrab(not love.mouse.isGrabbed())
elseif k == "-" then
rec = math.max(50, rec-10)
elseif k == "=" then
rec = math.min(300, rec+10)
elseif k == "r" or k == " " then
randomColors()
end
end
function love.mousepressed(x, y, b)
if b == "wd" then
mult = math.max(mult-.2, .5)
elseif b == "wu" then
mult = math.min(mult+.2, 6)
end
end
function randomColors()
cubecolor = {math.random(155, 255), math.random(155, 255), math.random(155, 255), 255}
backcolor = {math.random(0, 100), math.random(0, 100), math.random(0, 100), 255}
love.graphics.setBackgroundColor(unpack(backcolor))
end
Code: Select all
screen = {
width = love.graphics.getWidth(),
height = love.graphics.getHeight()
}
love.window.setMode(screen.width, screen.height, {fsaa = 4})
local objects = {}
local radius = 12.5
local width = math.floor(screen.width / radius)
local height = math.floor(screen.height / radius)
local color = {0, 255, 126}
local color_speed = 0.01
local canvas = {
love.graphics.newCanvas(screen.width, screen.height),
love.graphics.newCanvas(screen.width, screen.height)
}
local light = {
x = 0,
y = 0,
size = 32
}
function love.load()
local sx = radius
local sy = radius
local cx = sx
local cy = sy
for y=0, height-1 do
for x=0, height-1 do
objects[#objects + 1] = {
x = cx,
y = cy,
radius = radius,
alpha = 1,
color = color
}
cx = cx + (radius * 2)
end
cx = sx
cy = cy + (radius * 2)
end
end
function love.update(dt)
local mx, my = love.mouse.getPosition()
light.x = light.x + (mx - light.x) * 0.1
light.y = light.y + (my- light.y) * 0.1
for i,v in ipairs(objects) do
local dist = getDistance(light.x, light.y, v.x, v.y)
v.alpha = 1 / (dist / light.size)
if v.alpha > 1 then v.alpha = 1 end
v.color[1] = v.color[1] + color_speed * dt
if v.color[1] > 255 then v.color[1] = 0 end
end
end
function love.draw()
love.graphics.setColor(255, 255, 255, 255)
canvas[1]:clear()
love.graphics.setCanvas(canvas[1])
for i,v in ipairs(objects) do
love.graphics.setColor(hsl(v.color[1], v.color[2], v.color[3], 128 * v.alpha))
love.graphics.rectangle("fill", v.x - (v.radius * v.alpha), v.y - (v.radius * v.alpha), (v.radius * 2) * v.alpha, (v.radius * 2) * v.alpha)
love.graphics.setColor(hsl(v.color[1], v.color[2], v.color[3], 255 * v.alpha))
love.graphics.rectangle("line", v.x - (v.radius * v.alpha), v.y - (v.radius * v.alpha), (v.radius * 2) * v.alpha, (v.radius * 2) * v.alpha)
end
love.graphics.setCanvas(canvas[2])
--shader
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(canvas[1])
love.graphics.setCanvas()
--Final drawing
love.graphics.draw(canvas[2])
end
function love.keypressed(key)
if key == "escape" then love.event.push("quit") end
end
function getDistance(x1,y1, x2,y2)
return ((x2-x1)^2+(y2-y1)^2)^0.5
end
function hsl(h, s, l, a)
if s<=0 then return l,l,l,a end
h, s, l = h/256*6, s/255, l/255
local c = (1-math.abs(2*l-1))*s
local x = (1-math.abs(h%2-1))*c
local m,r,g,b = (l-.5*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)*255,(g+m)*255,(b+m)*255,a
end