Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local outline = paintutils.loadImage("tetris_files/board_outline")
- local score = 0
- local board = {
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- "..........",
- ".........."
- }
- local blocks = {
- {
- color = colors.cyan,
- height = 1,
- length = 4,
- layer = {"xxxx"}
- },
- {
- color = colors.blue,
- height = 2,
- length = 3,
- layer = {"x..","xxx"}
- },
- {
- color = colors.orange,
- height = 2,
- length = 3,
- layer = {"..x","xxx"}
- },
- {
- color = colors.yellow,
- height = 2,
- length = 2,
- layer = {"xx","xx"}
- },
- {
- color = colors.lime,
- height = 2,
- length = 3,
- layer = {".xx","xx."}
- },
- {
- color = colors.purple,
- height = 2,
- length = 3,
- layer = {".x.","xxx"}
- },
- {
- color = colors.red,
- height = 2,
- length = 3,
- layer = {"xx.",".xx"}
- }
- }
- local function drawBlock(block,x,y,rotation,next)
- layer = {}
- height = blocks[block].height
- length = blocks[block].length
- for c = 1,height do
- layer[c] = blocks[block].layer[c]
- end
- --rotates block
- if rotation == 1 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[height-b+1]:sub(a,a)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- elseif rotation == 2 then
- l = ""
- for a = 1,height do
- for b = 1,length do
- l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
- end
- layer[height-a+1] = l
- l = ""
- end
- elseif rotation == 3 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- end
- --draws block to screen
- for a = 1,height do
- term.setCursorPos(x+3,y+a+1)
- for b = 1,length do
- if layer[a]:sub(b,b) == "x" then
- term.setBackgroundColor(blocks[block].color)
- write(" ")
- if next ~= true then
- board[y+a-1] = board[y+a-1]:sub(1,x+b-2).."x"..board[y+a-1]:sub(x+b,10)
- end
- else
- term.setCursorPos(x+b+3,y+a+1)
- end
- end
- end
- end
- local function drawScore()
- term.setCursorPos(19,5)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- write("Score")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(22-(#tostring(score)/2),6)
- write(score)
- end
- local function removeBlock(block,x,y,rotation)
- layer = {}
- height = blocks[block].height
- length = blocks[block].length
- for c = 1,height do
- layer[c] = blocks[block].layer[c]
- end
- --rotates block
- if rotation == 1 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[height-b+1]:sub(a,a)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- elseif rotation == 2 then
- l = ""
- for a = 1,height do
- for b = 1,length do
- l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
- end
- layer[height-a+1] = l
- l = ""
- end
- elseif rotation == 3 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- end
- --draws block to screen
- for a = 1,height do
- term.setCursorPos(x+3,y+a+1)
- for b = 1,length do
- if layer[a]:sub(b,b) == "x" then
- term.setBackgroundColor(colors.white)
- write(" ")
- board[y+a-1] = board[y+a-1]:sub(1,x+b-2).."."..board[y+a-1]:sub(x+b,10)
- else
- term.setCursorPos(x+b+3,y+a+1)
- end
- end
- end
- end
- function checkRow(row)
- if board[row] == "xxxxxxxxxx" then
- for a = 1,row do
- board[row-a+1] = board[row-a]
- end
- board[1] = ".........."
- score = score+80
- drawScore()
- end
- end
- function moveBlock(block,x,y,rotation,direction)
- moving = true
- layer = {}
- local height = blocks[block].height
- local length = blocks[block].length
- for c = 1,height do
- layer[c] = blocks[block].layer[c]
- end
- --rotates block
- if rotation == 1 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[height-b+1]:sub(a,a)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- elseif rotation == 2 then
- l = ""
- for a = 1,height do
- for b = 1,length do
- l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
- end
- layer[height-a+1] = l
- l = ""
- end
- elseif rotation == 3 then
- l = ""
- for a = 1,length do
- for b = 1,height do
- l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
- end
- layer[a] = l
- l = ""
- end
- length = blocks[block].height
- height = blocks[block].length
- end
- local canMove = true
- --moves block
- if direction == 1 then
- for a = 1,height do
- for b = 1,length do
- if x+length > 10 then
- canMove = false
- elseif layer[a]:sub(b,b) == "x" and board[y+a-1]:sub(x+b,x+b) == "x" and layer[a]:sub(b+1,b+1) ~= "x" then
- canMove = false
- end
- end
- end
- if canMove then
- removeBlock(block,x,y,rotation)
- drawBlock(block,x+1,y,rotation,true)
- end
- elseif direction == 2 then
- for a = 1,height do
- for b = 1,length do
- if height+y > 16 then
- canMove = false
- elseif layer[a]:sub(b,b) == "x" and board[y+a]:sub(x+b-1,x+b-1) == "x" and layer[a+1]:sub(b,b) ~= "x" then
- canMove = false
- end
- end
- end
- if canMove then
- removeBlock(block,x,y,rotation)
- drawBlock(block,x,y+1,rotation,false)
- end
- elseif direction == 3 then
- for a = 1,height do
- for b = 1,length do
- if x < 2 then
- canMove = false
- elseif layer[a]:sub(b,b) == "x" and board[y+a-1]:sub(x-b,x-b) == "x" and layer[a]:sub(b-1,b-1) ~= "x" then
- canMove = false
- end
- end
- end
- if canMove then
- removeBlock(block,x,y,rotation)
- drawBlock(block,x-1,y,rotation)
- end
- end
- moving = false
- return canMove
- end
- function drawNextBlock(block)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(19,14)
- write("Next")
- term.setCursorPos(20,15)
- term.setBackgroundColor(colors.white)
- write(" ")
- term.setCursorPos(20,16)
- write(" ")
- drawBlock(block,17,13,0,true)
- end
- function drawBoard()
- for a = 1,16 do
- term.setCursorPos(32,2+a)
- write(board[a])
- end
- end
- moving = false
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,19)
- term.clearLine()
- paintutils.drawImage(outline,1,1)
- running = true
- currentBlock = math.random(1,7)
- nextBlock = math.random(1,7)
- drawNextBlock(nextBlock)
- drawBlock(currentBlock,4,1,0,false)
- local currentBlockPos = {4,1}
- local currentBlockRotation = 0
- os.startTimer(0.5)
- repeat
- drawScore()
- local e,button,x,y = os.pullEvent()
- if e == "timer" and not moving then
- if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,2) then
- currentBlockPos = {currentBlockPos[1],currentBlockPos[2]+1}
- else
- score = score + 2
- for a = 1,16 do
- checkRow(a)
- end
- currentBlock = nextBlock
- nextBlock = math.random(1,7)
- drawNextBlock(nextBlock)
- drawBlock(currentBlock,4,1,0,false)
- currentBlockPos = {4,1}
- currentBlockRotation = 0
- end
- drawBoard()
- os.startTimer(0.5)
- elseif e == "key" then
- key = keys.getName(button)
- if key == "left" then
- if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,3) then
- currentBlockPos[1] = currentBlockPos[1] - 1
- end
- elseif key == "right" then
- if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,1) then
- currentBlockPos[1] = currentBlockPos[1] + 1
- end
- end
- end
- until not running
Advertisement
Add Comment
Please, Sign In to add comment