smoothretro82

Bouncing text with gravity (floor is currently set to "-")

Nov 15th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. function spawnBouncingBall(options) {
  2. options = options || {};
  3.  
  4. // --- RANDOM BALL CHARACTER ---
  5. const randomChars = options.randomChars || ["๐Ÿ’ฉ", "๐Ÿ’ฉ", "๐Ÿ’ฉ", "๐Ÿ’ฉ", "๐Ÿ’ฉ", "๐Ÿ’ฉ", "0", "ยค", "โ€ข"];
  6. const ballChar = randomChars[Math.floor(Math.random() * randomChars.length)];
  7.  
  8. const colliderChar = options.colliderChar || "-";
  9. const speed = options.speed || 1;
  10. const friction = (options.friction !== undefined) ? options.friction : 0.98;
  11. const gravity = (options.gravity !== undefined) ? options.gravity : 0.05;
  12. const layer = options.layer || 0;
  13.  
  14. // --- Random Color ---
  15. function randomColor() {
  16. const r = Math.floor(Math.random() * 255);
  17. const g = Math.floor(Math.random() * 255);
  18. const b = Math.floor(Math.random() * 255);
  19. return `rgb(${r},${g},${b})`;
  20. }
  21.  
  22. const ballColor = randomColor();
  23.  
  24. // --- RANDOM SPAWN AROUND CURSOR ---
  25. const dirs = [
  26. [1,0],[-1,0],[0,1],[0,-1],
  27. [1,1],[1,-1],[-1,1],[-1,-1]
  28. ];
  29. const spawnDir = dirs[Math.floor(Math.random() * dirs.length)];
  30.  
  31. let x = cursor.x + spawnDir[0];
  32. let y = cursor.y + spawnDir[1];
  33.  
  34. writeCharAt(ballChar, layer, x, y, ballColor);
  35.  
  36. // --- Random initial velocity ---
  37. let vx = (Math.random() * 2 - 1) * speed;
  38. let vy = (Math.random() * 2 - 1) * speed;
  39.  
  40. if (Math.abs(vx) < 0.2) vx = 0.2 * Math.sign(vx || 1);
  41. if (Math.abs(vy) < 0.2) vy = 0.2 * Math.sign(vy || 1);
  42.  
  43. let rafId = null;
  44.  
  45. function tick() {
  46. writeCharAt(" ", layer, Math.round(x), Math.round(y));
  47.  
  48. vx *= friction;
  49. vy *= friction;
  50.  
  51. vy += gravity; // gravity
  52.  
  53. let newX = x + vx;
  54. let newY = y + vy;
  55.  
  56. let rx = Math.round(newX);
  57. let ry = Math.round(newY);
  58.  
  59. const info = getCharInfoXY(rx, ry);
  60. if (info && info.char === colliderChar) {
  61.  
  62. const horizInfo = getCharInfoXY(Math.round(x + vx), Math.round(y));
  63. const vertInfo = getCharInfoXY(Math.round(x), Math.round(y + vy));
  64.  
  65. if (horizInfo && horizInfo.char === colliderChar) vx = -vx;
  66. if (vertInfo && vertInfo.char === colliderChar) vy = -vy;
  67.  
  68. newX = x + vx;
  69. newY = y + vy;
  70. rx = Math.round(newX);
  71. ry = Math.round(newY);
  72. }
  73.  
  74. x = newX;
  75. y = newY;
  76.  
  77. writeCharAt(ballChar, layer, rx, ry, ballColor);
  78.  
  79. rafId = requestAnimationFrame(tick);
  80. }
  81.  
  82. rafId = requestAnimationFrame(tick);
  83.  
  84. return {
  85. stop() { cancelAnimationFrame(rafId); },
  86. setSpeed(newSpeed) {
  87. const factor = newSpeed / speed;
  88. vx *= factor;
  89. vy *= factor;
  90. },
  91. char: ballChar,
  92. color: ballColor
  93. };
  94. }
  95.  
  96. // Example:
  97. spawnBouncingBall({
  98. gravity: 0.1,
  99. randomChars: ["@", "#", "$", "%", "&", "o"]
  100. });
  101.  
Advertisement
Add Comment
Please, Sign In to add comment