Skip to content

Instantly share code, notes, and snippets.

@randrews
Created June 18, 2011 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrews/1033545 to your computer and use it in GitHub Desktop.
Save randrews/1033545 to your computer and use it in GitHub Desktop.
First draft of Love puzzle game
function make_board(size)
local board = { size = size }
setmetatable(board, { __tostring = board_tostring })
for n = 0, size * size - 1 do
board[n] = 0
end
return board
end
function populate_board(board, filled, seed)
local size = board.size
if seed then math.randomseed(seed) end
filled = filled or size * size * 3 / 4
local function rand()
local c
repeat c = math.random(size * size) - 1 until board[c] == 0
return c
end
if filled > 0 then
for _,v in ipairs{'a','b','c','d'} do board[rand()] = v end
for n = 1, filled-4 do
board[rand()] = math.random(4)
end
return fall(board)
end
end
function board_tostring(board)
local lines = {}
local size = board.size
for y = 0, size - 1 do
local line = "|"
for x = 0, size - 1 do
line = line .. " " .. board[x+y*size]
end
table.insert(lines, line .. " |")
end
return table.concat(lines,"\n")
end
function fall(board)
local size = board.size
local new_board = make_board(size, 0)
local function fall_column(col)
local dest = size - 1
for y = size-1, 0, -1 do
if board[y*size + col] ~= 0 then
new_board[dest*size + col] = board[y*size + col]
dest = dest - 1
end
end
end
for x=0, size-1 do
fall_column(x)
end
return new_board
end
function rotate(board)
local size = board.size
local new_board = make_board(size, 0)
for y = 0, size-1 do
local dest_col = size - 1 - y
for n = 0, size-1 do
new_board[n*size + dest_col] = board[y*size + n]
end
end
return new_board
end
function crush(board)
local size = board.size
local new_board = make_board(size, 0)
local crushers = {'a','b','c','d'}
for n=0, size-1 do
new_board[n] = board[n]
end
for n = size, size*size - 1 do
if board[n-size] == crushers[board[n]] then
new_board[n] = 0
else
new_board[n] = board[n]
end
end
return new_board
end
function game()
local board = populate_board(make_board(8))
local line = nil
repeat
print(board)
line = io.read()
local valid = false
if line == 'r' then
valid, board = true, rotate(board)
elseif line == 'l' then
valid, board = true, rotate(rotate(rotate(board)))
elseif line == 'exit' then
valid = true
end
if valid then
board = fall(crush(fall(board)))
else
print "Didn't understand that. Type 'l', 'r', or 'exit'."
end
until line == 'exit'
end
require 'fall'
rotation_speed = 0
angle = 0
function love.load()
love.graphics.setBackgroundColor(82,82,128)
math.randomseed(love.timer.getMicroTime())
board = populate_board(make_board(8))
img = love.graphics.newImage("arrow_blocks.png")
tiles = {}
local tile_dims = {48, 48, 48*4, 48*2} -- width, height, image dimensions
for k,v in pairs{'a','b','c','d'} do
tiles[k] = love.graphics.newQuad(48*(k-1), 0, unpack(tile_dims))
tiles[v] = love.graphics.newQuad(48*(k-1), 48, unpack(tile_dims))
end
end
function love.draw()
local g = love.graphics
local w, h = g.getWidth(), g.getHeight()
local left, top = w/2 - 48*board.size/2, h/2 - 48*board.size/2
g.translate(w/2, h/2)
g.rotate(angle)
g.translate(-w/2,-h/2)
for n=0, board.size * board.size - 1 do
local x, y = n % board.size, math.floor(n / board.size)
if board[n] ~= 0 then
g.drawq(img, tiles[board[n]],
left + x*48,
top + y*48)
end
end
end
function love.mousepressed(x, y, btn)
if rotation_speed ~= 0 then return
elseif btn == 'l' then rotation_speed = -2
elseif btn == 'r' then rotation_speed = 2
end
end
function love.update(dt)
angle = angle + rotation_speed * dt
if rotation_speed > 0 and angle >= math.pi/2 then
board, rotation_speed, angle = rotate(board), 0, 0
elseif rotation_speed < 0 and angle <= -math.pi/2 then
board, rotation_speed, angle = rotate(rotate(rotate(board))), 0, 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment