Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Code Doodles!

Post by veethree »

I make things like this all the time.
Here's a recent one:

Code: Select all

screen = {
		width = love.graphics.getWidth(),
		height = love.graphics.getHeight()
	}
	
local array = {}

--Settings
local amount = 32
local radius = 256
local rotation_speed = 2
local x = screen.width / 2
local y = screen.height / 2
local ball_radius = 0
local line_width = 12
local line_color = {0, 32, 64}
local ball_color = {0, 32, 64}
local background_color = {0, 16, 32}

function love.load()
	love.graphics.setBackgroundColor(background_color)
	for i=1, amount do
		local r = (radius / amount) * i
		local s = (rotation_speed / amount) * i
		local a = ((math.pi * 2) / amount) * i
		array[#array + 1] = {
				radius = r,
				speed = s,
				angle = a,
				x = radius * math.cos(a) + x,
				y = radius * math.sin(a) + y
			}
	end
end

function love.update(dt)
	for i,v in ipairs(array) do
		v.angle = v.angle + v.speed * dt
		if v.angle > (math.pi * 2) then v.angle = 0 end
		v.x = radius * math.cos(v.angle) + x
		v.y = radius * math.sin(v.angle) + y
	end
end

function love.draw()
	love.graphics.setBlendMode("additive")
	love.graphics.setLineWidth(line_width)
	for i,v in ipairs(array) do
		love.graphics.setColor(line_color)
		love.graphics.line(x, y, v.x, v.y)
		love.graphics.setColor(ball_color)
		love.graphics.circle("fill", v.x, v.y, ball_radius)
	end
end

function love.keypressed(key)
	if key == "escape" then love.event.push("quit") end
end
Image
(it rotates)
Attachments
doodle.love
.love for convenience.
(687 Bytes) Downloaded 227 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Sheepolution »

That looks awesome!

I failed my daily code doodle today :(

Edit: Decided to make another one, don't want to ruin my daily combo y'know.

#6!

Image

Code: Select all

--Code Doodle #6, Daniël 'Sheepolution' Haazen
function love.load()
	triangles = {}
	colors = {}

	for i=1,79 do
		table.insert(triangles,{x=300+math.cos(i/(math.pi*4))*250,y=300+math.sin(i/(math.pi*4))*250})
		table.insert(colors,{math.random(255),math.random(255),math.random(255)})
		timer = 0
	end

end

function love.update(dt)
	for i,v in ipairs(triangles) do
		local rad = math.atan2(v.y-300,v.x-300)
		v.x = v.x - math.sin(rad) * 200 * dt
		v.y = v.y + math.cos(rad) * 200 * dt
	end

	timer = timer - dt

	if timer < 0 then
		local temp = colors[1]
		table.remove(colors,1)
		table.insert(colors,temp)
		timer = 0.05
	end

end

function love.draw()
	local mx = love.mouse.getX()
	local my = love.mouse.getY()
	for i,v in ipairs(triangles) do
		love.graphics.setColor(unpack(colors[i]))
		love.graphics.translate(v.x, v.y)
		love.graphics.rotate(math.atan2(my-v.y,mx-v.x)+2.34)
		love.graphics.polygon("fill", 0, 0, 20, 10,10,20)
		love.graphics.origin()
		love.graphics.line(v.x,v.y,mx,my)
	end
end
Last edited by Sheepolution on Tue Apr 08, 2014 1:20 pm, edited 2 times in total.
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Code Doodles!

Post by Kasperelo »

Oh, and by the way, code doodles is a fantastic name. It fits, and when I google it I get no relevant results. :awesome:
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Code Doodles!

Post by Roland_Yonaba »

Kasperelo wrote:Oh, and by the way, code doodles is a fantastic name. It fits, and when I google it I get no relevant results. :awesome:
Code Doodle ?
Coodle :)

Fantastic thread.
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Sheepolution »

#7!

Image

Code: Select all

--Code Doodle #7 'Tetris' by Daniël 'Sheepolution' Haazen

function love.load()
	math.randomseed(os.time()*love.mouse.getX()*love.mouse.getY())
	tiles = {{0,0,0,0,2,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0},
			 {0,0,0,0,0,0,0,0,0,0,0}}

	timer = 1
	speed = 0


end

function love.update(dt)
	timer = timer - dt

	if timer < 0 then
		local falling = false
		for j=#tiles,1,-1 do
			for i,v in ipairs(tiles[j]) do
				print(v)
				if v > 0 then
					if j~=#tiles then
						if tiles[j+1][i]~=1 then
							tiles[j+1][i] = tiles[j][i]
							tiles[j][i] = 0
							falling = true
						else
							tiles[j][i] = 1
						end
					else
						tiles[j][i] = 1
					end
				end
			end
			if all(tiles[j],1) then
				tiles[j] = {0,0,0,0,0,0,0,0,0,0,0}
			end
		end
		if falling == false then
			tiles[1] = {0,0,0,0,0,0,0,0,0,0,0}
			tiles[1][math.random(#tiles[1])] = 2
		end
		timer = speed
	end
end

function love.keypressed(key)
	if key == "left" then
		for j,v in ipairs(tiles) do
			for i,t in ipairs(v) do
				if t == 2 then
					if i>1 then
						if tiles[j][i-1] ~= 1 then
							tiles[j][i-1] = 2
							tiles[j][i] = 0
							break
						end
					end
				end
			end
		end
	end

	if key == "right" then
		for j,v in ipairs(tiles) do
			for i,t in ipairs(v) do
				if t == 2 then
					if i<#v then
						if tiles[j][i+1] ~= 1 then
							tiles[j][i+1] = 2
							tiles[j][i] = 0
						break
						end
					end
				end
			end
		end
	end

end

function love.mousepressed(x, y, button)
	if button == "wu" then
		speed = speed + 0.01
	end
	if button == "wd" then
		speed = math.max(0,speed - 0.1)
	end
end

function love.draw()

	for j,v in ipairs(tiles) do
		for i,t in ipairs(v) do
			if t > 0 then
				love.graphics.rectangle("fill", 240+24*i, 24*j, 24, 24)
			else
				love.graphics.rectangle("line", 240+24*i, 24*j, 24, 24)
			end
		end
	end

end

function all(t,v)
	local a = true
	for i,b in ipairs(t) do
		if b~=v then
			a = false
		end
	end
	return a
end
Last edited by Sheepolution on Tue Apr 08, 2014 1:38 pm, edited 4 times in total.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Code Doodles!

Post by veethree »

Here's one that is definitely not in any way inspired by Sheepolution's tetris doodle.

Image

Code: Select all

screen = {
		width = love.graphics.getWidth(),
		height = love.graphics.getHeight()
	}

local array = {}
local cells = {}
local grid = {
		width, height,
		cell_size = 8
	}
local tick = 0
local update_rate = 30 --Updates per second

local show_fps = false
local spawn = true

	
function love.load()
	grid.width = math.floor(screen.width / grid.cell_size)
	grid.height = math.floor(screen.height / grid.cell_size)
	for y=0, grid.height-1 do
		array[y] = {}
		for x=0, grid.width-1 do
			array[y][x] = false
		end
	end
end

function love.update(dt)
	tick = tick + dt
	if tick > (1 / update_rate) then
		if spawn then
			local r = love.math.random(0, grid.width)
			array[0][r] = true
		end
		spawn = not spawn	
		
		cells = {}
		for y=0, grid.height-1 do
			for x=0, grid.width-1 do
				if array[y][x] then
					cells[#cells + 1] = {
							x = x,
							y = y
						}
				end
			end
		end
		if #cells > 0 then
			for i,v in ipairs(cells) do
				if (v.y + 1) < grid.height then
					if not array[v.y + 1][v.x] then
						array[v.y][v.x] = false
						array[v.y + 1][v.x] = true
					end
				end
			end
		end
		
		tick = 0
	end

end

function love.draw()
	for y=0, grid.height-1 do
		for x=0, grid.width-1 do
			love.graphics.setColor(0, 32, 64)
			if array[y][x] then
				love.graphics.setColor(255, 255, 255)
			end
			love.graphics.rectangle("fill", x * grid.cell_size, y * grid.cell_size, grid.cell_size-1, grid.cell_size-1)
		end
	end
	if show_fps then
		love.graphics.setColor(180, 0, 0)
		love.graphics.print(love.timer.getFPS(), 12, 12)
	end
end

function love.keypressed(key)
	if key == "escape" then love.event.push("quit") end
	if key == "tab" then show_fps = not show_fps end
end
There are probably better ways to do many things i did here, But i gave myself a launch brake (~40 minutes) to make this work. So efficiency and clean code weren't a priority.
Attachments
snow.love
.love for convenience.
(790 Bytes) Downloaded 237 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Sheepolution »

veethree wrote:Here's one that is definitely not in any way inspired by Sheepolution's tetris doodle.
Haha, I'm not sure if you're sarcastic or not.

Looks lovely, I tried to make it big as well, but it start lagging for me. Probably bad use of for loops.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Code Doodles!

Post by veethree »

Sheepolution wrote:
veethree wrote:Here's one that is definitely not in any way inspired by Sheepolution's tetris doodle.
Haha, I'm not sure if you're sarcastic or not.

Looks lovely, I tried to make it big as well, but it start lagging for me. Probably bad use of for loops.
wee need a [sarcasm][/sarcasm]tag or something. But yeah, that was definitely not sarcasm, There's absolutely no way i saw your thing and wanted to make something similar.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Code Doodles!

Post by Germanunkol »

[/sarcasm] :D


... Nice thread! Looking cool, keep posting, I'll keep watching :D
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Sheepolution »

veethree wrote:
Sheepolution wrote:
veethree wrote:Here's one that is definitely not in any way inspired by Sheepolution's tetris doodle.
Haha, I'm not sure if you're sarcastic or not.

Looks lovely, I tried to make it big as well, but it start lagging for me. Probably bad use of for loops.
wee need a [sarcasm][/sarcasm]tag or something. But yeah, that was definitely not sarcasm, There's absolutely no way i saw your thing and wanted to make something similar.
Haha sorry, now that I look back it's obvious sarcasm.

#8!

Image

This one has some more potential. Lower the starting size, and put the commented part in the bottom in the updater, for another effect.

Code: Select all

function love.load()
	math.randomseed(os.time())
	balls = {{x=400,y=300,size=10,active=false}}
	colors = {{0,0,0}}


	for i=1,1000 do
		local dis = 1000 + math.random(1000)
		local ang = math.rad(math.random(360))
		table.insert(balls,{x=400+math.cos(ang)*dis,y=300+math.sin(ang)*dis,size=30+math.random(30),active=true})
		table.insert(colors,{math.random(255),math.random(255),math.random(255)})
	end



end

function love.update(dt)

	for i,v in ipairs(balls) do
		if v.active then
			local ang = math.atan2(balls[1].y-v.y,balls[1].x-v.x)
			v.x = v.x + math.cos(ang) * 400 * dt
			v.y = v.y + math.sin(ang) * 400 * dt
			v.size = math.max(3,v.size - 18 * dt)

			if getDistance(v,balls[1])<10 then
				local dis = 1000 + math.random(1000)
				local ang = math.rad(math.random(360))
				v.x=400+math.cos(ang)*dis
				v.y=300+math.sin(ang)*dis
				v.size = 30+math.random(30)
			end
		end
	end



end

function love.draw()
	for i,v in ipairs(balls) do
		print(unpack(colors[i]))
		love.graphics.setColor(unpack(colors[i]))
		love.graphics.circle("fill", v.x, v.y, v.size, 25)
	end
	love.graphics.setColor(0,0,0)
	love.graphics.circle("fill", balls[1].x, balls[1].y, balls[1].size, 25)


end

function getDistance(a,b)
	return math.sqrt((a.x-b.x)^2+(a.y-b.y)^2)
end

function all(t,a)
	for i,v in ipairs(t) do
		if v ~= a then
			return false
		end
	end
	return true
end

	-- for i,v in ipairs(balls) do
	-- 	if v.active then
	-- 		local ang = math.atan2(balls[1].y-v.y,balls[1].x-v.x)
	-- 		v.x = v.x + math.cos(ang) * 400 * dt
	-- 		v.y = v.y + math.sin(ang) * 400 * dt

	-- 		if i~=1 then
	-- 			for j,w in ipairs(balls) do
	-- 				if v~=w then
	-- 					if getDistance(v,w) < v.size+w.size then
	-- 						if w.active == false then
	-- 							v.active = false
	-- 						end
	-- 					end
	-- 				end
	-- 			end
	-- 		end
	-- 	end
	-- end

Last edited by Sheepolution on Tue Apr 08, 2014 1:05 pm, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests