Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
CrackedP0t
Citizen
Posts: 69
Joined: Wed May 07, 2014 4:01 am
Contact:

Re: Code Doodles!

Post by CrackedP0t »

This is something inspired by one of the first things I ever did in Love.
It's simple, but it looks pretty cool.

Code: Select all

-- CrackedP0t's First Code Doodle

-- Arrow keys to maneuver, space for new rectangles

function makerects()
    rects = {}
    for i=1, math.random(10,20) do
        table.insert(rects, {x = math.random(0-winw, winw*2), y = math.random(0-winh, winh*2), width = math.random(20, 200), height = math.random(20, 200), color = {math.random(255),math.random(255),math.random(255),255}})
    end
end

function love.load()
    love.window.setTitle("CrackedP0t's First Code Doodle")
    winh = love.window.getHeight()
    winw = love.window.getWidth()
    love.window.setMode(winw, winh, {resizable = true})
    camera = {x = 0, y = 0}
    math.randomseed(os.time())
    makerects()
end

function love.update(dt)
    winh = love.window.getHeight()
    winw = love.window.getWidth()
    if love.keyboard.isDown(" ") then makerects() end
    if love.keyboard.isDown("left") then camera.x = camera.x + 10 end
    if love.keyboard.isDown("right") then camera.x = camera.x - 10 end
    if love.keyboard.isDown("up") then camera.y = camera.y + 10 end
    if love.keyboard.isDown("down") then camera.y = camera.y - 10 end
end

function love.draw()
    love.graphics.translate(camera.x, camera.y)
    for _, rect in ipairs(rects) do
        love.graphics.setColor(rect.color)
        love.graphics.rectangle("line", rect.x, rect.y, rect.width, rect.height)
        love.graphics.line(winw/2 - camera.x,winh/2 - camera.y, rect.x, rect.y)
        love.graphics.line(winw/2 - camera.x,winh/2 - camera.y, rect.x + rect.width, rect.y)
        love.graphics.line(winw/2 - camera.x,winh/2 - camera.y, rect.x + rect.width, rect.y + rect.height)
        love.graphics.line(winw/2 - camera.x,winh/2 - camera.y, rect.x, rect.y + rect.height)
    end
end
Attachments
first.love
(636 Bytes) Downloaded 231 times
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
gestaltist
Prole
Posts: 49
Joined: Thu May 29, 2014 10:56 am

Re: Code Doodles!

Post by gestaltist »

Hi everyone.

This thread has a lot of cool stuff! A great set of examples to learn from.

I have decided to add my own. This is my second ever program written in Love, so if you have any comments on how to do it better: I am open.

This doodle is a showcase for simple lighting using the alpha channel.

E and R change the size of the squares, D and F change the radius of the light.

Enjoy.
lighting doodle.png
lighting doodle.png (85.02 KiB) Viewed 5938 times
(Edit: fixed a few minor bugs for border cases.)
Attachments
lighting.love
(750 Bytes) Downloaded 414 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 »

:D

Code: Select all

--Code Doodle #10 by Daniël 'Sheepolution' Haazen
--Scroll up: More balls
--Scroll down: Less balls
--Left mouse: Increase grow speed
--Right mouse: Decrease grow speed

function love.load()
	balls = {}
	for i=1,100 do
		table.insert(balls,{x=math.random(800),y=math.random(600),size=0,dir=1,color=rndColor()})
	end
	growspeed = 10
end

function rndColor()
	return {math.random(255),math.random(255),math.random(255)}
end


function love.update(dt)
	if love.mouse.isDown("l") then
		growspeed = growspeed + 10 * dt
	end

	if love.mouse.isDown("r") then
		growspeed = math.max(1,growspeed - 10 * dt)
	end

	for i,v in ipairs(balls) do
		v.size = v.size + growspeed * dt * v.dir
		for i,w in ipairs(balls) do
			if v ~= w then
				
				if v.size < 0 then
					v.dir = 1
					v.x = math.random(800)
					v.y = math.random(600)
					v.color = rndColor()
					break
				end

				if collide(v,w) then
					v.dir = -1
				end
			end
		end
	end
end


function love.draw()
	for i,v in ipairs(balls) do
		love.graphics.setColor(unpack(v.color))
		love.graphics.circle("fill",v.x,v.y,v.size,25)
	end
end


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

function love.mousepressed(x,y,button)
	if button == "wu" then
		table.insert(balls,{x=math.random(800),y=math.random(600),size=0,dir=1,color=rndColor()})
		table.insert(balls,{x=math.random(800),y=math.random(600),size=0,dir=1,color=rndColor()})
		table.insert(balls,{x=math.random(800),y=math.random(600),size=0,dir=1,color=rndColor()})
	end
	if button == "wd" then
		table.remove(balls,1)
		table.remove(balls,1)
		table.remove(balls,1)
	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 »

I tried to make a little minigame sort of thing where you throw balls from a cannon and they collide with the walls and between themselves. The problem is that the angle-related code I wrote is probably VERY wrong, so you have now a general-buggy-physics app. Enjoy... I think...

Code: Select all

--	LÖVE Code Doodle #7
--	by HugoBDesigner
--	Completely failed :P

function love.load()
	wallsize = 20
	spawnx = 400
	spawny = 300
	shotangle = 0
	
	minspeed = 1000
	maxspeed = 1500
	minsize = 10
	maxsize = 30
	minlifetime = 5
	maxlifetime = 20
	
	shotimg = love.image.newImageData(shoot.width, shoot.height)
	for x = 0, shoot.width-1 do
		for y = 0, shoot.height-1 do
			local v = x*shoot.width+y+1
			local r, g, b, a = unpack(shoot.pixels[v])
			shotimg:setPixel(x, y, r, g, b, a)
		end
	end
	shotimg = love.graphics.newImage(shotimg)
	
	balls = {}
	toremove = {}
	
	love.graphics.setLineWidth(2)
end

function love.update(dt)
	local x, y = love.mouse.getPosition()
	shotangle = convertAngle(math.atan2(y-spawny, x-spawnx))
	
	for i, v in ipairs(toremove) do
		table.remove(balls, v)
	end
	
	toremove = {}
	
	for i, v in ipairs(balls) do
		--Collisions with walls (at least these work)
		if v.x-v.size < wallsize then
			v.speedx = -v.speedx
			v.x = wallsize+v.size
		elseif v.x+v.size > 800-wallsize then
			v.speedx = -v.speedx
			v.x = 800-wallsize-v.size
		end
		
		if v.y-v.size < wallsize then
			v.speedy = -v.speedy
			v.y = wallsize+v.size
		elseif v.y+v.size > 600-wallsize then
			v.speedy = -v.speedy
			v.y = 600-wallsize-v.size
		end
		
		--Collisions between balls
		for j, w in pairs(balls) do
			if j ~= i then
				if dist(v.x, v.y, w.x, w.y) < v.size+w.size then
					local toadd = (dist(v.x, v.y, w.x, w.y) - v.size+w.size)
					local spd1 = convertAngle(math.atan2(v.speedy, v.speedy))
					local spd2 = convertAngle(math.atan2(w.speedy, w.speedy))
					v.speedx = -speedX(v.speed, spd1)
					v.speedy = -speedY(v.speed, spd1)
					w.speedx = -speedX(w.speed, spd2)
					w.speedy = -speedY(w.speed, spd2)
					v.x = v.x + speedX(toadd/2, spd1)
					v.y = v.y + speedY(toadd/2, spd1)
					w.x = w.x + speedX(toadd/2, spd2)
					w.y = w.y + speedY(toadd/2, spd2)
				end
			end
		end
		
		--Speed setting
		v.x = v.x+ dt*v.speedx
		v.y = v.y+ dt*v.speedy
		
		v.lifetime = v.lifetime - dt
		if v.lifetime <= 0 then
			v.lifetime = 0
			table.insert(toremove, i)
		end
	end
end

function love.draw()
	--Walls
	love.graphics.setColor(255*.5, 255*.5, 255*.5, 255)
	love.graphics.rectangle("fill", 0, 0, wallsize, 600)
	love.graphics.rectangle("fill", 800-wallsize, 0, wallsize, 600)
	love.graphics.rectangle("fill", wallsize, 0, 800-2*wallsize, wallsize)
	love.graphics.rectangle("fill", wallsize, 600-wallsize, 800-2*wallsize, wallsize)
	
	--Balls
	for i, v in pairs(balls) do
		local alpha = 255
		if v.lifetime <= 1 then
			alpha = 255*v.lifetime
		end
		love.graphics.setColor(v.color[1], v.color[2], v.color[3], alpha)
		love.graphics.circle("fill", v.x, v.y, v.size, 32)
		love.graphics.setColor(v.color[1]*.5, v.color[2]*.5, v.color[3]*.5, alpha)
		love.graphics.circle("line", v.x, v.y, v.size, 32)
	end
	
	--Shooter
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.draw(shotimg, spawnx, spawny, shotangle, 1, 1, shotimg:getWidth()/2, shotimg:getHeight()/2)
end

function love.mousepressed(x, y, button)
	if button == "l" then
		spawnball()
	end
end

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

function convertAngle(a) --Probably wrong
	local a = a
	a = math.pi-a
	return math.offset(math.abs(a)+math.pi/2, 0, math.pi*2, math.pi*2)
end

function math.offset(n, mn, mx, f)
	local n = n
	local f = f or mx
	if math.abs(mn-mx) > f then f = mx end
	while n >= mx do
		n = n - f
	end
	while n < mn do
		n = n + f
	end
	return n
end

function speedX(angle, speed)
	return -math.sin(angle)*speed
end

function speedY(angle, speed)
	return math.cos(angle)*speed
end

function randomColor()
	return {math.random(155, 255), math.random(155, 255), math.random(155, 255)}
end

function spawnball()
	local b = {}
	b.x = spawnx
	b.y = spawny
	b.speed = math.random(minspeed, maxspeed)
	b.speedx = speedX(b.speed, shotangle)
	b.speedy = speedY(b.speed, shotangle)
	b.lifetime = math.random(minlifetime, maxlifetime)
	b.size = math.random(minsize, maxsize)
	b.color = randomColor()
	table.insert(balls, b)
end

function dist(x1, y1, x2, y2, ab)
	local ab = ab or true
	local w, h = x1-x2, y1-y2
	if ab then
		w = math.abs(w)
		h = math.abs(h)
	end
	return math.sqrt(w^2+h^2)
end
Also, paste THIS at the end (it's the code to load the image. It's too big, thus I uploaded it separately, but runs fine with the code)...


If anyone can fix it, feel free to take this over.
Captura de tela 2014-06-04 10.53.15.png
Captura de tela 2014-06-04 10.53.15.png (10.55 KiB) Viewed 5856 times
@HugoBDesigner - Twitter
HugoBDesigner - Blog
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 »

Just for testing and learning, I made an app to draw a simple line. I had to write a code to detect where the points are and set the pixels between them. Here it is:

Controls:
WASD: Controls the red end
Up/Down/Left/Right: Controls the blue end
Enter: Toggles end colors
-/+: Decrease/Increase the scale

Code: Select all

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

function love.load()
	timer = 0
	scale = 4
	maxscale = 8
	showdata = true
	wW = math.ceil(800/scale)
	wH = math.ceil(600/scale)
	x1 = math.floor(200/scale)
	y1 = math.floor(200/scale)
	x2 = math.floor(600/scale)
	y2 = math.floor(400/scale)
	accel = .25
	maxtimer = 1/(-scale+maxscale+1) * accel
	pointsBetween = {}
	getPoints()
end

down = love.keyboard.isDown
function love.update(dt)
	local hadDown = false
	if down("w") or down("s") or down("a") or down("d") then
		hadDown = true
	elseif down("up") or down("down") or down("left") or down("right") then
		hadDown = true
	end
	
	if hadDown then
		timer = timer + dt
		if timer >= maxtimer then
			timer = 0
			if down("w") then
				y1 = math.max(0, y1-1)
			end
			if down("s") then
				y1 = math.min(wH-1, y1+1)
			end
			if down("a") then
				x1 = math.max(0, x1-1)
			end
			if down("d") then
				x1 = math.min(wW-1, x1+1)
			end
			if down("up") then
				y2 = math.max(0, y2-1)
			end
			if down("down") then
				y2 = math.min(wH-1, y2+1)
			end
			if down("left") then
				x2 = math.max(0, x2-1)
			end
			if down("right") then
				x2 = math.min(wW-1, x2+1)
			end
			getPoints()
		end
	else
		timer = 0
	end
end

function love.keypressed(key, unicode)
	if key == "escape" then
		love.event.quit()
	elseif key == "-" then
		scale = math.max(1, scale-1)
		refreshScale()
	elseif key == "=" then
		scale = math.min(maxscale, scale+1)
		refreshScale()
	elseif key == "enter" or key == "return" or key == "kpenter" then
		showdata = not showdata
	else
		local updates  = true
		if key == "w" then
			y1 = math.max(0, y1-1)
		elseif key == "s" then
			y1 = math.min(wH-1, y1+1)
		elseif key == "a" then
			x1 = math.max(0, x1-1)
		elseif key == "d" then
			x1 = math.min(wW-1, x1+1)
		elseif key == "up" then
			y2 = math.max(0, y2-1)
		elseif key == "down" then
			y2 = math.min(wH-1, y2+1)
		elseif key == "left" then
			x2 = math.max(0, x2-1)
		elseif key == "right" then
			x2 = math.min(wW-1, x2+1)
		else
			updates = false
		end
		if updates then
			getPoints()
		end
	end
end

function getPoints()
	pointsBetween = {}
	local w, h = math.abs(x1-x2), math.abs(y1-y2)
	local dist = math.max(w, h)
	local distDir = "h"
	if h > w then
		distDir = "v"
	end
	for i = 1, dist do
		local px, py
		local factx, facty = 1, 1
		if distDir == "h" then
			px = i
			py = math.floor((i/w)*h)
		else
			px = math.floor((i/h)*w)
			py = i
		end
		if x1 > x2 then factx = -1 end
		if y1 > y2 then facty = -1 end
		table.insert(pointsBetween, {x1+px*factx, y1+py*facty})
	end
end

function love.draw()
	love.graphics.setColor(255, 255, 255, 255)
	for i, v in ipairs(pointsBetween) do
		love.graphics.rectangle("fill", v[1]*scale, v[2]*scale, scale, scale)
	end
	if showdata then
		love.graphics.setColor(255, 0, 0, 155)
		love.graphics.rectangle("fill", (x1-1)*scale, (y1-1)*scale, scale*3, scale*3)
		love.graphics.setColor(0, 0, 255, 155)
		love.graphics.rectangle("fill", (x2-1)*scale, (y2-1)*scale, scale*3, scale*3)
	end
end

function refreshScale()
	wW = math.ceil(800/scale)
	wH = math.ceil(600/scale)
	if x1 > wW-1 then x1 = wW-1 end
	if x2 > wW-1 then x2 = wW-1 end
	if y1 > wH-1 then y1 = wH-1 end
	if y2 > wH-1 then y2 = wH-1 end
	maxtimer = 1/(-scale+maxscale+1) * accel
	getPoints()
end
Captura de tela 2014-06-06 10.21.59.png
Captura de tela 2014-06-06 10.21.59.png (1.81 KiB) Viewed 5803 times
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: Code Doodles!

Post by Zarty55 »

I was playing around with bezier curves and inspired in this topic I come up with this. A little pretty animated wavy drawing thingy haha.

The code is pretty ugly, I tried to put some stuff in the top. Feel free to play with the code :D

Image
Image

Code: Select all

function setUpReferences()
 	la = love.audio
	le = love.event
	lf = love.filesystem
	lg = love.graphics
	li = love.image
	lj = love.joystick
	lk = love.keyboard
	lma = love.math
	lm = love.mouse
	lp = love.physics
	lso = love.sound
	lsy = love.system
	lth = love.thread
	lti = love.timer
	lw = love.window
end

function love.load(arg)
  setUpReferences()
  time = 0
  timeLimitU = 0.99
  timeLimitD = 0.01
  toggle = 1
  
  points = 60 -- amount of points
  
  --spiral
  spiralRadius = 300
  spirals = 8
  spiralVariation = 2
  spiralX, spiralY = lg.getWidth()/2, lg.getHeight()/2
  spiralDecrement = (spiralRadius-40)/(points)
  
  --Wave
  waveLimitX = 800
  waveDistance = 200
  waveOffY = 100
  waveCenterY = lg.getHeight()/2
  
  colors = {{math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(2, 10)/10},
    {math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(5, 20)}}
  radius = {32}
  
  curveTypes = {"wave", "random", "spiral"}
  currType = 1
  
  generatePoints(curveTypes[currType])
  
  math.randomseed(os.time())
end

function love.draw()
  lg.setColor(255, 0, 0)
  local text = "Type: " .. curveTypes[currType] .. "\nTime: " .. time
  lg.print(text)
  
  lg.setLineWidth(4)
  local point, total = evaluateBezier(time, lines)
  lg.setBlendMode("additive")
  for i = 2, #total do
    lg.setColor(colors[1])
    drawMiddlePoints(time, total[i], radius[1])
    lg.setColor(colors[2])
    drawMiddleLines(time, total[i])
  end
end

function love.update(dt)
  if time > timeLimitU then
    time = timeLimitU
    toggle = -1
    --timeLimitD = math.random(0, timeLimitU*1000)/1000
    colors[1] = {math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(10, 20)/10}
    radius[1] = math.random(8, 48)
  end
  if time < timeLimitD then
    time = timeLimitD
    toggle = 1
    --timeLimitU = math.random(timeLimitD*1000, 1000)/1000
    colors[2] = {math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(5, 20)}
    radius[1] = math.random(8, 48)
  end
  
  local speed = 1-math.abs((time - 0.5)*2)
  local mul = 1+speed*80
  
  time = time + ((dt*toggle)/(points))*mul
end

function love.keypressed(key, unicode)
  
  
  if key == "p" or key == "o" then
    if key == "o" then
      currType = currType + 1
      if currType > #curveTypes then
        currType = currType - #curveTypes
      end
    end
    
    lines = {}
    generatePoints(curveTypes[currType])
  end
end
  
  

function pointInLine(t, line)
  local lx = line[3]-line[1]
  local ly = line[4]-line[2]
  lx = lx*t+line[1]
  ly = ly*t+line[2]
  
  return {lx, ly}
end

function evaluateBezier(t, points, total)
  total = total or {}
  total[#total+1] = {}
  
  for i = 1, #points do
    table.insert(total[#total], points[i])
  end
  --print(points)
  if #points > 2 then
    local nP = {}
    for i = 1, #points-2, 2 do
      nP[i], nP[i+1] = unpack(pointInLine(t, {points[i], points[i+1], points[i+2], points[i+3]}))
    end
    return evaluateBezier(t, nP, total)
  else
    return points, total
  end
end

function drawMiddlePoints(t, points, size)
  for i = 1, #points, 2 do
    local x, y = points[i], points[i+1]--unpack(pointInLine(t, {points[i], points[i+1], points[i+2], points[i+3]}))
    lg.circle("fill", x, y, size)
  end
end

function drawMiddleLines(t, points)
  for i = 1, #points-2, 2 do
    lg.line(points[i], points[i+1], points[i+2], points[i+3])
  end
end

function generatePoints(type)
  lines = {}
  
  if type == "random" then
  
  --random
      for i = 1, points do
        table.insert(lines, math.random(80, lg.getWidth()-80))
        table.insert(lines, math.random(80, lg.getHeight()-80))
      end
  
  
  elseif type == "spiral" then
  
  --spiral
      local totalSpiral = (spiralRadius-40)/spirals
      for i = spiralRadius, 40, -spiralDecrement do
        local angle = ((i+math.random(-spiralVariation, spiralVariation))*spirals)%360
        table.insert(lines, spiralX+math.sin(math.rad(angle))*i)
        table.insert(lines, spiralY+math.cos(math.rad(angle))*i)
      end
  
  
  elseif type == "wave" then
  
  --wave
      local waveLength = waveLimitX/points
      local top = 1
      for i = 1, points do
        local x = waveLength*i
        local y = waveCenterY + (waveDistance+math.random(-waveOffY, waveOffY))*top
        table.insert(lines, x)
        table.insert(lines, y)
        top = top * -1
      end
      
      local x, y = 0, 0
      for i = 1, #lines, 2 do
        lines[i], lines[i+1] = lines[i]+x, lines[i+1]+y
      end
      
  end
end
P = Generates new points
O = Change type of generation (There are only 3: random, spiral, waves <3)
Attachments
bezier.love
(2.33 KiB) Downloaded 240 times
User avatar
alesan99
Citizen
Posts: 53
Joined: Sun Feb 09, 2014 3:13 am
Contact:

Re: Code Doodles!

Post by alesan99 »

I wanted the sky in my game to look less bland.
Instead of using an image I made a gradient function.

Code: Select all

function love.graphics.gradient(d, x, y, w, h, s, c1, c2)
	--By Alesan99
	local color
	local colors = {}
	for i = 1, s do
		color = {}
		for i2 = 1, 4 do
			if (c1[i2] or 255) == (c2[i2] or 255) then
				color[i2] = (c1[i2] or 255)
			elseif (c1[i2] or 255) > (c2[i2] or 255) then
				color[i2] = math.min(255, (c2[i2] or 255)+(((c1[i2]-c2[i2] or 255)/s)*(s-(i-1))))
			else
				color[i2] = math.max(0, (c1[i2] or 255)+(((c2[i2]-c1[i2] or 255-c1[i2])/s)*i))
			end
		end
		love.graphics.setColor(color)
		if d == "hor" then
			love.graphics.rectangle("fill", x+((w/s)*(i-1)), y, w/s, h)
		elseif d == "ver" then
			love.graphics.rectangle("fill", x, y+((h/s)*(i-1)), w, h/s)
		end
	end
end
Usage:
love.graphics.gradient(direction ("hor" or "ver"), x, y, width, height, # of rectangles drawn, color 1 in a table, color 2 in a table)

And here's a program I made to test it (left and right to select a value and up and down to change the value)
Image
Attachments
gradient.love
(1.07 KiB) Downloaded 192 times
lachlaan
Prole
Posts: 30
Joined: Sun Jun 30, 2013 7:23 pm

Re: Code Doodles!

Post by lachlaan »

I keep checking the forums hoping that it'll turn me into less of a lazy bum so I can actually code something, and this time round I made something hopefully cute, not sure if it's been done in the doodles thread before, but here it is!

Can use the up arrow to give them a little jolt upwards, hold down left or right arrows to give them sideways acceleration. Numpad + key adds more of them if you feel the thousand already spawned aren't enough xD

Enjoy :awesome:
balls.love
(1.7 KiB) Downloaded 200 times
User avatar
Foopy64
Prole
Posts: 2
Joined: Tue Apr 08, 2014 12:46 am
Location: Internet
Contact:

Re: Code Doodles!

Post by Foopy64 »

My doodle ain't great but it's something I guess
Attachments
linemouse.love
(856 Bytes) Downloaded 190 times
lachlaan
Prole
Posts: 30
Joined: Sun Jun 30, 2013 7:23 pm

Re: Code Doodles!

Post by lachlaan »

Could be the crosshairs for a trippy abstract shooter :D
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests