Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. // Memory Game
  2. // © 2014 Nate Wiley
  3. // License -- MIT
  4. // best in full screen, works on phones/tablets (min height for game is 500px..) enjoy ;)
  5. // Follow me on Codepen
  6.  
  7. (function(){
  8.  
  9. var Memory = {
  10.  
  11. init: function(cards){
  12. this.$game = $(".game");
  13. this.$modal = $(".modal");
  14. this.$overlay = $(".modal-overlay");
  15. this.$restartButton = $("button.restart");
  16. this.cardsArray = $.merge(cards, cards);
  17. this.shuffleCards(this.cardsArray);
  18. this.setup();
  19. },
  20.  
  21. shuffleCards: function(cardsArray){
  22. this.$cards = $(this.shuffle(this.cardsArray));
  23. },
  24.  
  25. setup: function(){
  26. this.html = this.buildHTML();
  27. this.$game.html(this.html);
  28. this.$memoryCards = $(".card");
  29. this.binding();
  30. this.paused = false;
  31. this.guess = null;
  32. },
  33.  
  34. binding: function(){
  35. this.$memoryCards.on("click", this.cardClicked);
  36. this.$restartButton.on("click", $.proxy(this.reset, this));
  37. },
  38. // kinda messy but hey
  39. cardClicked: function(){
  40. var _ = Memory;
  41. var $card = $(this);
  42. if(!_.paused && !$card.find(".inside").hasClass("matched") && !$card.find(".inside").hasClass("picked")){
  43. $card.find(".inside").addClass("picked");
  44. if(!_.guess){
  45. _.guess = $(this).attr("data-id");
  46. } else if(_.guess == $(this).attr("data-id") && !$(this).hasClass("picked")){
  47. $(".picked").addClass("matched");
  48. _.guess = null;
  49. } else {
  50. _.guess = null;
  51. _.paused = true;
  52. setTimeout(function(){
  53. $(".picked").removeClass("picked");
  54. Memory.paused = false;
  55. }, 600);
  56. }
  57. if($(".matched").length == $(".card").length){
  58. _.win();
  59. }
  60. }
  61. },
  62.  
  63. win: function(){
  64. this.paused = true;
  65. setTimeout(function(){
  66. Memory.showModal();
  67. Memory.$game.fadeOut();
  68. }, 1000);
  69. },
  70.  
  71. showModal: function(){
  72. this.$overlay.show();
  73. this.$modal.fadeIn("slow");
  74. },
  75.  
  76. hideModal: function(){
  77. this.$overlay.hide();
  78. this.$modal.hide();
  79. },
  80.  
  81. reset: function(){
  82. this.hideModal();
  83. this.shuffleCards(this.cardsArray);
  84. this.setup();
  85. this.$game.show("slow");
  86. },
  87.  
  88. // Fisher--Yates Algorithm -- https://bost.ocks.org/mike/shuffle/
  89. shuffle: function(array){
  90. var counter = array.length, temp, index;
  91. // While there are elements in the array
  92. while (counter > 0) {
  93. // Pick a random index
  94. index = Math.floor(Math.random() * counter);
  95. // Decrease counter by 1
  96. counter--;
  97. // And swap the last element with it
  98. temp = array[counter];
  99. array[counter] = array[index];
  100. array[index] = temp;
  101. }
  102. return array;
  103. },
  104.  
  105. buildHTML: function(){
  106. var frag = '';
  107. this.$cards.each(function(k, v){
  108. frag += '<div class="card" data-id="'+ v.id +'"><div class="inside">\
  109. <div class="front"><img src="'+ v.img +'"\
  110. alt="'+ v.name +'" /></div>\
  111. <div class="back"><img src="http://pngimg.com/upload/diamond_PNG6692.png"\
  112. alt="Codepen" /></div></div>\
  113. </div>';
  114. });
  115. return frag;
  116. }
  117. };
  118.  
  119. var cards = [
  120. {
  121. name: "php",
  122. img: "http://pngimg.com/upload/diamond_PNG6692.png",
  123. id: 1,
  124. },
  125. {
  126. name: "css3",
  127. img: "http://www.excite.com/events/blog/wp-content/uploads/2014/06/iStock_000000542191XSmall1.jpg",
  128. id: 2
  129. },
  130. {
  131. name: "html5",
  132. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/html5-logo.png",
  133. id: 3
  134. },
  135. {
  136. name: "jquery",
  137. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/jquery-logo.png",
  138. id: 4
  139. },
  140. {
  141. name: "javascript",
  142. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/js-logo.png",
  143. id: 5
  144. },
  145. {
  146. name: "node",
  147. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/nodejs-logo.png",
  148. id: 6
  149. },
  150. {
  151. name: "photoshop",
  152. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/photoshop-logo.png",
  153. id: 7
  154. },
  155. {
  156. name: "python",
  157. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/python-logo.png",
  158. id: 8
  159. },
  160. {
  161. name: "rails",
  162. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/rails-logo.png",
  163. id: 9
  164. },
  165. {
  166. name: "sass",
  167. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/sass-logo.png",
  168. id: 10
  169. },
  170. {
  171. name: "sublime",
  172. img: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/sublime-logo.png",
  173. id: 11
  174. },
  175. {
  176. name: "wordpress",
  177. img:"https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/wordpress-logo.png",
  178. id: 12
  179. },
  180. ];
  181.  
  182. Memory.init(cards);
  183.  
  184.  
  185. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement