Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- local timerLength = 0.5
- local pieceX
- local pieceY
- local pieceR
- local pieceType
- local nextPieceType
- local score
- local gameOver
- local pieceArray = {
- { {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 pieceColor = {colors.red, colors.cyan, colors.blue, colors.orange, colors.green, colors.purple}
- local numPieces = 6
- local boardArray
- local boardWidth = 12
- local boardHeight = 21
- local boardX = 8
- local boardY = -1
- local edgeColor = colors.white
- local boardColor = colors.black
- local scoreX = 26
- local scoreY = 4
- 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, 32)
- 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("Originally designed and programmed by Alexey Pajitnov circa 1984")
- term.setCursorPos(1,3)
- term.write("Lua adaptation by MKmisfit")
- term.setCursorPos(1,4)
- term.write("use arrow keys (left, right, down), use (1, 2) to rotate piece")
- 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)
- if rotation == 0 then
- for i = 1, 4 do
- if boardArray[x + piece[i][1]][y + piece[i][2]] ~= boardColor then
- return true
- end
- end
- end
- if rotation == 1 then
- for i = 1, 4 do
- if boardArray[x - piece[i][2]][y + piece[i][1]] ~= boardColor then
- return true
- end
- end
- end
- if rotation == 2 then
- for i = 1, 4 do
- if boardArray[x - piece[i][1]][y - piece[i][2]] ~= boardColor then
- return true
- end
- end
- end
- if rotation == 3 then
- for i = 1, 4 do
- if boardArray[x + piece[i][2]][y - piece[i][1]] ~= boardColor then
- return true
- end
- end
- end
- return false
- end
- local function drawScore()
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX + 7, scoreY)
- term.write(tostring(score) .. " ")
- 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
- 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
- --it would be more efficient to clear all lines at once
- 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
- end
- if linescleared == 2 then
- score = score + 200
- end
- if linescleared == 3 then
- score = score + 400
- end
- if linescleared == 4 then
- score = score + 800
- end
- drawScore()
- 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) 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
- 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(" ")
- end
- else
- boardArray[x][y] = boardColor
- term.setCursorPos(x + boardX, y + boardY)
- term.setBackgroundColor(boardColor)
- term.write(" ")
- end
- end
- end
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX, scoreY)
- term.write("Score: 0 ")
- term.setCursorPos(nextPieceX, nextPieceY)
- term.write("Next:")
- gameOver = false
- score = 0
- nextPieceType = math.floor(math.random(1, numPieces + 0.999))
- dropNewPiece()
- timer1 = os.startTimer(timerLength)
- local event, par
- while true do
- event, par = os.pullEvent()
- if event == "timer" and par == timer1 then
- if testPiece(pieceArray[pieceType], pieceX, pieceY + 1, pieceR) 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 par == 203 then
- if not testPiece(pieceArray[pieceType], pieceX - 1, pieceY, pieceR) 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 par == 205 then
- if not testPiece(pieceArray[pieceType], pieceX + 1, pieceY, pieceR) 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 par == 208 then
- if not testPiece(pieceArray[pieceType], pieceX, pieceY + 1, pieceR) then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceY = pieceY + 1
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- score = score + 2
- drawScore()
- end
- elseif par == 2 then
- if not testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 1) % 4) then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceR = (pieceR + 1) % 4
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif par == 3 then
- if not testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 3) % 4) then
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
- pieceR = (pieceR + 3) % 4
- drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
- end
- elseif par == 28 then
- term.setBackgroundColor(boardColor)
- term.setCursorPos(scoreX, scoreY + 1)
- term.write("Paused")
- repeat
- event, par = os.pullEvent()
- until par == 28
- term.setCursorPos(scoreX, scoreY + 1)
- term.write(" ")
- timer1 = os.startTimer(timerLength)
- end
- end
- end
- end
- boardArray = {}
- for x = 1, boardWidth do
- boardArray[x] = {}
- end
- term.setBackgroundColor(boardColor)
- term.clear()
- loadHighScore()
- drawCredits()
- main()
- term.setBackgroundColor(colors.black)
- term.clear()
- if testHighScore() then
- saveHighScore()
- clear()
- end
- drawCredits()
- term.setCursorPos(1,17)
- term.write("Your final score was only " .. tostring(score))
- term.setCursorPos(1,18)
- term.write("Thank You for Playing!")
Advertisement
Add Comment
Please, Sign In to add comment