//WINDOW-SNAKE//2005 Sylvain Vriens//(C) PROJECT-EUH.COMfunction hasPopupBocker(){ //return TRUE if pop-up blocker detected	test = window.open("", "test","width=1,height=1,scrollbars=no,left=5000,top=5000");	if(test==null || typeof(test)=="undefined" || test.closed==true){		return true;	}	try{    	test.close();    }catch(vEx){    	return true;    }	return false;}function startGame(){ //init the game	//test for pop-up blockers	if(hasPopupBocker()){		return;	}		//clear all thing from previous game	clearTimers();	closeAllWindows();	//init game variables	partsCount = 0;	direction = 1;	maxLength = 15;	updateTime = 300;	minTime = 50;	score = 0;	candyHit = false;	windowError = false;	snake = new Array();	positions = new Array();			//window sizes	partW = 100;	partH = 100;		itemW = 100;	itemH = 100;	//change my background	changeBGImage("img/snakeback_blurred.jpg");			//set up grid	screenW = window.screen.availWidth;	screenH = window.screen.availHeight;		gridW = Math.floor(screenW/partW);	gridH = Math.floor(screenH/partH);		gridOffX = (screenW-gridW*partW)/2;	gridOffY = (screenH-gridH*partH)/2;			//set score and windows	setScore(score);		placeCandy();		addPart();	addPart();	addPart();	moveToPosition(0);	moveToPosition(1);	moveToPosition(2);	placeCandy();		//first call to update positions	//starts the game	updatePositions();		//playSound!!	//playMusic();	//fadeUp(100);	}function gameOver(){ //in case of losers!	//change my background and images of windows	changeBGImage("img/snakeback.jpg");	for(x=0;x<partsCount;x++){		try{			snake[x].changeImage(0, "img/dead.gif");			snake[x].changeBGImage("img/snakeback_blurred.jpg");		}catch(vErr){}	}	try{		candy.close();	}catch(vErr){};	//start the timer to close all windows	closeTimer = window.setTimeout("closeAllWindows()",5000);	//stopMusic();	//fadeDown(25);	gameOverSound();}function addPart(){ //adds a part to the snake	//check if allowed to add	if(partsCount<=maxLength){		if(typeof(snake[partsCount])=="undefined"){			snake[partsCount] = window.open("snakepart.html", "snake" + partsCount,"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+partW+",height="+partH);		}				//add position to list		positions[partsCount] = new Array(0,1);		partsCount = partsCount + 1;	}	}function removePart(){	partsCount = partsCount - 1;	try{		snake[partsCount].close();	}catch(vErr){}}function placeCandy(){	//check if candy already opened and if not, open it!	try{		if(typeof(candy)!="object" || candy.closed){			candy = window.open("candy.html", "candy","status=0,toolbar=0,width="+itemW+",height="+itemH);		}	}catch(vErr){		candy = window.open("candy.html", "candy","status=0,toolbar=0,width="+itemW+",height="+itemH);	}	//create new position		candyPos = new Array(2);		//return handle	search_loop:	//find a free space to place candy	while(true){		vX = Math.round(Math.random()*gridW+0.5)-1;		vY = Math.round(Math.random()*gridH+0.5)-1;		for(x=0;x<partsCount;x++){			if(vX == positions[x][0] && vY == positions[x][1]){				//overlap, continue at return handle				continue search_loop;			}		}		//position found, exit loop		break;	}		candyPos[0] = vX;	candyPos[1] = vY;	vPos = gridLocToPos(candyPos);		//move candy to position	try{		candy.moveTo(vPos[0],vPos[1]);		candy.focus();	}catch(vErr){		handleWindowError();		return;	}	}function closeAllWindows(){//closes aal windeos	try{		for(x=0;x<partsCount;x++){			try{				snake[x].close();				snake[x] = null;			}catch(vErr){}		}		try{			candy.close();			candy = null;		}catch(vErr){}	}catch(vErr){}}function buttonPressed(vButton){//catches key presses from snakeparts	switch(vButton){		case 37: //left			direction = 3;			break;		case 38: //up			direction = 0;			break;		case 39: //right			direction = 1;			break;		case 40: //down			direction = 2;			break;	}}function hitCandy(){ //the snake got the prize!	//place candy and add a part if allowed	placeCandy();	addPart();			//change score	score = score+1;	setScore(score);		//update speed every 2 points	if(score%2==0){		updateTime = updateTime - 25;		updateTime = Math.max(updateTime, minTime);	}		//reset candy variable	candyHit = false;	}function updatePositions(){	//if hit candy last step, do it now.	if(candyHit){		hitCandy();	}	//new headposition	vPos = new Array(0,0);	vPos[0] = positions[0][0] + (direction%2) * (direction-2) * -1;	vPos[1] = positions[0][1] + (direction%2-1) * (direction-1) * -1;	positions.pop();	positions.unshift(vPos);	if(windowError){		//if window error detected don't do nothing ;)	}else if(checkCollision()==false){		//if no collision		//move last snakepart in list to 1st position		//only have to move 1 window this way		vWin = snake.pop();		snake.unshift(vWin);		moveToPosition(0);				if(!windowError){			//do it again in a while			timer = window.setTimeout("updatePositions()",updateTime);		}				moveSound();			}else{		//LOSER!		gameOver();	}}function moveToPosition(vID){ //move snakepart vID to it's position		try{		vPos = gridLocToPos(positions[vID]);		snake[vID].moveTo(vPos[0], vPos[1]);		snake[vID].focus();	}catch(vEx){		handleWindowError();		return;	}	}function checkCollision(){ //checks for collision		//check if outide screen...	if(positions[0][0]<0 || positions[0][0]>=gridW){		return true;	}	if(positions[0][1]<0 || positions[0][1]>=gridH){		return true;	}		//is the snake biting itself?	for(x=1;x<partsCount;x++){		if(positions[0][0]==positions[x][0] && positions[0][1]==positions[x][1]){				return true;		}	}		//is the snake eating candy	if(positions[0][0]==candyPos[0] && positions[0][1]==candyPos[1]){		//just set a flag, loading a new window here would render making a turn impossible !		candySound();		candyHit = true;	}		return false;}function gridLocToPos(vPos){ //grid position to x,y coordinate of screen	vReturn = new Array(2);	vReturn[0] = gridOffX + vPos[0]*partW;	vReturn[1] = gridOffY + vPos[1]*partH;	return vReturn;}function clearTimers(){ //clear all running timers	try{		window.clearTimeout(closeTimer);	}catch(vErr){}	try{		window.clearTimeout(timer);	}catch(vErr){}}function handleWindowError(){ //alert on windowError	if(!windowError){		windowError = true;		clearTimers();		changeBGImage("img/snakeback.jpg");		closeAllWindows();		window.focus();		alert("An error occurred.\n\nA pop-up blocker may still be active or a window may have been closed.");	}}//safari only with debug menu turned on!//terminal commando: defaults write com.apple.Safari IncludeDebugMenu 1function printToConsole(vStr){	if(window.console) {		window.console.log(vStr);	} }
