Advertisement
hellom38

Javascript2

Aug 16th, 2022 (edited)
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. var score = 0;
  3. var numTiles = 25;
  4. var tileCounter = 0;
  5. var chestCount = 0;
  6. var chestChance = 5;
  7. var gemChance = 58;
  8. var gemPoints = 500;
  9. var chestPoints = 1500;
  10. var gemcount = "";
  11. var starcount = "";
  12. var defaultTile = "grass";
  13. var newTile = "";
  14. var dirt = "dirt";
  15. var stone = "stone";
  16. var water = "water";
  17. var gemText = "Nice!";
  18. var chestText = "Wow!!";
  19. var startText = "Go!";
  20. var endText = "Yay!";
  21.  
  22. $(document).ready(function(){
  23.  
  24.   init(defaultTile,score);
  25.  
  26.   $('.'+defaultTile).on('click',function(){
  27.     if($(this).hasClass(defaultTile)){
  28.  
  29.     //get a % value of 100 for "drop rates"
  30.     random = Math.floor(Math.random() * 100);
  31.      
  32.     if(chestCount===1){
  33.     getGem($(this),random);
  34.     }
  35.     else{
  36.       if(random <= chestChance){
  37.       chest($(this));
  38.     }
  39.     else{
  40.       getGem($(this),random);
  41.     }
  42.     }
  43.    
  44.     }
  45.  
  46.   if($('.off').length === 25 && $('.chest-closed').length === 0){
  47.     $('.gameover').show();
  48.     $('.final-score').text(score);
  49.     speechText(endText,3000);
  50.   }
  51.  
  52.   });
  53.  
  54.   $('.reset').on('click',function(){
  55.   $('.chest').remove();
  56.     $('.gameBoard .gametile').removeClass('dirt water stone off').addClass(defaultTile);
  57.     score=0;
  58.     chestCount=0;
  59.     gemcount=0;
  60.     starcount=0;
  61.   $('.gemcount').text(0);
  62.   $('.starcount').text(0);
  63.     $('.score').text(score);
  64.   $('.final-score').text(score);
  65.   $('.gameover').hide();
  66.   speechText(startText,600);
  67.   });
  68.  
  69. });
  70.  
  71. function init(defaultTile,score){
  72.   $('.gameBoard .gametile').addClass(defaultTile);
  73.   $('.score').text(score);
  74. }
  75.  
  76. function getGem(tile){
  77.   if(random > chestChance && random <= (gemChance + chestChance)){
  78.     gem(tile);
  79.   }
  80.   else{
  81.     changeTile(tile,water);
  82.     sndEffects(0);
  83.   }
  84. }
  85.  
  86. function gem(tile) {  
  87.   changeTile(tile,dirt);
  88.   speechText(gemText,250);
  89.   sndEffects(1);  
  90.   scoreCalc(gemPoints);
  91.   gemcount++;
  92.   $('.gemcount').text(gemcount);
  93.   $(tile).append("<img class='gem' src='http://adrianpayne.me/game/assets/images/gem.png' />");
  94.   $(tile).find('.gem').animate({top:"-30px",opacity:"0"},700);
  95.   setTimeout(function() {
  96.      $('img:last-child', tile).remove();
  97.   }, 800);
  98. }
  99.  
  100. function chest(tile) {  
  101.   changeTile(tile,stone);
  102.   $(tile).append("<div class='chest chest-closed'></div>");
  103.   chestCount = 1;
  104.   sndEffects(0);
  105.  
  106.   $(".chest").click(function(){
  107.   $(this).unbind();
  108.   $(this).addClass('chest-open').removeClass('chest-closed');
  109.   $(this).append("<img class='star' src='http://adrianpayne.me/game/assets/images/star.png' />");
  110.     $(this).find('.star').animate({top:"-40px",opacity:"0"},800);
  111.   $('.starcount').text(1);
  112.   speechText(chestText,300);
  113.   sndEffects(1);
  114.   scoreCalc(chestPoints);
  115.   $(this).delay(200).fadeOut("slow");
  116.   });  
  117. }
  118.  
  119. function changeTile(tile, type){
  120.   $(tile).removeClass(defaultTile).addClass(type).addClass("off");
  121. }
  122.  
  123. function sndEffects(loot) {  
  124.   $("#pop-" + Math.ceil(Math.random() * 2))[0].play();
  125.   if(loot){
  126.   $("#gemfind")[0].play();  
  127.   }
  128. }
  129.  
  130. function speechText(text,delay){
  131.   $('.speech-bubble').stop(true, false).animate({opacity:"1"},200);
  132.   $('.speech-bubble').text(text);
  133.   $('.speech-bubble').delay(delay).animate({opacity:"0"},40);
  134. }
  135.  
  136. function scoreCalc(points) {
  137.   score += points;
  138.   $('.score').text(score);
  139. }
  140. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement