Advertisement
Tomlacko

Shades Game for ComputerCraft

Apr 13th, 2016
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. --Created by Tomlacko
  2. --recreation of the mobile game Shades
  3.  
  4. function clr()
  5.   m.clear()
  6.   m.setCursorPos(1,1)
  7. end
  8.  
  9. function clrterm()
  10.   term.clear()
  11.   term.setCursorPos(1,1)
  12. end
  13.  
  14. function setColor(color)
  15.   m.setBackgroundColor(color)
  16. end
  17.  
  18. function draw(x, y)
  19.   m.setCursorPos(x, y)
  20.   m.write(" ")
  21. end
  22.  
  23. function genBlock()
  24.   a = math.random(1, diff+2)
  25.   if a == 1 then return colors.white
  26.   elseif a == 2 then return colors.lime
  27.   elseif a == 3 then return colors.green
  28.   elseif a == 4 then return colors.brown
  29.   else return colors.black
  30.   end
  31. end
  32.  
  33. function stackBlock(curColor)
  34.   if curColor == colors.white then return colors.lime
  35.   elseif curColor == colors.lime then return colors.green
  36.   elseif curColor == colors.green then return colors.brown
  37.   else return colors.black
  38.   end
  39. end
  40.  
  41. function drawPanel()
  42.   m.setCursorPos(1,1)
  43.   setColor(colors.black)
  44.   m.write("Blocks: "..cycle.."   Score: "..score)
  45.   m.setCursorPos(sizeX-7,1)
  46.   m.write("Level: "..diff)
  47.  
  48.   setColor(nxt)
  49.   for i=1, sizeX do
  50.     draw(i,2)
  51.   end
  52. end
  53.  
  54. function drawStatic()
  55.   for col=1, 4 do
  56.     for row=2, 10 do
  57.       if blocks[col][row] == 0 then
  58.         if quick and col==posX then
  59.           setColor(colors.lightGray)
  60.         else
  61.           setColor(colors.gray)
  62.       end
  63.       else
  64.         setColor(blocks[col][row])
  65.       end
  66.       for i=(col*9)-8, col*9 do
  67.         draw(i, row*2)
  68.         draw(i, (row*2)-1)
  69.       end
  70.     end
  71.   end
  72. end
  73.  
  74. function drawBlock()
  75.   setColor(cur)
  76.   for i=(posX*9)-8, posX*9 do
  77.     draw(i, posY)
  78.     draw(i, posY-1)
  79.   end
  80. end
  81.  
  82. function drawPause()
  83.   setColor(colors.black)
  84.   m.setCursorPos(16, 10)
  85.   m.write("Paused")
  86.   setColor(colors.lightGray)
  87.   m.write(" ")
  88.   m.setCursorPos(15, 9)
  89.   m.write("        ")
  90.   m.setCursorPos(15, 10)
  91.   m.write(" ")
  92.   m.setCursorPos(15, 11)
  93.   m.write("        ")
  94. end
  95.  
  96. function drawExit()
  97.   setColor(colors.black)
  98.   m.setCursorPos(15, 14)
  99.   m.write("Continue")
  100.   setColor(colors.lightGray)
  101.   m.write(" ")
  102.   m.setCursorPos(14, 14)
  103.   m.write(" ")
  104.   m.setCursorPos(14, 16)
  105.   m.write(" ")
  106.   m.setCursorPos(14, 13)
  107.   m.write("          ")
  108.   m.setCursorPos(14, 15)
  109.   m.write("          ")
  110.   m.setCursorPos(14, 17)
  111.   m.write("          ")
  112.   setColor(colors.black)
  113.   m.setCursorPos(15, 16)
  114.   m.write("  Exit  ")
  115.   setColor(colors.lightGray)
  116.   m.write(" ")
  117. end
  118.  
  119. ----------------------------------------------------------------------------
  120. ----------------------------------------------------------------------------
  121.  
  122. term.setBackgroundColor(colors.black)
  123. term.setTextColor(colors.white)
  124. clrterm()
  125. repeat
  126.   print("Touch the monitor to start the game...")
  127.   event, side = os.pullEvent("monitor_touch")
  128.   m = peripheral.wrap(side)
  129.   m.setTextScale(2)
  130.   sizeX, sizeY = m.getSize()
  131.   if not(sizeX == 36 and sizeY == 20) then
  132.     clrterm()
  133.     print("Monitor doesn't have the right size!")
  134.     print("You need an 7x6 monitor to play this game.")
  135.     print(" ")
  136.   else
  137.     setColor(colors.black)
  138.     m.setTextColor(colors.white)
  139.     clr()
  140.   end
  141. until sizeX == 36 and sizeY == 20
  142. clrterm()
  143. print("Touch the top of the monitor to pause the game...")
  144.  
  145. score = 0
  146. diff = 1
  147. cycle = 0
  148. cur = genBlock()
  149. nxt = genBlock()
  150. blocks = {}
  151. for i=1, 4 do
  152.   blocks[i] = {}
  153.   for j=2, 10 do
  154.     blocks[i][j] = 0
  155.   end
  156. end
  157. posY = 4
  158. posX = 2
  159. new = true
  160. stack = 0
  161. speed = 0.3
  162. quick = false
  163.  
  164. while true do
  165.   if (posY%2 == 0 and blocks[posX][(posY/2)+1] ~= 0 and (blocks[posX][(posY/2)+1] ~= cur or blocks[posX][(posY/2)+1] == colors.black)) or posY == 20 then
  166.     cycle = cycle+1
  167.     score = score+2
  168.     if stack>0 then
  169.       score = score + (((stack+1)*2)*stack)
  170.       stack = 0
  171.     end
  172.     blocks[posX][posY/2] = cur
  173.     if blocks[1][(posY/2)] == cur and blocks[2][(posY/2)] == cur and blocks[3][(posY/2)] == cur and blocks[4][(posY/2)] == cur then
  174.       if cur == colors.white then score = score + 20
  175.       elseif cur == colors.lime then score = score + 40
  176.       elseif cur == colors.green then score = score + 60
  177.       elseif cur == colors.brown then score = score + 80
  178.       elseif cur == colors.black then score = score + 100
  179.       end
  180.       for x = 1, 4 do
  181.         for y = posY/2, 3, -1 do
  182.           blocks[x][y] = blocks[x][y-1]
  183.         end
  184.         blocks[x][2] = 0
  185.       end
  186.     end
  187.     cur = nxt
  188.     nxt = genBlock()
  189.     posY = 4
  190.     new = true
  191.     quick = false
  192.   end
  193.  
  194.   if new then
  195.     new = false
  196.   else
  197.     posY = posY+1
  198.   end
  199.   if cycle == 50 then
  200.     diff = 2
  201.     speed = 0.2
  202.   elseif cycle == 100 then
  203.     diff = 3
  204.     speed = 0.15
  205.   end
  206.   if posY%2 == 0 and blocks[posX][(posY/2)] == cur then
  207.     cur = stackBlock(cur)
  208.     stack = stack+1
  209.     blocks[posX][(posY/2)] = 0
  210.   end
  211.  
  212.   if quick then
  213.     timID = os.startTimer(0.05)
  214.   else
  215.     timID = os.startTimer(speed)
  216.   end
  217.  
  218.   event, side, x, y = os.pullEvent()
  219.   while not(event == "monitor_touch" or (event == "timer" and side == timID)) do
  220.     event, side, x, y = os.pullEvent()
  221.   end
  222.   if event == "monitor_touch" then
  223.     if y == 1 then
  224.       drawPause()
  225.       drawExit()
  226.       ev, si, xd, yd = os.pullEvent("monitor_touch")
  227.       while true do
  228.         if xd > 14 and xd < 23 and yd == 14 then
  229.           break
  230.         elseif xd > 14 and xd < 23 and yd == 16 then
  231.           setColor(colors.black)
  232.           m.clear()
  233.           m.setTextScale(1)
  234.           error("Game stopped.")
  235.         end
  236.         ev, si, xd, yd = os.pullEvent("monitor_touch")
  237.       end
  238.     elseif math.floor((x+8)/9) ~= posX then
  239.       if not(blocks[math.floor((x+8)/9)][(posY+(posY%2))/2] ~= 0 or stack>0) then
  240.         posX = math.floor((x+8)/9)
  241.       end
  242.     else
  243.       quick = true
  244.     end
  245.   end
  246.  
  247.   drawPanel()
  248.   drawStatic()
  249.   drawBlock()
  250.  
  251.   if posY == 4 and blocks[posX][(posY/2)+1] ~= 0 and blocks[posX][(posY/2)+1] ~= cur then break end
  252. end
  253.  
  254. setColor(colors.lightGray)
  255. m.setCursorPos(16, 9)
  256. m.write("      ")
  257. m.setCursorPos(15, 10)
  258. m.write("  ")
  259. m.setCursorPos(15, 11)
  260. m.write("  ")
  261. m.setCursorPos(16, 12)
  262. m.write("      ")
  263. m.setCursorPos(21, 10)
  264. m.write("  ")
  265. m.setCursorPos(21, 11)
  266. m.write("  ")
  267. setColor(colors.black)
  268. m.setCursorPos(17, 10)
  269. m.write("GAME")
  270. m.setCursorPos(17, 11)
  271. m.write("OVER")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement