Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Script für ein kleines Spiel, programmiert in Javascript und Jquery. Zu finden auf http://krugear.de/jsgame/
  2.  
  3.  
  4. $(document).ready(function(){
  5. //Startvariablen werden gesetzt
  6. var score = 0;
  7. var rand;
  8. var seconds = 10;
  9. var active = false;
  10. var finished = 0;
  11. var Game = {};
  12. $('#requestedKey').html("↑ to (Re)Start");
  13.  
  14.  
  15.  
  16. // Der erste Input wird abgefragt (in diesem Fall die Pfeiltaste nach oben),
  17. // außerdem wird ein Interval festgelegt, welcher prüft, ob die Zeit abgelaufen ist
  18. // und das Spiel beendet
  19. document.addEventListener('keydown', function(event) {
  20.     if(event.keyCode == 38 && active == 0) {
  21.     active = 1;
  22.     Game.requestKey();
  23.     if (rand == "←"){        
  24.         $('#requestedKey').css("margin-left","-200px");
  25.         }
  26.     else{
  27.         $('#requestedKey').css("margin-left","200px");
  28.         }
  29.     interval = setInterval(function(){
  30.         seconds -= 0.01;
  31.         $('#timer').html(seconds.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]);
  32.         if (seconds <= 0){
  33.         endGame();
  34.         }
  35.         },10);
  36.     }
  37. });
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. // Das Programm fragt in dieser Funktion nach einer der zwei Pfeiltasten..
  46. Game.requestKey = function() {
  47.     var choices = ['←','→'];
  48.     var rand = choices[Math.floor(Math.random() * choices.length)];
  49.     $('#requestedKey').html(rand);
  50.     if (active == 0){
  51.         $('#requestedKey').html("Start");
  52.     }
  53.     // ...und speichert den Keycode des eingegebenen Keys..
  54.     document.onkeydown = function(){
  55.     var keyPress = event.keyCode;
  56.     if (finished === 0){
  57.     if((keyPress == "39") || (keyPress == "37")){
  58.         if (rand == "←"){            
  59.             $('#requestedKey').css("margin-left","-200px");
  60.             }
  61.         else{
  62.             $('#requestedKey').css("margin-left","200px");
  63.             }
  64.                 }
  65.                     }          
  66.         Game.checkKey(keyPress,rand); // ...um ihn in einer weiteren Funktion zu prüfen
  67.         };
  68. };
  69.  
  70. // Diese Funktion prüft ob die eingegebene Taste mit der zufällig ausgewählten erfragten Taste übereinstimmt
  71. Game.checkKey = function(key,rand) {
  72.     // Wenn das Spiel noch nicht beendet ist..
  73.     if (finished === 0){
  74.     // .. und die richtige Taste eingegeben wurde..
  75.     if (rand == "←" && key == "37") {
  76.         // .. wird der Score erhöht und eine neue Taste erfragt
  77.         keyanimate();
  78.         score ++;
  79.         if (score % 10 === 0){
  80.             seconds +=3;
  81.             bonus();
  82.             }
  83.         Game.requestKey();
  84.     }
  85.     else if (rand == "→" && key == "39"){
  86.         keyanimate();
  87.         score ++;
  88.         console.log(score);
  89.         if (score % 10 === 0){
  90.         seconds +=3;
  91.         bonus();
  92.         }
  93.     Game.requestKey();
  94.     }
  95.     else if ((rand == "←" && key == "39") || (rand == "→" && key == "37") ){
  96.         // Wenn die falsche Taste eingegeben wurde, beendet sich das Spiel
  97.         endGame();
  98.     }
  99.     $('#score').html(score);
  100.  
  101. }
  102. else{
  103.     // Wenn das Spiel beendet ist, wird mit der Pfeiltaste oben neu gestartet
  104.     if (key=="38"){
  105.     score = 0;
  106.     $('#score').html(score);
  107.     finished = 0;
  108.     seconds = 10;
  109.     Game.requestKey();
  110.     interval = setInterval(function(){
  111.         seconds -= 0.01;
  112.          $('#timer').html(seconds.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]);
  113.              if (seconds <= 0){
  114.             endGame();
  115.         }
  116.     },10);
  117.  
  118. }
  119. }
  120. };
  121.  
  122. // Animationen für die Pfeile
  123. function keyanimate(){
  124.     $('#requestedKey').effect( "shake",{times:2, distance:10, direction:"up"},10);
  125.     $('#score').effect( "shake",{times:2, distance:10, direction:"up"},10);
  126. }
  127. function bonus(){
  128.     $('#bonus').css("display","block");
  129.     $('#bonus').fadeOut();
  130. }
  131.  
  132.  
  133. // Anzeige für das Spielende
  134. endGame = function(){
  135.     finished = 1;
  136.     clearInterval(interval);
  137.     $('#requestedKey').html("Score " + score);
  138.     $('#requestedKey').css("margin","0");
  139. };
  140.  
  141.    
  142. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement