Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Code Doodles!

Post by Sheepolution »

Code Doodles (as I call them), are short codes for small applications. The whole code is in a main.lua, and isn't very big. I don't want to set any rules to it, but I try to create them in an hour or 2. I started making them when helping a friend with programming, and me playing around with the small examples. Now I am making them every lunch break. I got 4 right now. Here they are! Maybe they inspire you to doodle some code yourself? Then share as you like!

Image

Code: Select all

function love.load()
        math.randomseed(os.time(),love.mouse.getX(),love.mouse.getY())
        xposition = love.mouse.getX()
        yposition = love.mouse.getY()
 
        a = {x = 200, y = 200 }
        b = {x = xposition, y = yposition}
 
        angle = math.atan2(b.y-a.y,b.x-a.x)
 
        speed = 0
 
        balls = {}
        colors = {{255,255,255}}
        directions = {}
        currentcolor = {255,255,255}
 
        tocenter = false
 
        rotate = 0
        rotatespeed = 0
       
end
 
 
function love.update(dt)
        if not love.mouse.isDown("l") then
                rotate = rotate + rotatespeed * dt
 
        angle = math.atan2(b.y-a.y,b.x-a.x)
        xposition = love.mouse.getX()
        yposition = love.mouse.getY()
 
        a.x = a.x + math.cos(angle) * getDistance(a.x,a.y,b.x,b.y) * 50 * dt
        a.y = a.y + math.sin(angle) * getDistance(a.x,a.y,b.x,b.y) * 50 * dt
        end
        if getDistance(a.x,a.y,b.x,b.y) < 200 then
                table.insert(balls,{x=b.x,y=b.y})
                table.insert(colors,{unpack(currentcolor)})
                table.insert(directions,angle)
                if tocenter then
                        b.x, b.y = 400,300
                else
                        b.x, b.y = math.random(800),math.random(600)
                end
                tocenter = not tocenter
                angle = math.atan2(b.y-a.y,b.x-a.x)
                currentcolor = {math.random(255),math.random(255),math.random(255)}
        end
 
        for i,v in ipairs(balls) do
                v.x  = v.x + math.cos(directions[i]) * 300 * dt
                v.y = v.y + math.sin(directions[i]) * 300 * dt
        end
 
        if #balls>100 then
                table.remove(balls,1)
                table.remove(colors,1)
                table.remove(directions,1)
        end
 
 
end
 
 
function love.draw()
        -- love.graphics.setColor(currentcolor)
        -- love.graphics.circle("fill",a.x,a.y,10,10)
        -- love.graphics.setColor(255,255,255)
        -- love.graphics.circle("line",b.x,b.y,10,10)
 
 
        -- for i,v in ipairs(balls) do
        --         love.graphics.setColor(unpack(colors[i]))
        --         love.graphics.circle("fill", v.x, v.y, 10, 20)
        -- end
        love.graphics.translate(400,300)
        love.graphics.rotate(rotate)
        love.graphics.translate(-400,-300)
        love.graphics.setLineWidth(2)
        love.graphics.setBlendMode("additive")
 
        for i=1,#balls do
                --love.graphics.setLineWidth((i*5)/#balls)
                local r,g,b = unpack(colors[math.ceil(i)])
                love.graphics.setColor(r,g,b,i*255/#balls)
                if i<#balls then
                        love.graphics.line(balls[i].x,balls[i].y,balls[i+1].x or a.x,balls[i+1].y or a.y)
                else
                        love.graphics.line(balls[i].x,balls[i].y,a.x, a.y)
                end
        end
 
end
 
 
function love.mousepressed(x, y, button)
 
        if button == "l" then
 
                b.x, b.y = x,y
                angle = math.atan2(b.y-a.y,b.x-a.x)
                speed = 100
 
               
        end
 
        if button == "wu" then
                rotatespeed = rotatespeed + 0.1
        elseif button == "wd" then
                rotatespeed = rotatespeed - 0.1
        end
 
end
 
 
function getDistance(x1,y1,x2,y2)
 
        local d = x1 - x2
        local e = y1 - y2
        d = d^2
        e = e^2
        f = d + e
        f = math.sqrt(f)
 
        return f
end
 
function love.keypressed(k)
        if k == " " then
                love.window.setFullscreen(not love.window.getFullscreen())
        end
end


Image

Code: Select all

function love.load()
        math.randomseed(os.time(),love.mouse.getX(),love.mouse.getY())
        head = {x=400,y=300}
        tail = {}
        colors = {}
        directions = {}
        for i=1,100 do
                table.insert(tail,{x=-6000+math.random(12000),y=-6000+math.random(12000)})
                table.insert(colors,{math.random(255),math.random(255),math.random(255)})
                table.insert(directions,math.random(math.pi*2))
        end
 
        angle = 0
       
        timer = 0.2
 
 
end
 
function love.update(dt)
 
        if love.keyboard.isDown(" ") then
                for i,v in ipairs(tail) do
                        v.x = v.x + math.cos(directions[i]) * 500 * dt
                        v.y = v.y + math.sin(directions[i]) * 500 * dt
                end
        else
 
                for i,v in ipairs(tail) do
                        if i == 1 then
                                if getDistance(head,v) > 30 then
                                        local ang = math.atan2(head.y-v.y,head.x-v.x)
                                        v.x = v.x + math.cos(ang) * 500 * dt
                                        v.y = v.y + math.sin(ang) * 500 * dt
                                end
                        else
                                local j = i-1
                                if getDistance(tail[j],v) > 15 then
                                        local ang = math.atan2(tail[j].y-v.y,tail[j].x-v.x)
                                        v.x = v.x + math.cos(ang) * 500 * dt
                                        v.y = v.y + math.sin(ang) * 500 * dt
                                end
                        end
                end
        end
 
        if love.keyboard.isDown("left") then
                angle = angle - 8 * dt
        end
 
        if love.keyboard.isDown("right") then
                angle = angle + 8 * dt
        end
 
        if love.keyboard.isDown("up") then
                head.x = head.x + 500 * math.cos(angle) * dt
                head.y = head.y + 500 * math.sin(angle) * dt
 
        end
 
        if head.x > 800 then
                head.x = 0
        end
        if head.y > 600 then
                head.y = 0
        end
 
        if head.x < 0 then
                head.x = 800
        end
        if head.y < 0 then
                head.y = 600
        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()
 
        for i,v in ipairs(tail) do
                love.graphics.setColor(colors[i][1],colors[i][2],colors[i][3])
                if i == 1 then
                        love.graphics.line(v.x,v.y,head.x,head.y)
                else
                        love.graphics.line(v.x,v.y,tail[i-1].x,tail[i-1].y)
                end
                love.graphics.circle("fill", v.x, v.y, 3, 10)
        end
 
    love.graphics.setColor(255,255,255)
    love.graphics.push()
    love.graphics.translate(head.x, head.y)
    love.graphics.rotate(angle)
    love.graphics.polygon("fill", 0, 0, -30, -10, -30,10)
    love.graphics.pop()
       
end
 
function getDistance(a,b)
        return math.sqrt((a.x-b.x)^2+(a.y-b.y)^2)
end

Image

Code: Select all

function love.load()
        math.randomseed(os.time()+love.mouse.getX()+love.mouse.getY())
        lines = {{x=400,y=300},{x=400,y=325}}
 
 
        timer = 0.5
        colors = {{255,255,255},{255,255,255}}
end
 
function love.update(dt)
 
        timer = timer - dt
 
        if timer < 0 then
                newLine()
                timer = 0.01
        end
       
 
 
end
 
function love.draw()
        love.graphics.setLineWidth(1)
        love.graphics.setColor(255,255,255)
        for i=1,713 do
                i = i-1
                love.graphics.circle("line", 25+(i%31)*25, 25+(math.floor(i/31))*25, 2, 10)
        end
        love.graphics.setLineWidth(3)
 
        for i,v in ipairs(lines) do
                if i~=#lines then
                        love.graphics.setColor(unpack(colors[i]))
                        love.graphics.line(v.x,v.y,lines[i+1].x,lines[i+1].y)
                end
        end
end
 
function newLine()
        local result = false
        local warning = 0
        local tries = {1,2,3,4}
        while not result and warning < 5 do
                warning = warning + 1
                local direction = tries[math.random(#tries)]
 
                if direction == 1 then
                        local newplace = lines[#lines].x + 25
                        local allowed = true
                        if newplace < 800 then
                                for i,v in ipairs(lines) do
                                        if v.x == newplace and v.y == lines[#lines].y then
                                                allowed = false
                                                table.remove(tries,direction)
                                                break
                                        end
                                end
                                if allowed then
                                        table.insert(lines,{x=newplace,y=lines[#lines].y})
                                        table.insert(colors,{100+math.random(155),100+math.random(155),100+math.random(155)})
                                        result = true
                                        break
                                end
                        end
                end
 
                if direction == 2 then
                        local newplace = lines[#lines].y + 25
                        local allowed = true
                        if newplace < 600 then
                                for i,v in ipairs(lines) do
                                        if v.x == lines[#lines].x and v.y == newplace then
                                                allowed = false
                                                table.remove(tries,direction)
                                                break
                                        end
                                end
                                if allowed then
                                        table.insert(lines,{x=lines[#lines].x,y=newplace})
                                        table.insert(colors,{100+math.random(155),100+math.random(155),100+math.random(155)})
                                        result = true
                                        break
                                end
                        end
                end
 
                if direction == 3 then
                        local newplace = lines[#lines].x - 25
                        local allowed = true
                        if newplace > 0 then
                                for i,v in ipairs(lines) do
                                        if v.x == newplace and v.y == lines[#lines].y then
                                                allowed = false
                                                table.remove(tries,direction)
                                                break
                                        end
                                end
                                if allowed then
                                        table.insert(lines,{x=newplace,y=lines[#lines].y})
                                        table.insert(colors,{100+math.random(155),100+math.random(155),100+math.random(155)})
                                        result = true
                                        break
                                end
                        end
                end
 
                if direction == 4 then
                        local newplace = lines[#lines].y - 25
                        local allowed = true
                        if newplace > 0 then
                                for i,v in ipairs(lines) do
                                        if v.x == lines[#lines].x and v.y == newplace then
                                                allowed = false
                                                table.remove(tries,direction)
                                                break
                                        end
                                end
                                if allowed then
                                        table.insert(lines,{x=lines[#lines].x,y=newplace})
                                        table.insert(colors,{100+math.random(155),100+math.random(155),100+math.random(155)})
                                        result = true
                                        break
                                end
                        end
                end
        end
end
 
 
function love.keypressed(key)
 
        if key == " " or key == "r" then
                love.load()
        end
 
end

Image

Code: Select all

function love.load()
        math.randomseed(os.time()+love.mouse.getX()+love.mouse.getY())
        balls = {{x=1000,y=1000,size=60,angle=0,force=100}}
        colors = {{255,255,255,100}}
        for i=1,510 do
                i = i-1
                table.insert(balls,{x=25+(i%30)*25,y=100+math.floor(i/30)*25,size=3+math.random(8),angle=0,force=0})
                table.insert(colors,{math.random(255),math.random(255),math.random(255),220})
        end
 
        toggle = false
 
 
end
 
function love.update(dt)
 
        if love.mouse.isDown("r") then
                balls[1].size = 1000
        else
                balls[1].size = 50
        end
 
        balls[1].x = love.mouse.getX()
        balls[1].y = love.mouse.getY()
 
        if love.mouse.isDown("l") then
                for i,v in ipairs(balls) do
                        if i~=1 then
                                local j = i-2
                                local p = {x=25+(j%30)*25,y=100+math.floor(j/30)*25}
                                local angle = math.atan2(p.y-v.y,p.x-v.x)
                                v.x = v.x + math.cos(angle) * 100 * dt
                                v.y = v.y + math.sin(angle) * 100 * dt
                                v.force = 0
                        end
                end
        else
                if toggle then
                        for i,b1 in ipairs(balls) do
                                for j,b2 in ipairs(balls) do
                                        if b1~=b2 and i~=1 then
                                                if getDistance(b1,b2) < b1.size + b2.size then
                                                        local angle = math.atan2(b2.y-b1.y,b2.x-b1.x)
                                                        b1.x = b1.x - math.cos(angle) * (b1.size + b2.size)*100/getDistance(b1,b2) * dt
                                                        b1.y = b1.y - math.sin(angle) * (b1.size + b2.size)*100/getDistance(b1,b2) * dt
                                                end
                                        end
                                end
                        end
                else
                        for i,b1 in ipairs(balls) do
                                if i~=1 then
                                        for j,b2 in ipairs(balls) do
                                                if b1~=b2 then
                                                        if getDistance(b1,b2) < b1.size + b2.size then
                                                                b1.angle = math.atan2(b2.y-b1.y,b2.x-b1.x)
                                                                b1.force = (b1.size + b2.size)*100/getDistance(b1,b2)
                                                        end
                                                end
                                        end
                                        b1.x = b1.x - math.cos(b1.angle) * b1.force * dt
                                        b1.y = b1.y - math.sin(b1.angle) * b1.force * dt
                                        b1.force = math.max(0,b1.force - 100 * dt)
                                end
                        end
                end
        end
end
 
function love.draw()
 
 
        for i,v in ipairs(balls) do
                love.graphics.setColor(unpack(colors[i]))
               
                love.graphics.circle("fill", v.x, v.y, v.size, 50)
        end
end
 
function getDistance(a,b)
        return math.sqrt((a.x-b.x)^2+(a.y-b.y)^2)
end
 
function love.keypressed(key)
 
        if key == " " then
                toggle = not toggle
        end
 
end
Last edited by Sheepolution on Sat Apr 05, 2014 6:00 pm, edited 1 time in total.
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: Code Doodles!

Post by Zarty55 »

The second one is really pretty! Especially if you change the amount of points to 270 and your rotation to 2. Then just hold right and up and watch the pretty thing go around :P Like so:

Code: Select all

function love.load()
        math.randomseed(os.time(),love.mouse.getX(),love.mouse.getY())
        head = {x=400,y=300}
        tail = {}
        colors = {}
        directions = {}
        for i=1,270 do
                table.insert(tail,{x=-6000+math.random(12000),y=-6000+math.random(12000)})
                table.insert(colors,{math.random(255),math.random(255),math.random(255)})
                table.insert(directions,math.random(math.pi*2))
        end
 
        angle = 0
       
        timer = 0.2
 
 
end
 
function love.update(dt)
 
        if love.keyboard.isDown(" ") then
                for i,v in ipairs(tail) do
                        v.x = v.x + math.cos(directions[i]) * 500 * dt
                        v.y = v.y + math.sin(directions[i]) * 500 * dt
                end
        else
 
                for i,v in ipairs(tail) do
                        if i == 1 then
                                if getDistance(head,v) > 30 then
                                        local ang = math.atan2(head.y-v.y,head.x-v.x)
                                        v.x = v.x + math.cos(ang) * 500 * dt
                                        v.y = v.y + math.sin(ang) * 500 * dt
                                end
                        else
                                local j = i-1
                                if getDistance(tail[j],v) > 15 then
                                        local ang = math.atan2(tail[j].y-v.y,tail[j].x-v.x)
                                        v.x = v.x + math.cos(ang) * 500 * dt
                                        v.y = v.y + math.sin(ang) * 500 * dt
                                end
                        end
                end
        end
 
        if love.keyboard.isDown("left") then
                angle = angle - 2 * dt
        end
 
        if love.keyboard.isDown("right") then
                angle = angle + 2 * dt
        end
 
        if love.keyboard.isDown("up") then
                head.x = head.x + 500 * math.cos(angle) * dt
                head.y = head.y + 500 * math.sin(angle) * dt
 
        end
 
        if head.x > love.graphics.getWidth() then
                head.x = 0
        end
        if head.y > love.graphics.getHeight() then
                head.y = 0
        end
 
        if head.x < 0 then
                head.x = love.graphics.getWidth()
        end
        if head.y < 0 then
                head.y = love.graphics.getHeight()
        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()
 
        for i,v in ipairs(tail) do
                love.graphics.setColor(colors[i][1],colors[i][2],colors[i][3])
                if i == 1 then
                        love.graphics.line(v.x,v.y,head.x,head.y)
                else
                        love.graphics.line(v.x,v.y,tail[i-1].x,tail[i-1].y)
                end
                love.graphics.circle("fill", v.x, v.y, 3, 10)
        end
 
    love.graphics.setColor(255,255,255)
    love.graphics.push()
    love.graphics.translate(head.x, head.y)
    love.graphics.rotate(angle)
    love.graphics.polygon("fill", 0, 0, -30, -10, -30,10)
    love.graphics.pop()
       
end
 
function getDistance(a,b)
        return math.sqrt((a.x-b.x)^2+(a.y-b.y)^2)
end
Would be nice if we could organize the code to be easy to change the values, it's just so much fun to play with them :D

I also like to create these, going to try to find some of mine to share here :p
User avatar
Reef
Prole
Posts: 33
Joined: Sun Mar 04, 2012 10:19 pm

Re: Code Doodles!

Post by Reef »

Cool! These remind me of the sketches that people make with Processing. Have you heard of Nature of Code? You might find some things in that book interesting like simulating flocking and AI evolution. I started working my way through it but got distracted... I've made a few sketches or 'doodles' as you call them, I'll have to see if I can dig them up.
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 »

Zarty55 wrote:The second one is really pretty! Especially if you change the amount of points to 270 and your rotation to 2. Then just hold right and up and watch the pretty thing go around :P Like so:
I like it! You can also change the distance, or circle size, for maybe a better effect.

Reef wrote:Cool! These remind me of the sketches that people make with Processing. Have you heard of Nature of Code? You might find some things in that book interesting like simulating flocking and AI evolution. I started working my way through it but got distracted... I've made a few sketches or 'doodles' as you call them, I'll have to see if I can dig them up.
Nope, I haven't. Looks interesting, but not planning on investing time on it at the moment.
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 »

#5!

My least favorite one so far to be honest, but whatever..

Image

Code: Select all

function love.load()
        squares = {}
 
        for i=1,200 do
        i = i-1
                table.insert(squares,{x=50*(i%17)+50,y=math.floor(i/17)*50+50,size=10+math.random(10),angle=0,speed=0,color={math.random(255),math.random(255),math.random(255)},line=0})
        end
 
        squares[123].speed = 100
 
 
end
 
 
function love.update(dt)
 
        for i,v in ipairs(squares) do
                v.x = v.x + math.cos(v.angle) * v.speed * dt
                v.y = v.y + math.sin(v.angle) * v.speed * dt
 
        v.speed = math.max(0,v.speed - 50 * dt)
 
 
 
                if v.x > 800 then
                        v.x = -v.size
                end
 
                if v.y > 600 then
                        v.y = -v.size
                end
 
                if v.x < -v.size then
                        v.x = 800
                end
 
                if v.y < -v.size then
                        v.y = 600
                end
 
        if v.line > 0 then
            v.line = v.line + 300 * dt
        end
 
        if v.line > 100 then
            v.line = 0            
        end
 
 
 
 
                for j,w in ipairs(squares) do
                        if v~=w then
                                if getCollision(v,w) then
                                        local a,b = 0,0
                                        local taupi = math.pi/2
                                        while a==b do
                                                a = math.random(3)
                                                b = math.random(3)
                                        end
 
                    v.x = v.x + math.cos(v.angle) * -v.speed * 2 * dt
                    v.y = v.y + math.sin(v.angle) * -v.speed * 2 * dt
 
                    w.x = w.x + math.cos(w.angle) * -w.speed * 2 * dt
                    w.y = w.y + math.sin(w.angle) * -w.speed * 2 * dt
 
 
 
                                        v.angle = v.angle + taupi * a
                                        w.angle = w.angle + taupi * b
 
                    v.line = v.size
                    w.line = w.size
 
                    v.speed = v.speed * 1.1
 
                    -- a,b = 0,0
                    -- while a==b do
                    --     a = math.random(2)
                    --     b = math.random(2)
                    -- end
 
                    -- if a>b then
                    --     v.size = v.size * 2
                    --     w.size = w.size / 2
                    -- else
                    --     v.size = v.size / 2
                    --     w.size = w.size * 2
                    -- end
 
                                        v.speed = 100
                                        w.speed = 100
                                end
                        end
                end
        end
       
 
 
end
 
function love.draw()
 
        for i,v in ipairs(squares) do
        local r,g,b = unpack(v.color)
        love.graphics.setColor(r,g,b)
                love.graphics.rectangle("fill", v.x-v.size/2, v.y-v.size/2, v.size, v.size)
        love.graphics.setColor(r,g,b,255)
        print(v.size)
        love.graphics.rectangle("line", v.x-v.line/2, v.y-v.line/2, v.line,v.line)
        end
 
end
 
function getCollision(a,b)
        return a.x+a.size/2 > b.x-b.size/2 and a.x-a.size/2 < b.x + b.size/2 and a.y+a.size/2 > b.y-b.size/2 and a.y-a.size/2 < b.y + b.size/2
end
User avatar
Ragzouken
Citizen
Posts: 84
Joined: Fri Aug 10, 2012 7:59 am
Contact:

Re: Code Doodles!

Post by Ragzouken »

really like the generative art aspect to these! you should post the packaged love files so it's quick to see them in action
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's me being lazy, asking others to be less lazy.

You can copy the code, put it in a single main.lua, and build (which is really simple and fast assuming you have a text-editor with a build shortcut).
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: Code Doodles!

Post by SneakySnake »

Sheepolution wrote:You can copy the code, put it in a single main.lua, and build (which is really simple and fast assuming you have a text-editor with a build shortcut).
I just need to click "Select All" then press a shortcut key which runs this script:

Code: Select all

xdotool key ctrl+C && \
mkdir -p /tmp/love-try && \
xclip -o > /tmp/love-try/main.lua && \
love /tmp/love-try
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 »

Woah, that's awesome! Thanks!
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 »

I löve making mini-games like these!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 8 guests