Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- dancinglines v2
- code by GopherAtl.
- Do anything you want with this code except claim credit for it.
- If you distribute a program that uses it, please link users to the
- official thread on the cc forums here:
- http://www.computercraft.info/forums2/index.php?/topic/4006-logo-interpreter/
- If you distribute a modified version with fixes or enhancements, please link to
- it in the official thread as well.
- --]]
- --Bresenham's line algo, adapted to lua by me
- function line(device,x1,y1,x2,y2,ch)
- local steep = math.abs(y2-y1)>math.abs(x2-x1)
- local setPos
- if steep then
- y1,x1=x1,y1
- y2,x2=x2,y2
- setPos=function(y,x) device.setCursorPos(x,y) end
- else
- setPos=function(x,y) device.setCursorPos(x,y) end
- end
- if x1>x2 then
- x1,x2=x2,x1
- y1,y2=y2,y1
- end
- local dx=x2-x1
- local dy=math.abs(y2-y1)
- local err=dx/2
- local ys= y1<y2 and 1 or -1
- local y=y1
- for x=x1,x2 do
- setPos(x,y)
- device.write(ch)
- err=err-dy
- if err<0 then
- y=y+ys
- err=err+dx
- end
- end
- end
- local numLines=2
- local tailLen=5
- local drawEvery=1
- local speed=20
- local x1, x2, y1, y2
- local termW, termH=term.getSize()
- local lines={}
- local args={...}
- if #args>0 and tonumber(args[1]) then
- numLines=tonumber(args[1])
- if numLines<1 then numLines=1 end
- if numLines>10 then numLines=5 end
- if #args>1 and tonumber(args[1]) then
- speed=tonumber(args[2])
- if speed>100 then speed=100 end
- if speed<.1 then speed=.1 end
- end
- end
- function initLines()
- for i=1,numLines do
- lines[i]= { {
- x1=math.random()*termW,
- x2=math.random()*termW,
- y1=math.random()*termH,
- y2=math.random()*termH,
- },
- x1v=math.random()/1+1,
- x2v=math.random()/1+1,
- y1v=math.random()/1+1,
- y2v=math.random()/1+1,
- }
- end
- end
- initLines()
- local chars={{"O","o","o",".","."},{"X","x","x",",",","}}
- function dancingLines()
- while true do
- term.clear()
- for i=#lines[1],1,-2 do
- for j=1,#lines do
- logo.line(term, lines[j][i].x1,lines[j][i].y1,lines[j][i].x2,lines[j][i].y2,chars[j][(i-i%drawEvery)/drawEvery])
- end
- end
- for j=1,#lines,1 do
- if #lines[j]==tailLen then
- table.remove(lines[j],#lines[j])
- end
- newline={
- x1=lines[j][1].x1+lines[j].x1v,
- y1=lines[j][1].y1+lines[j].y1v,
- x2=lines[j][1].x2+lines[j].x2v,
- y2=lines[j][1].y2+lines[j].y2v,
- }
- if newline.x1<1 then
- newline.x1=-newline.x1
- lines[j].x1v=-lines[j].x1v
- elseif newline.x1>termW then
- newline.x1=termW*2-newline.x1
- lines[j].x1v=-lines[j].x1v
- end
- if newline.x2<1 then
- newline.x2=-newline.x2
- lines[j].x2v=-lines[j].x2v
- elseif newline.x2>termW then
- newline.x2=termW*2-newline.x2
- lines[j].x2v=-lines[j].x2v
- end
- if newline.y1<1 then
- newline.y1=-newline.y1
- lines[j].y1v=-lines[j].y1v
- elseif newline.y1>termH then
- newline.y1=termH*2-newline.y1
- lines[j].y1v=-lines[j].y1v
- end
- if newline.y2<1 then
- newline.y2=-newline.y2
- lines[j].y2v=-lines[j].y2v
- elseif newline.y2>termH then
- newline.y2=termH*2-newline.y2
- lines[j].y2v=-lines[j].y2v
- end
- table.insert(lines[j], 1, newline)
- local myTimer=os.startTimer(1/speed)
- while true do
- evt,p1=os.pullEvent()
- if evt=="timer" and p1==myTimer then
- break
- end
- if evt=="char" and p1~=1 then
- if p1=='r' then
- initLines()
- else
- return
- end
- end
- end
- end
- end
- end
- dancingLines()
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement