Advertisement
Nezn

Battleship [OC]

Jul 23rd, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. --Реквайрики
  2. local g = require("component").gpu
  3. local term = require("term")
  4. local colors = require("colors")
  5. local event = require("event")
  6.  
  7. --Настройка экрана
  8. local w, h = 46, 14
  9. g.setResolution(w,h)
  10.  
  11. --Аргументы
  12. args = {...}
  13.  
  14. --Очищаем экран
  15. g.setBackground(colors.silver, true)
  16. term.clear()
  17.  
  18. --Переменные для кораблей
  19. local ships =  {{3, 3, 8},
  20.         {3, 5, 6},
  21.         {11, 5, 6},
  22.         {3, 7, 4},
  23.         {9, 7, 4},
  24.         {15, 7, 4},
  25.         {3, 9, 2},
  26.         {7, 9, 2},
  27.         {11, 9, 2},
  28.         {15, 9, 2}}
  29. local shipsE = {{0, 0, 8},
  30.         {0, 0, 6},
  31.         {0, 0, 6},
  32.         {0, 0, 4},
  33.         {0, 0, 4},
  34.         {0, 0, 4},
  35.         {0, 0, 2},
  36.         {0, 0, 2},
  37.         {0, 0, 2},
  38.         {0, 0, 2}}
  39.  
  40. --Переменные попаданий
  41. local shots = 0
  42. local shotsE = {{0, 0}}
  43. local shotsE2 = 0
  44.  
  45. --Пишем заголовок
  46. g.setBackground(colors.gray, true)
  47. g.fill(1,1,w,1," ")
  48. term.setCursor(math.floor(w/2-6),1)
  49. g.setForeground(colors.white, true)
  50. term.write("Морской Бой")
  51. g.setBackground(colors.black, true)
  52. term.setCursor(w-3,1)
  53. term.write("   ")
  54.  
  55. --Функция определения координаты для поля
  56. function makePixelX(x, b)
  57.     return b+math.floor((x-b)/2)*2
  58. end
  59.  
  60. --Рисуем кораблики
  61. function drawShips()
  62.     for i=1,10 do
  63.         g.setBackground(colors.brown, true)
  64.         g.fill(ships[i][1], ships[i][2], ships[i][3], 1, " ")
  65.     end
  66. end
  67.  
  68. --Автоматически установить кораблики
  69. function setShipsAuto(var)
  70.     local s = ships
  71.     if var ~= 25 then
  72.         s = shipsE 
  73.     end
  74.     for i=1,10 do
  75.         local x, y = 0, 0
  76.         local yes = true
  77.         while yes do
  78.             x = math.random(var, var+19)
  79.             y = math.random(3, 12)
  80.             if x+s[i][3]-1 < var+20 then
  81.                 for j=1,10 do
  82.                     if i ~= j and (x < s[j][1]+s[j][3]+2 and x+s[i][3] > s[j][1]-2 and y > s[j][2]-2 and y < s[j][2]+2) then
  83.                         x = math.random(var, var+19)
  84.                         y = math.random(3, 12)
  85.                         break
  86.                     elseif i ~= j and (j == 10 or (i == 10 and j == 9)) then
  87.                         s[i][1] = makePixelX(x, var)
  88.                         s[i][2] = y
  89.                         yes = false
  90.                     end
  91.                 end
  92.             end
  93.         end
  94.     end
  95.     if var == 25 then
  96.         ships = s
  97.     else
  98.         shipsE = s
  99.     end
  100. end
  101.  
  102. --Рисуем поле
  103. function drawField()
  104.     g.setBackground(colors.cyan, true)
  105.     g.fill(25,3,20,10," ")
  106.     g.setBackground(colors.lightblue, true)
  107.     if args[1] ~= "fast" then
  108.         for i=1,10 do
  109.             local delta = math.fmod(i,2)
  110.             for j=1,5 do
  111.                 g.fill(23+4*j-2*delta, i+2, 2, 1, " ")
  112.             end
  113.         end
  114.     end
  115. end
  116.  
  117. --Рисуем поле врага
  118. function drawFieldE()
  119.     g.setBackground(colors.cyan, true)
  120.     g.fill(3,3,20,10," ")
  121.     g.setBackground(colors.lightblue, true)
  122.     if args[1] ~= "fast" then
  123.         for i=1,10 do
  124.             local delta = math.fmod(i,2)
  125.             for j=1,5 do
  126.                 g.fill(1+4*j-2*delta, i+2, 2, 1, " ")
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. --Кнопка готово после рандома
  133. function drawButton2()
  134.     g.setBackground(colors.pink, true)
  135.     term.setCursor(13, 11)
  136.     term.write("  Готово  ")
  137. end
  138.  
  139. --Кнопка рандома своих кораблей
  140. function drawButton()
  141.     g.setBackground(colors.lime, true)
  142.     term.setCursor(3, 11)
  143.     term.write("  Авто  ")
  144. end
  145.  
  146. --Очищаем пустое место
  147. function clearShipsField()
  148.     g.setBackground(colors.silver, true)
  149.     g.fill(3,3,22,10," ")
  150. end
  151.  
  152. --Гуишечка
  153. drawField()
  154. drawShips()
  155. drawButton()
  156. g.setBackground(colors.silver, true)
  157. g.setForeground(colors.black, true)
  158. term.setCursor(3,13)
  159. term.write("Установите корабли")
  160.  
  161. --Цикл для установки своих корабликов вручную
  162. local ship = 0
  163. local prevX = 0
  164. local shipCoords = {0,0}
  165. local setting = true
  166. local playing = true
  167. local button2 = false
  168. while setting do
  169.     local event, _, x, y = event.pull()
  170.     if event == "touch" then
  171.         if x > 2 and x < 13 and y == 11 then
  172.             setShipsAuto(25)
  173.             drawField()
  174.             clearShipsField()
  175.             drawShips()
  176.             drawButton()
  177.             drawButton2()
  178.             button2 = true
  179.         elseif button2 and x > 12 and x < 24 and y == 11 then
  180.             setting = false
  181.             break
  182.         elseif x > w-4 and x < w and y == 1 then
  183.             setting = false
  184.             playing = false
  185.             break
  186.         end
  187.     elseif event == "drag" then
  188.         if ship == 0 then
  189.             for i=1,10 do
  190.                 if x > ships[i][1] and x < ships[i][1]+ships[i][3] and y == ships[i][2] then
  191.                     ship = i
  192.                     shipCoords[1] = ships[i][1]
  193.                     shipCoords[2] = ships[i][2]
  194.                     break
  195.                 end
  196.             end
  197.         else
  198.             ships[ship][1] = ships[ship][1] + x - prevX
  199.             ships[ship][2] = y
  200.             if ships[ship][1] > 2 and ships[ship][1]+ships[ship][3]-1 < 45 and y > 2 and y < 13 then
  201.                 drawField()
  202.                 clearShipsField()
  203.                 drawShips()
  204.                 drawButton()
  205.             end
  206.         end
  207.         prevX = x
  208.     elseif event == "drop" then
  209.         if ship > 0 then
  210.             if ships[ship][1] < 25 or ships[ship][1]+ships[ship][3]-1 > 45 or y < 3 or y > 13then
  211.                 ships[ship][1] = shipCoords[1]
  212.                 ships[ship][2] = shipCoords[2]
  213.             end
  214.             for i=1,10 do
  215.                 if i ~= ship and (ships[ship][1] < ships[i][1]+ships[i][3]+1 and ships[ship][1]+ships[ship][3]-1 > ships[i][1]-2 and ships[ship][2] > ships[i][2]-2 and ships[ship][2] < ships[i][2]+2) then
  216.                     ships[ship][1] = shipCoords[1]
  217.                     ships[ship][2] = shipCoords[2]
  218.                     break
  219.                 end
  220.             end
  221.             ships[ship][1] = makePixelX(ships[ship][1], 25)
  222.         end
  223.         ship = 0
  224.         drawField()
  225.         clearShipsField()
  226.         drawShips()
  227.         drawButton()
  228.         for i=1,10 do
  229.             if ships[i][1] < 25 then
  230.                 break
  231.             elseif i == 10 then
  232.                 setting = false
  233.                 break
  234.             end
  235.         end
  236.     end
  237. end
  238.  
  239. --Следующий цикл для игры
  240. setShipsAuto(3)
  241. drawFieldE()
  242. g.setBackground(colors.silver, true)
  243. g.setForeground(colors.black, true)
  244. term.setCursor(3,13)
  245. term.write("Противник          ")
  246. term.setCursor(25,13)
  247. term.write("Вы")
  248. g.setBackground(colors.magenta, true)
  249. g.fill(23, 3, 2, 10, " ")
  250. while playing do
  251.     local event, _, x, y = event.pull()
  252.     if event == "touch" then
  253.         if shots < 20 and shotsE2 < 20 and x > 2 and x < 23 and y > 2 and y < 13 then
  254.             x = makePixelX(x, 3)
  255.             for i=1,10 do
  256.                 if x > shipsE[i][1]-1 and x < shipsE[i][1]+shipsE[i][3] and y == shipsE[i][2] then
  257.                     shots = shots + 1
  258.                     g.setBackground(colors.red, true)
  259.                     break
  260.                 end
  261.                 g.setBackground(colors.blue, true)
  262.             end
  263.             g.fill(x, y, 2, 1, " ")
  264.             local yes = true
  265.             local xE, yE = 0, 0
  266.             while yes do
  267.                 xE = makePixelX(math.random(25,44), 3)
  268.                 yE = math.random(3,12)
  269.                 for i=1,#shotsE do
  270.                     if xE == shotsE[i][1] and yE == shotsE[i][2] then
  271.                         break
  272.                     elseif i == #shotsE then
  273.                         yes = false
  274.                         break
  275.                     end
  276.                 end
  277.             end
  278.             table.insert(shotsE, {makePixelX(xE, 3), yE})
  279.             if args[2] ~= "notime" then
  280.                 g.setBackground(colors.purple, true)
  281.                 g.fill(23, 3, 2, 10, " ")
  282.                 os.sleep(math.floor(math.random(2))-0.5)
  283.                 g.setBackground(colors.magenta, true)
  284.                 g.fill(23, 3, 2, 10, " ")
  285.             end
  286.             for i=1,10 do
  287.                 if xE > ships[i][1]-1 and xE < ships[i][1]+ships[i][3] and yE == ships[i][2] then
  288.                     shotsE2 = shotsE2 + 1
  289.                     g.setBackground(colors.red, true)
  290.                     break
  291.                 end
  292.                 g.setBackground(colors.blue, true)
  293.             end
  294.             g.fill(xE, yE, 2, 1, " ")
  295.             if shots == 20 or shotsE2 == 20 then
  296.                 g.setBackground(colors.silver, true)
  297.                 g.fill(2, 3, 43, 12, " ")
  298.                 g.setBackground(colors.white, true)
  299.                 g.fill(15, 5, 16, 3, " ")
  300.                
  301.                 if shots == 20 then
  302.                     term.setCursor(20, 6)
  303.                     term.write("Победа")
  304.                 elseif shotsE2 == 20 then
  305.                     term.setCursor(18, 6)
  306.                     term.write("Поражение")
  307.                 end
  308.             end
  309.         elseif x > w-4 and x < w and y == 1 then
  310.             playing = false
  311.             break
  312.         end
  313.     end
  314. end
  315.  
  316. --При выходе
  317. g.setForeground(colors.white, true)
  318. g.setBackground(colors.black, true)
  319. term.clear()
  320. g.setResolution(g.maxResolution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement