Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Script für ein kleines Spiel, programmiert in Javascript und Jquery. Zu finden auf http://krugear.de/jsgame/
- $(document).ready(function(){
- //Startvariablen werden gesetzt
- var score = 0;
- var rand;
- var seconds = 10;
- var active = false;
- var finished = 0;
- var Game = {};
- $('#requestedKey').html("↑ to (Re)Start");
- // Der erste Input wird abgefragt (in diesem Fall die Pfeiltaste nach oben),
- // außerdem wird ein Interval festgelegt, welcher prüft, ob die Zeit abgelaufen ist
- // und das Spiel beendet
- document.addEventListener('keydown', function(event) {
- if(event.keyCode == 38 && active == 0) {
- active = 1;
- Game.requestKey();
- if (rand == "←"){
- $('#requestedKey').css("margin-left","-200px");
- }
- else{
- $('#requestedKey').css("margin-left","200px");
- }
- interval = setInterval(function(){
- seconds -= 0.01;
- $('#timer').html(seconds.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]);
- if (seconds <= 0){
- endGame();
- }
- },10);
- }
- });
- // Das Programm fragt in dieser Funktion nach einer der zwei Pfeiltasten..
- Game.requestKey = function() {
- var choices = ['←','→'];
- var rand = choices[Math.floor(Math.random() * choices.length)];
- $('#requestedKey').html(rand);
- if (active == 0){
- $('#requestedKey').html("Start");
- }
- // ...und speichert den Keycode des eingegebenen Keys..
- document.onkeydown = function(){
- var keyPress = event.keyCode;
- if (finished === 0){
- if((keyPress == "39") || (keyPress == "37")){
- if (rand == "←"){
- $('#requestedKey').css("margin-left","-200px");
- }
- else{
- $('#requestedKey').css("margin-left","200px");
- }
- }
- }
- Game.checkKey(keyPress,rand); // ...um ihn in einer weiteren Funktion zu prüfen
- };
- };
- // Diese Funktion prüft ob die eingegebene Taste mit der zufällig ausgewählten erfragten Taste übereinstimmt
- Game.checkKey = function(key,rand) {
- // Wenn das Spiel noch nicht beendet ist..
- if (finished === 0){
- // .. und die richtige Taste eingegeben wurde..
- if (rand == "←" && key == "37") {
- // .. wird der Score erhöht und eine neue Taste erfragt
- keyanimate();
- score ++;
- if (score % 10 === 0){
- seconds +=3;
- bonus();
- }
- Game.requestKey();
- }
- else if (rand == "→" && key == "39"){
- keyanimate();
- score ++;
- console.log(score);
- if (score % 10 === 0){
- seconds +=3;
- bonus();
- }
- Game.requestKey();
- }
- else if ((rand == "←" && key == "39") || (rand == "→" && key == "37") ){
- // Wenn die falsche Taste eingegeben wurde, beendet sich das Spiel
- endGame();
- }
- $('#score').html(score);
- }
- else{
- // Wenn das Spiel beendet ist, wird mit der Pfeiltaste oben neu gestartet
- if (key=="38"){
- score = 0;
- $('#score').html(score);
- finished = 0;
- seconds = 10;
- Game.requestKey();
- interval = setInterval(function(){
- seconds -= 0.01;
- $('#timer').html(seconds.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]);
- if (seconds <= 0){
- endGame();
- }
- },10);
- }
- }
- };
- // Animationen für die Pfeile
- function keyanimate(){
- $('#requestedKey').effect( "shake",{times:2, distance:10, direction:"up"},10);
- $('#score').effect( "shake",{times:2, distance:10, direction:"up"},10);
- }
- function bonus(){
- $('#bonus').css("display","block");
- $('#bonus').fadeOut();
- }
- // Anzeige für das Spielende
- endGame = function(){
- finished = 1;
- clearInterval(interval);
- $('#requestedKey').html("Score " + score);
- $('#requestedKey').css("margin","0");
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement