Lucun_Ji

bounce.lua

Jul 5th, 2020 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. component = require("component")
  2. thread = require("thread")
  3. term = require("term")
  4. event = require("event")
  5. math = require("math")
  6. computer = require("computer")
  7. gpu = component.gpu
  8. colors = {r={0x00,0x33,0x66,0x99,0xcc,0xff}, g={0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff}, b={0x00,0x40,0x80,0xc0,0xff}}
  9.  
  10. function sign(val)
  11.   if val > 0 then
  12.     return 1
  13.   elseif val < 0 then
  14.     return -1
  15.   else
  16.     return 0
  17.   end
  18. end
  19.  
  20. function fill(x, y, width, height, color)
  21.   gpu.setBackground(color)
  22.   x = x * 2 - 1
  23.   gpu.fill(x, y, width*2, height, " ")
  24.   gpu.setBackground(0x00000)
  25. end
  26.  
  27. function drawBall(x, y, color)
  28.   gpu.setBackground(color)
  29.   x = x * 2 - 1
  30.   gpu.fill(x+4, y, 10, 6, " ")
  31.   gpu.fill(x+2, y+1, 14, 4, " ")
  32.   gpu.fill(x, y+2, 18, 2, " ")
  33.   gpu.setBackground(0x00000)
  34. end
  35.  
  36. function hitVerticalEdge(x, w, width)
  37.   return (x <= 1 or x + w > width)
  38. end
  39.  
  40. function hitHorizontalEdge(y, h, height)
  41.   return (y <= 1 or y + h > height)
  42. end
  43.  
  44. function main()
  45.   term.clear()
  46.   local width, height = gpu.getResolution()
  47.   width = width / 2
  48.   math.randomseed(computer.uptime())
  49.   local motion = {x=2, y=1}
  50.   if math.random(0, 1) == 0 then
  51.     motion.x = -motion.x
  52.   end
  53.   if math.random(0, 1) == 0 then
  54.     motion.y = -motion.y
  55.   end
  56.   local hitbox = {w=9, h=6}
  57.   local position = {x=width/2 - hitbox.w, y=height/2 - hitbox.h}
  58.   local r = #colors.r
  59.   local g = 1
  60.   local b = 1
  61.  
  62.   while true do
  63.  
  64.     drawBall(position.x, position.y, 0x000000)
  65.  
  66.     for i = 1, math.abs(motion.x) do
  67.       if hitVerticalEdge(position.x + sign(motion.x), hitbox.w, width) then
  68.         motion.x = -motion.x
  69.       end
  70.       position.x = position.x + sign(motion.x)
  71.     end
  72.     for i = 1, math.abs(motion.y) do
  73.       if hitHorizontalEdge(position.y + sign(motion.y), hitbox.h, height) then
  74.         motion.y = -motion.y
  75.       end
  76.       position.y = position.y + sign(motion.y)
  77.     end
  78.  
  79.     if r == #colors.r and b == 1 and g < #colors.g then
  80.       g = g + 1
  81.     elseif g == #colors.g and b == 1 and r > 1 then
  82.       r = r - 1
  83.     elseif g == #colors.g and r == 1 and b < #colors.b then
  84.       b = b + 1
  85.     elseif b == #colors.b and r == 1 and g > 1 then
  86.       g = g - 1
  87.     elseif b == #colors.b and g == 1 and r < #colors.r then
  88.       r = r + 1
  89.     elseif r == #colors.r and g == 1 and b > 1 then
  90.       b = b - 1
  91.     end
  92.     local color = colors.r[r] * 0x10000 + colors.g[g] * 0x100 + colors.b[b]
  93.  
  94.     fill(1, 1, width, 1, 0x787878)
  95.     fill(1, 1, 1, height, 0x787878)
  96.     fill(1, height, width, 1, 0x787878)
  97.     fill(width, 1, 1, height, 0x787878)
  98.  
  99.     drawBall(position.x, position.y, color)
  100.  
  101.     os.sleep(0.5)
  102.   end
  103. end
  104.  
  105. local t = thread.create(main)
  106. local _, x, y = event.pull("touch")
  107. t:kill()
  108. gpu.setBackground(0x000000)
  109. term.clear()
Add Comment
Please, Sign In to add comment