Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tetris by MKmisfit
- --level increases every 10 lines cleared
- --score rewards increase by 100% per 5 levels
- --speed increases by 100% per 10 levels
- --
- local edgeCharacter = " " --wall character can be changed here
- local edgeColor = colors.white
- local boardColor = colors.black
- local pieceColor = {colors.magenta, colors.lightBlue, colors.red, colors.lime, colors.yellow, colors.blue, colors.orange}
- local timerLength = 0.5
- local timerLengthMin = 0.05
- local pieceX
- local pieceY
- local pieceR
- local pieceType
- local nextPieceType
- local score
- local level
- local gameOver
- local pieceArray = {
- { {0,0},{-1,0},{1,0},{0,1} },
- { {0,0},{-1,0},{-2,0},{1,0} },
- { {0,0},{-1,0},{0,1},{1,1} },
- { {0,0},{-1,1},{0,1},{1,0} },
- { {0,0},{1,0},{0,1},{1,1} },
- { {0,0},{-1,0},{1,0},{1,1} },
- { {0,0},{-1,0},{1,0},{-1,1} }
- }
- local numPieces = 7
- local boardArray
- local boardWidth = 12
- local boardHeight = 21
- local boardX = 8
- local boardY = -1
- local scoreX = 26
- local scoreY = 3
- local nextPieceX = 26
- local nextPieceY = 8
- local highScore = {}
- local saveFile = "tetris_scores"
- local function loadHighScore()
- if fs.exists(saveFile) then
- local file = fs.open(saveFile,"r")
- for n = 1, 10 do
- highScore[n] = {file.readLine(), tonumber(file.readLine())}
- end
- file.close()
- else
- for n = 1, 10 do
- highScore[n] = {"", 0}
- end
- end
- end
- local function saveHighScore()
- local file = fs.open(saveFile,"w")
- for n = 1, 10 do
- file.write(highScore[n][1].."\n")
- file.write(tostring(highScore[n][2]).."\n")
- end
- file.close()
- end
- local function testHighScore()
- for n = 1, 10 do
- if score > highScore[n][2] then
- term.setCursorPos(1,1)
- term.write("You got a High Score!")
- term.setCursorPos(1,2)
- term.write("Enter your name: ")
- name = string.sub(io.read(), 1, 16)
- name = name .. string.sub(" ", 1, 18 - string.len(name))
- table.insert(highScore, n, {name, score})
- return true
- end
- end
- return false
- end
- local function drawCredits()
- term.setCursorPos(1,1)
- term.write("Tetris")
- term.setCursorPos(1,2)
- term.write("Original by Alexey Pajitnov circa 1984")
- term.setCursorPos(1,3)
- term.write("Lua adaptation by MKmisfit")
- term.setCursorPos(1,4)
- term.write("arrow keys move, (1, 2) keys rotate, ENTER pauses")
- if highScore[1][2] > 0 then
- term.setCursorPos(1,6)
- term.write("High Scores")
- for n = 1, 10 do
- if highScore[n][2] == 0 then break end
- term.setCursorPos(1,n+6)
- term.write(highScore[n][1] .. highScore[n][2])
- end
- end
- end
- local function testPiece(piece, x, y, rotation)
- local c
- if rotation == 0 then
- for i = 1, 4 do
- c = boardArray[x + piece[i][1]][y + piece[i][2]]
- if c ~= boardColor then
- return c
- end
- end
- end
- if rotation == 1 then
- for i = 1, 4 do
- c = boardArray[x - piece[i][2]][y + piece[i][1]]
- if c ~= boardColor then
- return c
- end
- end
- end
- if rotation == 2 then
- for i = 1, 4 do
- c = boardArray[x - piece[i][1]][y - piece[i][2]]
- if c ~= boardColor then
- return c
- end
- end
- end
- if rotation == 3 then
- for i = 1, 4 do
- c = boardArray[x + piece[i][2]][y - piece[i][1]]
- if c ~= boardColor then
- return c
- end
- end
- end
- return boardColor
- end
- local function setPiece(piece, x, y, rotation, color)
- if rotation == 0 then
- for i = 1, 4 do
- boardArray[x + piece[i][1]][y + piece[i][2]] = color
- end
- end
- if rotation == 1 then
- for i = 1, 4 do
- boardArray[x - piece[i][2]][y + piece[i][1]] = color
- end
- end
- if rotation == 2 then
- for i = 1, 4 do
- boardArray[x - piece[i][1]][y - piece[i][2]] = color
- end
- end
- if rotation == 3 then
- for i = 1, 4 do
- boardArray[x + piece[i][2]][y - piece[i][1]] = color
- end
- end
- --test for completed lines
- local minX = 2
- local maxX = boardWidth - 1
- local minY = y - 2
- local maxY = y + 2
- local hasPixels
- local linesCleared = 0
- if minY < 3 then minY = 3 end
- if maxY >= boardHeight then maxY = boardHeight - 1 end
- for by = minY, maxY do
- for bx = minX, maxX do
- if boardArray[bx][by] == boardColor then break end
- if bx == maxX then
- linesCleared = linesCleared + 1
- for copyy = by - 1, 2, -1 do
- term.setCursorPos(minX + boardX, copyy + 1 + boardY)
- hasPixels = false
- for copyx = minX, maxX do
- term.setBackgroundColor(boardArray[copyx][copyy])
- term.write(" ")
- hasPixels = hasPixels or (boardArray[copyx][copyy] ~= boardColor)
- boardArray[copyx][copyy + 1] = boardArray[copyx][copyy]
- end
- if not hasPixels then break end
- --clear top line
- if copyy == 2 then
- term.setCursorPos(minX + boardX, copyy + boardY)
- term.setBackgroundColor(boardColor)
- for copyx = minX, maxX do
- term.write(" ")
- boardArray[copyx][copyy] = boardColor
- end
- end
- end
- sleep(0.5)
- end
- end
- end
- if linesCleared ~= 0 then
- if linesCleared == 1 then
- score = score + 100 * (1 + level / 5)
- end
- if linesCleared == 2 then
- score = score + 200 * (1 + level / 5)
- end
- if linesCleared == 3 then
- score = score + 400 * (1 + level / 5)
- end
- if linesCleared == 4 then
- score = score + 800 * (1 + level / 5)
- end
- score = math.floor(score)
- level = level + linesCleared * 0.1
- timerLength = 0.5 / (1 + level / 10)
- if timerLength < timerLengthMin then timerLength = timerLengthMin end
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX + 7, scoreY)
- term.write(tostring(score) .. " ")
- term.setCursorPos(scoreX + 7, scoreY + 1)
- term.write(tostring(math.floor(level)) .. " ")
- end
- end
- local function drawPiece(piece, x, y, rotation, color)
- if rotation == 0 then
- for i = 1, 4 do
- term.setCursorPos(x + piece[i][1], y + piece[i][2])
- term.setBackgroundColor(color)
- term.write(" ")
- end
- end
- if rotation == 1 then
- for i = 1, 4 do
- term.setCursorPos(x - piece[i][2], y + piece[i][1])
- term.setBackgroundColor(color)
- term.write(" ")
- end
- end
- if rotation == 2 then
- for i = 1, 4 do
- term.setCursorPos(x - piece[i][1], y - piece[i][2])
- term.setBackgroundColor(color)
- term.write(" ")
- end
- end
- if rotation == 3 then
- for i = 1, 4 do
- term.setCursorPos(x + piece[i][2], y - piece[i][1])
- term.setBackgroundColor(color)
- term.write(" ")
- end
- end
- end
- local function dropNewPiece()
- pieceX = boardWidth / 2
- pieceY = 2
- pieceType = nextPieceType
- nextPieceType = math.floor(math.random(1, numPieces + 0.999))
- pieceR = 0
- if testPiece(pieceArray[pieceType], pieceX, pieceY, pieceR) ~= boardColor then
- gameOver = true
- end
- drawPiece(pieceArray[pieceType], nextPieceX + 2, nextPieceY + 2, 0, boardColor)
- drawPiece(pieceArray[nextPieceType], nextPieceX + 2, nextPieceY + 2, 0, pieceColor[nextPieceType])
- end
- local function main()
- --clear the board
- term.setTextColor(colors.black)
- for x = 1, boardWidth do
- for y = 1, boardHeight do
- if x == 1 or y == 1 or x == boardWidth or y == boardHeight then
- boardArray[x][y] = edgeColor
- if y > 1 and y < boardHeight then
- term.setCursorPos(x + boardX, y + boardY)
- term.setBackgroundColor(edgeColor)
- term.write(edgeCharacter)
- end
- else
- boardArray[x][y] = boardColor
- term.setCursorPos(x + boardX, y + boardY)
- term.setBackgroundColor(boardColor)
- term.write(" ")
- end
- end
- end
- term.setTextColor(colors.white)
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX, scoreY)
- term.write("Score: 0 ")
- term.setCursorPos(scoreX, scoreY + 1)
- term.write("Level: 0 ")
- term.setCursorPos(nextPieceX, nextPieceY)
- term.write("Next:")
- gameOver = false
- score = 0
- level = 0
- nextPieceType = math.floor(math.random(1, numPieces + 0.999))
- dropNewPiece()
- local event, param
- local blockColor
- timer1 = os.startTimer(timerLength)
- while true do
- event, param = os.pullEvent()
- if event == "timer" and param == timer1 then
- if testPiece(pieceArray[pieceType], pieceX, pieceY + 1, pieceR) ~= boardColor then
- setPiece(pieceArray[pieceType], pieceX, pieceY, pieceR, pieceColor[pieceType])
- dropNewPiece()
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- else
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceY = pieceY + 1
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- if gameOver then
- break
- end
- timer1 = os.startTimer(timerLength)
- end
- if event == "key" then
- if param == 203 then
- if testPiece(pieceArray[pieceType], pieceX - 1, pieceY, pieceR) == boardColor then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceX = pieceX - 1
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif param == 205 then
- if testPiece(pieceArray[pieceType], pieceX + 1, pieceY, pieceR) == boardColor then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceX = pieceX + 1
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif param == 208 then
- local adjustment = 0
- while testPiece(pieceArray[pieceType], pieceX, pieceY + adjustment + 1, pieceR) == boardColor do
- adjustment = adjustment + 1
- end
- if adjustment ~= 0 then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceY = pieceY + adjustment
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- score = score + 2 * adjustment
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX + 7, scoreY)
- term.write(tostring(score) .. " ")
- timer1 = os.startTimer(timerLength)
- end
- elseif param == 2 then
- local blockColor
- local adjustment = 0
- blockColor = testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 1) % 4)
- if blockColor == edgeColor then
- if pieceX < boardWidth / 2 then
- while adjustment < 2 and blockColor == edgeColor do
- adjustment = adjustment + 1
- blockColor = testPiece(pieceArray[pieceType], pieceX + adjustment, pieceY, (pieceR + 1) % 4)
- end
- else
- while adjustment > -2 and blockColor == edgeColor do
- adjustment = adjustment - 1
- blockColor = testPiece(pieceArray[pieceType], pieceX + adjustment, pieceY, (pieceR + 1) % 4)
- end
- end
- end
- if blockColor == boardColor then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceX = pieceX + adjustment
- pieceR = (pieceR + 1) % 4
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif param == 3 then --this code duplicated from above with only the rotation offset changed
- local blockColor
- local adjustment = 0
- blockColor = testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 3) % 4)
- if blockColor == edgeColor then
- if pieceX < boardWidth / 2 then
- while adjustment < 2 and blockColor == edgeColor do
- adjustment = adjustment + 1
- blockColor = testPiece(pieceArray[pieceType], pieceX + adjustment, pieceY, (pieceR + 3) % 4)
- end
- else
- while adjustment > -2 and blockColor == edgeColor do
- adjustment = adjustment - 1
- blockColor = testPiece(pieceArray[pieceType], pieceX + adjustment, pieceY, (pieceR + 3) % 4)
- end
- end
- end
- if blockColor == boardColor then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceX = pieceX + adjustment
- pieceR = (pieceR + 3) % 4
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif param == 28 then
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX, scoreY + 2)
- term.write("Paused")
- repeat
- event, param = os.pullEvent()
- until event == "key" and param == 28
- term.setCursorPos(scoreX, scoreY + 2)
- term.write(" ")
- timer1 = os.startTimer(timerLength)
- end
- end
- end
- end
- boardArray = {}
- for x = 1, boardWidth do
- boardArray[x] = {}
- end
- loadHighScore()
- term.setBackgroundColor(boardColor)
- term.clear()
- drawCredits()
- term.setCursorPos(1,19)
- term.write("Press ENTER to continue")
- repeat
- event, param = os.pullEvent()
- until event == "key" and param == 28
- term.clear()
- main()
- term.setBackgroundColor(colors.black)
- term.clear()
- if testHighScore() then
- saveHighScore()
- term.clear()
- end
- drawCredits()
- term.setCursorPos(1,17)
- term.write("Your final score was " .. tostring(score))
- term.setCursorPos(1,18)
- term.write("Thank You for Playing!")
- term.setCursorPos(1,19)
Advertisement
Add Comment
Please, Sign In to add comment