Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. window.onload = function () {
  2.  
  3. var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  4. 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  5. 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  6.  
  7. var categories; // Array of topics
  8. var chosenCategory; // Selected catagory
  9. var getHint ; // Word getHint
  10. var word ; // Selected word
  11. var guess ; // Geuss
  12. var geusses = [ ]; // Stored geusses
  13. var lives ; // Lives
  14. var counter ; // Count correct geusses
  15. var space; // Number of spaces in word '-'
  16.  
  17. // Get elements
  18. var showLives = document.getElementById("mylives");
  19. var showCatagory = document.getElementById("scatagory");
  20. var getHint = document.getElementById("hint");
  21. var showClue = document.getElementById("clue");
  22.  
  23.  
  24.  
  25. // create alphabet ul
  26. var buttons = function () {
  27. myButtons = document.getElementById('buttons');
  28. letters = document.createElement('ul');
  29.  
  30. for (var i = 0; i < alphabet.length; i++) {
  31. letters.id = 'alphabet';
  32. list = document.createElement('li');
  33. list.id = 'letter';
  34. list.innerHTML = alphabet[i];
  35. check();
  36. myButtons.appendChild(letters);
  37. letters.appendChild(list);
  38. }
  39. }
  40.  
  41.  
  42. // Select Catagory
  43. var selectCat = function () {
  44. if (chosenCategory === categories[0]) {
  45. catagoryName.innerHTML = "The Chosen Category Is Premier League Football Teams";
  46. } else if (chosenCategory === categories[1]) {
  47. catagoryName.innerHTML = "The Chosen Category Is Films";
  48. } else if (chosenCategory === categories[2]) {
  49. catagoryName.innerHTML = "The Chosen Category Is Cities";
  50. }
  51. }
  52.  
  53. // Create geusses ul
  54. result = function () {
  55. wordHolder = document.getElementById('hold');
  56. correct = document.createElement('ul');
  57.  
  58. for (var i = 0; i < word.length; i++) {
  59. correct.setAttribute('id', 'my-word');
  60. guess = document.createElement('li');
  61. guess.setAttribute('class', 'guess');
  62. if (word[i] === "-") {
  63. guess.innerHTML = "-";
  64. space = 1;
  65. } else {
  66. guess.innerHTML = "_";
  67. }
  68.  
  69. geusses.push(guess);
  70. wordHolder.appendChild(correct);
  71. correct.appendChild(guess);
  72. }
  73. }
  74.  
  75. // Show lives
  76. comments = function () {
  77. showLives.innerHTML = "You have " + lives + " lives";
  78. if (lives < 1) {
  79. showLives.innerHTML = "Game Over";
  80. }
  81. for (var i = 0; i < geusses.length; i++) {
  82. if (counter + space === geusses.length) {
  83. showLives.innerHTML = "You Win!";
  84. }
  85. }
  86. }
  87.  
  88. // Animate man
  89. var animate = function () {
  90. var drawMe = lives ;
  91. drawArray[drawMe]();
  92. }
  93.  
  94.  
  95. // Hangman
  96. canvas = function(){
  97.  
  98. myStickman = document.getElementById("stickman");
  99. context = myStickman.getContext('2d');
  100. context.beginPath();
  101. context.strokeStyle = "#fff";
  102. context.lineWidth = 2;
  103. };
  104.  
  105. head = function(){
  106. myStickman = document.getElementById("stickman");
  107. context = myStickman.getContext('2d');
  108. context.beginPath();
  109. context.arc(60, 25, 10, 0, Math.PI*2, true);
  110. context.stroke();
  111. }
  112.  
  113. draw = function($pathFromx, $pathFromy, $pathTox, $pathToy) {
  114.  
  115. context.moveTo($pathFromx, $pathFromy);
  116. context.lineTo($pathTox, $pathToy);
  117. context.stroke();
  118. }
  119.  
  120. frame1 = function() {
  121. draw (0, 150, 150, 150);
  122. };
  123.  
  124. frame2 = function() {
  125. draw (10, 0, 10, 600);
  126. };
  127.  
  128. frame3 = function() {
  129. draw (0, 5, 70, 5);
  130. };
  131.  
  132. frame4 = function() {
  133. draw (60, 5, 60, 15);
  134. };
  135.  
  136. torso = function() {
  137. draw (60, 36, 60, 70);
  138. };
  139.  
  140. rightArm = function() {
  141. draw (60, 46, 100, 50);
  142. };
  143.  
  144. leftArm = function() {
  145. draw (60, 46, 20, 50);
  146. };
  147.  
  148. rightLeg = function() {
  149. draw (60, 70, 100, 100);
  150. };
  151.  
  152. leftLeg = function() {
  153. draw (60, 70, 20, 100);
  154. };
  155.  
  156. drawArray = [rightLeg, leftLeg, rightArm, leftArm, torso, head, frame4, frame3, frame2, frame1];
  157.  
  158.  
  159. // OnClick Function
  160. check = function () {
  161. list.onclick = function () {
  162. var geuss = (this.innerHTML);
  163. this.setAttribute("class", "active");
  164. this.onclick = null;
  165. for (var i = 0; i < word.length; i++) {
  166. if (word[i] === geuss) {
  167. geusses[i].innerHTML = geuss;
  168. counter += 1;
  169. }
  170. }
  171. var j = (word.indexOf(geuss));
  172. if (j === -1) {
  173. lives -= 1;
  174. comments();
  175. animate();
  176. } else {
  177. comments();
  178. }
  179. }
  180. }
  181.  
  182.  
  183. // Play
  184. play = function () {
  185. categories = [
  186. ["everton", "liverpool", "swansea", "chelsea", "hull", "manchester-city", "newcastle-united"],
  187. ["alien", "dirty-harry", "gladiator", "finding-nemo", "jaws"],
  188. ["manchester", "milan", "madrid", "amsterdam", "prague"]
  189. ];
  190.  
  191. chosenCategory = categories[Math.floor(Math.random() * categories.length)];
  192. word = chosenCategory[Math.floor(Math.random() * chosenCategory.length)];
  193. word = word.replace(/\s/g, "-");
  194. console.log(word);
  195. buttons();
  196.  
  197. geusses = [ ];
  198. lives = 10;
  199. counter = 0;
  200. space = 0;
  201. result();
  202. comments();
  203. selectCat();
  204. canvas();
  205. }
  206.  
  207. play();
  208.  
  209. // Hint
  210.  
  211. hint.onclick = function() {
  212.  
  213. hints = [
  214. ["Based in Mersyside", "Based in Mersyside", "First Welsh team to reach the Premier Leauge", "Owned by A russian Billionaire", "Once managed by Phil Brown", "2013 FA Cup runners up", "Gazza's first club"],
  215. ["Science-Fiction horror film", "1971 American action film", "Historical drama", "Anamated Fish", "Giant great white shark"],
  216. ["Northern city in the UK", "Home of AC and Inter", "Spanish capital", "Netherlands capital", "Czech Republic capital"]
  217. ];
  218.  
  219. var catagoryIndex = categories.indexOf(chosenCategory);
  220. var hintIndex = chosenCategory.indexOf(word);
  221. showClue.innerHTML = "Clue: - " + hints [catagoryIndex][hintIndex];
  222. };
  223.  
  224. // Reset
  225.  
  226. document.getElementById('reset').onclick = function() {
  227. correct.parentNode.removeChild(correct);
  228. letters.parentNode.removeChild(letters);
  229. showClue.innerHTML = "";
  230. context.clearRect(0, 0, 400, 400);
  231. play();
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement