Advertisement
GopherAtl

dancingLines v2 (computercraft lua)

Sep 12th, 2012
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. --[[
  2.  
  3. dancinglines v2
  4. code by GopherAtl.
  5. Do anything you want with this code except claim credit for it.
  6. If you distribute a program that uses it, please link users to the
  7. official thread on the cc forums here:
  8. http://www.computercraft.info/forums2/index.php?/topic/4006-logo-interpreter/
  9. If you distribute a modified version with fixes or enhancements, please link to
  10. it in the official thread as well.
  11.  
  12. --]]
  13.  
  14.  
  15.  
  16. --Bresenham's line algo, adapted to lua by me
  17. function line(device,x1,y1,x2,y2,ch)  
  18.   local steep = math.abs(y2-y1)>math.abs(x2-x1)
  19.   local setPos
  20.   if steep then    
  21.     y1,x1=x1,y1
  22.     y2,x2=x2,y2
  23.     setPos=function(y,x) device.setCursorPos(x,y) end
  24.   else
  25.     setPos=function(x,y) device.setCursorPos(x,y) end
  26.   end
  27.   if x1>x2 then
  28.     x1,x2=x2,x1
  29.     y1,y2=y2,y1
  30.   end
  31.   local dx=x2-x1
  32.   local dy=math.abs(y2-y1)
  33.   local err=dx/2
  34.   local ys= y1<y2 and 1 or -1
  35.   local y=y1
  36.  
  37.   for x=x1,x2 do
  38.     setPos(x,y)
  39.     device.write(ch)
  40.     err=err-dy
  41.     if err<0 then
  42.       y=y+ys
  43.       err=err+dx
  44.     end
  45.   end  
  46. end
  47.  
  48.  
  49. local numLines=2
  50. local tailLen=5
  51. local drawEvery=1
  52. local speed=20
  53.  
  54. local x1, x2, y1, y2
  55. local termW, termH=term.getSize()
  56. local lines={}
  57.  
  58. local args={...}
  59. if #args>0 and tonumber(args[1]) then
  60.   numLines=tonumber(args[1])
  61.   if numLines<1 then numLines=1 end
  62.   if numLines>10 then numLines=5 end
  63.   if #args>1 and tonumber(args[1]) then
  64.     speed=tonumber(args[2])
  65.     if speed>100 then speed=100 end
  66.     if speed<.1 then speed=.1 end
  67.   end
  68. end
  69.  
  70. function initLines()
  71.  
  72.   for i=1,numLines do
  73.     lines[i]= { {
  74.         x1=math.random()*termW,
  75.         x2=math.random()*termW,
  76.         y1=math.random()*termH,
  77.         y2=math.random()*termH,
  78.       },
  79.       x1v=math.random()/1+1,
  80.       x2v=math.random()/1+1,
  81.       y1v=math.random()/1+1,
  82.       y2v=math.random()/1+1,
  83.  
  84.       }  
  85.    end
  86. end
  87.  
  88. initLines()
  89.  
  90. local chars={{"O","o","o",".","."},{"X","x","x",",",","}}
  91.  
  92.  
  93. function dancingLines()
  94.   while true do
  95.     term.clear()
  96.     for i=#lines[1],1,-2 do
  97.       for j=1,#lines do
  98.         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])
  99.       end
  100.     end
  101.     for j=1,#lines,1 do
  102.       if #lines[j]==tailLen then
  103.         table.remove(lines[j],#lines[j])    
  104.       end
  105.       newline={
  106.               x1=lines[j][1].x1+lines[j].x1v,
  107.               y1=lines[j][1].y1+lines[j].y1v,
  108.               x2=lines[j][1].x2+lines[j].x2v,
  109.               y2=lines[j][1].y2+lines[j].y2v,
  110.         }      
  111.       if newline.x1<1 then
  112.         newline.x1=-newline.x1
  113.         lines[j].x1v=-lines[j].x1v
  114.       elseif newline.x1>termW then
  115.         newline.x1=termW*2-newline.x1
  116.         lines[j].x1v=-lines[j].x1v
  117.       end
  118.       if newline.x2<1 then
  119.         newline.x2=-newline.x2
  120.         lines[j].x2v=-lines[j].x2v
  121.       elseif newline.x2>termW then
  122.         newline.x2=termW*2-newline.x2
  123.         lines[j].x2v=-lines[j].x2v
  124.       end
  125.    
  126.       if newline.y1<1 then
  127.         newline.y1=-newline.y1
  128.         lines[j].y1v=-lines[j].y1v
  129.       elseif newline.y1>termH then
  130.         newline.y1=termH*2-newline.y1
  131.         lines[j].y1v=-lines[j].y1v
  132.       end
  133.       if newline.y2<1 then
  134.         newline.y2=-newline.y2
  135.         lines[j].y2v=-lines[j].y2v
  136.       elseif newline.y2>termH then
  137.         newline.y2=termH*2-newline.y2
  138.         lines[j].y2v=-lines[j].y2v
  139.       end
  140.  
  141.       table.insert(lines[j], 1, newline)
  142.  
  143.       local myTimer=os.startTimer(1/speed)
  144.       while true do
  145.         evt,p1=os.pullEvent()
  146.         if evt=="timer" and p1==myTimer then
  147.           break
  148.         end
  149.         if evt=="char" and p1~=1 then
  150.           if p1=='r' then
  151.             initLines()
  152.           else
  153.             return
  154.           end
  155.         end
  156.       end
  157.        
  158.     end
  159.    
  160.   end  
  161. end
  162.  
  163. dancingLines()
  164. term.clear()
  165. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement