Matdupy

final_star

Aug 6th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. print('Choose mode (debug, prod) : ')
  2. choice = read()
  3.  
  4. if choice == "debug" then
  5.     screen = peripheral.wrap("left")
  6.     debug = peripheral.wrap("bottom")
  7. else
  8.     screen = peripheral.wrap("monitor_0")
  9.     debug = peripheral.wrap("monitor_1")
  10. end
  11. width, height = screen.getSize()
  12. symbols = {"x", "+", "*"}
  13. max_star = 5
  14.  
  15. tail_length = 3
  16.  
  17. function draw_tail(base_pos, dir)
  18.     local tail_char = ""
  19.     local side = 0
  20.     if dir == "W" then
  21.         tail_char = "/"
  22.         side = 1
  23.     else
  24.         tail_char = "\\"
  25.         side = -1
  26.     end
  27.     for l=1,tail_length,1 do
  28.         local m_x = base_pos.x + (side * l)
  29.         local m_y = base_pos.y - (1 * l)
  30.         screen.setCursorPos(m_x, m_y)
  31.         screen.write(tail_char)
  32.     end
  33. end
  34.  
  35. borne = {min=-4, max=width+4}
  36. borne.middle = borne.max / 2
  37.  
  38. function generate_meteor(speed)
  39.     x_pos = math.random(borne.min, borne.max)
  40.     n_dir = ""
  41.     if x_pos < borne.middle  then n_dir="E" else n_dir="W" end
  42.     meteor = {pos={x=x_pos,y=-3}, speed=speed, direction=n_dir}
  43.     debug.setCursorPos(1,1)
  44.     debug.write(meteor.direction)
  45.     debug.setCursorPos(1,2)
  46.     debug.write("X : "..meteor.pos.x.."/ Y : "..meteor.pos.y)
  47.     return meteor
  48. end
  49.  
  50. function update_meteor(meteor)
  51.     if meteor.direction == "W" then
  52.         meteor.pos.x = meteor.pos.x - meteor.speed
  53.     else
  54.         meteor.pos.x = meteor.pos.x + meteor.speed
  55.     end
  56.     meteor.pos.y = meteor.pos.y + meteor.speed
  57.     screen.setCursorPos(meteor.pos.x, meteor.pos.y)
  58.     screen.write("O")
  59.     draw_tail(meteor.pos, meteor.direction)
  60.     -- print(meteor.pos.x, meteor.pos.y)
  61.     -- sleep(0.1)
  62. end
  63.  
  64. m1 = generate_meteor(1)
  65.  
  66. function init_star_tab(star_count)
  67.     tab = {index=1, list={}}
  68.     for i=1, star_count,1 do
  69.         rand_x = math.random(1, width)
  70.         rand_y = math.random(1, height)
  71.         tab.list[i] = {rand_x, rand_y}
  72.     end
  73.     return tab
  74. end
  75.  
  76. start_char = {"/", "|", "\\"}
  77. end_char = {"\\", "|", "/"}
  78.  
  79. function draw_cadre(text_pos, text_w)
  80.     start_height = text_pos.y - 1
  81.     for h=0,2,1 do
  82.         new_line = ""
  83.         new_line = start_char[h+1]..new_line
  84.         for w=1,text_w,1 do
  85.             new_line = new_line.."-"
  86.         end
  87.         new_line = new_line..end_char[h+1]
  88.         screen.setCursorPos(text_pos.x-1, start_height+h)
  89.         screen.write(new_line)
  90.     end
  91. end
  92.  
  93. function text_middle_screen(text)
  94.     t_w = #text / 2
  95.     s_w = (width / 2) - (t_w)
  96.     s_h = (height / 2) - 1
  97.     -- screen.setCursorPos(s_w, s_h)
  98.     draw_cadre({x=s_w,y=s_h}, #text )
  99.     screen.setCursorPos(s_w, s_h)
  100.     screen.write(text)
  101. end
  102.  
  103.  
  104. function addStar(star_arr)
  105.     for i=1, #star_arr.list, 1 do
  106.         if i == star_arr.index then
  107.             pos_x = math.random(1,width)
  108.             pos_y = math.random(1,height)
  109.             star_arr.list[i] = {pos_x, pos_y}
  110.         end
  111.         next_x = star_arr.list[i][1]
  112.         next_y = star_arr.list[i][2]
  113.         screen.setCursorPos(next_x, next_y)
  114.         random_symbol = symbols[math.random(1, #symbols)]
  115.         screen.write(random_symbol)
  116.     end
  117.    
  118.     star_arr.index = star_arr.index + 1
  119.     if star_arr.index > #star_arr.list then
  120.         star_arr.index = 1
  121.     end
  122. end
  123.  
  124. test = init_star_tab(50)
  125.  
  126. function meteor_management()
  127.     update_meteor( m1)
  128.     if meteor.pos.y > height+3 or
  129.         meteor.pos.x > width+3 or
  130.         meteor.pos.x < -3 then
  131.         m1 = generate_meteor(math.random(1,3))
  132.     end
  133. end
  134.  
  135. while true do
  136.     screen.clear()
  137.     addStar(test)
  138.     meteor_management()
  139.     text_middle_screen("Salut les p'tits amis ! ")
  140.     sleep(0.2)
  141. end
Add Comment
Please, Sign In to add comment