--[[ Game made by: David Gomes The game is open source but please do not use the code or the game as yours ]]-- --Define Constant Variables and Functions possibleLocations = {0, 26.5, 53, 79.5, 106, 132.5, 159, 185.5, 212, 238.5, 265, 291.5} -- 12 possible locations playerImage = image.new("\026\000\000\000\026\000\000\000\000\000\000\000\052\000\000\000\016\000\001\000\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222\247\222") function initGame() --[[ This function initiates all variables needed for the game itself ]]-- blocks = {} playerX = 132.5 -- X position of player, Y position is fixed: 185.5 gameOver = false difficulty = 7 onGame = true onGameOver = false onTitleScreen = false score = 0 timer.start(0.1) end function checkCollision() --[[ This function checks if a block is hitting the sprite and if yes set gameOver boolean to true ]]-- for i=1,#blocks do if blocks[i][1] == playerX and blocks[i][2] >= 159 and blocks[i][2] <= 212 then gameOver = true end end end function on.create() mathvar = "highscorevar" if var.recall(mathvar) == nil then bestScore = 0 else bestScore = var.recall(mathvar) end blocks = {} --For title screen background math.randomseed(timer.getMilliSecCounter()) onTitleScreen = true onGameOver = false ongame = false timer.start(0.1) end function on.paint(gc) if onTitleScreen then gc:setColorRGB(128,128,128) for i=1,#blocks do gc:fillRect(blocks[i][1],blocks[i][2],26.5,26.5) --Draw Blocks end gc:setColorRGB(0,0,0) gc:setFont("sansserif", "b", 30) gc:drawString("Falling Blocks",50, 10, "top") gc:setFont("sansserif", "r", 15) gc:drawString("David Gomes", 108, 50, "top") end if onGame then gc:setColorRGB(0,0,0) if not gameOver then for i=1,#blocks do gc:fillRect(blocks[i][1],blocks[i][2],26.5,26.5) --Draw Blocks end end if gameOver then gc:setColorRGB(0, 0, 0) for i=1,#blocks do if blocks[i][1] == playerX and blocks[i][2] >= 159 and blocks[i][2] <= 212 then gc:fillRect(blocks[i][1],blocks[i][2],26.5,26.5) --Draw Killing Block end end end gc:setFont("sansserif", "b", 15) if not gameOver then gc:drawString(tostring(score),2, 0, "top") --Display Score gc:drawString(tostring(var.recall(mathvar)), 2, 17, "top") --Display highscore end if gameOver then gc:setColorRGB(0,0,0) gc:setFont("sansserif", "b", 30) gc:drawString("GAME OVER",2,0,"top") --Draw game over gc:setFont("sansserif", "b", 15) gc:drawString("Score: " .. tostring(score),2,36,"top") -- Draw Score gc:drawString("HighScore: " .. tostring(bestScore), 2, 56, "top") -- Draw Highscore onGameOver = true timer.stop() end gc:drawImage(playerImage, playerX, 185.5) --Draw Sprite end end function on.timer() if onGame then score = score+1 --Update Score --Update highscore if needed if score>bestScore then bestScore = score var.store(mathvar, bestScore) document.markChanged() end --Difficulty if score>250 and score < 500 then difficulty = 6 end if score>500 and score <750 then difficulty = 5 end if score>750 and score < 1000 then difficulty = 4 end if score>1000 then difficulty = 3 end -- Gravity for i=1,#blocks do blocks[i][2] = blocks[i][2]+26.5 end --Add random blocks if math.floor(math.random(difficulty))==3 then table.insert(blocks, {possibleLocations[math.ceil(math.random(1,12))],5}) end --Add blocks on top of player if math.floor(math.random(10))==5 then table.insert(blocks, {playerX,5}) end --Remove blocks below line from the array indexesToRemove = {} for i=1,#blocks do if blocks[i][2] >= 212 then table.insert(indexesToRemove,i) end end for i=1,#indexesToRemove do table.remove(blocks, indexesToRemove[i]) end --Collision checkCollision() end if onTitleScreen then if math.floor(math.random(4))==2 then table.insert(blocks, {possibleLocations[math.ceil(math.random(1,12))],5}) end --Remove blocks below line from the array indexesToRemove = {} for i=1,#blocks do if blocks[i][2] >= 212 then table.insert(indexesToRemove,i) end end for i=1,#indexesToRemove do table.remove(blocks, indexesToRemove[i]) end -- Gravity for i=1,#blocks do blocks[i][2] = blocks[i][2]+26.5 end end platform.window:invalidate() end function on.enterKey() if gameOver then on.create() end if onTitleScreen then initGame() --Start the game end end function on.charIn(ch) if onGame then if ch=="6" and playerX<291.5 then --Move Right if possible playerX = playerX + 26.5 end if ch=="4" and playerX>0 then --Move Left if possible playerX = playerX - 26.5 end end end