Skip to content

Instantly share code, notes, and snippets.

@hhrhhr
Created November 8, 2018 20:02
Show Gist options
  • Save hhrhhr/d0629ff0c3eaf8c40866d230096814d0 to your computer and use it in GitHub Desktop.
Save hhrhhr/d0629ff0c3eaf8c40866d230096814d0 to your computer and use it in GitHub Desktop.
Invaders generator for LÖVE
local lg = love.graphics
local rnd = math.random
local canvas
local show_help
local invader_sz
local invader_num
local function hsv2rgb(H, S, V) -- 0...360
local Hi = math.floor(H / 60) % 6
S = S or 75 -- saturate
V = V or 75 -- brightness
local Vmin = ((100 - S) * V) / 100
local a = (V - Vmin) * ((H % 60) / 60)
local Vinc = Vmin + a
local Vdec = V - a
local r, g, b
if Hi == 0 then r = V; g = Vinc; b = Vmin
elseif Hi == 1 then r = Vdec; g = V; b = Vmin
elseif Hi == 2 then r = Vmin; g = V; b = Vinc
elseif Hi == 3 then r = Vmin; g = Vdec; b = V
elseif Hi == 4 then r = Vinc; g = Vmin; b = V
elseif Hi == 5 then r = V; g = Vmin; b = Vdec
else assert(false)
end
return {r/100, g/100, b/100}
end
local function create_invader(x0, y0, w, size)
local rndColor = {}
local h = rnd(359)
local s = rnd(50, 100)
local v = rnd(50, 100)
rndColor = {
hsv2rgb(h , s, v),
hsv2rgb(h + 40, s, v),
hsv2rgb(h + 80, s, v),
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}
rndColor[0] = #rndColor
local middle = math.floor(size / 2)
for y = 0, size-1 do
local color = {}
for x = 0, size-1 do
local c
if x < middle then
c = rndColor[rnd(rndColor[0])]
table.insert(color, c)
elseif x == middle then
c = rndColor[rnd(rndColor[0])]
else
c = table.remove(color)
end
local X = x * w + x0
local Y = y * w + y0
lg.setColor(c)
lg.rectangle("fill", X, Y, w, w)
end
end
end
local function generate()
local sx, sy = lg.getDimensions()
canvas = lg.newCanvas(sx, sy)
local invaderW = sx / invader_num
local padding = invaderW / (invader_sz)
lg.setCanvas(canvas)
for x = 0, invader_num-1 do
for y = 0, invader_num-1 do
local X = x * invaderW + padding
local Y = y * invaderW + padding
local W = (invaderW - padding * 2) / invader_sz
create_invader(X, Y, W, invader_sz)
end
end
lg.setCanvas()
end
function love.load(arg)
math.randomseed(os.time() + tonumber(tostring({}):sub(8)))
invader_sz = arg[1] or 7
invader_num = arg[2] or 8
generate()
show_help = true
local font = love.graphics.newFont(16)
love.graphics.setFont(font)
end
function love.draw()
--// canvas start
lg.setColor(1, 1, 1, 1)
lg.draw(canvas)
--\\ canvas end
if show_help then
lg.setColor(1, 1, 1, 1)
lg.print([[
Esc quit
Space reload
Num+ + larger invaders
Num- - smaller invaders
Num* ] more invaders
Num/ [ less invaders
F1 show/hide help
]])
end
end
function love.keyreleased(k, s, r)
if k == "escape" then
love.event.quit()
elseif k == "space" then
generate()
elseif k == "kp+" or s == "=" then
invader_sz = invader_sz + 2
generate()
elseif k == "kp-" or s == "-" then
invader_sz = math.max(3, invader_sz - 2)
generate()
elseif k == "kp*" or s == "]" then
invader_num = invader_num + 1
generate()
elseif k == "kp/" or s == "[" then
invader_num = math.max(1, invader_num - 1)
generate()
elseif k == "f1" then
show_help = not show_help
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment