Advertisement
Asummonster

OpenTanks [OpenComputers]

Jul 11th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.23 KB | None | 0 0
  1. --===========================КАРТА=====================--
  2. --ПЕРЕМЕННЫЕ - ДАННЫЕ О КАРТЕ/ИГРОКЕ
  3. texture={}
  4. map_background=0x355E3B
  5. texture.bullets_number=20
  6. FUEL=1000
  7. map_label="Заминированное☢болото"
  8. map_resolution={}
  9. map_resolution.x=80
  10. map_resolution.y=25
  11. myPosX=2
  12. myPosY=2
  13. SpawnX=12
  14. SpawnY=12
  15. HEALTH=20
  16. CANSHOOT=true
  17. texture.mines={}
  18. collizions={}
  19. direction="right"
  20. lastKey=""
  21. DESTROYED=false
  22. --БИБЛИОТЕКИ
  23. gpu=require("component").gpu
  24. term=require("term")
  25. keyboard=require("keyboard")
  26. event=require("event")
  27. unicode=require("unicode")
  28. --ТЕКСТУРЫ
  29. texture.myTank="■"
  30. texture.wall="█"
  31. texture.duloYB="↑"
  32. texture.duloYM="↓"
  33. texture.duloXB="→"
  34. texture.duloXM="←"
  35. texture.bullet="✺"
  36. texture.mine_default="×"
  37. texture.mine_nuclear="☢"
  38. texture.mine_default_boomed="☼"
  39. --КОНЕЦ ТЕКСТУР
  40. --======================КОНЕЦ КАРТЫ=======================--
  41. --ФУНКЦИИ
  42. W, H = gpu.getResolution()
  43. function printTheTank()
  44.     createCollizion(myPosX, myPosY, "myTank", 0xffffff, "myTANK")
  45.     if direction=="right" then
  46.         createCollizion(myPosX+1, myPosY, "duloXB", 0xffffff, "myDULO")
  47.     elseif direction=="left" then
  48.         createCollizion(myPosX-1, myPosY, "duloXM", 0xffffff, "myDULO")
  49.     elseif direction=="up" then
  50.         createCollizion(myPosX, myPosY-1, "duloYB", 0xffffff, "myDULO")
  51.     elseif direction=="down" then
  52.         createCollizion(myPosX, myPosY+1, "duloYM", 0xffffff, "myDULO")
  53.     end
  54. end
  55. function hit_calculation_x(BULL)
  56.     for NUM=1, #texture.mines do
  57.         local x, y = texture.mines[NUM].x, texture.mines[NUM].y
  58.         if x==BULL and y==myPosY then
  59.             texture.mines[NUM]=nil
  60.             createCollizion(x, y, "mine_default_boomed", 0xffffff)
  61.             _G.NEWSHOOT=false
  62.         end
  63.     end
  64.     for NUMCOLLIZION=1, #collizions do
  65.         local x, y = collizions[NUMCOLLIZION].x, collizions[NUMCOLLIZION].y
  66.         if x==BULL and y==myPosY then
  67.             _G.NEWSHOOT=false
  68.         end
  69.     end
  70. end
  71. function hit_calculation_y(BULL)
  72.     for NUM=1, #texture.mines do
  73.         local x, y = texture.mines[NUM].x, texture.mines[NUM].y
  74.         if x==myPosX and y==BULL then
  75.             texture.mines[NUM]=nil
  76.             createCollizion(x, y, "mine_default_boomed", 0xffffff)
  77.             _G.NEWSHOOT=false
  78.         end
  79.     end
  80.     for NUMCOLLIZION=1, #collizions do
  81.         local x, y, opsisanie = collizions[NUMCOLLIZION].x, collizions[NUMCOLLIZION].y, collizions[NUMCOLLIZION].descrption
  82.         if x==myPosX and y==BULL and opsisanie~="myDULO" then
  83.             _G.NEWSHOOT=false
  84.         end
  85.     end
  86. end
  87. function shoot()
  88.     if texture.bullets_number~=0 then
  89.         _G.NEWSHOOT=true
  90.         if direction=="right" then
  91.             for BULL=myPosX+1, W do
  92.                 if _G.NEWSHOOT==true then
  93.                     printTheTank()
  94.                     printTheData()
  95.                     gpu.set(BULL, myPosY, texture.bullet)
  96.                     gpu.set(BULL-1, myPosY, " ")
  97.                     hit_calculation_x(BULL)
  98.                     os.sleep(0.0000000000000000000001)
  99.                 end
  100.                 printTheTank()
  101.                 printTheData()
  102.             end
  103.         elseif direction=="left" then
  104.             for BULL=myPosX-1, 0, -1 do
  105.                 if _G.NEWSHOOT==true then
  106.                     printTheTank()
  107.                     printTheData()
  108.                     gpu.set(BULL, myPosY, texture.bullet)
  109.                     gpu.set(BULL+1, myPosY, " ")
  110.                     hit_calculation_x(BULL)
  111.                     os.sleep(0.0000000000000000000001)
  112.                 end
  113.                 printTheTank()
  114.                 printTheData()
  115.             end
  116.         elseif direction=="up" then
  117.             for BULL=myPosY-1, -1, -1 do
  118.                 if _G.NEWSHOOT==true then
  119.                     printTheTank()
  120.                     printTheData()
  121.                     gpu.set(myPosX, BULL, texture.bullet)
  122.                     gpu.set(myPosX, BULL+1, " ")
  123.                     hit_calculation_y(BULL)
  124.                     os.sleep(0.0000000000000000000001)
  125.                 end
  126.                 printTheTank()
  127.                 printTheData()
  128.             end
  129.         elseif direction=="down" then
  130.             for BULL=myPosY+1, H+1 do
  131.                 if _G.NEWSHOOT==true then
  132.                     printTheTank()
  133.                     printTheData()
  134.                     gpu.set(myPosX, BULL, texture.bullet)
  135.                     gpu.set(myPosX, BULL-1, " ")
  136.                     hit_calculation_y(BULL)
  137.                     os.sleep(0.0000000000000000000001)
  138.                 end
  139.                 printTheTank()
  140.                 printTheData()
  141.             end
  142.         end
  143.         texture.bullets_number=texture.bullets_number-1
  144.         printTheData()
  145.     end
  146. end
  147. function clearTheTank()
  148.     collizions["myTANK"]=nil
  149.     collizions["myDULO"]=nil
  150. end
  151. function createMine(x, y, power)
  152.     tmp={}
  153.     tmp.x=x
  154.     tmp.y=y
  155.     tmp.power=power
  156.     table.insert(texture.mines, tmp)
  157.     gpu.set(x, y, texture.mine_default)
  158. end
  159. function checkMines()
  160.     if type(HEALTH)=="number" then
  161.         for i=1, #texture.mines do
  162.             local x, y = texture.mines[i].x, texture.mines[i].y
  163.             if x==myPosX and y==myPosY then
  164.                     HEALTH=HEALTH-texture.mines[i].power
  165.                     texture.mines[i]=nil
  166.                     createCollizion(x, y, "mine_default_boomed", 0xffffff)
  167.             end
  168.         end
  169.     end
  170. end
  171. function printTheMines()
  172.     for MINE=1, #texture.mines do
  173.         local x, y = texture.mines[MINE].x, texture.mines[MINE].y
  174.         gpu.set(x, y, texture.mine_default)
  175.     end
  176. end
  177. function checkHealth()
  178.     if type(HEALTH)=="number" then
  179.         if HEALTH<=0 then
  180.             DESTROYED=true
  181.             clearTheTank()
  182.             FUEL="ТОПЛИВО СГОРЕЛО"
  183.             HEALTH="ТАНК УНИЧТОЖЕН"
  184.             texture.bullets_number="БОЕУКЛАДКА УНИЧТОЖЕНА"
  185.             printTheData()
  186.         end
  187.     end
  188. end
  189. function move()
  190.     printTheData()
  191.     if myPosX-2>-1 and myPosX+2<W+1 and myPosY-2>-1 and myPosY+2<H+1 then
  192.         if lastKey=="w" then
  193.             if  direction=="up" and canIGoForward() and canIGoForwardD() then
  194.                 if type(FUEL)=="number" then
  195.                     if FUEL<=0 then
  196.                         return false
  197.                     else
  198.                         FUEL=FUEL-1
  199.                     end
  200.                 end
  201.                 clearTheTank()
  202.                 myPosY=myPosY-1
  203.                 printTheTank()
  204.             else
  205.                 direction="up"
  206.                 clearTheTank()
  207.                 printTheTank()
  208.             end
  209.         elseif lastKey=="d" then
  210.             if direction=="right" and canIGoForward() and canIGoForwardD() then
  211.                 if type(FUEL)=="number" then
  212.                     if FUEL<=0 then
  213.                         return false
  214.                     else
  215.                         FUEL=FUEL-1
  216.                     end
  217.                 end
  218.                 clearTheTank()
  219.                 myPosX=myPosX+1
  220.                 printTheTank()
  221.             else
  222.                 direction="right"
  223.                 clearTheTank()
  224.                 printTheTank()
  225.             end
  226.         elseif lastKey=="a" then
  227.             if direction=="left" and canIGoForward() and canIGoForwardD() then
  228.                 if type(FUEL)=="number" then
  229.                     if FUEL<=0 then
  230.                         return false
  231.                     else
  232.                         FUEL=FUEL-1
  233.                     end
  234.                 end
  235.                 clearTheTank()
  236.                 myPosX=myPosX-1
  237.                 printTheTank()
  238.             else
  239.                 direction="left"
  240.                 clearTheTank()
  241.                 printTheTank()
  242.             end
  243.         elseif lastKey=="s" then
  244.             if direction=="down" and canIGoForward() and canIGoForwardD() then
  245.                 if type(FUEL)=="number" then
  246.                     if FUEL<=0 then
  247.                         return false
  248.                     else
  249.                         FUEL=FUEL-1
  250.                     end
  251.                 end
  252.                 clearTheTank()
  253.                 myPosY=myPosY+1
  254.                 printTheTank()
  255.             else
  256.                 direction="down"
  257.                 clearTheTank()
  258.                 printTheTank()
  259.             end
  260.         elseif lastKey=="space" then
  261.             shoot()
  262.         end
  263.     else
  264.         clearTheTank()
  265.         myPosX=SpawnX
  266.         myPosY=SpawnY
  267.         clearTheTank()
  268.         printTheTank()
  269.     end
  270. end
  271. function printMapLabel()
  272.     local map_len=unicode.len(map_label)/2
  273.     local startx=math.floor(W/2-map_len)
  274.     gpu.set(startx, 1, map_label)
  275. end
  276. function clearMapLabel()
  277.     local map_len=unicode.len(map_label)/2
  278.     local startx=math.floor(W/2-map_len)
  279.     gpu.fill(1, 1, W, 1, " ")
  280. end
  281. function go()
  282.     _, _, _, key = event.pull("key_down")
  283.     lastKey=keyboard.keys[key]
  284.     move() 
  285. end
  286. function printTheData()
  287.     gpu.set(1, H, "Жизни: "..HEALTH.."  ")
  288.     gpu.set(1, H-1, "Снаряды: "..texture.bullets_number.."  ")
  289.     gpu.set(1, H-2, "Топливо: "..FUEL.."  ")
  290. end
  291. function printOtherTank(x, y, direction, color)
  292.     createCollizion(x, y, "myTank", color)
  293.     if direction=="right" then
  294.         createCollizion(x+1, y, "duloXB", color)
  295.     elseif direction=="left" then
  296.         createCollizion(x-1, y, "duloXM", color)
  297.     elseif direction=="up" then
  298.         createCollizion(x, y-1, "duloYB", color)
  299.     elseif direction=="down" then
  300.         createCollizion(x, y+1, "duloYM", color)
  301.     end
  302.     gpu.setForeground(0xffffff)
  303. end
  304. function clearOtherTank(x, y)
  305.     gpu.set(x, y, " ")
  306.     gpu.set(x+1, y, " ")
  307.     gpu.set(x-1, y, " ")
  308.     gpu.set(x, y+1, " ")
  309.     gpu.set(x, y-1, " ")
  310. end
  311. function getFacingX()
  312.     if  direction=="right" then
  313.         return myPosX+2
  314.     elseif  direction=="left" then
  315.         return myPosX-2
  316.     else
  317.         return myPosX
  318.     end
  319. end
  320. function getFacingY()
  321.     if  direction=="up" then
  322.         return myPosY-2
  323.     elseif  direction=="down" then
  324.         return myPosY+2
  325.     else
  326.         return myPosY
  327.     end
  328. end
  329. function getFacingXX()
  330.     if  direction=="right" then
  331.         return myPosX+1
  332.     elseif  direction=="left" then
  333.         return myPosX-1
  334.     else
  335.         return myPosX
  336.     end
  337. end
  338. function getFacingYY()
  339.     if  direction=="up" then
  340.         return myPosY-1
  341.     elseif  direction=="down" then
  342.         return myPosY+1
  343.     else
  344.         return myPosY
  345.     end
  346. end
  347. function canIGoForwardD(X, Y)
  348.     for TABLENUM=1, #collizions do
  349.         local x, y, textureCollizion, color, descrption = collizions[TABLENUM].x, collizions[TABLENUM].y, collizions[TABLENUM].textureCollizion, collizions[TABLENUM].color, collizions[TABLENUM].descrption
  350.         if  x==getFacingXX() and y==getFacingYY() and descrption~="myDULO" and descrption~="myTANK" then
  351.             return false
  352.         end
  353.     end
  354.     return true
  355. end
  356. function createCollizion(x, y, textureCollizion, color, descrption)
  357.     local tmp={}
  358.     tmp.x=x
  359.     tmp.y=y
  360.     tmp.textureCollizion=textureCollizion
  361.     tmp.color=color
  362.     tmp.descrption=descrption or "nil"
  363.     if descrption~="myDULO" and descrption~="myTANK" then
  364.         table.insert(collizions, tmp)
  365.     elseif descrption=="myDULO" then
  366.         collizions["myDULO"]=tmp
  367.     elseif descrption=="myTANK" then
  368.         collizions["myTANK"]=tmp
  369.     end
  370. end
  371. function printCollizions()
  372.     for TABLENUM=1, #collizions do
  373.         local x, y, textureCollizion, color = collizions[TABLENUM].x, collizions[TABLENUM].y, collizions[TABLENUM].textureCollizion, collizions[TABLENUM].color
  374.         gpu.setForeground(color)
  375.         gpu.set(x, y, texture[textureCollizion])
  376.     end
  377.     local x, y, textureCollizion, color = collizions["myTANK"].x, collizions["myTANK"].y, collizions["myTANK"].textureCollizion, collizions["myTANK"].color
  378.     gpu.setForeground(color)
  379.     gpu.set(x, y, texture[textureCollizion])
  380.     local x, y, textureCollizion, color = collizions["myDULO"].x, collizions["myDULO"].y, collizions["myDULO"].textureCollizion, collizions["myDULO"].color
  381.     gpu.setForeground(color)
  382.     gpu.set(x, y, texture[textureCollizion])
  383.     gpu.setForeground(0xffffff)
  384. end
  385. function canIGoForward(X, Y)
  386.     for TABLENUM in pairs(collizions) do
  387.         local x, y, textureCollizion, color, descrption = collizions[TABLENUM].x, collizions[TABLENUM].y, collizions[TABLENUM].textureCollizion, collizions[TABLENUM].color, collizions[TABLENUM].descrption
  388.         if  x==getFacingX() and y==getFacingY() and descrption~="myDULO" and descrption~="myTANK" then
  389.             return false
  390.         end
  391.     end
  392.     return true
  393. end
  394. --СТАРТ ПРОГРАММЫ
  395. gpu.setBackground(map_background)
  396. local x, y = map_resolution.x, map_resolution.y
  397. local Wmax, Hmax = gpu.maxResolution()
  398. if x>Wmax and y>Hmax then
  399.     print("Ошибка! Разрешение карты не соответствует разрешению монитора!")
  400.     os.exit()
  401. else
  402.     gpu.setResolution(x, y)
  403. end
  404. term.clear()
  405. printTheTank()
  406. printMapLabel()
  407. printOtherTank(50, 15, "left", 0x004DFF)
  408. createCollizion(15, 15, "wall", 0xff0000)
  409. event.timer(3, clearMapLabel)
  410. --УСТАНОВКА МИНЫ
  411. createMine(40, 14, math.random(1, 20))
  412. --КОНЕЦ:)
  413. while true do
  414.     if DESTROYED~=true then
  415.         term.clear()
  416.         printTheMines()
  417.         printCollizions()
  418.         checkMines()
  419.         checkHealth()
  420.         printTheData()
  421.         go()
  422.         os.sleep(0.1)
  423.     else
  424.         gpu.set(myPosX, myPosY, "†")
  425.         os.sleep(0.5)
  426.     end
  427. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement