Advertisement
TraerAlone

Untitled

Jul 8th, 2016
303
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 gpu = c.gpu
  4. local event = require("event")
  5. local math = require("math")
  6.  
  7. local w, h = gpu.getResolution()
  8. x = w - 4
  9. y = h - 4
  10. l = 16
  11. t = 0
  12. platformaS = w / 2 - l / 2 - 1  -- стартовая позиция платформ
  13. platformaS2 = platformaS        -- платформа2 позиция старта
  14. platformaS1 = platformaS        --          1
  15. dy = -1                         -- изменение положения шарика по у
  16. dx = 0                          -- по х (вектор = 1)
  17. ez = 16                         -- не знаю работает ли... в теории дает возможность двигаться платформе в 16 быстрее шара.
  18.  
  19. colorPlatforma = 321321         -- цвет платформы
  20. colorShar = 123123              -- цвет шара
  21.  
  22.  
  23. function GameOver(player)       -- конец игры(player = loser)      
  24.   computer.beep(20, 3)
  25.   return 0
  26. end
  27.  
  28. function PutPixel(x, y, color, text) -- люблю эту функцию, получает пиксель там где надо
  29.   gpu.setBackground(color)
  30.   gpu.set(x, y, text)
  31.   gpu.setBackground(0)
  32. end
  33.  
  34.  
  35.  
  36.  
  37. function getPlatform()  -- подготовка, рисует платформу
  38.   for i = 0, l - 1 do
  39.     PutPixel(platformaS + i , h, colorPlatforma, " ")
  40.     PutPixel(platformaS + i , 1, colorPlatforma, " ")
  41.   end
  42. end
  43.  
  44. function start()  -- начало игры
  45.   gpu.setBackground(0)
  46.   gpu.fill(1, 1, w, h, " ")
  47.   PutPixel(w / 2 - 6, h / 2 - 6, 777777, "Hello World")
  48.   os.sleep(1)
  49.   gpu.fill(1, 1, w, h, " ")
  50.   getPlatform()
  51. end
  52.  
  53.  
  54. function platforma()  -- движение платформы - 205 = --> 203 = <--  208 = <-- 200 = -->
  55.   for i = 1, ez do
  56.     event.listen("key_down", function(k1, k2, k3, k4) key = k4 end)  -- подходит с трудом ибо если оба нажали работает у последнего
  57.     if key == 203 and platformaS1 - 1 > 0 then            --1 игрок
  58.       PutPixel(platformaS1 + l - 1, h, 0, " ")
  59.       PutPixel(platformaS1 - 1, h, colorPlatforma, " ")
  60.       platformaS1 = platformaS1 - 1
  61.       key = 0
  62.     end
  63.     if key == 205 and platformaS1 + l < w + 1 then
  64.       PutPixel(platformaS1, h, 0, " ")
  65.       PutPixel(platformaS1 + l, h, colorPlatforma, " ")
  66.       platformaS1 = platformaS1 + 1
  67.       key = 0
  68.     end
  69.     if key == 208 and platformaS2 + 1 > 0 then            --2 игрок
  70.       PutPixel(platformaS2 + l - 1, 1, 0, " ")
  71.       PutPixel(platformaS2 - 1, 1, colorPlatforma, " ")
  72.       platformaS2 = platformaS2 - 1
  73.       key = 0
  74.     end
  75.     if key == 200 and platformaS2 + l < w + 1 then
  76.       PutPixel(platformaS2, 1, 0, " ")
  77.       PutPixel(platformaS2 + l, 1, colorPlatforma, " ")
  78.       platformaS2 = platformaS2 + 1
  79.       key = 0
  80.     end
  81.     os.sleep(0,05)
  82.   end
  83. end
  84.  
  85.  
  86. function test()  -- физика(нет) не работает. Если у шарика у < 1 то меняет направление несколько раз = не работает
  87.   if 3 > x or x > w - 3 then
  88.     dx = 0 - dx
  89.   end
  90.   if 3 > y or y > h - 2 then
  91.     dy = 0 - dy
  92.   end
  93.   if t < 1 then
  94.   if y == 2 then
  95.     if x < platformaS2 or x > platformaS2 + l then
  96.       GameOver(0)
  97.     else
  98.       dx = ((x % platformaS2) - l / 2) / l * 2  -- зависимость dx от места попадание на платформу игрок
  99.       dy = 0 - math.sqrt(1 - dx * dx)
  100.     end
  101.   end
  102.  if y == h - 1 then
  103.     if x < platformaS1 or x > platformaS1 + l then
  104.       GameOver(1)
  105.     else
  106.       dx = ((x % platformaS2) - l / 2) / l * 2  -- зависимость dx от места попадание на платформу игрок
  107.       dy = 0 - math.sqrt(1 - dx * dx)
  108.       t = 8
  109.     end
  110.   end
  111.   end
  112. end
  113.  
  114. function shar() --движение шара, надо переделать последовательность.
  115.   PutPixel(x, y, 0, " ")
  116.   x = x + dx
  117.   y = y + dy
  118.   test()
  119.   PutPixel(x, y, colorShar, " ")
  120. end
  121.  
  122.  
  123. start()
  124. while key ~= 28 do  -- конец игры если нажали enter
  125. shar()
  126. platforma()  
  127. platforma()  -- ez не работало сделал костыль безногому
  128. end
  129. gpu.setBackground(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement