Jojoxx.net logotyp

 

 

Du är här: Start > JavaScript ikon JavaScript > Arkiv ikon Scriptarkiv > Spel > Worm
Användare ikonAnvändare
Besökare just nu: 88
Snabblänkar ikonSnabblänkar
JavaScript-arkivet
Perl
FAQ

Sök på Jojoxx.net


Worm

Exempel

Worm

Styr med W,A,S,D-tangenterna (eller Q, E).

0 Poäng

Kod


<h4>Worm</h4>
<p>Styr med W,A,S,D-tangenterna (eller Q, E).</p>
<form><input type="button" value="Starta" onclick="restart();"></form>
<style type="text/css">
.cell{ border:solid rgb(220,220,220) 1px; overflow:hidden; }
</style>
<div id="scoreboard">0 Poäng</div>
<div id="playfield" style="position:relative;"></div>
<script type="text/javascript">
/* Detta script finns att hämta på http://www.jojoxx.net och
   får användas fritt så länge som dessa rader står kvar. */

var blocks=[
	[
		[0,0,0,0],
		[0,0,1,0],
		[1,1,1,0],
		[0,0,0,0]
	]
]; // Used for tetris-like games.

var blocksize=10;
var pfwidth=40;
var pfheight=40;
var playfieldhtml="";
var renderMode=(document.documentMode)?(document.documentMode==8)?"IE8 Standards":(document.documentMode==7)?"IE7 Strict Mode":"IE5 Quirks Mode":(document.compatMode == 'CSS1Compat')?'Standards':'Quirks'
var pixelsubst=(renderMode!="Quirks" || navigator.userAgent.indexOf("Firefox")>-1)?2:0;

for(var y=0;y<pfheight;y++){
	for(var x=0;x<pfwidth;x++){
		playfieldhtml+='<div id="cell'+x+'x'+y+'" class="cell" style="position:absolute;width:'+(blocksize+1-pixelsubst)+'px;height:'+(blocksize+1-pixelsubst)+'px;left:'+(blocksize*x)+'px;top:'+(blocksize*y)+'px;"></div>';
	}
}
document.getElementById("playfield").style.width=(pfwidth*blocksize)+"px";
document.getElementById("playfield").style.height=(pfheight*blocksize)+"px";
document.getElementById("playfield").innerHTML=playfieldhtml;
function drawBlock(x,y,block){
	for(var yb=0;yb<block.length;yb++){
		for(var xb=0;xb<block[0].length;xb++){
			color=(block[yb][xb])?"green":"white";
			document.getElementById("cell"+(x+xb)+"x"+(y+yb)).style.backgroundColor=color;
		}
	}
}
function plot(x,y,color){
	document.getElementById("cell"+(x)+"x"+(y)).style.backgroundColor=color;
}
function clearPixel(x,y){
	document.getElementById("cell"+(x)+"x"+(y)).style.backgroundColor="";
}
function getPixel(x,y){
	return document.getElementById("cell"+(x)+"x"+(y)).style.backgroundColor;
}
function rotateBlock(block,d){
	var r=new Array(); for(var i=0;i<block[0].length;i++){ r[i]=new Array(); }
	var ys=0;
	for(var yb=0;yb<block.length;yb++){
		var xs=0;
		for(var xb=0;xb<block[0].length;xb++){
			xs++;
			r[ys][xs]=block[yb][xb];
		}
		ys++;
	}
	return r;
}
if (!document.all){ document.captureEvents(Event.KEYDOWN|Event.KEYUP); } 
var keyboard=new Object();
window.onblur = function(){ for(var a in keyboard){ keyboard[a]=false; } }
document.onkeyup = function(e){
if(!document.all){ event=e; }
if(event.keyCode>0){ keyboard[event.keyCode]=false; }
}
document.onkeydown = function(e){
if(!allowNewKey){ return true; }
if(!document.all){ event=e; }
if(event.keyCode>0){ keyboard[event.keyCode]=true; }
if(event.keyCode==87){ direction=0; } // W ^
if(event.keyCode==83){ direction=180; }  // S v
if(event.keyCode==65){ direction=270; } // A <
if(event.keyCode==68){ direction=90; }  // D >
if(event.keyCode==81){ direction-=90; }  // Q Left turn
if(event.keyCode==69){ direction+=90; }  // E Right turn
ax=Math.round(Math.sin(direction*Math.PI/180));
ay=-Math.round(Math.cos(direction*Math.PI/180));
allowNewKey=false;
}

var x=0; var y=0; var ay=0; var ax=0; var direction=180; var gameover=false; var timer; var delay=200; var score=0; var allowNewKey;
function restart(){
	clearTimeout(timer);
	x=parseInt(pfwidth/2)-1;y=parseInt(pfheight/2)-1;ay=1;ax=0;direction=180;gameover=false;score=0;
	document.getElementById("playfield").innerHTML=playfieldhtml;
	timer=setTimeout("main()",delay);
}
function main(){
	x+=ax; y+=ay;
	allowNewKey=true;
	if(x>pfwidth-1 || y>pfheight-1 || x<0 || y<0){ gameover=true; }
	if(!gameover){
		if(getPixel(x,y)){ gameover=true; }
	}
	if(!gameover){
		plot(x,y,"red");
		score+=10;
		document.getElementById("scoreboard").innerHTML=score+" Poäng";
		timer=setTimeout("main()",delay);
	}
	if(gameover){ alert("GAME OVER"); }
}
</script>


,