Guest User

javascript dice. Multi size

a guest
Jul 13th, 2024
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. <html><head><base href="https://diceroller.fun/" />
  2. <title>DiceRoller.fun - ASCII Dice Simulator</title>
  3. <style>
  4. body {
  5. font-family: 'Roboto', sans-serif;
  6. background-color: #f0f0f0;
  7. margin: 0;
  8. padding: 0;
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. min-height: 100vh;
  13. }
  14. .container {
  15. background-color: #ffffff;
  16. border-radius: 10px;
  17. box-shadow: 0 0 20px rgba(0,0,0,0.1);
  18. padding: 30px;
  19. text-align: center;
  20. max-width: 400px;
  21. width: 100%;
  22. }
  23. h1 {
  24. color: #4a4a4a;
  25. margin-bottom: 20px;
  26. }
  27. .controls {
  28. display: flex;
  29. justify-content: center;
  30. gap: 10px;
  31. margin-bottom: 20px;
  32. }
  33. button {
  34. background-color: #4CAF50;
  35. border: none;
  36. color: white;
  37. padding: 10px 20px;
  38. text-align: center;
  39. text-decoration: none;
  40. display: inline-block;
  41. font-size: 16px;
  42. margin: 4px 2px;
  43. cursor: pointer;
  44. border-radius: 5px;
  45. transition: background-color 0.3s;
  46. }
  47. button:hover {
  48. background-color: #45a049;
  49. }
  50. select {
  51. padding: 10px;
  52. font-size: 16px;
  53. border-radius: 5px;
  54. border: 1px solid #ddd;
  55. }
  56. .result {
  57. font-size: 24px;
  58. font-weight: bold;
  59. color: #4a4a4a;
  60. margin-top: 20px;
  61. }
  62. .dice-ascii {
  63. font-family: monospace;
  64. font-size: 14px;
  65. white-space: pre;
  66. line-height: 1;
  67. margin-top: 20px;
  68. text-align: center;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div class="container">
  74. <h1>DiceRoller.fun</h1>
  75. <div class="controls">
  76. <select id="dice-type">
  77. <option value="d4">D4</option>
  78. <option value="d6" selected>D6</option>
  79. <option value="d8">D8</option>
  80. <option value="d10">D10</option>
  81. <option value="d12">D12</option>
  82. <option value="d20">D20</option>
  83. </select>
  84. <button id="roll-button">Roll Dice</button>
  85. </div>
  86. <div class="result" id="result"></div>
  87. <div class="dice-ascii" id="dice-ascii"></div>
  88. </div>
  89.  
  90. <script>
  91. const diceTypeSelect = document.getElementById('dice-type');
  92. const rollButton = document.getElementById('roll-button');
  93. const resultElement = document.getElementById('result');
  94. const diceAsciiElement = document.getElementById('dice-ascii');
  95.  
  96. const diceTypes = {
  97. d4: { sides: 4 },
  98. d6: { sides: 6 },
  99. d8: { sides: 8 },
  100. d10: { sides: 10 },
  101. d12: { sides: 12 },
  102. d20: { sides: 20 }
  103. };
  104.  
  105. const diceAscii = {
  106. 1: `
  107. +-------+
  108. | |
  109. | • |
  110. | |
  111. +-------+`,
  112. 2: `
  113. +-------+
  114. | • |
  115. | |
  116. | • |
  117. +-------+`,
  118. 3: `
  119. +-------+
  120. | • |
  121. | • |
  122. | • |
  123. +-------+`,
  124. 4: `
  125. +-------+
  126. | • • |
  127. | |
  128. | • • |
  129. +-------+`,
  130. 5: `
  131. +-------+
  132. | • • |
  133. | • |
  134. | • • |
  135. +-------+`,
  136. 6: `
  137. +-------+
  138. | • • |
  139. | • • |
  140. | • • |
  141. +-------+`,
  142. 7: `
  143. +-------+
  144. | • • • |
  145. | • • |
  146. | • • • |
  147. +-------+`,
  148. 8: `
  149. +-------+
  150. | • • • |
  151. | • • • |
  152. | • • • |
  153. +-------+`,
  154. 9: `
  155. +-------+
  156. | • • • |
  157. | • • • |
  158. | • • • |
  159. | • • |
  160. +-------+`,
  161. 10: `
  162. +-------+
  163. | • • • |
  164. | • • • |
  165. | • • • |
  166. | • • • |
  167. +-------+`,
  168. };
  169.  
  170. function getAsciiNumber(num) {
  171. if (num <= 10) {
  172. return diceAscii[num];
  173. } else {
  174. return `
  175. +-------+
  176. | ${num.toString().padStart(2, ' ')} |
  177. | |
  178. | |
  179. +-------+`;
  180. }
  181. }
  182.  
  183. function rollDice() {
  184. const selectedDice = diceTypeSelect.value;
  185. const { sides } = diceTypes[selectedDice];
  186. const result = Math.floor(Math.random() * sides) + 1;
  187.  
  188. resultElement.textContent = `You rolled a ${selectedDice} and got:`;
  189. diceAsciiElement.textContent = getAsciiNumber(result);
  190. }
  191.  
  192. rollButton.addEventListener('click', rollDice);
  193.  
  194. // Initial roll
  195. rollDice();
  196. </script>
  197. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment