Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Pictionary

Sep 14th, 2024 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. -- Client program (run on each player's ComputerCraft computer)
  2.  
  3. -- Find the monitor
  4. local monitor = peripheral.find("monitor")
  5. if not monitor then
  6. print("No monitor found!")
  7. return
  8. end
  9.  
  10. local blockWidth, blockHeight = 7, 5
  11. local blocksHorizontal, blocksVertical = 8, 4
  12. local width, height = blocksHorizontal * blockWidth, blocksVertical * blockHeight
  13. local drawingData = {} -- Table to store drawing data
  14.  
  15. -- Function to draw the received data on the monitor
  16. function drawOnMonitor()
  17. monitor.clear()
  18. for _, pixel in ipairs(drawingData) do
  19. local blockX, blockY, color = pixel[1], pixel[2], pixel[3]
  20. -- Calculate pixel coordinates
  21. local startX = (blockX - 1) * blockWidth + 1
  22. local startY = (blockY - 1) * blockHeight + 1
  23.  
  24. -- Draw the block
  25. for dx = 0, blockWidth - 1 do
  26. for dy = 0, blockHeight - 1 do
  27. local x = startX + dx
  28. local y = startY + dy
  29. if x >= 1 and x <= width and y >= 1 and y <= height then
  30. monitor.setCursorPos(x, y)
  31. monitor.setBackgroundColor(color)
  32. monitor.write(" ")
  33. end
  34. end
  35. end
  36. end
  37. end
  38.  
  39. -- Function to display the timer on the monitor
  40. function displayTimer(timeLeft)
  41. monitor.setCursorPos(1, height)
  42. monitor.setBackgroundColor(colors.black)
  43. monitor.setTextColor(colors.white)
  44. monitor.write("Time left: " .. timeLeft)
  45. end
  46.  
  47. -- Function to handle incoming messages from the server
  48. function handleServerMessages()
  49. rednet.open("top")
  50. while true do
  51. local senderId, message = rednet.receive()
  52.  
  53. if message:find("DRAWING") then
  54. drawingData = textutils.unserialize(message:sub(9))
  55. drawOnMonitor()
  56. elseif message:find("TIMER") then
  57. local timeLeft = message:sub(7)
  58. displayTimer(timeLeft)
  59. end
  60. end
  61. end
  62.  
  63. -- Notify the server that this client has joined
  64. rednet.open("top")
  65. rednet.broadcast("JOIN")
  66.  
  67. handleServerMessages()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement