|
Post by Tag365 on Nov 15, 2017 2:21:06 GMT
draw.setscreen(1) local width, height = draw.getport()
local blocksX, blocksY = 8, 12 local squareDrawSize = math.min(width/blocksX, height/(blocksY + 1)) local speed = 1
local onColor = {0, 0, 255/255, 1} local offColor = {0, 0, 16/255, 1}
local gameOver = true local timePerRow = .13 local removeRow = timePerRow local rowsRemoved = blocksY
local directionMoving = 1 local placeY = false -- The column where the falling blocks will land, or false otherwise. local score = 0 -- The game score local currentX = 1 -- The first column with the blocks waiting to be placed local currentY = 0 -- The current row of the blocks being placed local currentWidth = 3 -- The current width of the row of blocks being placed local currentBlocks = {}
local t, f = true, false local gameOverText = "game over Stacker Stacker Place blocks local gameOverAniFont = { "g" = [[ zzaaazzz zzazazzz zzaaazzz zzzzazzz zzzaazzz ]] } local gameOverAnimation = {}
-- Resets the game. function resetGame() score = 0 speed = 1 currentWidth = 3
gameOver = false removeRow = timePerRow rowsRemoved = 0
-- Reset the blocks for y = 1, blocksY do currentBlocks[y] = {} for x = 1, blocksX do currentBlocks[y][x] = (y == blocksY and (x == math.floor(blocksX*.5))) end end end
-- Draws a frame of gameplay function drawFrame() draw.clear(draw.black)
-- Draw the background local top = height - math.floor(blocksY*squareDrawSize) for y = 1, blocksY do for x = 1, blocksX do local color = (currentBlocks[y][x] and onColor) or offColor draw.fillrect(math.floor((x - 1)*squareDrawSize) + 1, top + math.floor((y - 1)*squareDrawSize), math.floor(x*squareDrawSize) - 1, top + math.floor(y*squareDrawSize) - 1, color) end end
-- Draw the falling block for x = math.floor(currentX), math.floor(currentX - 1 + currentWidth) do draw.fillrect(math.floor((x - 1)*squareDrawSize) + 1, top + math.floor((math.floor(currentY) - 1)*squareDrawSize), math.floor(x*squareDrawSize) - 1, top + math.floor(math.floor(currentY)*squareDrawSize) - 1, onColor) end end
-- Updates the game. function updateFrame(timeSinceLastFrame) -- Check if the game is over if gameOver then if rowsRemoved < blocksY then removeRow = removeRow - timeSinceLastFrame if removeRow <= 0 then table.remove(currentBlocks, blocksY) table.insert(currentBlocks, 1, {}) for x = 1, blocksX do currentBlocks[1][x] = false end rowsRemoved = rowsRemoved + 1 removeRow = timePerRow end end return end
-- Check if the row of blocks is already falling. if currentY > 0 then currentY = math.min(currentY + (timeSinceLastFrame*speed*blocksY), placeY or (blocksY + 1)) if currentY >= (placeY or (blocksY + 1)) then if placeY then -- Place the blocks down local stackingTooTall = placeY < 3 for x = currentX, currentX - 1 + currentWidth do currentBlocks[placeY][x] = true end if stackingTooTall then table.remove(currentBlocks, blocksY) table.insert(currentBlocks, 1, {}) for x = 1, blocksX do currentBlocks[1][x] = false end end
if score >= 70 then currentWidth = 1 elseif score >= 4 then currentWidth = 2 end
currentY = 0 speed = speed + .2 score = score + 1 math.randomseed(score*(score*.1)*23)
currentX = math.random(blocksX - currentWidth + 1)--(currentX >= math.ceil(blocksX*.5) and 1) or (blocksX - currentWidth + 1) else gameOver = true end end else -- Move the row of blocks currentX = currentX + (directionMoving*speed*timeSinceLastFrame) if directionMoving < 0 and currentX <= 2 then directionMoving = -directionMoving currentX = 1 end if directionMoving > 0 and currentX + currentWidth >= blocksX + 2 then directionMoving = -directionMoving currentX = blocksX - currentWidth + 1 end end end
-- Places the blocks. function place() if currentY > 0 then return false end
-- Set the column of the falling block currentX = math.floor(currentX) currentY = .1
-- Check if the block will land on something or fall and case the player to lose. placeY = false for x = currentX, currentX - 1 + currentWidth do -- Find where for y = 1, blocksY do if currentBlocks[y][x] then placeY = math.min(y - 1, placeY or blocksY) break end end end
-- Check if placeY then
end end
-- Default tap function function tap() end
-- Start tracking touches draw.tracktouches(tap, tap, place)
-- Reset the game resetGame()
-- Main game loop local lastFrameTime = sys.gettime() while true do -- Draw this frame draw.beginframe() drawFrame() draw.endframe()
-- Update the game updateFrame(sys.gettime() - lastFrameTime) lastFrameTime = sys.gettime()
-- Handle touches draw.doevents() end
|
|