astral17

sameGame

Mar 25th, 2018
218
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local gui = require("AGUI") -- pastebin get s4UFSFwn /lib/AGUI.lua
  2.  
  3.  
  4. ----------SETTINGS----------
  5. local cfg = {}
  6. cfg.mapWidth = 80
  7. cfg.mapHeight = 49
  8. --local SUPERQUALITY = true
  9. --local GENERATORMODE = 0 -- 0 - absolute random; 1 - specific random
  10. local startX = 1
  11. local startY = 2
  12. local colors = {0xff0000, 0x00ff00, 0x0000ff}--, 0xffffff}
  13. colors[0] = 0x000000
  14.  
  15. function scoreFormula(blocks)
  16.   if blocks < 1 then
  17.     return 0
  18.   end
  19.   return (blocks - 1) * (blocks - 1)
  20. end
  21.  
  22. ----------------------------
  23.  
  24. local event = require("event")
  25. local gpu = require("component").gpu
  26. local term = require("term")
  27.  
  28. local score = 0
  29. local map = {}
  30.  
  31. function drawBlock(x, y)
  32.   gpu.setBackground(colors[ map[y][x] ])
  33.   gpu.set((startX + x - 1) * 2 - 1, startY + y - 1, "  ")
  34. end
  35.  
  36. function fillBlocks(x, y, w, h)
  37.   gpu.setBackground(0x000000)
  38.   gpu.fill((startX + x - 1) * 2 - 1, startY + y - 1, w * 2, h, " ")
  39. end
  40.  
  41. function drawScore(score)
  42.   gpu.setBackground(0x000000)
  43.   gpu.setForeground(0xffffff)
  44.   gpu.set(10, 1, "score: " .. tostring(score))
  45. end
  46.  
  47. function drawMap(startX, startY)
  48.   drawScore(score)
  49.   if startX == nil then
  50.     startX = 1
  51.   end
  52.   if startY == nil then
  53.     startY = 1
  54.   end
  55. --  term.clear()
  56.   for y = 1, cfg.mapHeight do
  57.     for x = 1, cfg.mapWidth do
  58.       gpu.setBackground(colors[ map[y][x] ])
  59.       gpu.set((startX + x - 1) * 2 - 1, startY + y - 1, "  ")
  60.     end
  61.   end
  62. end
  63.  
  64. compressXCount = {}
  65.  
  66. function compressXPrecalc(noCheat) -- cheat if generator no create empty cell
  67.   if noCheat == nil then
  68.     for x = 1, cfg.mapWidth do
  69.       compressXCount[x] = cfg.mapHeight
  70.     end
  71.   else
  72.     for x = 1, cfg.mapWidth do
  73.       for y = 1, cfg.mapHeight do
  74.         if map[y][x] ~= 0 then
  75.           compressXCount[x] = compressXCount[x] + 1
  76.         end
  77.       end
  78.     end
  79.   end
  80. end
  81.  
  82. function mapGenerate()
  83.   map = {}
  84.   for i = 1, cfg.mapHeight do
  85.     table.insert(map,{})
  86.   end
  87.   for y = 1, cfg.mapHeight do
  88.     for x = 1, cfg.mapWidth do
  89.       map[y][x] = math.random(1,#colors)
  90.     end
  91.   end
  92.   compressXPrecalc()
  93.   compressYQueue = {}
  94. end
  95.  
  96. function convertX(x)
  97.   return math.floor((x - 1) / 2) + 2 - startX
  98. end
  99.  
  100. function convertY(y)
  101.   return y - startY + 1
  102. end
  103.  
  104. function canDestroy(x, y)
  105.   if x > 1 then
  106.     if map[y][x - 1] == map[y][x] then
  107.       return true
  108.     end
  109.   end
  110.   if x < cfg.mapWidth then
  111.     if map[y][x + 1] == map[y][x] then
  112.       return true
  113.     end
  114.   end
  115.   if y > 1 then
  116.     if map[y - 1][x] == map[y][x] then
  117.       return true
  118.     end
  119.   end
  120.   if y < cfg.mapHeight then
  121.     if map[y + 1][x] == map[y][x] then
  122.       return true
  123.     end
  124.   end
  125.   return false
  126. end
  127.  
  128. --compressXQueue = {}
  129. compressYQueue = {}
  130.  
  131. function destroy(x, y, draw)
  132. --  print(x, y)
  133.   if map[y][x] == 0 then
  134.     return 0
  135.   end
  136.   compressXCount[x] = compressXCount[x] - 1
  137. --  if compressXCount[x] == 0 then
  138. --    table.insert(compressXQueue, x)
  139. --  end
  140.   compressYQueue[x] = true
  141.   local count = 1
  142.   local saved = map[y][x]
  143.   map[y][x] = 0
  144.   if draw == true then
  145.     drawBlock(x, y)
  146.   end
  147.   if x > 1 then
  148.     if map[y][x - 1] == saved then
  149.       count = count + destroy(x - 1, y)
  150.     end
  151.   end
  152.   if x < cfg.mapWidth then
  153.     if map[y][x + 1] == saved then
  154.       count = count + destroy(x + 1, y)
  155.     end
  156.   end
  157.   if y > 1 then
  158.     if map[y - 1][x] == saved then
  159.       count = count + destroy(x, y - 1)
  160.     end
  161.   end
  162.   if y < cfg.mapHeight then
  163.     if map[y + 1][x] == saved then
  164.       count = count + destroy(x, y + 1)
  165.     end
  166.   end
  167.   return count
  168. end
  169.  
  170. function compressX()
  171.   offset = 0
  172.   for x = 1, cfg.mapWidth do
  173. --  for _, x in pairs(compressXQueue) do
  174.     b = (compressXCount[x] == 0)
  175. --[[
  176.     for y = 1, cfg.mapHeight do
  177.       if map[y][x] ~= 0 then
  178.         b = false
  179.         break
  180.       end
  181.     end--]]
  182.     if b then
  183.       gpu.copy((startX + x - offset) * 2 - 1, startY, cfg.mapWidth * 2, cfg.mapHeight, -2, 0)
  184.       offset = offset + 1
  185.     elseif offset > 0 then
  186.       for y = 1, cfg.mapHeight do
  187.         map[y][x - offset] = map[y][x]
  188.         map[y][x] = 0
  189.       end
  190.       compressXCount[x - offset] = compressXCount[x]
  191.     end
  192.   end
  193.   compressXQueue = {}
  194.   fillBlocks(cfg.mapWidth - offset + 1, 1, offset, cfg.mapHeight)
  195. end
  196.  
  197. function compressY()
  198. --  for x = 1, cfg.mapWidth do
  199.   for x in pairs(compressYQueue) do
  200.     offset = 0
  201.     for y = cfg.mapHeight, 1, -1 do
  202.       if map[y][x] == 0 then
  203. --        drawBlock(x, y)
  204.         offset = offset + 1
  205.       elseif offset > 0 then
  206.         map[y + offset][x] = map[y][x]
  207.         map[y][x] = 0
  208.         drawBlock(x, y + offset)
  209.         drawBlock(x, y)
  210.       end
  211.     end
  212.     fillBlocks(x, 1, 1, offset)
  213. --[[
  214.     for y = 1, offset do
  215.       drawBlock(x, y)
  216.     end--]]
  217.   end
  218.   compressYQueue = {}
  219. end
  220.  
  221. function onTouch(_, _, x, y, _, name)
  222. --  print(x,y,name)
  223.   x = convertX(x)
  224.   y = convertY(y)
  225. --  print(x,y,canDestroy(x,y))
  226.   if canDestroy(x, y) then
  227.     score = score + scoreFormula(destroy(x, y))
  228.     --print("DESTROYED")
  229.     compressY()
  230.     compressX()
  231.     drawScore(score)
  232. --    drawMap(startX, startY)
  233.   end
  234. end
  235.  
  236. -------------------- GUI --------------------
  237. gui.Init()
  238.  
  239. GameForm = gui.backend.Form:Create(cfg.mapWidth * 2, cfg.mapHeight + 1,
  240.   {
  241.     gui.backend.Button:Create(1, 1, 8, 1, "[Return]", function()
  242.       event.ignore("touch", onTouch)
  243.       GameForm:Disable(true)
  244.       MainForm:Enable():Paint()
  245.     end),
  246.   }
  247. ):Init()
  248.  
  249. SettingsForm = gui.backend.Form:Create(20, 11,
  250.   {
  251.     gui.backend.Text:Create(1, 1, 20, "Settings"),
  252.    
  253.     gui.backend.Text:Create(1, 3, nil, "Width"),
  254.     Width = gui.backend.TextBox:Create(1, 4, 20, cfg.mapWidth .. "", "0123456789"),
  255.    
  256.     gui.backend.Text:Create(1, 6, nil, "Height"),
  257.     Height = gui.backend.TextBox:Create(1, 7, 20, cfg.mapHeight .. "", "0123456789"),
  258.    
  259.     gui.backend.Button:Create(1, 9, 20, 3, "Back", function()
  260.       cfg.mapWidth = SettingsForm.Elements["Width"].Text + 0
  261.       cfg.mapHeight = SettingsForm.Elements["Height"].Text + 0
  262.       GameForm.Width = cfg.mapWidth * 2
  263.       GameForm.Height = cfg.mapHeight + 1
  264.       SettingsForm:Disable(true)
  265.       MainForm:Enable():Paint()
  266.     end),
  267.   }
  268. ):Init()
  269.  
  270. MainForm = gui.backend.Form:Create(20, 11,
  271.   {
  272.     gui.backend.Button:Create(1, 1, 20, 3, "Start", function()
  273.       MainForm:Disable(true)
  274.       GameForm:Enable():Paint()
  275.       mapGenerate()
  276.       score = 0
  277.       drawMap(startX, startY)
  278.       -- event.ignore("touch", onTouch)
  279.       event.listen("touch", onTouch)
  280.     end),
  281.     gui.backend.Button:Create(1, 5, 20, 3, "Settings", function() MainForm:Disable(true) SettingsForm:Enable():Paint() end),
  282.     gui.backend.Button:Create(1, 9, 20, 3, "Exit", function() quit = true end),
  283.   }
  284. )
  285.  
  286. MainForm:Init():Enable():Paint()
  287.  
  288. quit = false
  289. pcall(function()
  290.   while not quit do
  291.     event.pull()
  292.   end
  293. end)
  294.  
  295. -- gpu.setBackground(0x000000)
  296. -- term.clear()
  297. -- mapGenerate()
  298. -- drawMap(startX, startY)
  299.  
  300. -- event.pull("key_down")
  301. gui.Destroy()
  302. event.ignore("touch",onTouch)
  303. --]]
  304. --drawMap(startX, startY)
  305. gpu.setBackground(0x000000)
  306. gpu.setForeground(0xffffff)
RAW Paste Data