Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component = require("component")
- thread = require("thread")
- term = require("term")
- event = require("event")
- math = require("math")
- computer = require("computer")
- gpu = component.gpu
- colors = {r={0x00,0x33,0x66,0x99,0xcc,0xff}, g={0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff}, b={0x00,0x40,0x80,0xc0,0xff}}
- function sign(val)
- if val > 0 then
- return 1
- elseif val < 0 then
- return -1
- else
- return 0
- end
- end
- function fill(x, y, width, height, color)
- gpu.setBackground(color)
- x = x * 2 - 1
- gpu.fill(x, y, width*2, height, " ")
- gpu.setBackground(0x00000)
- end
- function drawBall(x, y, color)
- gpu.setBackground(color)
- x = x * 2 - 1
- gpu.fill(x+4, y, 10, 6, " ")
- gpu.fill(x+2, y+1, 14, 4, " ")
- gpu.fill(x, y+2, 18, 2, " ")
- gpu.setBackground(0x00000)
- end
- function hitVerticalEdge(x, w, width)
- return (x <= 1 or x + w > width)
- end
- function hitHorizontalEdge(y, h, height)
- return (y <= 1 or y + h > height)
- end
- function main()
- term.clear()
- local width, height = gpu.getResolution()
- width = width / 2
- math.randomseed(computer.uptime())
- local motion = {x=2, y=1}
- if math.random(0, 1) == 0 then
- motion.x = -motion.x
- end
- if math.random(0, 1) == 0 then
- motion.y = -motion.y
- end
- local hitbox = {w=9, h=6}
- local position = {x=width/2 - hitbox.w, y=height/2 - hitbox.h}
- local r = #colors.r
- local g = 1
- local b = 1
- while true do
- drawBall(position.x, position.y, 0x000000)
- for i = 1, math.abs(motion.x) do
- if hitVerticalEdge(position.x + sign(motion.x), hitbox.w, width) then
- motion.x = -motion.x
- end
- position.x = position.x + sign(motion.x)
- end
- for i = 1, math.abs(motion.y) do
- if hitHorizontalEdge(position.y + sign(motion.y), hitbox.h, height) then
- motion.y = -motion.y
- end
- position.y = position.y + sign(motion.y)
- end
- if r == #colors.r and b == 1 and g < #colors.g then
- g = g + 1
- elseif g == #colors.g and b == 1 and r > 1 then
- r = r - 1
- elseif g == #colors.g and r == 1 and b < #colors.b then
- b = b + 1
- elseif b == #colors.b and r == 1 and g > 1 then
- g = g - 1
- elseif b == #colors.b and g == 1 and r < #colors.r then
- r = r + 1
- elseif r == #colors.r and g == 1 and b > 1 then
- b = b - 1
- end
- local color = colors.r[r] * 0x10000 + colors.g[g] * 0x100 + colors.b[b]
- fill(1, 1, width, 1, 0x787878)
- fill(1, 1, 1, height, 0x787878)
- fill(1, height, width, 1, 0x787878)
- fill(width, 1, 1, height, 0x787878)
- drawBall(position.x, position.y, color)
- os.sleep(0.5)
- end
- end
- local t = thread.create(main)
- local _, x, y = event.pull("touch")
- t:kill()
- gpu.setBackground(0x000000)
- term.clear()
Add Comment
Please, Sign In to add comment