Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var scale = 40;
  2. var iloscpol = 21;
  3. var snakehtml = document.getElementById('snake');
  4. var snakejs;
  5. var fruithtml = document.getElementById('fruit');
  6. var fruitjs;
  7. var currscore = document.getElementById('currscore');
  8. var playagain = document.getElementById('playagain');
  9. var result = document.getElementById('result');
  10. var pausehtml = document.getElementById('pause');
  11. var r, r2;
  12. var score = 0;
  13. var speed = 300;
  14. var pause = 0;
  15.    pausehtml.classList.toggle('visible');
  16.  
  17. function setup(){
  18.    score = 0;
  19.    currscore.innerHTML = 'Current score: </br>' + score;
  20.    speed=300;
  21.    result.classList.toggle('visible');
  22.    playagain.classList.toggle('visible');
  23.    snakejs = new Snakejs(Math.floor(iloscpol/2), Math.floor(iloscpol/2))
  24.    createfruit();
  25.    run();
  26. }
  27.  
  28. function Snakejs(x, y){
  29.    this.x = x;
  30.    this.y = y;
  31.    this.dir = Math.floor(Math.random()*4);
  32.    this.alive = true;
  33.    this.move = function(){
  34.       switch(this.dir){
  35.          case 0:
  36.             if(this.y==0){this.kill();break;}
  37.             this.y--;
  38.             break;
  39.          case 1:
  40.             if(this.x==iloscpol-1){this.kill();break;}
  41.             this.x++;
  42.             break;
  43.          case 2:
  44.             if(this.y==iloscpol-1){this.kill();break;}
  45.             this.y++;
  46.             break;
  47.          case 3:
  48.             if(this.x==0){this.kill();break;}
  49.             this.x--;
  50.             break;
  51.       }
  52.    }
  53.    this.kill = function(){
  54.       this.alive = false;
  55.       aftergame();
  56.    }
  57.    this.turn = function(x){
  58.       if(x==37){
  59.          if(this.dir>0){this.dir--;}
  60.          else {this.dir=3;}
  61.       } else if (x==39){
  62.          if(this.dir<3){this.dir++;}
  63.          else {this.dir=0;}
  64.       }
  65.    }
  66. }
  67.  
  68. function Fruit(x, y){
  69.    this.x=x;
  70.    this.y=y;
  71. }
  72.  
  73. function createfruit(){
  74.    r = Math.floor(Math.random()*iloscpol);
  75.    r2 = Math.floor(Math.random()*iloscpol);
  76.    while(r==snake.x&&r2==snake.y||r==snake.x-1&&r2==snake.y-1||r==snake.x+1&&r2==snake.y+1||r==snake.x-1&&r2==snake.y+1||r==snake.x+1&&r2==snake.y-1){
  77.       r = Math.floor(Math.random()*iloscpol);
  78.       r2 = Math.floor(Math.random()*iloscpol);
  79.    }
  80.    fruitjs= new Fruit(r, r2);
  81.    fruithtml.style.marginTop = fruitjs.y*scale + 'px';
  82.    fruithtml.style.marginLeft = fruitjs.x*scale + 'px';
  83. }
  84.  
  85. function aftergame(){
  86.    result.innerHTML = 'You lost, your score is: </br>' + score;
  87.    result.classList.toggle('visible');
  88.    playagain.classList.toggle('visible');
  89.    
  90. }
  91.  
  92. function pausefunc(){
  93.    pausehtml.classList.toggle('visible');
  94.    currscore.classList.toggle('visible');
  95. }
  96. function continuegame(){
  97.    pausehtml.classList.toggle('visible');
  98.    currscore.classList.toggle('visible');
  99.    pause = 0;
  100.    run();
  101. }
  102.  
  103. window.onkeydown = function(event){
  104.    if (event.keyCode == 37){
  105.       snakejs.turn(37);
  106.    } else if (event.keyCode==39){
  107.       snakejs.turn(39);
  108.    } else if (event.keyCode==32 && snakejs.alive==true){
  109.          pause=pause+1;
  110.          if (pause%2==0){
  111.             continuegame();
  112.          }
  113.       } else if(event.keyCode==32 && snakejs.alive==false){
  114.            return 0;
  115.         }
  116.    }
  117.  
  118. playagain.addEventListener("click", setup);
  119.  
  120. function run(){
  121.    if(snakejs.alive==false){
  122.       return 0;
  123.    } else if (pause%2==1){
  124.       pausefunc();
  125.       return 0;
  126.    }else if (pause%2==0){
  127.       snakejs.move();
  128.       snakehtml.style.marginTop = snakejs.y*scale + 'px';
  129.       snakehtml.style.marginLeft = snakejs.x*scale + 'px';
  130.       if(snakejs.x==fruitjs.x&&snakejs.y==fruitjs.y){
  131.          score++;
  132.          currscore.innerHTML = 'Current score: </br>' + score;
  133.          createfruit();
  134.          speed = speed - 25;
  135.          if(speed<100){
  136.             speed=100;
  137.          }
  138.       }
  139.       setTimeout(run, speed);
  140.    }
  141. }
  142.  
  143.  
  144. setup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement