Page 9 of 20

Re: Code Doodles!

Posted: Wed Sep 10, 2014 2:59 pm
by HugoBDesigner
Made a little Doodle that lists all the files where "main.lua" is placed, all the folders and their contents too (much like Löve's Recursive Enumerate). It also shows the amount of files and folders and show their paths in a tree pattern:

Image

Code: Select all

function love.load()
	font = love.graphics.newFont(12)
	fontbig = love.graphics.newFont(24)
	love.graphics.setFont(fontbig)
	totalfiles = 0
	totalfolders = 0
	t = rE("")
	love.graphics.setFont(font)
	offset = 0
	
	nameToPrint = "" --replace this with "[FOLDERNAME]/" so you can also show the folder where main.lua is
end

function love.draw()
	rP(t, 5, 5, 20, 20, offset, font:getHeight())
	local txt = "Total files: " .. totalfiles
	love.graphics.print(txt, 800-5-font:getWidth(txt), 600-5-font:getHeight()*3)
	txt = "Total folders: " .. totalfolders
	love.graphics.print(txt, 800-5-font:getWidth(txt), 600-5-font:getHeight()*2)
	txt = "Total files+folders: " .. totalfiles+totalfolders
	love.graphics.print(txt, 800-5-font:getWidth(txt), 600-5-font:getHeight())
end

function rE(f)
	loading(f)
	local t = {}
	local files = love.filesystem.enumerate(f)
	local folder = f
	if folder ~= "" then folder = folder .. "/" end
	for i, v in pairs(files) do
		if love.filesystem.isFile(folder .. v) then
			totalfolders = totalfolders + 1
			table.insert(t, folder .. v)
		else
			totalfiles = totalfiles + 1
			table.insert(t, rE(folder .. v))
		end
	end
	return {f, t}
end

function loading(f)
	love.graphics.clear()
	love.graphics.print("Loading:", 400-fontbig:getWidth("Loading:")/2, 300-fontbig:getHeight())
	love.graphics.print(f, 400-fontbig:getWidth(f)/2, 300)
	love.graphics.present()
end

function rP(t, x, y, tx, ty, off, fh, hasline)
	local hasline = hasline or false
	local y = y
	local first = true
	local lasty = y
	for i, v in pairs(t[2]) do
		if type(v) == "table" then
			if y-off+fh >= 0 then
				love.graphics.print(nameToPrint .. v[1], x, y-off)
			end
			if hasline then
				local ny = lasty-off
				if first then ny = y-off end
				if y+ty/2-off > 0 then
					if ny < 600 then
						love.graphics.line(x-tx/2, ny, x-tx/2, y+ty/2-off)
					else
						return y
					end
				end
				if y+ty/2-off < 600 and y+ty/2-off > 0 then
					love.graphics.line(x-tx/2, y+ty/2-off, x-2, y+ty/2-off)
				end
			end
			y = rP(v, x+tx, y+ty, tx, ty, off, fh, true)
			y = y - ty
		else
			if y-off+fh >= 0 then
				love.graphics.print(nameToPrint .. v, x, y-off)
			end
			if hasline then
				local ny = lasty-off
				if first then ny = y-off end
				if y+ty/2-off > 0 then
					if ny < 600 then
						love.graphics.line(x-tx/2, ny, x-tx/2, y+ty/2-off)
					else
						return y
					end
				end
				if y+ty/2-off < 600 and y+ty/2-off > 0 then
					love.graphics.line(x-tx/2, y+ty/2-off, x-2, y+ty/2-off)
				end
			end
		end
		y = y + ty
		first = false
	end
	return y
end

function love.mousepressed(x, y, button)
	local v = 15
	if love.keyboard.isDown("lshift") or love.keyboard.isDown("lctrl") then v = 30 end
	if button == "wu" then
		offset = math.max(0, offset - 15)
	elseif button == "wd" then
		offset = math.min((totalfiles+totalfolders)*20-600, offset + 15)
	end
end
EDIT: Forgot to tell I made this for LÖVE 0.8.0. I'm so used to it that sometimes I forget :P

Re: Code Doodles!

Posted: Mon Sep 15, 2014 3:03 am
by artofwork
Took me about 5 or 10 minutes to do. This was when i was a bit new to love2d, right now im just less new :D
matrix.PNG

Code: Select all

function love.load()
end

function love.update()
end

function love.draw()
	Matrix()
end

function Matrix()
local m = 15
local n = 54
local l = 0
matrix = love.graphics.newFont("matrix code nfi.ttf", 15);
love.graphics.setFont(matrix)
	for i = 0, n do 
		love.graphics.setColor(math.random(0,100), math.random(100,255), 0, math.random(0,255))
		love.graphics.print(string.char(math.random(string.byte('#'), string.byte('Z'))), 0, i * m)
			for j = 1, n do 
				love.graphics.setColor(math.random(0,100), math.random(100,255), 0, math.random(0,255))
				love.graphics.print(string.char(math.random(string.byte('#'), string.byte('Z'))), j * m, l)
			end
		l = l + 15
	end
end

Re: Code Doodles!

Posted: Thu Oct 02, 2014 6:26 pm
by HugoBDesigner
Another Doodle to play with how pixels work!

Code: Select all

--	LÖVE Code Doodle #12
--		by HugoBDesigner

-- Technically, the files tree doodle should be the #12, but I forgot to number it :P

function love.load()
	startx = 50
	endx = 100
	speed = 1
	colors = {{255, 255, 255, 255}}
	steps = {1, 2}
	
	for i = 1, (endx-startx)-1 do
		table.insert(colors, {0, 0, 0, 255})
	end
end

function love.update(dt)
	dt = math.min(dt, 1/60)
	local p1, p2 = steps[1], steps[2]
	
	for a = 1, 2 do
		local b = a*2-3
		local skip = false
		for i = 1, 3 do
			local n = colors[steps[a]][i]
			if not skip then
				colors[steps[a]][i] = math.max(0, math.min(255, colors[steps[a]][i]+dt*b*speed*255))
				if colors[steps[a]][i] ~= n then
					skip = true
				end
			end
			if a == 2 and i == 3 and colors[p1][1]+colors[p1][2]+colors[p1][3] == 0 and colors[p2][1]+colors[p2][2]+colors[p2][3] == 255*3 then
				if steps[1] < #colors then
					steps[1] = steps[1] + 1
				else
					steps[1] = 1
				end
				if steps[2] < #colors then
					steps[2] = steps[2] + 1
				else
					steps[2] = 1
				end
			end
		end
	end
end

function love.draw()
	for i, v in ipairs(colors) do
		love.graphics.setColor(v)
		if v[1] + v[2] + v[3] > 0 then
			love.graphics.point(startx+(i-.5), 200.5)
		end
		for a = 1, 3 do
			local mins = 25
			local t = {0, 0, 0}
			t[a] = v[a]/255*(255-mins)+mins
			love.graphics.setColor(t)
			love.graphics.rectangle("fill", startx+(i-1)*3+(a-1), 250, 1, 3)
			love.graphics.rectangle("fill", startx+(i-1)*14+(a-1)*4, 300, 3, 11)
		end
	end
	
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.print("Speed: " .. speed .. "x", 5, 5)
end

function love.keypressed(key, unicode)
	if key == "escape" then
		love.event.quit()
	end
end

function love.mousepressed(x, y, button)
	if button == "wd" then
		if speed <= 1 then
			speed = math.max(speed-.25, .25)
		else
			speed = speed-1
		end
	elseif button == "wu" then
		if speed < 1 then
			speed = speed+.25
		else
			speed = math.min(speed+1, 30)
		end
	elseif button == "m" then
		speed = 1
	end
end
What I did here was trying to make a white dot move from point A to point B, with small fading effects (to make the thing more smooth). This would be very easy on regular codes, but this time I wanted to take advantage of something I just realized: pixels are a combination of red, green and blue, no matter in what order they are. So what if I could control two side-by-side pixels and make them generate a white dot BETWEEN them? That's right, instead of going from pixel to pixel, the dot would be going from 1/3 of a pixel to 1/3 of a pixel, breaking the rules of "pixel as the minimum measure" thing. It doesn't looks so fancy, since most pixels are separated by a veeeery tiny space, but it's still a nice little effect!

Image

Re: Code Doodles!

Posted: Thu Oct 02, 2014 7:55 pm
by DaedalusYoung
Interesting idea. I did change line 61 to this:

Code: Select all

love.graphics.point(startx+(i-1) + 0.5, 200.5)
Otherwise, the pixels would not be drawn exactly on the pixel, making it look slightly different.

Re: Code Doodles!

Posted: Fri Oct 03, 2014 8:15 am
by HugoBDesigner
Oh, right, I forgot that points in love are drawn in pixel.5
Thanks for the heads up!

Re: Code Doodles!

Posted: Mon Oct 06, 2014 12:25 pm
by Zilarrezko
uh.... snake.


Re: Code Doodles!

Posted: Wed Oct 08, 2014 10:58 pm
by HugoBDesigner
Another doodle that actually evolved into a pretty good minigame: "Lights Off" (dunno if that's the game name, but you've probably played it already).
ppMzEgH.png
ppMzEgH.png (35.98 KiB) Viewed 4492 times
If you want you can use and edit and whatever. You're not required to credit me, but it'd be nice. Just don't say it was made by you :P

Controls:
  • Mouse left click to play (duh)
    Mouse Wheel to zoom in/out
    Ctrl+N to restart
    Ctrl+I to invert the screen
    Ctrl+A to click every tile (dunno why :P )

Re: Code Doodles!

Posted: Wed Oct 08, 2014 11:05 pm
by Zilarrezko
The original game was called Lights Out. Good stuff.

Re: Code Doodles!

Posted: Wed Oct 08, 2014 11:58 pm
by davisdude
I see you watch Coding Math.
Great videos. Very informative.

Re: Code Doodles!

Posted: Thu Oct 09, 2014 12:21 am
by HugoBDesigner
davisdude wrote:I see you watch Coding Math.
Great videos. Very informative.
Do you mean me? Because I never heard of this channel before :/

EDIT: It's a quite interesting channel, so I think I'll check it more often :)