Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var scale = 40;
- var iloscpol = 21;
- var snakehtml = document.getElementsByClassName('snake');
- var snakejs = [];
- var fruithtml = document.getElementById('fruit');
- var fruitjs;
- var currscore = document.getElementById('currscore');
- var playagain = document.getElementById('playagain');
- var result = document.getElementById('result');
- var pausehtml = document.getElementById('pause');
- var r, r2;
- var score = 0;
- var speed = 200;
- var pause = 0;
- var length = 0;
- function setup(){
- score = 0;
- currscore.innerHTML = 'Current score: </br>' + score;
- speed=200;
- result.classList.toggle('visible');
- playagain.classList.toggle('visible');
- snakejs[0] = new Snakejs(Math.floor(iloscpol/2), Math.floor(iloscpol/2));
- createfruit();
- run();
- }
- function Snakejs(x, y){
- this.x = x;
- this.y = y;
- this.dir = Math.floor(Math.random()*4);
- this.alive = true;
- this.update = function(){
- switch(this.dir){
- case 0:
- snakehtml.classList.add('rotate0');
- snakehtml.classList.remove('rotate1');
- snakehtml.classList.remove('rotate3');
- break;
- case 1:
- snakehtml.classList.add('rotate1');
- snakehtml.classList.remove('rotate0');
- snakehtml.classList.remove('rotate2');
- break;
- case 2:
- snakehtml.classList.add('rotate2');
- snakehtml.classList.remove('rotate1');
- snakehtml.classList.remove('rotate3');
- break;
- case 3:
- snakehtml.classList.add('rotate3');
- snakehtml.classList.remove('rotate0');
- snakehtml.classList.remove('rotate2');
- break;
- }
- snakehtml.style.marginTop = (iloscpol-1)*scale - snakejs.y*scale + 'px';
- snakehtml.style.marginLeft = snakejs.x*scale + 'px';
- }
- this.move = function(){
- switch(this.dir){
- case 0:
- if(this.y==iloscpol-1){this.kill();break;}
- snakehtml.classList.add('rotate0');
- snakehtml.classList.remove('rotate1');
- snakehtml.classList.remove('rotate3');
- this.y++;
- break;
- case 1:
- if(this.x==iloscpol-1){this.kill();break;}
- snakehtml.classList.add('rotate1');
- snakehtml.classList.remove('rotate0');
- snakehtml.classList.remove('rotate2');
- this.x++;
- break;
- case 2:
- if(this.y==0){this.kill();break;}
- snakehtml.classList.add('rotate2');
- snakehtml.classList.remove('rotate1');
- snakehtml.classList.remove('rotate3');
- this.y--;
- break;
- case 3:
- if(this.x==0){this.kill();break;}
- snakehtml.classList.add('rotate3');
- snakehtml.classList.remove('rotate0');
- snakehtml.classList.remove('rotate2');
- this.x--;
- break;
- }
- snakehtml.style.marginTop = (iloscpol-1)*scale - snakejs.y*scale + 'px';
- snakehtml.style.marginLeft = snakejs.x*scale + 'px';
- }
- this.kill = function(){
- this.alive = false;
- aftergame();
- }
- this.turn = function(x){
- if(x==37){
- if(this.dir>0){this.dir--;}
- else {this.dir=3;}
- } else if (x==39){
- if(this.dir<3){this.dir++;}
- else {this.dir=0;}
- }
- }
- }
- function Fruit(x, y){
- this.x=x;
- this.y=y;
- }
- function createfruit(){
- r = Math.floor(Math.random()*iloscpol);
- r2 = Math.floor(Math.random()*iloscpol);
- 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)){
- r = Math.floor(Math.random()*iloscpol);
- r2 = Math.floor(Math.random()*iloscpol);
- }
- fruitjs= new Fruit(r, r2);
- fruithtml.style.marginTop = (iloscpol-1)*scale - fruitjs.y*scale + 'px';
- fruithtml.style.marginLeft = fruitjs.x*scale + 'px';
- }
- function checkfruit(){
- if(snakejs.x==fruitjs.x&&snakejs.y==fruitjs.y){
- switch(snakejs[length-1].dir){
- case 0:
- snakejs[length]= new Snakejs(snakejs[0].x, snakejs[0].y-1);
- break;
- case 1:
- snakejs[length]= new Snakejs(snakejs[0].x-1, snakejs[0].y);
- break;
- case 2:
- snakejs[length]= new Snakejs(snakejs[0].x, snakejs[0].y+1);
- break;
- case 3:
- snakejs[length]= new Snakejs(snakejs[0].x+1, snakejs[0].y);
- break;
- }
- score++;
- currscore.innerHTML = 'Current score: </br>' + score;
- createfruit();
- speed = speed - 6;
- if(speed<90){
- speed=90;
- }
- }
- }
- function aftergame(){
- result.innerHTML = 'You lost, your score is: </br>' + score;
- result.classList.toggle('visible');
- playagain.classList.toggle('visible');
- }
- function pausefunc(){
- //PAUZA XD
- }
- function continuegame(){
- //WZNOWIENIE XD
- pause = 0;
- run();
- }
- window.onkeydown = function(event){
- if (event.keyCode==37){
- snakejs.turn(37);
- } else if (event.keyCode==39){
- snakejs.turn(39);
- } else if (event.keyCode==32 && snakejs.alive==true){
- pause++;
- if (pause%2==0){
- continuegame();
- }
- } else {
- return 0;
- }
- }
- playagain.addEventListener("click", setup);
- function run(){
- if(snakejs.alive==false){
- return 0;
- } else if (pause%2==1){
- pausefunc();
- return 0;
- } else {
- for(x=length;x>=0;x--){
- if(x>0){
- snakejs[x].x = snakejs[x-1].x;
- snakejs[x].y = snakejs[x-1].y;
- snakejs[x].dir = snakejs[x-1].dir;
- } else {
- snakejs[0].move();
- }
- }
- checkfruit();
- setTimeout(run, speed);
- }
- }
- setup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement