Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
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 »

Kasperelo wrote:---
Nice! I see room for expansion!

I made the lamest thing ever.

Image

Code: Select all

--Code Doodle #9 by Daniël 'Sheepolution' Haazen

function love.load()
   math.randomseed(os.time()*love.mouse.getX()*love.mouse.getY())
   lines = {0,300,50,300}


   for i=1,20 do
      local tx = lines[#lines-1]
      local ty = lines[#lines]
      table.insert(lines,tx+20)
      table.insert(lines,ty-200+math.random(400))
      table.insert(lines,lines[#lines-1]+20)
      table.insert(lines,300)
   end


   y = 300
   x = 0
   timer = 0
end

function love.update(dt)
   x = x - 100 * dt
   timer = timer + 100 * dt
   if timer > 20 then
      local tx = lines[#lines-1]
      local ty = lines[#lines]
      table.insert(lines,tx+20)
      table.insert(lines,ty-200+math.random(400))
      table.insert(lines,lines[#lines-1]+20)
      table.insert(lines,300)
      timer = 0
   end
   


end

function love.draw()
   love.graphics.setLineWidth(1)
   love.graphics.setColor(0,200,0)
   for i=1,80 do
      i = i -1
      love.graphics.rectangle("line", 80*(i%10), 80*math.floor(i/10)-20, 80, 80)
   end
   love.graphics.setLineWidth(3)
   love.graphics.translate(x,0)
   love.graphics.setColor(0,255,0)
   for i,v in ipairs(lines) do
      love.graphics.line(lines)
   end

end
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Code Doodles!

Post by veethree »

Speaking of lame.
Image

Code: Select all

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

local origin = {
		x = screen.width / 2,
		y = screen.height / 2
	}
local x, y, angle = 0, 0, 0
local radius = 128
local speed = 0.5

local line = {}
local line_count = 4
local line_width = 1

--Top
for i=0, line_count do
	line[#line + 1] = {
			x = (screen.width / line_count) * i,
			y = 0,
			x2 = 0,
			y2 = 0
		}
end

--bottom
for i=0, line_count do
	line[#line + 1] = {
			x = (screen.width / line_count) * i,
			y = screen.height,
			x2 = 0,
			y2 = 0
		}
end


--left
for i=1, line_count-1 do
	line[#line + 1] = {
			x = 0,
			y = (screen.height / line_count) * i,
			x2 = 0,
			y2 = 0
		}
end

--right
for i=1, line_count-1 do
	line[#line + 1] = {
			x = screen.width,
			y = (screen.height / line_count) * i,
			x2 = 0,
			y2 = 0
		}
end

function love.load()
	love.graphics.setLineWidth(line_width)
	love.graphics.setBackgroundColor(0, 8, 16)
end

function love.update(dt)
	angle = angle + speed * dt
	if angle > (math.pi * 2) then angle = 0 end
	x = radius * math.cos(angle) + origin.x
	y = radius * math.sin(angle) + origin.y
	
	--Line update
	for i,v in ipairs(line) do
		v.x2 = x
		v.y2 = y
	end
	
	love.window.setTitle(love.timer.getFPS())
end

function ste()
	love.graphics.circle("fill", origin.x, origin.y, radius)
end

function love.draw()
	love.graphics.setStencil(ste)
	love.graphics.setColor(0, 16, 32)
	love.graphics.circle("fill", origin.x, origin.y, radius)
	
	love.graphics.setColor(0, 126, 255)
	love.graphics.circle("line", origin.x, origin.y, radius-1)
	for i,v in ipairs(line) do
		love.graphics.line(v.x, v.y, v.x2, v.y2)
	end
end

function love.keypressed(key)
	if key == "escape" then love.event.push("quit") 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 »

Mine is a bit big, but truly amazing, even if I do say so myself. This is my first doodle, so yeah.

"Controls":
- Mouse hold: reverts the magnetic force (repulses balls).
- Enter: Toggles economic mode: increases FPS (at least for me)
- Space bar: doubles the magnetic field and the magnetic force
- "up", "w" or "+": increases simulation speed (up to 5x faster)
- "down", "s" or "-": decreases simulation speed (up to 5x slower)

Code: Select all

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

function love.load()
	balls = {}
	local m = math.random(50, 150)
	for i = 1, m do
		balls[i] = makeBall()
	end
	
	table.sort(balls, function(a, b) return a.r > b.r end)
	
	friction = .99
	mousefield = 150
	mouseforce = 10
	
	color = 0
	bg1 = {0, 35, 55}
	
	econ = false
	simspeed = 1
	
	love.graphics.setLineWidth(2)
end

function love.update(ndt)
	local dt = ndt*simspeed
	local mx, my = love.mouse.getPosition()
	for i, v in pairs(balls) do
		if v.x < 50 then
			v.speedx = -v.speedx
			while v.x < 50 do
				v.x = v.x+1
			end
		end
		
		if v.x+2*v.r > 750 then
			v.speedx = -v.speedx
			while v.x+2*v.r > 750 do
				v.x = v.x-1
			end
		end
		
		if v.y < 50 then
			v.speedy = -v.speedy
			while v.y < 50 do
				v.y = v.y+1
			end
		end
		
		if v.y+2*v.r > 550 then
			v.speedy = -v.speedy
			while v.y+2*v.r > 550 do
				v.y = v.y-1
			end
		end
		
		v.x = v.x + dt*v.speedx
		v.y = v.y + dt*v.speedy
		
		local dx = mx-v.x-v.r
		local dy = my-v.y-v.r
		local dis = dist(mx, my, v.x-v.r, v.y-v.r)
		
		if dis <= mousefield then
			v.speedx = v.speedx + dx*mouseforce/dis
			v.speedy = v.speedy + dy*mouseforce/dis
		end
		
		--fricton
		v.speedx = v.speedx*friction
		v.speedy = v.speedy*friction
	end
	
	color = color+dt
	if color > math.pi then
		color = 0
	end
end

function love.draw()
	love.graphics.setBackgroundColor(math.sin(color)*bg1[1], math.sin(color)*bg1[2], math.sin(color)*bg1[3], 255)
	
	love.graphics.setColor(105, 105, 105, 255)
	love.graphics.rectangle("fill", 0, 0, 50, 600)
	love.graphics.rectangle("fill", 750, 0, 50, 600)
	love.graphics.rectangle("fill", 50, 0, 700, 50)
	love.graphics.rectangle("fill", 50, 550, 700, 550)
	love.graphics.setColor(55, 55, 55, 255)
	love.graphics.rectangle("line", 50, 50, 700, 500)
	
	local mx, my = love.mouse.getPosition()
	local amount = 10
	love.graphics.setColor(105, 255, 0, 10)
	if mouseforce < 0 then
		love.graphics.setColor(255, 105, 0, 10)
	end
	for i = 1, amount do
		local m = mousefield
		love.graphics.circle("fill", mx-(m/amount)*(i/amount)/2, my-(m/amount)*(i/amount)/2, m*(i/amount), 32)
	end
	
	for i, v in pairs(balls) do
		love.graphics.setColor(unpack(v.color))
		love.graphics.circle("fill", v.x+v.r, v.y+v.r, v.r, 32)
		if not econ then
			love.graphics.setColor(unpack(light(v.color, 205)))
			love.graphics.circle("line", v.x+v.r, v.y+v.r, v.r, 32)
		end
	end
	
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.print("Simulation speed: " .. simspeed .. "x", 5, 5)
	love.graphics.print("FPS: "..love.timer.getFPS(), 5, 575)
end

function light(t, d)
	local r, g, b, a = unpack(t)
	return {r/255*d, g/255*d, b/255*d, a}
end

function makeBall()
	local self = {}
	self.r = math.random(5, 25)
	self.x = math.random(50+self.r, 750-self.r)
	self.y = math.random(50+self.r, 550-self.r)
	self.speedx = math.random(100, 200)
	self.speedy = math.random(100, 200)
	self.color = {math.random(150, 255), math.random(150, 255), math.random(150, 255), 255}
	return self
end

function love.keypressed(k, u)
	if k == "escape" then
		love.event.quit()
	elseif k == "enter" or k == "return" or k == "kpenter" then
		econ = not econ
	elseif (k == "up" or k == "=" or k == "w") and simspeed < 5 then
		if simspeed < 1 then
			simspeed = simspeed+1/5
		else
			simspeed = simspeed+1
		end
	elseif (k == "down" or k == "-" or k == "s") and simspeed > 1/5 then
		if simspeed > 1 then
			simspeed = simspeed-1
		else
			simspeed = simspeed-1/5
		end
	elseif k == " " then
		mouseforce = mouseforce*2
		mousefield = mousefield*2
	end
end

function love.keyreleased(k, u)
	if k == " " then
		mouseforce = mouseforce/2
		mousefield = mousefield/2
	end
end

function love.mousepressed(x, y, button)
	mouseforce = -mouseforce
end

function love.mousereleased(x, y, button)
	mouseforce = -mouseforce
end

function dist(x1, y1, x2, y2)
	local w = math.abs(x1-x2)
	local h = math.abs(y1-y2)
	return math.sqrt(w^2+h^2)
end
Also, could someone please tell me why the game decreases the simulation speed to under 1/5 when I made it not do it?
2 screenshots at once. Niiiice :)
2 screenshots at once. Niiiice :)
Doodle1.png (47.2 KiB) Viewed 9248 times

EDIT: Added a few more things to this doodle:
- Fields are affected by "economic mode"

And added more "controls":
- "r": Restarts the doodle
- "f": Creates a copy of the force field of your mouse (position, force, field)
- "c": Clears all the copies of force fields you made

The new code:

Code: Select all

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

function love.load()
	balls = {}
	
	local m = math.random(50, 150)
	for i = 1, m do
		balls[i] = makeBall()
	end
	
	table.sort(balls, function(a, b) return a.r > b.r end)
	
	friction = .99
	mousefield = 150
	mouseforce = 10
	
	fields = {{x = 0, y = 0, force = mouseforce, field = mousefield}}
	
	color = 0
	bg1 = {0, 35, 55}
	
	econ = false
	simspeed = 1
	
	love.graphics.setLineWidth(2)
end

function love.update(ndt)
	local dt = ndt*simspeed
	local mx, my = love.mouse.getPosition()
	fields[1] = {x = mx, y = my, force = mouseforce, field = mousefield}
	for i, v in pairs(balls) do
		for j, w in pairs(fields) do
			if v.x < 50 then
				v.speedx = -v.speedx
				while v.x < 50 do
					v.x = v.x+1
				end
			end
			
			if v.x+2*v.r > 750 then
				v.speedx = -v.speedx
				while v.x+2*v.r > 750 do
					v.x = v.x-1
				end
			end
			
			if v.y < 50 then
				v.speedy = -v.speedy
				while v.y < 50 do
					v.y = v.y+1
				end
			end
			
			if v.y+2*v.r > 550 then
				v.speedy = -v.speedy
				while v.y+2*v.r > 550 do
					v.y = v.y-1
				end
			end
			
			local dx = w.x-v.x-v.r
			local dy = w.y-v.y-v.r
			local dis = dist(w.x, w.y, v.x-v.r, v.y-v.r)
			
			if dis <= w.field then
				v.speedx = v.speedx + dx*w.force/dis
				v.speedy = v.speedy + dy*w.force/dis
			end
		end
		v.x = v.x + dt*v.speedx
		v.y = v.y + dt*v.speedy
		--fricton
		v.speedx = v.speedx*friction
		v.speedy = v.speedy*friction
	end
	
	color = color+dt
	if color > math.pi then
		color = 0
	end
end

function love.draw()
	love.graphics.setBackgroundColor(math.sin(color)*bg1[1], math.sin(color)*bg1[2], math.sin(color)*bg1[3], 255)
	
	love.graphics.setColor(105, 105, 105, 255)
	love.graphics.rectangle("fill", 0, 0, 50, 600)
	love.graphics.rectangle("fill", 750, 0, 50, 600)
	love.graphics.rectangle("fill", 50, 0, 700, 50)
	love.graphics.rectangle("fill", 50, 550, 700, 550)
	love.graphics.setColor(55, 55, 55, 255)
	love.graphics.rectangle("line", 50, 50, 700, 500)
	
	local mx, my = love.mouse.getPosition()
	
	
	for j, w in pairs(fields) do
		local amount = 10
		if econ then
			amount = 5
		end
		love.graphics.setColor(105, 255, 0, 100/amount)
		if w.force < 0 then
			love.graphics.setColor(255, 105, 0, 100/amount)
		end
		
		for i = 1, amount do
			local m = w.field
			love.graphics.circle("fill", w.x-(m/amount)*(i/amount)/2, w.y-(m/amount)*(i/amount)/2, m*(i/amount), 32)
		end
	end
	
	for i, v in pairs(balls) do
		love.graphics.setColor(unpack(v.color))
		love.graphics.circle("fill", v.x+v.r, v.y+v.r, v.r, 32)
		if not econ then
			love.graphics.setColor(unpack(light(v.color, 205)))
			love.graphics.circle("line", v.x+v.r, v.y+v.r, v.r, 32)
		end
	end
	
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.print("Simulation speed: " .. simspeed .. "x", 5, 5)
	love.graphics.print("FPS: "..love.timer.getFPS(), 5, 575)
end

function light(t, d)
	local r, g, b, a = unpack(t)
	return {r/255*d, g/255*d, b/255*d, a}
end

function makeBall()
	local self = {}
	self.r = math.random(5, 25)
	self.x = math.random(50+self.r, 750-self.r)
	self.y = math.random(50+self.r, 550-self.r)
	self.speedx = math.random(100, 200)
	self.speedy = math.random(100, 200)
	self.color = {math.random(150, 255), math.random(150, 255), math.random(150, 255), 255}
	return self
end

function love.keypressed(k, u)
	if k == "escape" then
		love.event.quit()
	elseif k == "enter" or k == "return" or k == "kpenter" then
		econ = not econ
	elseif (k == "up" or k == "=" or k == "w") and simspeed < 5 then
		if simspeed < 1 then
			simspeed = simspeed+1/5
		else
			simspeed = simspeed+1
		end
	elseif (k == "down" or k == "-" or k == "s") and simspeed > 1/5 then
		if simspeed > 1 then
			simspeed = simspeed-1
		else
			simspeed = simspeed-1/5
		end
	elseif k == " " then
		mouseforce = mouseforce*2
		mousefield = mousefield*2
	elseif k == "r" then
		love.load()
	end
	
	if k == "f" then
		table.insert(fields, {x = love.mouse.getX(), y = love.mouse.getY(), force = mouseforce, field = mousefield})
	elseif k == "c" then
		fields = {fields[1]}
	end
end

function love.keyreleased(k, u)
	if k == " " then
		mouseforce = mouseforce/2
		mousefield = mousefield/2
	end
end

function love.mousepressed(x, y, button)
	mouseforce = -mouseforce
end

function love.mousereleased(x, y, button)
	mouseforce = -mouseforce
end

function dist(x1, y1, x2, y2)
	local w = math.abs(x1-x2)
	local h = math.abs(y1-y2)
	return math.sqrt(w^2+h^2)
end
And another screenshot:
Force fields. WHOAAA!!!
Force fields. WHOAAA!!!
Doodle1_2.png (59.87 KiB) Viewed 9245 times
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Rednas
Prole
Posts: 2
Joined: Tue Apr 08, 2014 1:02 pm

Re: Code Doodles!

Post by Rednas »

Image
i wanna be the very best... (any tips would always help) thanks. :o

Code: Select all

function love.load()
	thing = {{x=400,y=300,radius=1,color1 = math.random(255), color2 = math.random(255), color3 = math.random(255), alpha = 100}}
	segments = 50
	timeLoad = 0
end
 
function love.update(dt)
	timeLoad = timeLoad + dt
	for i,v in ipairs(thing) do
		v.radius = v.radius + 100 * dt
		if v.alpha < 0  then
			table.remove(thing,i)
		end
		v.alpha = v.alpha - 20 * dt
	end
	if timeLoad > 0.3 then
		table.insert(thing,{x=400,y=300,radius=1,color1 = math.random(255), color2 = math.random(255), color3 = math.random(255), alpha = 100})
		timeLoad = 0.2
	end
	if love.keyboard.isDown(" ") then
		timeLoad = 0.05
	end
end

function love.draw()
	for i,v in ipairs(thing) do
		love.graphics.setColor(v.color1,v.color2,v.color3,v.alpha)
		love.graphics.circle("fill", v.x, v.y, v.radius, segments)
	end
end 



User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Code Doodles!

Post by Ref »

Simple L_System doodle.
Sierpinski.jpg
Sierpinski.jpg (92.43 KiB) Viewed 9207 times
Attachments
L_System.love
Fractals
(2.92 KiB) Downloaded 321 times
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Code Doodles!

Post by DaedalusYoung »

#2: Cube

Image

Not the best coding, but it works :P

Left/right (or A/D) to change speed, down (or S) to go back to default speed, space to select new random colours.

Code: Select all

--[[ CODE DOODLE #2 by DaedalusYoung ]]--
--[[               Cube              ]]--

local colour = {
	left = { 1, 1, 1 },
	side = { 2, 2, 2 },
	right = { 3, 3, 3 },
	white = { 255, 255, 255 },
	black = { 0, 0, 0 },
	}
local width, height = 800, 600
local timer = 0
local direction = 10
local mathsin = math.sin
local mathcos = math.cos
local mathpi = math.pi

function colour.alpha(t, a)
	local r, g, b = unpack(t)
	a = a or 127
	return { r, g, b, a }
end

local function randomcolour()
	colour.left = { love.math.random(255), love.math.random(255), love.math.random(255) }
	colour.right = { love.math.random(255), love.math.random(255), love.math.random(255) }
	colour.side = { love.math.random(255), love.math.random(255), love.math.random(255) }
end

function love.load()
	width, height = love.window.getWidth(), love.window.getHeight()
	randomcolour()
end

function love.update(dt)
	if love.keyboard.isDown("left", "a") then
		direction = direction - 1
	elseif love.keyboard.isDown("right", "d") then
		direction = direction + 1
	end
	timer = timer + (direction * dt)
end

function love.draw()
	local xmax = (width / 40) + 1
	local ymax = (height / 40) - 2
	for x = 1, xmax do
		for y = 2, ymax do
			local xp = ((timer + (x * 40)) % (width + 40)) - 20
			local alpha = (xp + 32) / ((width + 64) / 255)
			local xh = (((xp - (width/2)) / xmax) ^ 3) / 70
			xp = xp + xh
			local yh = math.abs(xh) * ((y - (ymax / 2)) / 6)
			local sh = math.abs(xh) / 10
			love.graphics.setColor(colour.left)
			love.graphics.circle('fill', xp, (y * 40) + yh, 16 + sh, 16)
			love.graphics.setColor(colour.alpha(colour.right, alpha))
			love.graphics.circle('fill', xp, (y * 40) + yh, 16 + sh, 16)
		end
	end
	love.graphics.setColor(colour.black)
	local boxheight = 170
	local boxtimer = timer / 100
	local ax, bx, cx, dx = (width / 2) + (mathcos(boxtimer) * 128), (width / 2) + (mathcos(boxtimer + (mathpi / 2)) * 128), (width / 2) + (mathcos(boxtimer + mathpi) * 128), (width / 2) + (mathcos(boxtimer + (mathpi * 1.5)) * 128)
	local ay, by, cy, dy = (height / 2) + (mathsin(boxtimer) * 64) - 64, (height / 2) + (mathsin(boxtimer + (mathpi / 2)) * 64) - 64, (height / 2) + (mathsin(boxtimer + mathpi) * 64) - 64, (height / 2) + (mathsin(boxtimer + (mathpi * 1.5)) * 64) - 64
	love.graphics.polygon('fill', { ax, ay, bx, by, cx, cy, dx, dy})
	love.graphics.setColor(colour.alpha(colour.side, 191))
	love.graphics.polygon('fill', { ax, ay, bx, by, cx, cy, dx, dy})
	love.graphics.setColor(colour.white)
	love.graphics.polygon('line', { ax, ay, bx, by, cx, cy, dx, dy})
	if ax > bx then
		love.graphics.setColor(colour.black)
		love.graphics.polygon('fill', { ax, ay, bx, by, bx, by + boxheight, ax, ay + boxheight} )
		love.graphics.setColor(colour.alpha(colour.side, ax - bx))
		love.graphics.polygon('fill', { ax, ay, bx, by, bx, by + boxheight, ax, ay + boxheight} )
		love.graphics.setColor(colour.white)
		love.graphics.polygon('line', { ax, ay, bx, by, bx, by + boxheight, ax, ay + boxheight} )
	end
	if bx > cx then
		love.graphics.setColor(colour.black)
		love.graphics.polygon('fill', { bx, by, cx, cy, cx, cy + boxheight, bx, by + boxheight} )
		love.graphics.setColor(colour.alpha(colour.side, bx - cx))
		love.graphics.polygon('fill', { bx, by, cx, cy, cx, cy + boxheight, bx, by + boxheight} )
		love.graphics.setColor(colour.white)
		love.graphics.polygon('line', { bx, by, cx, cy, cx, cy + boxheight, bx, by + boxheight} )
	end
	if cx > dx then
		love.graphics.setColor(colour.black)
		love.graphics.polygon('fill', { cx, cy, dx, dy, dx, dy + boxheight, cx, cy + boxheight} )
		love.graphics.setColor(colour.alpha(colour.side, cx - dx))
		love.graphics.polygon('fill', { cx, cy, dx, dy, dx, dy + boxheight, cx, cy + boxheight} )
		love.graphics.setColor(colour.white)
		love.graphics.polygon('line', { cx, cy, dx, dy, dx, dy + boxheight, cx, cy + boxheight} )
	end
	if dx > ax then
		love.graphics.setColor(colour.black)
		love.graphics.polygon('fill', { dx, dy, ax, ay, ax, ay + boxheight, dx, dy + boxheight} )
		love.graphics.setColor(colour.alpha(colour.side, dx - ax))
		love.graphics.polygon('fill', { dx, dy, ax, ay, ax, ay + boxheight, dx, dy + boxheight} )
		love.graphics.setColor(colour.white)
		love.graphics.polygon('line', { dx, dy, ax, ay, ax, ay + boxheight, dx, dy + boxheight} )
	end
end

function love.keypressed(key)
	if key == "down" or key == "s" then
		direction = 10
	elseif key == " " then
		randomcolour()
	end
end
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Code Doodles!

Post by DaedalusYoung »

#3: Fish Tank

Use mousewheel up and down to change quality/width of the lightbeams. It looks best at width 1, but bigger beams work better with some fish swimming under most beams and some swim over most beams.

Image

Code: Select all

--[[ CODE DOODLE #3 by DaedalusYoung ]]--
--[[            Fish Tank            ]]--

local colour = {
	bg = { 63, 105, 127 },
	light = { 127, 181, 255 },
	white = { 255, 255, 255 },
	black = { 0, 0, 0 },
	}
local beams = {}
local img = {}
local fish = {}
local bubbles = {}
local width, height = 800, 600
local timer = 0
local spawntime = 0
local mathpi = math.pi
local mathsin = math.sin
local beamwidth = 10
local beamtimer = 5

function colour.alpha(t, a)
	local r, g, b = unpack(t)
	a = a or 127
	return { r, g, b, a }
end

function colour.darken(t, a)
	local r, g, b = unpack(t)
	a = a or 127
	a = a / 255
	return { r * a, g * a, b * a }
end

local function spawnfish()
	local x = love.math.random(2)
	local y = love.math.random(height - 60)
	local dir
	local shade = love.math.random(192, 255)
	local col = love.math.random(2)
	local scale = (love.math.random() + 0.3) * 2
	if x == 1 then
		x = -32
		dir = love.math.random(8, 20)
	else
		x = width + 32
		dir = love.math.random(-20, -8)
	end
	local r, g, b
	if col == 1 then
		r = love.math.random(192, 255)
		g = love.math.random(48, 127)
		b = love.math.random(63)
	else
		r = love.math.random(31)
		g = love.math.random(32, 127)
		b = love.math.random(192, 200)
	end
	y = y + 30
	table.insert(fish, { x = x, y = y, dir = dir, shade = shade, col = { r, g, b }, scale = scale } )
end

function love.load()
	width, height = love.window.getWidth(), love.window.getHeight()
	sw, sh = width / 256, height / 256
	love.graphics.setBackgroundColor(colour.bg)
	local imgdata = love.image.newImageData(256, 256)
	for y = 0, 255 do
		for x = 0, 255 do
			local col = (((255 - y) / 255) ^ 0.5) * 255
			local ran = (love.math.random() * 4) - 2
			col = col + ran -- dither to reduce banding
			if col < 0 then col = 0 elseif col > 255 then col = 255 end
			imgdata:setPixel(x, y, col, col, col, 255)
		end
	end
	img.blur = love.graphics.newImage(imgdata)
	for x = 1, width / beamwidth do
		beams[x] = love.math.noise(x / 10, timer) ^ 6
	end
	for x = 1, 5 do
		spawnfish()
	end
	for x = 1, 20 do
		table.insert(bubbles, { x = love.math.random(width), y = love.math.random(height), size = love.math.random(3, 7), speed = love.math.random(3, 10) })
	end
end

local function drawbeam(x)
	love.graphics.setColor(colour.alpha(colour.light, beams[x] * 127))
	love.graphics.polygon('fill', (x - 1) * beamwidth, 0, x * beamwidth, 0, ((x * beamwidth) * 2.4) - (width * 1.1), height, (((x - 1) * beamwidth) * 2.4) - (width * 1.1), height)
end

local function drawfish(t)
	local tailpoly = {}
	local fishpoly = { t.x - (16 * t.scale), t.y, t.x - (8 * t.scale), t.y - (5 * t.scale), t.x, t.y - (6 * t.scale), t.x + (8 * t.scale), t.y - (5 * t.scale),
					t.x + (16 * t.scale), t.y, t.x + (8 * t.scale), t.y + (5 * t.scale), t.x, t.y + (6 * t.scale), t.x - (8 * t.scale), t.y + (5 * t.scale) }
	if t.dir < 0 then
		tailpoly = { t.x + (16 * t.scale), t.y, t.x + (25 * t.scale), t.y - (4 * t.scale), t.x + (25 * t.scale), t.y + (4 * t.scale) }
	else
		tailpoly = { t.x - (16 * t.scale), t.y, t.x - (25 * t.scale), t.y - (4 * t.scale), t.x - (25 * t.scale), t.y + (4 * t.scale) }
	end
	love.graphics.setColor(colour.darken(t.col, t.shade))
	love.graphics.polygon('fill', fishpoly)
	love.graphics.polygon('fill', tailpoly)
	love.graphics.setColor(colour.alpha(colour.white))
	local finoffset = mathsin((t.x / 2)) / (3 * t.scale)
	if t.dir < 0 then
		love.graphics.arc('fill', t.x, t.y, 5 * t.scale, (mathpi / 8) + finoffset, ((mathpi / 4) * 2.5) + finoffset)
	else
		love.graphics.arc('fill', t.x, t.y, 5 * t.scale, ((mathpi / 4) * 1.5) - finoffset, ((mathpi / 4) * 3.5) - finoffset)
	end
	love.graphics.setColor(colour.black)
	if t.dir < 0 then
		love.graphics.circle('fill', t.x - (8 * t.scale), t.y - (3 * t.scale), t.scale, 6)
	else
		love.graphics.circle('fill', t.x + (8 * t.scale), t.y - (3 * t.scale), t.scale, 6)
	end
end

function love.update(dt)
	timer = timer + dt
	spawntime = spawntime + dt
	if beamtimer > 0 then
		beamtimer = beamtimer - dt
		if beamtimer < 0 then beamtimer = 0 end
	end
	if spawntime > 3.75 then
		spawntime = spawntime - 3.75
		if #fish < 30 then
			spawnfish()
		end
	end
	for x = 1, width / beamwidth do
		beams[x] = love.math.noise((x * beamwidth) / 120, timer / 3) ^ 6
	end
	if #fish > 0 then
		for x = #fish, 1, -1 do
			fish[x].x = fish[x].x + (dt * fish[x].dir)
			if fish[x].x < -64 or fish[x].x > width + 64 then
				table.remove(fish, x)
			end
		end
	end
	for x = 1, #bubbles do
		bubbles[x].y = bubbles[x].y - ((bubbles[x].speed * 5) * dt)
		if bubbles[x].y < -32 then
			bubbles[x] = { x = love.math.random(width), y = height + 32, size = love.math.random(3, 7), speed = love.math.random(3, 10) }
		end
	end
end

function love.draw()
	for x = 1, #beams, 3 do
		drawbeam(x)
	end
	love.graphics.setColor(colour.alpha(colour.white, 60))
	for x = 1, #bubbles, 2 do
		local xoffset = love.math.noise(bubbles[x].x, bubbles[x].y / 60) * 32
		love.graphics.circle('line', bubbles[x].x + xoffset, bubbles[x].y, bubbles[x].size)
	end
	for x = 1, #fish, 2 do
		drawfish(fish[x])
	end
	for x = 2, #beams, 3 do
		drawbeam(x)
	end
	for x = 2, #fish, 2 do
		drawfish(fish[x])
	end
	love.graphics.setColor(colour.alpha(colour.white, 80))
	for x = 2, #bubbles, 2 do
		local xoffset = love.math.noise(bubbles[x].x, bubbles[x].y / 60) * 32
		love.graphics.circle('line', bubbles[x].x + xoffset, bubbles[x].y, bubbles[x].size)
	end
	for x = 3, #beams, 3 do
		drawbeam(x)
	end
	love.graphics.setColor(colour.white)
	love.graphics.setBlendMode('multiplicative')
	love.graphics.draw(img.blur, 0, 0, 0, sw, sh)
	love.graphics.setBlendMode('alpha')
	local alpha = 255 * beamtimer
	if alpha > 255 then alpha = 255 end
	if alpha > 0 then
		love.graphics.setColor(colour.alpha(colour.white, alpha))
		love.graphics.print("width = " .. beamwidth .. "\n" .. love.timer.getFPS() .. " FPS", 16, 16)
	end
end

function love.mousepressed(x, y, btn)
	if btn == "wu" then
		beamwidth = math.floor(beamwidth + 1.1)
		beamtimer = 5
	elseif btn == "wd" then
		beamwidth = math.floor(beamwidth - 0.9)
		beamtimer = 5
	end
	if beamwidth < 1 then beamwidth = 1 elseif beamwidth > 50 then beamwidth = 50 end
end
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 »

Wow, these things are becoming more advanced than I expected. They all look really cool! I kinda stopped doing the daily thing. I'm out of ideas for now.
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 »

Not sure if you guys liked my last doodle, but this one, even though doesn't look really advanced, was pretty hard to make (for me).

"Controls":
Space bar: switches between inwards and outwards circle
"R": Sets random colors for the circles and background
"I": Switches the colors of the circle/backgrounds
"-": Decreases the mouse radius
"=": Increases the mouse radius
Enter: Changes the drawing mode

Code: Select all

--	LÖVECode Doodle #2
--		by HugoBDesigner

function love.load()
	mouseradius = 10
	balldefaultr = 6
	showin = true
	showout = true
	drawmode = false
	shows = 1
	
	balls = {}
	local mx, my = math.floor(800/(balldefaultr*2)), math.floor(600/(balldefaultr*2))
	for x = 1, mx do
		balls[x] = {}
		for y = 1, my do
			balls[x][y] = {r = 0}
		end
	end
	ballcolor = {255, 255, 255, 255}
	backcolor = {0, 0, 0, 255}
	love.graphics.setBackgroundColor(unpack(backcolor))
end

function love.update(dt)
	local x, y = love.mouse.getPosition()
	x = --[[math.floor(]]x/(balldefaultr*2)--)
	y = --[[math.floor(]]y/(balldefaultr*2)--)
	x = math.max(x+1, 1)
	y = math.max(y+1, 1)
	x = math.min(x, #balls*balldefaultr*2)
	y = math.min(y, #balls[1]*balldefaultr*2)
	for nx = 1, #balls do
		for ny = 1, #balls[nx] do
			local intensifier = 3--*(mouseradius/d)
			
			if drawmode then
				if dist(nx, ny, x, y) <= mouseradius*intensifier then
					local d = mouseradius-dist(nx, ny, x, y)
					balls[nx][ny].r = math.min((mouseradius/2)/d*intensifier, balldefaultr*2)
					if balls[nx][ny].r <= 0 then
						balls[nx][ny].r = balldefaultr*2
					end
				else
					balls[nx][ny].r = balldefaultr*2
				end
			else
				if dist(nx, ny, x, y) <= mouseradius*intensifier then
					local d = dist(nx, ny, x, y)
					balls[nx][ny].r = math.min((mouseradius/2)/d*intensifier, balldefaultr*2)
				else
					balls[nx][ny].r = 0
				end
			end
		end
	end
end

function love.draw()
	love.graphics.setColor(unpack(ballcolor))
	for x = 1, #balls do
		for y = 1, #balls[x] do
			local posx, posy = (x-1)*balldefaultr*2, (y-1)*balldefaultr*2
			local v = balls[x][y]
			
			if v.r > 0 then
				--love.graphics.setScissor(posx, posy, balldefaultr*2, balldefaultr*2)
				local nr, ng, nb, na = unpack(ballcolor)
				local m = math.min(v.r/balldefaultr, 1)
				love.graphics.setColor(nr, ng, nb, na*m)
				if showin then
					love.graphics.circle("fill", posx+balldefaultr, posy+balldefaultr, v.r, 32)
				end
				
				if showout then
					love.graphics.circle("line", posx+balldefaultr, posy+balldefaultr, v.r, 32)
				end
				--love.graphics.setScissor()
			end
		end
	end
end

function love.keypressed(key, unicode)
	if key == "escape" then
		love.event.quit()
	end
	
	if key == "-" then
		mouseradius = math.max(mouseradius-5, 5)
	elseif key == "=" then
		mouseradius = math.min(mouseradius+5, 100)
	end
	
	if key == "i" then
		local r, g, b, a = unpack(ballcolor)
		ballcolor = backcolor
		backcolor = {r, g, b, a}
		love.graphics.setBackgroundColor(unpack(backcolor))
	end
	
	if key == "r" then
		for i = 1, 3 do
			ballcolor[i] = math.random(255)
			backcolor[i] = math.random(255)
		end
		love.graphics.setBackgroundColor(unpack(backcolor))
	end
	
	if key == "enter" or key == "return" or key == "kpenter" then
		showin = false
		showout = false
		shows = shows+1
		if shows > 4 then shows = 1 end
		if shows == 1 or shows == 2 then
			showout = true
		end
		if shows == 1 or shows == 3 then
			showin = true
		end
	end
	
	if key == " " then
		drawmode = not drawmode
	end
end

function dist(x1, y1, x2, y2)
	local w = math.abs(x1-x2)
	local h = math.abs(y1-y2)
	return math.sqrt(w^2 + h^2)
end
Edited in MS Paint!!!
Edited in MS Paint!!!
img1.PNG (59.09 KiB) Viewed 9077 times
Doodle2_betterImage.png
Doodle2_betterImage.png (19.61 KiB) Viewed 9003 times
Last edited by HugoBDesigner on Tue Apr 22, 2014 1:41 am, edited 1 time in total.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Code Doodles!

Post by Ref »

This does something similar but not as nice.

Code: Select all

function love.load()
end
function love.update(dt)
	mouseX, mouseY = love.mouse.getPosition()
end
function love.draw()
	for j = 1, 600, 10 do
		for i=1, 800, 10 do
		r = fade( i, j, mouseX, mouseY )
		love.graphics.setColor(30*r,30*r,30*r)
		love.graphics.circle( 'fill', i, j, r )
		end
	end
end
function love.keypressed( key )
   if key == "escape" then love.event.quit() end
end
function fade( x1, y1, x2, y2 )
   local w = (x1-x2)
   local h = (y1-y2)
   return 20 / ( w^2 + h^2 ) ^ 0.29
end

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests