Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Alien Text Generator
- -- Save as main.lua, run with Love2D
- -- First it show you the alphabet
- -- Then press any key to get a page of filled-up random alien text
- -- Then press any key twice more to get a page of random alien text with spacing
- local CharParams={Width=3,Height=4}
- local CharSheets={}
- local Space={}
- local State=0
- local Matrix={}
- --
- -- ^
- -- | ^
- -- | j,v,y |
- -- |
- -- | #
- -- | # #
- -- | ###
- -- | # #
- -- | i,u,x ->
- -- +-------------------->
- --
- local xor = function(a,b) return (a and not b) or (b and not a) end
- local function Shuffle(list)
- for k=#list,1,-1 do
- list[#list]=table.remove(list,math.random(k))
- end
- end
- local function isCharCorrect(c,u,v)
- -- Forbids 2x2 blobs of pixels
- for i=2,u do
- for j=2,v do
- if c[i][j] and c[i][j-1] and c[i-1][j] and c[i-1][j-1] then
- return false
- end
- end
- end
- -- Must have a pixel on the bottom
- local hasPixel=false
- for i=1,u do
- if c[i][1] then
- hasPixel=true
- end
- end
- if not hasPixel then
- return false
- end
- -- Must be centered
- for i=1,math.floor(u/2) do
- local hasPixel=false
- for j=1,v do
- if c[i][j] then
- hasPixel=true
- end
- end
- if hasPixel then
- hasPixel=false
- for j=1,v do
- if c[u-i][j] or c[u-i+1][j] then
- hasPixel=true
- end
- end
- if not hasPixel then
- return false
- end
- else
- for j=1,v do
- if c[u-i+1][j] then
- return false
- end
- end
- end
- end
- -- Must not have disjoint pixels
- local d={}
- local k=1
- for i=1,u do
- d[i]={}
- for j=1,v do
- if c[i][j] then
- d[i][j]=k
- k=k+1
- end
- end
- end
- local retry=true
- while retry do
- retry=false
- local merges={}
- for i=1,u do
- for j=1,v do
- if d[i][j] then
- for x=math.max(1,i-1),math.min(u,i+1) do
- for y=math.max(1,j-1),math.min(v,j+1) do
- if d[x][y] and not (d[x][y]==d[i][j]) then
- d[x][y]=math.min(d[x][y],d[i][j])
- d[i][j]=d[x][y]
- retry=true
- end
- end
- end
- end
- end
- end
- end
- local n=0
- for i=1,u do
- for j=1,v do
- n=math.max(n,d[i][j] or n)
- end
- end
- if not (n==1) then
- return false
- end
- return true
- end
- local function MakeCharSheets(u,v)
- Space={}
- for i=1,u do
- Space[i]={}
- end
- local s={}
- local b={}
- for n=1,2^(u*v) do
- local r=false
- for k=1,u*v do
- if b[k] then
- b[k]=not b[k]
- else
- b[k]=true
- break
- end
- end
- local c={}
- for i=1,u do
- c[i]={}
- for j=1,v do
- c[i][j]=b[i+u*v-u*j]
- end
- end
- if isCharCorrect(c,u,v) then
- table.insert(s,c)
- end
- end
- return s
- end
- function love.load()
- love.graphics.setCaption("Alien Letters")
- love.graphics.setMode(512,768)
- love.graphics.setBlendMode("alpha")
- love.graphics.setBackgroundColor(0,0,0)
- love.graphics.setColor(32,255,32,255)
- Font12 = love.graphics.newFont(12)
- Font24 = love.graphics.newFont(24)
- Font48 = love.graphics.newFont(48)
- love.graphics.setFont(Font12)
- CharSheets=MakeCharSheets(CharParams.Width,CharParams.Height)
- --love.mouse.setVisible(false) -- make default mouse invisible
- --love.mouse.setGrab(true)
- State=0
- end
- local function DrawText(text,x,y)
- love.graphics.print(text,x,love.graphics.getHeight()-y-12)
- end
- local function DrawPoint(x,y)
- love.graphics.point(0.5+x,0.5+love.graphics.getHeight()-y)
- end
- local function DrawCharOverRec(c,x,y)
- for i=1,CharParams.Width do
- for j=1,CharParams.Height do
- if c[i][j] then
- love.graphics.setColor(32,255,32,255)
- else
- love.graphics.setColor(96,32,32,255)
- end
- DrawPoint(x+2*i-1,y+2*j-1)
- DrawPoint(x+2*i-1,y+2*j-2)
- DrawPoint(x+2*i-2,y+2*j-1)
- DrawPoint(x+2*i-2,y+2*j-2)
- end
- end
- end
- local function DrawChar(c,x,y)
- for i=1,CharParams.Width do
- for j=1,CharParams.Height do
- if c[i][j] then
- DrawPoint(x+2*i-1,y+2*j-1)
- DrawPoint(x+2*i-1,y+2*j-2)
- DrawPoint(x+2*i-2,y+2*j-1)
- DrawPoint(x+2*i-2,y+2*j-2)
- end
- end
- end
- end
- function love.draw()
- love.graphics.setColor(32,255,32,255)
- ;(({[0]=function()
- local k=1
- for y=love.graphics.getHeight()-20,16,-2*(CharParams.Height+1) do
- for x=8,love.graphics.getWidth()-16,2*(CharParams.Width+1) do
- if CharSheets[k] then
- DrawCharOverRec(CharSheets[k],x,y)
- end
- k=k+1
- end
- end
- end,
- [2]=function()
- local k=0
- for y=love.graphics.getHeight()-20,16,-2*(CharParams.Height+1) do
- for x=8,love.graphics.getWidth()-16,2*(CharParams.Width+1) do
- k=k+1
- if not Matrix[k] then
- Matrix[k]=CharSheets[math.random(1,#CharSheets)]
- end
- if Matrix[k]=="\n" then
- break
- end
- DrawChar(Matrix[k],x,y)
- end
- end
- end
- })[State]or(function() end))()
- end
- function love.keypressed(key,unicode)
- if key=="escape" then
- if love.event.quit then
- love.event.quit() -- 0.8.0
- else
- love.event.push('q') -- 0.7.2
- end
- end
- State=State+1
- if State>=1 then
- if Matrix["2"]==2 then
- for k=1,#CharSheets*12/100 do
- table.insert(CharSheets,Space)
- end
- for k=1,#CharSheets*12/1000 do
- table.insert(CharSheets,"\n")
- end
- end
- Shuffle(CharSheets)
- Matrix={["2"]=(Matrix["2"] or 0)+1}
- State=2
- end
- end
- function love.update(dt)
- if love.event.quit then
- love.timer.sleep(0.030) -- 0.8.0
- else
- love.timer.sleep(30) -- 0.7.2
- end
- if #(Matrix or {})>1 then
- Matrix[math.random(1,#Matrix)]=CharSheets[math.random(1,#CharSheets)]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement