LDDestroier

c4test

Aug 6th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. -- X and Y dimensions of the playfield
  2. local boardX, boardY = 7, 6
  3. -- Colors of the different sides
  4. local pone_col, ptwo_col = colors.black, colors.red
  5. -- Color of board and board frame
  6. local board_col, boardframe_col = colors.yellow, colors.blue
  7. -- Color of background
  8. local bg_col = colors.cyan
  9.  
  10. local scr_x,scr_y = term.getSize()
  11.  
  12. local makeNewBoard = function()
  13.     local output = {}
  14.     for a = 1, boardY do
  15.         output[#output+1] = ("0"):rep(boardX)
  16.     end
  17.     return output
  18. end
  19.  
  20. local grid = makeNewBoard()
  21.  
  22. local substrrepl = function(str,char,pos)
  23.     return str:sub(1,pos-1)..char:sub(1,1)..str:sub(pos+1)
  24. end
  25.  
  26. local placePiece = function(pos,side)
  27.     grid[1] = substrrepl(grid[1],side,pos)
  28. end
  29.  
  30. local boardGravity = function()
  31.     local hadFallen = false
  32.     for y = 1, boardY-1 do
  33.         for x = 1, boardX do
  34.             if grid[y+1]:sub(x,x) == "0" then
  35.                 grid[y+1] = substrrepl(grid[y+1],grid[y]:sub(x,x),x)
  36.                 grid[y] = substrrepl(grid[y],"0",x)
  37.                 hadFallen = true
  38.             end
  39.         end
  40.     end
  41.     return hadFallen
  42. end
  43.  
  44. local renderBoard = function(row,posX,posY)
  45.     for y = 1, boardX do
  46.         for x = row or 1, row or boardY do
  47.             term.setCursorPos(posX + x, posY + y)
  48.             if grid[y][x] == "0" then
  49.                 term.setTextColor(bg_col)
  50.             elseif grid[y][x] == "1" then
  51.                 term.setTextColor(pone_col)
  52.             elseif grid[y][x] == "2" then
  53.                 term.setTextColor(ptwo_col)
  54.             end
  55.             term.setBackgroundColor(bg_col)
  56.             term.clearLine()
  57.             term.setBackgroundColor(board_col)
  58.             term.write(string.char(7))
  59.         end
  60.     end
  61. end
Add Comment
Please, Sign In to add comment