Advertisement
Guest User

stacc.lua

a guest
Apr 26th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. -- Stacc, An game.
  2. -- Written by djm
  3. -- Don't look inside if you're easily disturbed by poor code.
  4. -- You have been warned.
  5.  
  6. local xSize = 7
  7. local ySize = 12
  8. local tileColour = colors.blue
  9. local bgColour = colours.black
  10. local blockChr = " "
  11.  
  12. local twoRow = 9  --row at which two squares is forced
  13. local oneRow = 5  --row at which one square is forced
  14.  
  15.  
  16. local jua = require("jua")
  17. local mon = peripheral.find("monitor")
  18.  
  19. local gameState = "idle"
  20. -- Possible modes: SUBJECT TO CHANGE
  21. --  idle -- no game in progress
  22. --  play -- gameplay active
  23. --  wait -- pauses in gameplay (maybe dont need)
  24.  
  25.  
  26. local tiles = {}           -- grid of tiles
  27. local scrollTiles = {}
  28. local scrollRow = {}
  29. local currentSize = 0
  30. local scrollDirection = ""
  31. local counter
  32. local delay   -- smaller values are faster
  33.  
  34. function setup()    -- for setting up / resetting game
  35.     tiles = {}           -- grid of tiles
  36.     for i = 1, ySize do
  37.         tiles[i] = {}
  38.         for j = 1, xSize do
  39.             tiles[i][j] = false
  40.         end
  41.     end
  42.    
  43.     scrollTiles = tiles[1]       -- storage for current line
  44.     scrollRow = ySize+1
  45.     currentSize = 3
  46.     scrollDirection = "right"
  47.     delay = 10
  48.     counter = 0
  49.     gameState = "play"
  50. end
  51.  
  52.  
  53. function displayTiles()
  54.     mon.clear()
  55.     for i = 1, ySize do
  56.         for j = 1, xSize do
  57.             if tiles[i][j] == true then
  58.                 mon.setBackgroundColour(tileColour)
  59.             else
  60.                 mon.setBackgroundColour(bgColour)
  61.             end
  62.             mon.setCursorPos(j, i)
  63.             mon.write(blockChr)
  64.         end
  65.     end
  66. end
  67.  
  68. function newBlock()
  69.     local block = {}
  70.     for i = 1, xSize do
  71.         if i <= currentSize then
  72.             block[i] = true
  73.         else   
  74.             block[i] = false
  75.         end
  76.     end
  77.     return block
  78. end
  79.  
  80. function scrollBlock()
  81.     counter = counter +1    --count every time is run
  82.     if counter % delay ~= 0 then return scrollTiles end -- only change when counter matches delay
  83.    
  84.     local block = table.pack(table.unpack(scrollTiles)) --copying tables hahahah wtf
  85.     if block[1] == true then
  86.         scrollDirection = "right"
  87.     elseif block[xSize] == true then
  88.         scrollDirection = "left"
  89.     end
  90.     for i = 1, xSize do
  91.         if scrollDirection == "right" then
  92.             if i == 1 then
  93.                 block[i] = false
  94.             else
  95.                 local nextT = i-1
  96.                 block[i] = scrollTiles[nextT]
  97.                
  98.             end
  99.         elseif scrollDirection == "left" then
  100.             if i == xSize then
  101.                 block[i] = false
  102.             else
  103.                 block[i] = scrollTiles[i+1]
  104.             end
  105.         end
  106.     end
  107.     return block
  108. end
  109.  
  110. function displayScroll()
  111.     for i = 1, xSize do
  112.         if scrollTiles[i] == true then
  113.             mon.setBackgroundColour(tileColour)
  114.         else
  115.             mon.setBackgroundColour(bgColour)
  116.         end
  117.         mon.setCursorPos(i, scrollRow)
  118.         mon.write(blockChr)
  119.     end
  120. end
  121.  
  122. function nextRow()
  123.     scrollRow = scrollRow-1
  124.     currentSize = 3
  125.     if      scrollRow <= oneRow then currentSize = 1
  126.     elseif  scrollRow <= twoRow then currentSize = 2 end
  127.     scrollTiles = newBlock()
  128. end
  129.  
  130. function placeRow() --something broke
  131.     local tilesLeft = {}
  132.     for i = 1, xSize do
  133.         tilesLeft[i] = false
  134.         if scrollTiles[i] == true then
  135.             if scrollRow == ySize then
  136.                 tilesLeft[i] = true
  137.             elseif tiles[scrollRow+1][i] == true then
  138.                 print(".")
  139.                 tilesLeft[i] = true
  140.             elseif scrollRow == 1 then
  141.                 --last row
  142.             end
  143.         end
  144.     end
  145.     return tilesLeft
  146. end
  147.  
  148. function setTile(row, tile, val)
  149.     if tiles[row] ~= nil then
  150.         if tiles[row][tile] ~= nil then
  151.             tiles[row][tile] = val
  152.         end
  153.     end
  154. end
  155.  
  156. setup()
  157.  
  158. jua.setInterval(function()
  159.         displayScroll()
  160.         scrollTiles = scrollBlock()
  161. end, 0.01)
  162.  
  163.  
  164.  
  165. jua.on("terminate", function()
  166.     printError("Terminated *_*")
  167.     mon.setBackgroundColour(bgColour)
  168.     mon.clear()
  169.     jua.stop()
  170. end)
  171.  
  172. jua.on("monitor_touch", function()
  173.     print("touched")
  174.     if gameState == "play" then
  175.         local row = placeRow()
  176.         nextRow()
  177.         tiles[scrollRow+1] = row
  178.        
  179.     end
  180. end)
  181.  
  182. jua.go(function()
  183.     print("go")
  184.     nextRow()
  185.  
  186. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement