lua
function love.load()
love.window.setMode(540, 960)
love.window.setTitle("Block Tower")
gravity = 800
friction = 0.98
restitution = 0.3
resetGame()
end
function resetGame()
blocks = {}
platform = {x = 270, y = 850, width = 200, height = 20}
dropTimer = 0
dropInterval = 1.5
gameTime = 0
fallenBlocks = 0
maxFallenBlocks = 10
table.insert(blocks, {
x = platform.x,
y = platform.y - 25,
width = 50,
height = 25,
vx = 0,
vy = 0,
angle = 0,
angularVel = 0,
color = {1, 0.8, 0.2},
trail = {},
settled = true
})
end
function love.update(dt)
gameTime = gameTime + dt
dropTimer = dropTimer + dt
if dropTimer >= dropInterval then
dropTimer = 0
spawnBlock()
end
updateBlocks(dt)
checkGameEnd()
end
function spawnBlock()
local topY = getTopBlockY()
local randomOffset = (love.math.random() - 0.5) * 100
table.insert(blocks, {
x = 270 + randomOffset,
y = topY - 50,
width = 50,
height = 25,
vx = (love.math.random() - 0.5) * 50,
vy = 0,
angle = (love.math.random() - 0.5) * 0.2,
angularVel = (love.math.random() - 0.5) * 2,
color = {
0.5 + love.math.random() * 0.5,
0.3 + love.math.random() * 0.7,
0.2 + love.math.random() * 0.8
},
trail = {},
settled = false
})
end
function getTopBlockY()
local minY = platform.y
for _, block in ipairs(blocks) do
if block.y < minY then
minY = block.y
end
end
return minY
end
function updateBlocks(dt)
for i, block in ipairs(blocks) do
if not block.settled then
block.vy = block.vy + gravity * dt
block.x = block.x + block.vx * dt
block.y = block.y + block.vy * dt
block.angle = block.angle + block.angularVel * dt
block.vx = block.vx * friction
block.angularVel = block.angularVel * friction
table.insert(block.trail, {x = block.x, y = block.y, alpha = 1})
if #block.trail > 8 then
table.remove(block.trail, 1)
end
for j = 1, #block.trail do
block.trail[j].alpha = block.trail[j].alpha - dt * 2
end
local timeColor = (math.sin(gameTime * 2) + 1) * 0.5
block.color[1] = 0.5 + timeColor * 0.5
block.color[2] = 0.3 + (1 - timeColor) * 0.4
end
handleCollisions(block, i)
if block.y > 1000 then
fallenBlocks = fallenBlocks + 1
table.remove(blocks, i)
end
end
end
function handleCollisions(block, index)
if block.y + block.height/2 >= platform.y and
block.x > platform.x - platform.width/2 and
block.x < platform.x + platform.width/2 then
block.y = platform.y - block.height/2
block.vy = -block.vy * restitution
if math.abs(block.vy) < 50 and math.abs(block.vx) < 30 then
block.settled = true
block.vy = 0
end
block.x = block.x + (love.math.random() - 0.5) * 2
end
for i, other in ipairs(blocks) do
if i ~= index and checkBlockCollision(block, other) then
resolveBlockCollision(block, other)
end
end
end
function checkBlockCollision(a, b)
return math.abs(a.x - b.x) < (a.width + b.width) / 2 and
math.abs(a.y - b.y) < (a.height + b.height) / 2
end
function resolveBlockCollision(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local distance = math.sqrt(dx*dx + dy*dy)
if distance > 0 then
dx = dx / distance
dy = dy / distance
local overlap = (a.width + b.width) / 2 - distance
if overlap > 0 then
a.x = a.x + dx * overlap * 0.5
a.y = a.y + dy * overlap * 0.5
b.x = b.x - dx * overlap * 0.5
b.y = b.y - dy * overlap * 0.5
local relativeVx = a.vx - b.vx
local relativeVy = a.vy - b.vy
local speed = relativeVx * dx + relativeVy * dy
if speed > 0 then
a.vx = a.vx - speed * dx * restitution
a.vy = a.vy - speed * dy * restitution
b.vx = b.vx + speed * dx * restitution
b.vy = b.vy + speed * dy * restitution
a.x = a.x + (love.math.random() - 0.5) * 3
b.x = b.x + (love.math.random() - 0.5) * 3
end
end
end
end
function getTowerAngle()
if #blocks < 3 then return 0 end
local topBlocks = {}
for _, block in ipairs(blocks) do
if block.y < platform.y - 100 then
table.insert(topBlocks, block)
end
end
if #topBlocks < 2 then return 0 end
table.sort(topBlocks, function(a, b) return a.y < b.y end)
local top = topBlocks[1]
local bottom = topBlocks[math.min(#topBlocks, 5)]
return math.atan2(top.x - bottom.x, bottom.y - top.y)
end
function checkGameEnd()
local angle = getTowerAngle()
if math.abs(angle) > math.pi/4 or fallenBlocks >= maxFallenBlocks or #blocks > 50 then
resetGame()
end
end
function getAverageSpeed()
local totalSpeed = 0
local movingBlocks = 0
for _, block in ipairs(blocks) do
if not block.settled then
totalSpeed = totalSpeed + math.sqrt(block.vx^2 + block.vy^2)
movingBlocks = movingBlocks + 1
end
end
return movingBlocks > 0 and totalSpeed / movingBlocks or 0
end
function love.draw()
love.graphics.setBackgroundColor(0.1, 0.1, 0.2)
love.graphics.setColor(0.6, 0.6, 0.6)
love.graphics.rectangle("fill", platform.x - platform.width/2, platform.y, platform.width, platform.height)
for _, block in ipairs(blocks) do
for j, trail in ipairs(block.trail) do
if trail.alpha > 0 then
love.graphics.setColor(block.color[1], block.color[2], block.color[3], trail.alpha * 0.3)
love.graphics.rectangle("fill", trail.x - block.width/4, trail.y - block.height/4, block.width/2, block.height/2)
end
end
love.graphics.push()
love.graphics.translate(block.x, block.y)
love.graphics.rotate(block.angle)
love.graphics.setColor(block.color[1], block.color[2], block.color[3])
love.graphics.rectangle("fill", -block.width/2, -block.height/2, block.width, block.height)
love.graphics.pop()
end
love.graphics.setColor(1, 1, 1)
love.graphics.print("Blocks: " .. #blocks, 10, 10)
love.graphics.print("Fallen: " .. fallenBlocks .. "/" .. maxFallenBlocks, 10, 30)
love.graphics.print("Avg Speed: " .. math.floor(getAverageSpeed()), 10, 50)
love.graphics.print("Tower Angle: " .. math.floor(math.deg(getTowerAngle())) .. "°", 10, 70)
love.graphics.print("Time: " .. math.floor(gameTime), 10, 90)
end