Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. var game = { //game object
  2. level: 1, //current level
  3. turn: 0, //current turn
  4. difficulty: 1, // user difficulty
  5. score: 0, //current score
  6. active: false, //whether a turn is active or not
  7. handler: false, // whether the click and sound handlers are active
  8. shape: '.shape', // cached string for the pad class
  9. genSequence: [], //array containing the generated/randomized pads
  10. plaSequence: [], //array containing the users pad selections
  11. colors: ['green', 'red', 'purple', 'blue'],
  12. init: function () { //initialises the game
  13. if (this.handler === false) { //checks to see if handlers are already active
  14. this.initPadHandler(); //if not activate them
  15. }
  16. this.newGame(); //reset the game defaults
  17.  
  18. },
  19.  
  20. initPadHandler: function () {
  21.  
  22. that = this;
  23.  
  24. $('.pad').on('mouseup', function () {
  25.  
  26. if (that.active === true) {
  27.  
  28. var pad = parseInt($(this).data('pad'), 10);
  29.  
  30. that.flash($(this), 1, 300, pad);
  31.  
  32. that.logPlayerSequence(pad);
  33.  
  34. }
  35. });
  36.  
  37. this.handler = true;
  38.  
  39. },
  40.  
  41. newGame: function () { //resets the game and generates a starts a new level
  42.  
  43. this.level = 1;
  44. this.score = 0;
  45. this.newLevel();
  46. this.displayLevel();
  47. this.displayScore();
  48.  
  49. //initialize timer to 10 seconds (10.0)
  50. this.timer = 10;
  51.  
  52. },
  53.  
  54. newLevel: function () {
  55.  
  56. this.genSequence.length = 0;
  57. this.plaSequence.length = 0;
  58. this.pos = 0;
  59. this.turn = 0;
  60. this.active = true;
  61.  
  62. this.randomizePad(this.level); //randomize pad with the correct amount of numbers for this level
  63. this.displaySequence(); //show the user the sequence
  64. },
  65.  
  66. flash: function (element, times, speed, pad) { //function to make the pads appear to flash
  67.  
  68. var that = this; //cache this
  69.  
  70. if (times > 0) { //make sure we are supposed to flash
  71. that.playSound(pad); //play the corresponding pad sound
  72. element.stop().animate({
  73. opacity: '1'
  74. }, { //animate the element to appear to flash
  75. duration: 50,
  76. complete: function () {
  77. element.stop().animate({
  78. opacity: '0.6'
  79. }, 200);
  80. }
  81. }); //end animation
  82.  
  83. }
  84.  
  85. if (times > 0) { //call the flash function again until done the correct amount of times
  86. setTimeout(function () {
  87. that.flash(element, times, speed, pad);
  88. }, speed);
  89. times -= 1; //times - 1 for each time it's called
  90. }
  91. },
  92.  
  93. playSound: function (clip) { //plays the sound that corresponds to the pad chosen
  94.  
  95. var sound = $('.sound' + clip)[0];
  96. console.log(sound);
  97. console.log($('.sound' + clip));
  98. sound.currentTime = 0; //resets audio position to the start of the clip
  99. sound.play(); //play the sound
  100.  
  101.  
  102. },
  103.  
  104. randomizePad: function (passes) { //generate random numbers and push them to the generated number array iterations determined by current level
  105.  
  106. for (i = 0; i < passes; i++) {
  107.  
  108. this.genSequence.push(Math.floor(Math.random() * 4) + 1);
  109.  
  110. }
  111. },
  112.  
  113. logPlayerSequence: function (pad) { //log the player selected pad to user array and call the checker function
  114.  
  115. this.plaSequence.push(pad);
  116. this.checkSequence(pad);
  117.  
  118.  
  119. },
  120.  
  121. checkSequence: function (pad) { //checker function to test if the pad the user pressed was next in the sequence
  122.  
  123. that = this;
  124.  
  125. if (pad !== this.genSequence[this.turn]) { //if not correct
  126.  
  127. this.incorrectSequence();
  128.  
  129. } else { //if correct
  130. this.keepScore(); //update the score
  131. this.turn++; //incrememnt the turn
  132.  
  133. }
  134.  
  135. if (this.turn === this.genSequence.length) { //if completed the whole sequence
  136.  
  137. this.level++; //increment level, display it, disable the pads wait 1 second and then reset the game
  138. this.displayLevel();
  139. this.active = false;
  140.  
  141. // Stop counting when sequence is correct to avoid time running out before starting next level
  142. clearInterval(this.timerInterval);
  143.  
  144. //Add 5.0 seconds each 5th level
  145. this.timer = 10 + 5 * Math.floor(this.level / 5);
  146.  
  147. //Update timerdisplay to show fulltime while displaying next level sequence
  148. $(".Timer p").html(this.timer);
  149.  
  150. setTimeout(function () {
  151. that.newLevel();
  152. }, 1000);
  153. }
  154. },
  155.  
  156. // Countdown and update timer, call incorrectsequence when time's up
  157. countDown: function () {
  158. this.timer -= 0.1;
  159. $(".Timer p").html(this.timer.toFixed(1)); // Display 9.0 instad of 9
  160. if (this.timer < 0.1) {
  161. this.incorrectSequence();
  162. }
  163. },
  164.  
  165. displaySequence: function () { //display the generated sequence to the user
  166.  
  167. var that = this;
  168.  
  169. var timerCount = 0;
  170.  
  171. $.each(this.genSequence, function (index, val) { //iterate over each value in the generated array
  172. timerCount = index;
  173. setTimeout(function () {
  174.  
  175. that.flash($(that.shape + val), 1, 300, val);
  176. $(".TextBox").children(":first").html('<b>'+that.colors[val-1]+'</b>');
  177. }, 500 * index * that.difficulty); // multiply timeout by how many items in the array so that they play sequentially and multiply by the difficulty modifier
  178. });
  179.  
  180. // Wait to start timer until full sequence is displayed
  181. setTimeout(function () {
  182. that.timerInterval = setInterval(function () {
  183. that.countDown()
  184. }, 100)
  185.  
  186. setTimeout(function(){$(".TextBox").children(":first").html('');}, 500);
  187. }, 500 * timerCount * that.difficulty);
  188. },
  189.  
  190. displayLevel: function () { //just display the current level on screen
  191.  
  192. $('.level h2').text('Level: ' + this.level);
  193.  
  194. },
  195.  
  196. displayScore: function () { //display current score on screen
  197. $('.score h2').text('Score: ' + this.score);
  198. },
  199.  
  200. keepScore: function () { //keep the score
  201.  
  202. var multiplier = 0;
  203.  
  204. switch (this.difficulty) //choose points modifier based on difficulty
  205. {
  206. case '2':
  207. multiplier = 1;
  208. break;
  209.  
  210. case '1':
  211. multiplier = 2;
  212. break;
  213.  
  214. case '0.5':
  215. multiplier = 3;
  216. break;
  217.  
  218. case '0.25':
  219. multiplier = 4;
  220. break;
  221. }
  222.  
  223. this.score += (1 * multiplier); //work out the score
  224.  
  225. this.displayScore(); //display score on screen
  226. },
  227.  
  228. incorrectSequence: function () { //if user makes a mistake
  229.  
  230. //Stop counting down timer and display start message
  231. clearInterval(this.timerInterval);
  232. $(".Timer p").html("Get Ready your time starts when you click start");
  233.  
  234. var corPad = this.genSequence[this.turn], //cache the pad number that should have been pressed
  235.  
  236. that = this;
  237. this.active = false;
  238. this.displayLevel();
  239. this.displayScore();
  240.  
  241. setTimeout(function () { //flash the pad 4 times that should have been pressed
  242. that.flash($(that.shape + corPad), 4, 300, corPad);
  243. }, 500);
  244.  
  245. $(".TextBox").children(":first").html("<b>the good answer was "+that.colors[corPad-1]+"</b>");
  246.  
  247. $('.start').show(); //enable the start button again and allow difficulty selection again
  248. $('.difficulty').show();
  249.  
  250. }
  251.  
  252. };
  253. $(document).ready(function () { //document ready
  254.  
  255. $('.start').on('mouseup', function () { //initialise a game when the start button is clicked
  256. $(this).hide();
  257. game.difficulty = $('input[name=difficulty]:checked').val();
  258. $('.difficulty').hide();
  259. game.init();
  260.  
  261.  
  262. });
  263.  
  264.  
  265. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement