TraerAlone

Untitled

Jul 19th, 2016
228
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. local fs = require("filesystem")
  7. local serialization = require("serialization")
  8. local arc = {}
  9. --os.execute("resolution 124 48")
  10. local gpu = c.gpu
  11. local w, h = gpu.getResolution()
  12. x = w / 2
  13. y = h / 2
  14. dy = 1
  15. dx = 0
  16. --
  17. colorPlatforma = 321321
  18. colorShar = 123123
  19. N = 1
  20. mass = {}
  21. MN = 1
  22.  
  23. function PutPixel(x, y, color, text)
  24.   gpu.setBackground(color)
  25.   gpu.set(x, y, text)
  26.   gpu.setBackground(0)
  27. end
  28.  
  29. function GameOver(_)            
  30.   computer.beep(20, 3)
  31.   dy = 0 - dy
  32.   gpu.setBackground(0)
  33.   print("YOU LOSER")
  34. end
  35.  
  36. function getKirpich(xk, yk, colorK)
  37.   for i = 1, #mass - 1 do
  38.     if mass[i][1] == math.floor(xk) and mass[i][2] == math.floor(yk) then
  39.       gpu.setBackground(colorK)
  40.       gpu.fill(mass[i][1], mass[i][2], mass[i][3], mass[i][4], " ")
  41.       N = N + 1
  42.       if n == #mass - 1 then
  43.         GameOver(1)
  44.       end
  45.     end
  46.   end
  47. end
  48.  
  49. function printKirpich()
  50.   for i = 1, #mass - 1 do
  51.     gpu.setBackground(mass[i][5])
  52.     gpu.fill(mass[i][1], mass[i][2], mass[i][3], mass[i][4], " ")
  53.   end
  54. end
  55.  
  56. function printPlatform()
  57.   for i = 1, l do
  58.     PutPixel(platformaS + i , h, colorPlatforma, " ")
  59.   end
  60. end
  61.  
  62. function platforma()
  63.   event.listen("key_down", function(k1, k2, k3, k4) key = k4 end)
  64.   if key == 203 and platformaS > 1 then
  65.     PutPixel(platformaS + l, h, 0, " ")
  66.     PutPixel(platformaS, h, colorPlatforma, " ")
  67.     platformaS = platformaS - 1
  68.     key = 0
  69.   end
  70.   if key == 205 and platformaS + l < w then
  71.     PutPixel(platformaS, h, 0, " ")
  72.     PutPixel(platformaS + l, h, colorPlatforma, " ")
  73.     platformaS = platformaS + 1
  74.     key = 0
  75.   end
  76.   event.ignore("key_down", function(k1, k2, k3, k4) end)
  77.   os.sleep(1 / 20)
  78. end
  79.  
  80. function test()
  81.   if x < 3  or x > w - 2 then
  82.     dx = 0 - dx
  83.   end
  84.   if y < 3 then
  85.     dy = 0 - dy
  86.   end
  87.   if y < h - 5 then
  88.     local char, foreColor, backColor = gpu.get(x + dx, y + dy)
  89.     kx = x + dx
  90.     ky = y + dy
  91.     if backColor ~= 0 then
  92.       char, foreColor, backColor1 = gpu.get(x + 1, y)
  93.       char, foreColor, backColor2 = gpu.get(x - 1, y)
  94.       char, foreColor, backColor3 = gpu.get(x, y + 1)
  95.       char, foreColor, backColor4 = gpu.get(x, y - 1)
  96.       if backColor1 ~= backColor and backColor2 ~= backColor then
  97.         dy = 0 - dy
  98.       end
  99.       if backColor3 ~= backColor and backColor4 ~= backColor then
  100.         dx = 0 - dx
  101.       end
  102.       char, foreColor, backColor5 = gpu.get(kx - 1, ky)
  103.       while backColor5 == backColor and kx - 3 > 0 do
  104.         char, foreColor, backColor5 = gpu.get(kx - 2, ky)
  105.         kx = kx - 1
  106.       end
  107.       char, foreColor, backColor5 = gpu.get(kx, ky - 1)
  108.       while backColor5 == backColor and ky - 3 > 0 do
  109.         char, foreColor, backColor5 = gpu.get(kx, ky - 2)
  110.         ky = ky - 1
  111.       end
  112.       getKirpich(kx, ky, 0)
  113.     end
  114.   end
  115.   if y + dy > h or y + dy == h then
  116.     if x < platformaS + 1 or x > platformaS + l + 1 then
  117.       GameOver(1)
  118.     else
  119.       dy = 0 - dy
  120.       dx = ((x % (platformaS + 1)) - l / 2) / l * 2  -- зависимость dx от места попадание на платформу игрок
  121.       dy = 0 - math.sqrt(1 - dx * dx)
  122.     end
  123.   end
  124. end
  125.  
  126. function shar()
  127.   PutPixel(x, y, 0, " ")
  128.   test()
  129.   x = x + dx
  130.   y = y + dy
  131.   PutPixel(x, y, colorShar, " ")
  132. end
  133.  
  134. function Game()
  135.   while key ~= 28 do
  136.     platforma()
  137.     platforma()
  138.     shar()
  139.   end
  140. end
  141.  
  142. function arc.GameStart(file)
  143.   mass = serialization.unserialize(io.lines(file)())
  144.   l = mass[#mass][1]
  145.   platformaS = w / 2 - l / 2
  146.   printKirpich()
  147.   printPlatform()
  148.   Game()
  149. end
  150.  
  151. return arc
Advertisement
Add Comment
Please, Sign In to add comment