Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
92
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.getElementsByClassName('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 = 200;
  14. var pause = 0;
  15. var length = 0;
  16.  
  17. function setup(){
  18.    score = 0;
  19.    currscore.innerHTML = 'Current score: </br>' + score;
  20.    speed=200;
  21.    result.classList.toggle('visible');
  22.    playagain.classList.toggle('visible');
  23.    snakejs[0] = 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.update = function(){
  34.       switch(this.dir){
  35.          case 0:
  36.             snakehtml.classList.add('rotate0');
  37.             snakehtml.classList.remove('rotate1');
  38.             snakehtml.classList.remove('rotate3');
  39.             break;
  40.          case 1:
  41.             snakehtml.classList.add('rotate1');
  42.             snakehtml.classList.remove('rotate0');
  43.             snakehtml.classList.remove('rotate2');
  44.             break;
  45.          case 2:
  46.             snakehtml.classList.add('rotate2');
  47.             snakehtml.classList.remove('rotate1');
  48.             snakehtml.classList.remove('rotate3');
  49.             break;
  50.          case 3:
  51.             snakehtml.classList.add('rotate3');
  52.             snakehtml.classList.remove('rotate0');
  53.             snakehtml.classList.remove('rotate2');
  54.             break;
  55.       }
  56.       snakehtml.style.marginTop = (iloscpol-1)*scale - snakejs.y*scale + 'px';
  57.       snakehtml.style.marginLeft = snakejs.x*scale + 'px';
  58.    }
  59.    this.move = function(){
  60.       switch(this.dir){
  61.          case 0:
  62.             if(this.y==iloscpol-1){this.kill();break;}
  63.             snakehtml.classList.add('rotate0');
  64.             snakehtml.classList.remove('rotate1');
  65.             snakehtml.classList.remove('rotate3');
  66.             this.y++;
  67.             break;
  68.          case 1:
  69.             if(this.x==iloscpol-1){this.kill();break;}
  70.             snakehtml.classList.add('rotate1');
  71.             snakehtml.classList.remove('rotate0');
  72.             snakehtml.classList.remove('rotate2');
  73.             this.x++;
  74.             break;
  75.          case 2:
  76.             if(this.y==0){this.kill();break;}
  77.             snakehtml.classList.add('rotate2');
  78.             snakehtml.classList.remove('rotate1');
  79.             snakehtml.classList.remove('rotate3');
  80.             this.y--;
  81.             break;
  82.          case 3:
  83.             if(this.x==0){this.kill();break;}
  84.             snakehtml.classList.add('rotate3');
  85.             snakehtml.classList.remove('rotate0');
  86.             snakehtml.classList.remove('rotate2');
  87.             this.x--;
  88.             break;
  89.       }
  90.       snakehtml.style.marginTop = (iloscpol-1)*scale - snakejs.y*scale + 'px';
  91.       snakehtml.style.marginLeft = snakejs.x*scale + 'px';
  92.    }
  93.    this.kill = function(){
  94.       this.alive = false;
  95.       aftergame();
  96.    }
  97.    this.turn = function(x){
  98.       if(x==37){
  99.          if(this.dir>0){this.dir--;}
  100.          else {this.dir=3;}
  101.       } else if (x==39){
  102.          if(this.dir<3){this.dir++;}
  103.          else {this.dir=0;}
  104.       }
  105.    }
  106. }
  107.  
  108. function Fruit(x, y){
  109.    this.x=x;
  110.    this.y=y;
  111. }
  112.  
  113. function createfruit(){
  114.    r = Math.floor(Math.random()*iloscpol);
  115.    r2 = Math.floor(Math.random()*iloscpol);
  116.    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)){
  117.       r = Math.floor(Math.random()*iloscpol);
  118.       r2 = Math.floor(Math.random()*iloscpol);
  119.    }
  120.    fruitjs= new Fruit(r, r2);
  121.    fruithtml.style.marginTop = (iloscpol-1)*scale - fruitjs.y*scale + 'px';
  122.    fruithtml.style.marginLeft = fruitjs.x*scale + 'px';
  123. }
  124.  
  125. function checkfruit(){
  126.    if(snakejs.x==fruitjs.x&&snakejs.y==fruitjs.y){
  127.       switch(snakejs[length-1].dir){
  128.          case 0:
  129.             snakejs[length]= new Snakejs(snakejs[0].x, snakejs[0].y-1);
  130.             break;
  131.          case 1:
  132.             snakejs[length]= new Snakejs(snakejs[0].x-1, snakejs[0].y);
  133.             break;
  134.          case 2:
  135.             snakejs[length]= new Snakejs(snakejs[0].x, snakejs[0].y+1);
  136.             break;
  137.          case 3:
  138.             snakejs[length]= new Snakejs(snakejs[0].x+1, snakejs[0].y);
  139.             break;
  140.       }
  141.       score++;
  142.       currscore.innerHTML = 'Current score: </br>' + score;
  143.       createfruit();
  144.       speed = speed - 6;
  145.       if(speed<90){
  146.          speed=90;
  147.       }
  148.    }
  149. }
  150.  
  151. function aftergame(){
  152.    result.innerHTML = 'You lost, your score is: </br>' + score;
  153.    result.classList.toggle('visible');
  154.    playagain.classList.toggle('visible');
  155. }
  156.  
  157. function pausefunc(){
  158.    //PAUZA XD
  159. }
  160.  
  161. function continuegame(){
  162.    //WZNOWIENIE XD
  163.    pause = 0;
  164.    run();
  165. }
  166.  
  167. window.onkeydown = function(event){
  168.    if (event.keyCode==37){
  169.       snakejs.turn(37);
  170.    } else if (event.keyCode==39){
  171.       snakejs.turn(39);
  172.    } else if (event.keyCode==32 && snakejs.alive==true){
  173.          pause++;
  174.          if (pause%2==0){
  175.             continuegame();
  176.          }
  177.       } else {
  178.            return 0;
  179.         }
  180.    }
  181.  
  182. playagain.addEventListener("click", setup);
  183.  
  184. function run(){
  185.    if(snakejs.alive==false){
  186.       return 0;
  187.    } else if (pause%2==1){
  188.       pausefunc();
  189.       return 0;
  190.    } else {
  191.       for(x=length;x>=0;x--){
  192.          if(x>0){
  193.             snakejs[x].x = snakejs[x-1].x;
  194.             snakejs[x].y = snakejs[x-1].y;
  195.             snakejs[x].dir = snakejs[x-1].dir;
  196.          } else {
  197.             snakejs[0].move();
  198.          }
  199.       }
  200.       checkfruit();
  201.       setTimeout(run, speed);
  202.    }
  203. }
  204.  
  205.  
  206. setup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement