smoothretro82

Control a character with the arrow keys

Nov 16th, 2025 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. function spawnGridCharacter(options = {}) {
  2. const char = options.char || "@";
  3. const color = options.color || "white";
  4. const layer = options.layer || 10;
  5.  
  6. let x = options.x || cursor.x;
  7. let y = options.y || cursor.y;
  8.  
  9. // Draw initial
  10. writeCharAt(char, layer, x, y, color);
  11.  
  12. function move(dx, dy) {
  13. const oldX = x;
  14. const oldY = y;
  15.  
  16. x += dx;
  17. y += dy;
  18.  
  19. writeCharAt(" ", layer, oldX, oldY);
  20. writeCharAt(char, layer, x, y, color);
  21. }
  22.  
  23. // KEYBOARD INPUT
  24. function onKeyDown(e) {
  25. if (e.repeat) return; // Prevent holding key from repeating
  26. if (e.key === "ArrowUp") move(0, -1);
  27. if (e.key === "ArrowDown") move(0, 1);
  28. if (e.key === "ArrowLeft") move(-1, 0);
  29. if (e.key === "ArrowRight") move(1, 0);
  30. }
  31.  
  32. window.addEventListener("keydown", onKeyDown);
  33.  
  34. // ---------------------------------------
  35. // ONSCREEN ARROW KEYS
  36. // ---------------------------------------
  37. function createButton(txt, dx, dy) {
  38. const btn = document.createElement("button");
  39. btn.textContent = txt;
  40. btn.style.fontSize = "24px";
  41. btn.style.margin = "4px";
  42. btn.style.width = "50px";
  43. btn.style.height = "50px";
  44.  
  45. btn.addEventListener("mousedown", () => move(dx, dy));
  46. btn.addEventListener("touchstart", (e) => {
  47. e.preventDefault();
  48. move(dx, dy);
  49. });
  50.  
  51. return btn;
  52. }
  53.  
  54. // Container for onscreen buttons
  55. const pad = document.createElement("div");
  56. pad.style.position = "fixed";
  57. pad.style.bottom = "20px";
  58. pad.style.left = "20px";
  59. pad.style.display = "grid";
  60. pad.style.gridTemplateColumns = "50px 50px 50px";
  61. pad.style.gridTemplateRows = "50px 50px 50px";
  62. pad.style.gap = "4px";
  63. pad.style.userSelect = "none";
  64.  
  65. pad.appendChild(document.createElement("div")); // empty
  66. pad.appendChild(createButton("↑", 0, -1)); // up
  67. pad.appendChild(document.createElement("div")); // empty
  68. pad.appendChild(createButton("←", -1, 0)); // left
  69. pad.appendChild(document.createElement("div")); // empty
  70. pad.appendChild(createButton("→", 1, 0)); // right
  71. pad.appendChild(document.createElement("div")); // empty
  72. pad.appendChild(createButton("↓", 0, 1)); // down
  73. pad.appendChild(document.createElement("div")); // empty
  74.  
  75. document.body.appendChild(pad);
  76.  
  77. return {
  78. stop() {
  79. window.removeEventListener("keydown", onKeyDown);
  80. pad.remove();
  81. },
  82. setChar(newChar) {
  83. writeCharAt(" ", layer, x, y);
  84. writeCharAt(newChar, layer, x, y, color);
  85. },
  86. setColor(newColor) {
  87. writeCharAt(char, layer, x, y, newColor);
  88. },
  89. get position() { return { x, y }; }
  90. };
  91. }
  92.  
  93. // Example usage:
  94. const player = spawnGridCharacter({
  95. char: "@",
  96. color: "white"
  97. });
  98.  
Advertisement
Add Comment
Please, Sign In to add comment