Advertisement
Spirit13300

Untitled

Jan 22nd, 2025 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. -- Initialisation du terminal ou monitor
  2. local monitor = peripheral.find("monitor") or term -- Utilise un monitor ou le terminal par défaut
  3.  
  4. -- Configuration de base
  5. monitor.setTextScale(1) -- Ajuste la taille pour un monitor
  6. monitor.setBackgroundColor(colors.black) -- Fond noir par défaut
  7. monitor.setTextColor(colors.white) -- Texte blanc
  8. monitor.clear()
  9.  
  10. -- Tableau pour stocker les chiffres du pavé et leurs positions
  11. local keypad = {}
  12. local rows, cols = 2, 5 -- Position initiale du pavé (ligne, colonne)
  13.  
  14. -- Fonction pour dessiner un pavé numérique
  15. local function drawKeypad()
  16. local keys = {
  17. "7", "8", "9",
  18. "4", "5", "6",
  19. "1", "2", "3",
  20. " ", "0", " " -- Espace pour correspondre au pavé
  21. }
  22.  
  23. for i, key in ipairs(keys) do
  24. monitor.setCursorPos(cols, rows)
  25. monitor.write(key) -- Affiche chaque chiffre ou espace
  26. keypad[rows .. "," .. cols] = key -- Stocke le caractère et sa position
  27.  
  28. cols = cols + 2 -- Avance d'une colonne
  29. if i % 3 == 0 then -- Nouvelle ligne après 3 chiffres
  30. cols = 5
  31. rows = rows + 1
  32. end
  33. end
  34. end
  35.  
  36. -- Fonction pour colorer un chiffre cliqué
  37. local function highlightKey(x, y, color)
  38. local key = keypad[y .. "," .. x] -- Récupère le caractère basé sur la position
  39. if key and key ~= " " then -- Si c'est un chiffre (et pas un espace)
  40. local oldBg = monitor.getBackgroundColor() -- Sauvegarde l'ancienne couleur de fond
  41. monitor.setCursorPos(x, y)
  42. monitor.setBackgroundColor(color) -- Change le fond
  43. monitor.write(key) -- Réécrit le caractère avec la nouvelle couleur de fond
  44. monitor.setBackgroundColor(oldBg) -- Réinitialise le fond
  45. end
  46. end
  47.  
  48. -- Dessine le pavé numérique initial
  49. drawKeypad()
  50.  
  51. -- Boucle principale pour gérer les clics
  52. while true do
  53. local event, side, x, y = os.pullEvent("monitor_touch") -- Événement pour un clic sur monitor
  54. highlightKey(x, y, colors.green) -- Exemple : change en vert au clic
  55. end
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement