Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Thanks you ! Make sure to use the installer for this because it need other files
- Based on a idea from dlcruz
- You are unauthorized to redistribute it as your own or without asking me
- ]]--
- local path = shell.resolve(".")
- os.loadAPI(path.."/gui")
- os.loadAPI(path.."/gameutils")
- --Declaration of variables
- local tx, ty = term.getSize()
- local midx = tx/2
- local midy = ty/2
- local playerPos = {}
- local champT = {}
- local turtleT = {}
- local entities = {}
- local collided = {}
- local champSpeed = 0.5
- local turtleSpeed = 0.6
- gameutils.initializeBuffer()
- --End of declaration
- --Define file functions
- local function saveGame(playerName, saveSlot, toSave, newGame)
- if newGame == "true" then
- local mode = "w"
- else
- local mode = "a"
- end
- local fileSave = fs.open(path.."/saves/"..playerName.."/"..saveSlot, mode)
- fileSave.write(textutils.serialize(toSave))
- fileSave.close()
- end
- local function loadGame(playerName, saveSlot)
- if fs.exist(path.."/saves/"..playerName.."/"..saveSlot) then
- local loadFile = fs.open(path.."/saves/"..playerName.."/"..saveSlot, "r")
- local saveData = loadFile.readAll()
- loadFile.close()
- local saveDataT = textutils.unserialize(saveData)
- return saveDataT
- else
- print("Error : load save : could not find save file")
- return "false"
- end
- end
- local function loadLastGame()
- local dataFile = fs.open(path.."/.data/lastGame", "r")
- local dataToLoad = dataFile.readAll()
- dataFile.close()
- return loadGame(dataToLoad[1], dataToLoad[2])
- end
- local function loadNewGame()
- savePoint = 1
- lifes = 5
- end
- --End of file functions
- --Define the draw functions
- local function clearScreen()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function crBackground()
- local background = gameutils.loadSprite(path.."/.data/.sprites/background.nfp", midMx, midMy)
- return background
- end
- local function crPlayer(x, y)
- local playerEntity = gameutils.loadSprite(path.."/.data/.sprites/player.nfp", x, y)
- playerPos.x = x
- playerPos.y = y
- return playerEntity
- end
- local function crEntity(type, x, y)
- if type == "champEnnemy" then
- local champEnnemyEntity = gameutils.loadSprite(path.."/.data/.sprites/champEnnemy.nfp", x, y)
- table.insert(champT, x)
- table.insert(champT, y)
- champEnnemyEntity.marioID = "champ"
- entities.insert(champEnnemyEntity)
- return champEnnemyEntity
- elseif type == "turtleEnnemy" then
- local turtleEnnemyEntity = gameutils.loadSprite(path.."/.data/.sprites/turtleEnnemy.nfp", x, y)
- table.insert(turtleT, x)
- table.insert(turtleT, y)
- turtleEnnemyEntity.marioID = "turtle"
- entities.insert(turtleEnnemyEntity)
- return turtleEnnemyEntity
- elseif type == "upEntity" then
- local upEntity = gameutils.loadSprite(path.."/.data/.sprites/upEntity.nfp", x, y)
- upEntity.marioID = "up"
- entities.insert(upEntity)
- return upEntity
- end
- end
- local function crFloor(file, x, y)
- local floor = gameutils.loadSprite(path.."/.data/.sprites/"..file, x, y)
- floor.marioID = "floor"
- entities.insert(floor)
- return floor
- end
- local function drawMap()
- local background = crBackground()
- gameutils.drawToBuffer(background)
- local floor1 = crFloor("floor8", 1, ty-1)
- gameutils.drawToBuffer(floor1)
- local floor2 = crFloor("floor4", 10, ty-1)
- gameutils.drawToBuffer(floor2)
- local floor3 = crFloor("floor6", 16, ty-1)
- gameutils.drawToBuffer(floor3)
- local floor1_1 = crFloor("floor2", 18, ty-2)
- gameutils.drawToBuffer(floor1_1)
- end
- local function updateScreen()
- drawMap()
- clearScreen()
- term.setCursorPos(1, 1)
- write("Lifes : "..lifes)
- gameutils.drawBuffer()
- gameutils.clearBuffer()
- end
- --End of draw functions
- local function runGame(savePoint)
- if savePoint == 1 then
- playerEntity = crPlayer(2, 13)
- updateScreen()
- elseif savePoint == 2 then
- playerPos.x = math.random(16, 20)
- playerPos.y = 13
- elseif savePoint == 3 then
- playerPos.x = math.random(30, 35)
- playerPos.y = 13
- end
- end
- local function moveEnnemy()
- while true do
- local event, param1 = os.pullEvent("timer")
- if param1 == champTimer then
- champT.x = champT.x-1
- champTimer = os.startTimer(champSpeed)
- elseif param2 == turtleTimer then
- turtleT.x = turtleT.x-1
- turtleTimer = os.startTimer(turtleSpeed)
- end
- if champEntity then
- champEntity:moveTo(champT.x, champT.y)
- gameutils.drawToBuffer(champEntity)
- end
- if turtleEntity then
- turtleEntity:moveTo(turtleT.x, turtleT.y)
- gameutils.drawToBuffer(turtleEntity)
- end
- end
- end
- local function movePlayer()
- while not dead do
- local event, param1, param2 = os.pullEvent()
- if event == "key" then
- if param1 == 205 then
- playerPos.x = playerPos.x + 1
- elseif param1 == 203 then
- playerPos.x = playerPos.x - 1
- elseif param1 == 57 then
- os.queueEvent("jump", 1)
- end
- elseif event == "jump" and param1 == 1 or event == "jump" and param1 == 2 then
- playerPos.y = playerPos.y - 1
- os.queueEvent("jump", param1 + 1)
- elseif event == "jump" and param1 == 3 then
- playerPos.y = playerPos.y - 1
- os.queueEvent("jump", param1 + 1)
- elseif event == "jump" and param1 == 4 then
- playerPos.y = playerPos.y - 1
- for x=1, #entities do
- entity = entities[x]
- collided.x, collided.y = pCollidesWith(playerEntity, entity)
- if entity.marioID == "floor" then --if player hit a floor
- if collided.y == playerEntity.y + 3 then --if it is the feet of the player
- falling = false
- end
- if collided.y == playerEntity.y then --if it is the head
- jumping = false
- falling = true
- end
- end
- if collided.x ~= nil and entity.marioID ~= "floor"then break end
- end
- end
- if playerPos.y > ty - 5 then
- dead = true
- end
- playerEntity:moveTo(playerPos.x, playerPos.y)
- gameutils.drawToBuffer(playerEntity)
- end
- if dead then
- if lifes > 0 then
- dead = false
- loadLastGame()
- lifes = lifes - 1
- else
- drawMainMenu()
- selectMain()
- end
- end
- end
- --Define function for menu
- local function drawPlayMenu()
- gui.render(true)
- gui.clear()
- gui.setHeader(" Play", colors.red)
- gui.setButton(midx-4, 5, "Continue", colors.red, colors.orange)
- gui.setButton(midx-7, 9, "Start new game", colors.red, colors.orange)
- gui.setButton(midx-5, 13, "Load game", colors.red, colors.orange)
- gui.setButton(midx-3, 17, "Cancel", colors.red, colors.orange)
- gui.render()
- end
- local function selectPlay()
- local clickedButton = gui.getSelection()
- if clickedButton == 1 then
- loadLastGame()
- runGame()
- elseif clickedButton == 2 then
- loadNewGame()
- runGame()
- elseif clickedButton == 3 then
- loadSave()
- drawSaveMenu()
- selectSave()
- elseif clickedButton == 4 then
- drawMainMenu()
- selectMain()
- else
- error("Error : play menu : wrong selection")
- end
- end
- local function drawOptionMenu()
- gui.render(true)
- gui.clear()
- gui.setHeader(" Options", colors.magenta)
- gui.setButton(midx-6, 5, "Change name", colors.magenta, colors.purple)
- gui.setButton(midx-4, 9, "Credits", colors.magenta, colors.purple)
- gui.setButton(midx-5, 13, "Reset all", colors.red, colors.yellow)
- gui.setButton(midx-3, 17, "Cancel", colors.magenta, colors.purple)
- gui.render()
- end
- local function selectOption()
- local clickedButton = gui.getSelection()
- if clickedButton == 1 then
- changeName()
- elseif clickedButton == 2 then
- printCredits()
- elseif clickedButton == 3 then
- local popBClicked = gui.popUp(midx, midy, "Reset all", "Are you sure you want to delete everythings ?", colors.red, colors.orange, "no", "yes")
- if popBClicked == 1 then
- drawOptionMenu()
- selectOption()
- elseif popBClicked == 2 then
- deleteAll()
- else
- error("Error : delete option : wrong pop-up selection")
- end
- elseif clickedButton == 4 then
- drawMainMenu()
- selectMain()
- else
- error("Error : option menu : wrong selection")
- end
- end
- function drawMainMenu()
- gui.render(true)
- gui.clear()
- gui.setHeader(" Main menu", colors.lightBlue)
- gui.setButton(midx-2, 5, "Play", colors.lightBlue, colors.blue)
- gui.setButton(midx-4, 9, "Options", colors.lightBlue, colors.blue)
- gui.setButton(midx-2, 13, "Quit", colors.lightBlue, colors.blue)
- gui.render()
- end
- function selectMain()
- local buttonClicked = gui.getSelection()
- if buttonClicked == 1 then
- drawPlayMenu()
- selectPlay()
- elseif buttonClicked == 2 then
- drawOptionMenu()
- selectOption()
- elseif buttonClicked == 3 then
- gui.clear()
- return
- else
- error("Error : main menu : wrong selection")
- end
- end
- --End of menus fonctions
- drawMainMenu()
- selectMain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement