Matdupy

Untitled

Aug 6th, 2021 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. -- print("Text to draw : ")
  2. -- text = read()
  3. speed = 0.1
  4. marge = 3
  5.  
  6.  
  7. screen = peripheral.wrap("left")
  8. debug = peripheral.wrap("bottom")
  9. width, height = screen.getSize()
  10.  
  11. function debug_write(text,line)
  12.     debug.setCursorPos(1,line)
  13.     debug.write(text)
  14. end
  15.  
  16.    
  17. start_char = {"/", "|", "\\"}
  18. end_char = {"\\", "|", "/"}
  19.  
  20. function generate_center_text(text, pos_y, dir)
  21.     return {
  22.         content=text,
  23.         pos={x=-#text, y=pos_y or height/2},
  24.         direction = dir or "none",
  25.         borne={min=4,max=30}
  26.     }
  27. end
  28.  
  29. function draw_cadre(text_pos, text_w)
  30.     start_height = text_pos.y - 1
  31.     for h=0,2,1 do
  32.         new_line = ""
  33.         new_line = start_char[h+1]..new_line
  34.         for w=1,text_w,1 do
  35.             new_line = new_line.."-"
  36.         end
  37.         new_line = new_line..end_char[h+1]
  38.         screen.setCursorPos(text_pos.x-1, start_height+h)
  39.         screen.write(new_line)
  40.     end
  41. end
  42.  
  43. function update_text_pos(text)
  44.     -- debug_write(text.borne.min.."/"..text.borne.max, 1)
  45.     debug_write(text.pos.x < text.borne.max and text.direction == "right",1)
  46.     debug_write(text.direction, 2)
  47.     debug_write(text.pos.x,3)
  48.     debug_write(text.borne.max,4)
  49.     if text.direction ~= "none" then
  50.         if text.direction == "right" then
  51.             if text.pos.x < text.borne.max then
  52.                 text.pos.x = text.pos.x + 1
  53.             else
  54.                 text.direction = "left"
  55.             end
  56.         elseif text.direction == "left" then
  57.             if text.pos.x > text.borne.min then
  58.                 text.pos.x = text.pos.x - 1
  59.             else
  60.                 text.direction = "right"
  61.             end
  62.         end
  63.     else
  64.         print(text.content.."ne bouge pas")
  65.     end
  66. end
  67.  
  68. function draw_text(text_obj)
  69.     f_pos = {
  70.         x=text_obj.pos.x - #text_obj.content/2,
  71.         y=text_obj.pos.y - 1
  72.     }
  73.    
  74.     draw_cadre(f_pos, #text_obj.content)
  75.     screen.setCursorPos(f_pos.x, f_pos.y)
  76.     screen.write(text_obj.content)
  77. end
  78.  
  79. -- list = {"Salut", "Les", "P'tits", "Amis"}
  80. -- text_list = {}
  81. -- for i=1,#list,1 do
  82. --     table.insert(text_list, generate_center_text(list[i], i*marge, "right", 40))
  83. -- end
  84.  
  85. text = generate_center_text("Salut", 3, "right")
  86.  
  87. while true do
  88.     screen.clear()
  89.     update_text_pos(text)
  90.     draw_text(text)
  91.     sleep(speed)
  92. end
Add Comment
Please, Sign In to add comment