love sketches

This commit is contained in:
James Turk 2024-01-06 18:13:20 -06:00
commit 92b099ac3e
10 changed files with 387 additions and 0 deletions

20
arcs/main.lua Normal file
View File

@ -0,0 +1,20 @@
luacolors = require "luacolors"
curves = 0
function love.load()
end
function love.update(dt)
curves = curves + 1
end
function love.draw()
love.graphics.setBackgroundColor(1, 1, 1)
r,g,b = luacolors.HSLtoRGB(curves, 1, 1)
print(r, g, b)
love.graphics.setColor(r, g, b, 1)
for i=0, curves do
love.graphics.arc("line", "open", i*5, 300, 100, 0, i/100*6.28)
end
end

38
asteroids/main.lua Normal file
View File

@ -0,0 +1,38 @@
player = {
x = 100,
y = 100,
angle = 0
}
speed = 200
function love.load()
end
function love.update(dt)
if love.keyboard.isDown("space") then
player.x = player.x + dt * math.sin(player.angle) * speed
player.y = player.y - dt * math.cos(player.angle) * speed
end
if love.keyboard.isDown("left") then
player.angle = player.angle - dt
elseif love.keyboard.isDown("right") then
player.angle = player.angle + dt
end
end
FRONT_SIZE = 20
BACK_SIZE = 20
ship_shape = {
0, -FRONT_SIZE,
-10, BACK_SIZE,
10, BACK_SIZE
}
function love.draw()
love.graphics.setColor(0, 0.6, 0.0)
love.graphics.push()
love.graphics.translate(player.x, player.y)
love.graphics.rotate(player.angle)
love.graphics.polygon('line', ship_shape)
love.graphics.pop()
end

41
bezier/main.lua Normal file
View File

@ -0,0 +1,41 @@
DRAW_POINTS = 100
MIN = 20
MAX = 700
cx = 20
cy = 20
all_points = {}
function resetCurve()
local ctrl = {cx, cy}
for i=0, 2 do
cx = love.math.random( MIN, MAX )
cy = love.math.random( MIN, MAX )
table.insert(ctrl, cx)
table.insert(ctrl, cy)
end
curve = love.math.newBezierCurve(ctrl)
points = 0
end
function love.load()
resetCurve()
end
function love.update(dt)
points = points + 1
if points > DRAW_POINTS then
resetCurve()
end
local x, y = curve:evaluate(points/DRAW_POINTS)
table.insert(all_points, {x, y})
end
function love.draw()
love.graphics.setBackgroundColor( 0.3, 0.3, 0.30)
love.graphics.setColor(0.3, 0, 0, 0.1)
love.graphics.line(curve:render())
love.graphics.setColor(1, 0, 0)
for i, point in pairs(all_points) do
love.graphics.points(point)
end
end

50
fire/main.lua Normal file
View File

@ -0,0 +1,50 @@
local mask = love.graphics.newImage("wwe.png")
local mask_shader = love.graphics.newShader[[
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
if (Texel(texture, texture_coords).rgb == vec3(0.0)) {
// a discarded pixel wont be applied as the stencil.
discard;
}
return vec4(1.0);
}
]]
function love.load()
local canvas = love.graphics.newCanvas(20, 20)
love.graphics.setCanvas(canvas)
love.graphics.clear()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(1, 1, 1, 0.8)
love.graphics.circle('fill', 10, 10, 5)
love.graphics.setCanvas()
psystem = love.graphics.newParticleSystem(canvas, 1000)
psystem:setParticleLifetime(2, 8)
psystem:setEmissionRate(60)
psystem:setSizeVariation(0.7)
psystem:setSpeed(5)
--psystem:setLinearAcceleration(-40, -40, 40, 0)
psystem:setRadialAcceleration(4,8)
psystem:setEmissionArea('borderrectangle', 150, 10, 0, false)
psystem:setColors(
1,0.35,0,1,
0.85, 0.85, 0.85, 0
)
end
local function myStencilFunction()
love.graphics.setShader(mask_shader)
love.graphics.draw(mask, 0, 0)
love.graphics.setShader()
end
function love.draw()
love.graphics.stencil(myStencilFunction, "increment")
--love.graphics.setStencilTest("less", 1)
love.graphics.draw(psystem, love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5)
love.graphics.setStencilTest()
end
function love.update(dt)
psystem:update(dt)
end

BIN
fire/wwe.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
fire/wwe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

43
genart/main.lua Normal file
View File

@ -0,0 +1,43 @@
Tree = { x = 0, y = 0, r = 100 }
function Tree:create(o)
o.parent = self
return o
end
function Tree:collides(first, other)
print(first.x, first.y, other.x, other.y)
return math.sqrt((first.x - other.x)^2 + (first.y - other.y)^2) < (other.r + first.r)
end
function love.load()
trees = {}
nextNum = 0
r = 100
repeat
t = Tree:create({x=math.random(0, 500), y=math.random(0, 500), r=r})
collides = false
for j=0, nextNum-1 do
if Tree:collides(t, trees[j]) then
collides = true
break
end
end
if not collides then
trees[nextNum] = t
nextNum = nextNum + 1
else
r = r - math.random(0, 10)
end
until nextNum >= 20
end
function love.draw()
love.graphics.setColor(1, 1, 1)
for k, t in pairs(trees) do
love.graphics.circle("line", t.x, t.y, t.r, 100)
end
end

BIN
ghostballer/ghost.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

28
ghostballer/main.lua Normal file
View File

@ -0,0 +1,28 @@
function love.load()
local canvas = love.graphics.newCanvas(16, 16)
-- love.graphics.setCanvas(canvas)
-- love.graphics.clear()
-- love.graphics.setBlendMode("alpha")
-- love.graphics.setColor(1, 0, 0, 0.8)
-- love.graphics.rectangle('fill', 0, 0, 100, 100)
-- love.graphics.setCanvas()
local img = love.graphics.newImage('ghost.png')
psystem = love.graphics.newParticleSystem(img, 32)
psystem:setParticleLifetime(1, 5) -- Particles live at least 2s and at most 5s.
psystem:setEmissionRate(5)
psystem:setSizeVariation(0.7)
psystem:setLinearAcceleration(-40, -40, 40, 40) -- Random movement in all directions.
psystem:setColors(1, 1, 1, 0.5, 1, 1, 1, 0) -- Fade to transparency.
end
function love.draw()
-- Draw the particle system at the center of the game window.
love.graphics.draw(psystem, love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5)
end
local frame = 0
function love.update(dt)
psystem:update(dt)
frame = frame + 1
s = love.graphics.captureScreenshot(string.format( "%012d.png", frame ) )
end

167
luacolors.lua Normal file
View File

@ -0,0 +1,167 @@
local luacolors = {
_VERSION = 'luacolors v0.1.0',
_DESCRIPTION = 'Color utility library for Lua',
_URL = 'https://github.com/icrawler/luacolors',
_LICENSE = [[
MIT LICENSE
Copyright (c) 2014 Phoenix Enero
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}
-- private methods
local function f1(t)
return t > 0.0088564516790356 and t^0.3333333333333333 or
0.3333333333333333*23.3611111111111111*t + 0.1379310344827586
end
local function f2(t)
return t > 0.2068965517241379 and t*t*t or
3*0.0428061831153388*(t-0.1379310344827586)
end
-- public functions
-- Convert gamma-corrected RGB to CIEXYZ
local function sRGBtoXYZ(r, g, b)
local f = 1/255
r = r*f
g = g*f
b = b*f
r = r <= 0.04045 and r/12.92 or ((r+0.055)/(1.055))^2.4
g = g <= 0.04045 and g/12.92 or ((g+0.055)/(1.055))^2.4
b = b <= 0.04045 and b/12.92 or ((b+0.055)/(1.055))^2.4
local X = 0.4124*r + 0.3576*g + 0.1805*b
local Y = 0.2126*r + 0.7152*g + 0.0722*b
local Z = 0.0193*r + 0.1192*g + 0.9502*b
return X*100, Y*100, Z*100
end
-- Convert CIEXYZ to gamma-corrected RGB
local function XYZtosRGB(X, Y, Z)
X, Y, Z = X/100, Y/100, Z/100
local r = 3.2406*X - 1.5372*Y - 0.4986*Z
local g = -0.9689*X + 1.8758*Y + 0.0415*Z
local b = 0.0557*X - 0.2040*Y + 1.0570*Z
local k = 1/2.4
local floor = math.floor
r = floor((r <= 0.0031308 and 12.92*r or 1.055*r^k-0.055)*255+0.5)
g = floor((g <= 0.0031308 and 12.92*g or 1.055*g^k-0.055)*255+0.5)
b = floor((b <= 0.0031308 and 12.92*b or 1.055*b^k-0.055)*255+0.5)
return r, g, b
end
-- convert CIEXYZ to CIELAB
local function XYZtoLab(X, Y, Z)
local yon = f1(Y/100)
local L = 116*(yon) - 16
local a = 500*(f1(X/95.047) - yon)
local b = 200*(yon - f1(Z/108.883))
return L, a, b
end
-- convert gamma-corrected RGB to CIELAB
local function sRGBtoLab(r, g, b)
return XYZtoLab(sRGBtoXYZ(r, g, b))
end
-- convert CIELAB to CIEXYZ
local function LabtoXYZ(L, a, b)
local k = 0.0086206896551724*(L+16)
local Y = 100*f2(k)
local X = 95.047*f2(k+0.002*a)
local Z = 108.883*f2(k-0.005*b)
return X, Y, Z
end
-- convert CIELAB to gamma-corrected RGB
local function LabtosRGB(L, a, b)
return XYZtosRGB(LabtoXYZ(L, a, b))
end
-- convert HSL to RGB
-- (taken from https://www.love2d.org/wiki/HSL_color with a few modifications)
local function HSLtoRGB(h, s, l)
if s<=0 then return l,l,l end
h, s, l = h/360*6, s/255, l/255
local c = (1-math.abs(2*l-1))*s
local x = (1-math.abs(h%2-1))*c
local m,r,g,b = (l-.5*c), 0,0,0
if h < 1 then r,g,b = c,x,0
elseif h < 2 then r,g,b = x,c,0
elseif h < 3 then r,g,b = 0,c,x
elseif h < 4 then r,g,b = 0,x,c
elseif h < 5 then r,g,b = x,0,c
else r,g,b = c,0,x
end return (r+m)*255,(g+m)*255,(b+m)*255
end
-- convert RGB to HSL
local function RGBtoHSL(r, g, b)
r, g, b = r/255, g/255, b/255
local M, m = math.max(r, g, b),
math.min(r, g, b)
local c, H = M - m, 0
if M == r then H = (g-b)/c%6
elseif M == g then H = (b-r)/c+2
elseif M == b then H = (r-g)/c+4
end local L = 0.5*M+0.5*m
local S = c == 0 and 0 or c/(1-math.abs(2*L-1))
return ((1/6)*H)*360%360, S*255, L*255
end
-- public interface
luacolors.sRGBtoXYZ = sRGBtoXYZ
luacolors.XYZtosRGB = XYZtosRGB
luacolors.XYZtoLab = XYZtoLab
luacolors.LabtoXYZ = LabtoXYZ
luacolors.LabtosRGB = LabtosRGB
luacolors.sRGBtoLab = sRGBtoLab
luacolors.HSLtoRGB = HSLtoRGB
luacolors.RGBtoHSL = RGBtoHSL
-- colors
-- taken from http://en.wikipedia.org/wiki/CSS_colors
luacolors.white = {255, 255, 255}
luacolors.silver = {191, 191, 191}
luacolors.gray = {127, 127, 127}
luacolors.black = {0, 0, 0}
luacolors.red = {255, 0, 0}
luacolors.maroon = {127, 0, 0}
luacolors.yellow = {255, 255, 0}
luacolors.olive = {127, 127, 0}
luacolors.lime = {0, 255, 0}
luacolors.green = {0, 127, 0}
luacolors.aqua = {0, 255, 255}
luacolors.teal = {0, 127, 127}
luacolors.blue = {0, 0, 255}
luacolors.navy = {0, 0, 127}
luacolors.fuchsia = {255, 0, 255}
luacolors.purple = {127, 0, 127}
return luacolors