Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post 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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
artofwork
Citizen
Posts: 91
Joined: Mon Sep 15, 2014 1:17 am
Location: East Coast USA

Re: Code Doodles!

Post 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
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post 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
Last edited by HugoBDesigner on Fri Oct 03, 2014 8:16 am, edited 1 time in total.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Code Doodles!

Post 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.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post by HugoBDesigner »

Oh, right, I forgot that points in love are drawn in pixel.5
Thanks for the heads up!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Code Doodles!

Post by Zilarrezko »

uh.... snake.

User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post 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 4140 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 )
Attachments
Lights Off.love
Version 1.1
(2.2 KiB) Downloaded 162 times
Last edited by HugoBDesigner on Thu Oct 09, 2014 2:37 pm, edited 1 time in total.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Code Doodles!

Post by Zilarrezko »

The original game was called Lights Out. Good stuff.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Doodles!

Post by davisdude »

I see you watch Coding Math.
Great videos. Very informative.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post 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 :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Majestic-12 [Bot] and 3 guests