Онлайн игра «Змейка»
Без рубрики Ноябрь 6th, 2007

Мне, с какой-то радости, игра помогла занять около часа свободного времени :) Возможно, понравится еще кому-либо. Играйте!
/**************************************************** * Cross Browser Snake game- By Brady Mulhollem- http://www.webtech101.com *This game is available at http://www.dynamicdrive.com ****************************************************/ function cbsnake(){
//Pixels to move at once this.jump = 8; //Size of snake. Make this one less than jump. Doesn't have to be,but adds good effect this.sos = 7; //Size of board //DANGER!!! this.sofb must be EVENLY dividable by this.jump DANGER!!!! this.sofb = 400; //Set things up this.daway = this.sofb - this.jump; this.correct = new Array(); this.correct[0] = 0; while(this.correct[this.correct.length -1] != this.daway){ this.correct[this.correct.length] = this.correct[this.correct.length -1]+this.jump } this.zero = 0; var gameboard = '
'; document.write(gameboard); }
cbsnake.prototype.setup = function(setspeed){ var thisObj = this; //Score... this.score = 0; //Snake Direction this.sdir = 'none'; this.sdirb = 'none'; this.sdirp = 'none'; //Snake arrays this.ctop = new Array(); this.cleft = new Array(); //Top of snake class this.ctop[0] = 200; this.ctop[1] = -8; //Left of Snake class this.cleft[0] = 200; this.cleft[1] = -8; //current top of apple this.atop = 0; //current left of apple this.aleft = 0; //Milliseconds between move this.speed = setspeed; document.getElementById('board').innerHTML = '
'; this.moveapple(); this.stopgame = false; setTimeout(function(){ thisObj.msnake() },this.speed); document.onkeydown = function(e){ return thisObj.snakedir(e); }; } cbsnake.prototype.slow = function(){ this.setup(100); this.buttons('true'); document.getElementById('slow').blur(); } cbsnake.prototype.medium = function(){ this.setup(70); this.buttons('true'); document.getElementById('medium').blur(); } cbsnake.prototype.fast = function(){ this.setup(30); this.buttons('true'); document.getElementById('fast').blur(); } cbsnake.prototype.rannum = function(num1,num2){ num1 = parseInt(num1); num2 = parseInt(num2); var generator = Math.random()*(Math.abs(num2-num1)); generator = Math.round(num1+generator); return generator; } cbsnake.prototype.moveapple = function(){ var usethis = false; while(!usethis){ this.atop = this.correct[this.rannum(0,this.correct.length-1)]; this.aleft = this.correct[this.rannum(0,this.correct.length-1)]; if(this.numInArray(this.ctop,this.cleft,this.atop,this.aleft) == 0){ usethis = true; } } document.getElementById('apple').style.top = this.atop+"px"; document.getElementById('apple').style.left = this.aleft+"px"; } cbsnake.prototype.snakedir = function(e){ if(!e){ //IE... e = window.event; } switch(e.keyCode){ case 38: if(this.sdir != 'down' && this.sdirp != 'down'){ this.sdirb = 'up'; this.sdirp = 'up'; } break; case 40: if(this.sdir != 'up' && this.sdirp != 'up'){ this.sdirb = 'down'; this.sdirp = 'down'; } break; case 37: if(this.sdir != 'right' && this.sdirp != 'right'){ this.sdirb = 'left'; this.sdirp = 'left'; } break; case 39: if(this.sdir != 'left' && this.sdirp != 'left'){ this.sdirb = 'right'; this.sdirp = 'right'; } break; case 32: if(this.sdir == 'none' && this.sdirp != 'none'){ this.sdirb = this.sdirp; this.sdirp = 'none'; } else{ this.sdirp = this.sdir; this.sdirb = 'none'; } break; } return this.stopgame;
} cbsnake.prototype.msnake = function(){ if(this.stopgame === false){ if(this.sdir != 'none'){ this.moveall(); } var thisObj = this; switch(this.sdir){ case 'up': this.ctop[0] = this.ctop[0] - this.jump; document.getElementById('snake0').style.top = this.ctop[0]+"px"; if((this.ctop[0] == this.zero && this.sdirb == 'up') || this.ctop[0] < this.zero){ this.gover(); } break; case 'down': this.ctop[0] = this.ctop[0] + this.jump; document.getElementById('snake0').style.top = this.ctop[0]+"px"; if((this.ctop[0] == this.daway && this.sdirb == 'down') || this.ctop[0] > this.daway){ this.gover(); } break; case 'left': this.cleft[0] = this.cleft[0] - this.jump; document.getElementById('snake0').style.left = this.cleft[0]+"px"; if((this.cleft[0] == this.zero && this.sdirb == 'left') || this.cleft[0] < this.zero){ this.gover(); } break; case 'right': this.cleft[0] = this.cleft[0] + this.jump; document.getElementById('snake0').style.left = this.cleft[0]+"px"; if((this.cleft[0] == this.daway && this.sdirb == 'right') || this.cleft[0] > this.daway){ this.gover(); } break; } if(this.sdir != 'none'){ this.hitself(); this.happle(); } this.sdir = this.sdirb setTimeout(function(){ thisObj.msnake() },this.speed); } } cbsnake.prototype.gover = function(){ if(!this.stopgame){ this.stopgame = true; var inner = document.getElementById('board').innerHTML; document.getElementById('board').innerHTML = inner+'
';
document.getElementById('apple').style.backgroundColor = '#D7BEBE';
for(i=0;i
About