Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local stars = {}
- local velocity = 1
- local tArgs = {...}
- local compnum = tonumber(tArgs[1])
- local sendnum = compnum + 1
- if sendnum == 5 then sendnum = 1 end
- Star = {
- x = 0,
- y = 0
- }
- function Star:new(x, y)
- local class = {x = x, y = y}
- setmetatable(class, self)
- self.__index = self
- return class
- end
- function generateStars()
- local generatelist = {}
- for i = 1, 30 do
- local x = nil
- local y = nil
- local success = false
- while not success do
- x = math.random(1, w)
- y = math.random(1, h)
- success = true
- for i, v in pairs(generatelist) do
- if i == x and v == y then
- success = false
- end
- end
- end
- stars[i] = Star:new(x, y)
- generatelist[#generatelist + 1] = {x, y}
- end
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- generateStars()
- local deleteprev = false
- while true do
- if velocity < 0 then
- sendnum = sendnum - 2
- elseif velocity > 0 then
- sendnum = sendnum + 2
- end
- if sendnum < 0 then sendnum = sendnum + 4 end
- if sendnum > 4 then sendnum = sendnum - 4 end
- local newstar = nil
- newstar = {rednet.receive(.05)}
- if newstar and newstar[3] == tostring(compnum) then
- stars[#stars + 1] = Star:new(w, tonumber(newstar[2]))
- elseif newstar and newstar[2] == "velocity +" then
- velocity = velocity + 1
- elseif newstar and newstar[2] == "velocity -" then
- velocity = velocity - 1
- end
- if velocity > 3 then velocity = 3 end
- if velocity < -3 then velocity = -3 end
- for i, _ in pairs(stars) do
- if stars[i] ~= false then
- local star = stars[i]
- if (star.x < 1 and velocity > 0) or (star.x > w and velocity < 0) then
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, star.y)
- term.write(" ")
- rednet.broadcast(tostring(star.y), tostring(sendnum))
- stars[i] = false
- else
- term.setBackgroundColor(colors.black)
- term.setCursorPos(star.x + velocity, star.y)
- term.write(" ")
- term.setCursorPos(star.x, star.y)
- term.setBackgroundColor(colors.white)
- term.write("*")
- star.x = star.x - velocity
- stars[i] = star
- end
- end
- end
- --[[local delList = {}
- for i, _ in pairs(stars) do --note: this way is actually more efficient, but in actual use, it doesn't change
- if not stars[i] then --enough to make the extra lag worth it
- delList[#delList + 1] = i
- end
- end
- for i, v in pairs(delList) do
- table.remove(stars, v - i + 1)
- end]]--
- for i = 1, #stars do
- if stars[i] ~= nil and stars[i] == false then
- table.remove(stars, i)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement