Advertisement
TraerAlone

Untitled

Jul 4th, 2016
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require("component")
  2. local computer = require("computer")
  3. local math = require("math")
  4. --local os = require("os")
  5. local event = require("event")
  6. os.execute("resolution 124 48")
  7. local gpu = c.gpu
  8. local w, h = gpu.getResolution()
  9. x = w / 2                            -- шарик
  10. y = h / 2                            --
  11. dy = 1
  12. dx = 0
  13. l = 32                               --длина платформы в пикселях
  14. platformaS = w / 2 - l / 2
  15. kirpichNX = 6                        --кол-во кирпичей
  16. kirpichNY = 4
  17. PBTkir = 0                           --расстояние между кирпичами
  18. heightKirpich = 2                    
  19. widthKirpich = 10                    --размер кирпича
  20. strengthKirpich = 321321             --не рабочий стафф
  21. colorPlatforma = 321321              --цвет платформы
  22. colorShar = 123123                   --цвет шарика
  23.  
  24. function PutPixel(x, y, color, text)  --просто удобная штука. мне так проще
  25.   gpu.setBackground(color)
  26.   gpu.set(x, y, text)
  27.   gpu.setBackground(0)
  28. end
  29.  
  30. function getKirpich(xk, yk, colorK)  --рисуем кирпич
  31.   gpu.setBackground(colorK)
  32.   gpu.fill(xk, yk, widthKirpich, heightKirpich, " ")
  33. end
  34.  
  35. function printKirpich()              --рисуем кирпичи
  36.   for i1 = 0, kirpichNX - 1 do
  37.     for i2 = 0, kirpichNY - 1 do
  38.       getKirpich(PBTkir + (widthKirpich + PBTkir) * i1 + 2, PBTkir + (heightKirpich + PBTkir) * i2 + 2, math.random() * 1000000)
  39.     end
  40.   end
  41. end
  42.  
  43. function getPlatform()               --рисуем платформу
  44.   for i = 1, l do
  45.     PutPixel(platformaS + i , h, colorPlatforma, " ")
  46.   end
  47. end
  48.  
  49. function GameOver(reason)            --ну тут понятно гейм овер
  50.   computer.beep(20, 3)
  51.   dy = 0 - dy
  52.   gpu.setBackground(0)
  53.   computer.shutdown()
  54. end
  55.  
  56. function start()                     --стартует игра и вызывает всякие функции для отрисовки всего
  57.   gpu.setBackground(0)
  58.   gpu.fill(1, 1, w, h, " ")
  59.   PutPixel(w / 2 - 6, h / 2 - 6, 777777, "Hello World")
  60.   os.sleep(1)
  61.   gpu.fill(1, 1, w, h, " ")
  62.   getPlatform()
  63.   printKirpich()
  64. end
  65.  
  66. function platforma()                 --платформа едет
  67.   event.listen("key_down", function(k1, k2, k3, k4) key = k4 end)
  68.   if key == 203 and platformaS > 1 then
  69.     PutPixel(platformaS + l, h, 0, " ")
  70.     PutPixel(platformaS, h, colorPlatforma, " ")
  71.     platformaS = platformaS - 1
  72.     key = 0
  73.   end
  74.   if key == 205 and platformaS + l < w - 2 then
  75.     PutPixel(platformaS, h, 0, " ")
  76.     PutPixel(platformaS + l, h, colorPlatforma, " ")
  77.     platformaS = platformaS + 1
  78.     key = 0
  79.   end
  80.   event.ignore("key_down", function(k1, k2, k3, k4) end)
  81.   os.sleep(1 / 20)
  82. end
  83.  
  84. function test()                      --проверка всего
  85.   if x < 3  or x > w - 2 then        --отражение от границ экрана
  86.     dx = 0 - dx
  87.   end
  88.   if y < 3 then                      --см верх
  89.     dy = 0 - dy
  90.   end
  91.   if y < h - 5 then                  --определение отражение от кирпичей
  92.     local char, foreColor, backColor = gpu.get(x + dx, y + dy)
  93.     kx = x + dx
  94.     ky = y + dy
  95.     if backColor ~= 0 then
  96.       char, foreColor, backColor1 = gpu.get(x + 1, y)
  97.       char, foreColor, backColor2 = gpu.get(x - 1, y)
  98.       char, foreColor, backColor3 = gpu.get(x, y + 1)
  99.       char, foreColor, backColor4 = gpu.get(x, y - 1)
  100.       if backColor1 ~= backColor and backColor2 ~= backColor then
  101.         dy = 0 - dy
  102.       end
  103.       if backColor3 ~= backColor and backColor4 ~= backColor then
  104.         dx = 0 - dx
  105.       end
  106.       char, foreColor, backColor5 = gpu.get(kx - 1, ky)
  107.       while backColor5 == backColor and kx - 2 > 0 do
  108.         char, foreColor, backColor5 = gpu.get(kx - 2, ky)
  109.         kx = kx - 1
  110.       end
  111.       char, foreColor, backColor5 = gpu.get(kx, ky - 1)
  112.       while backColor5 == backColor and ky - 2 > 0 do
  113.         char, foreColor, backColor5 = gpu.get(kx, ky - 2)
  114.         ky = ky - 1
  115.       end
  116.       getKirpich(kx, ky, 0)         --удаляет киприч
  117.     end
  118.   end
  119.   if y + dy > h or y + dy == h then --отражение шарика от платформы
  120.     if x < platformaS + 1 or x > platformaS + l + 1 then
  121.       GameOver(1)
  122.     else
  123.       dy = 0 - dy
  124.       dx = ((x % (platformaS + 1)) - l / 2) / l * 2  --зависимость dx от места попадание на платформу игрок, надо доработать
  125.       dy = 0 - math.sqrt(1 - dx * dx)
  126.     end
  127.   end
  128. end
  129.  
  130. function shar()                                      --полет шарика
  131.   PutPixel(x, y, 0, " ")
  132.   test()
  133.   x = x + dx
  134.   y = y + dy
  135.   PutPixel(x, y, colorShar, " ")
  136. end
  137.  
  138. start()                                             --старт игры
  139. while key ~= 28 do
  140.   platforma()
  141.   platforma()
  142.   shar()
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement