javascript写的刽子手游戏

放假快二十天了,晒晒这段时间的代码!先学了一段时间的javascript,写了几个小程序!
这是用javascript写的一个刽子手的小游戏!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>JavaScript刽子手游戏 </title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<script language="javascript">
<!--
gallows=new Array("--------\n|   |\n|\n|\n|\n|\n=====",
                  "--------\n|   O\n|\n|\n|\n|\n=====",
                  "--------\n|   O\n|   |\n|\n|\n|\n=====",
                  "--------\n|   O\n|   |\n|\n|\n|\n=====",
                  "--------\n|   O\n|   |\n|\n|\n|\n=====",
                  "--------\n|   O\n|  \\|/\n|   |\n|\n|\n=====",
                  "--------\n|   O\n|  \\|/\n|   |\n|  /\n|\n=====",
                  "--------\n|   O\n|  \\|/\n|   |\n|  / \\\n|\n=====")
	guessChoices=new Array("JavaScript","Navigator","LiveConnect","LiveWire");
function startAgain(){
	guesses=0;
	max=gallows.length-1;
	guessed="";
	len=guessChoices.length-1;
	toGuess=guessChoices[Math.round(len*Math.random())].toUpperCase();
	displayHangman();
	displayToGuess();
	displayGuessed();
	}
function stayAway(){
	alert("Don't mess with this field element!");
	}
function displayHangman(){
	document.game.status.value=gallows[guesses];
	}
function displayToGuess(){
	pattern="";
	for(i=0;i<toGuess.length;i++){
		if(guessed.indexOf(toGuess.charAt(i))!=-1)
		pattern+=(toGuess.charAt(i)+"")
		  else pattern+="_";
		}
		document.game.toGuess.value=pattern;
	}
function displayGuessed(){
	document.game.guessed.value=guessed;
	}
function badGuess(s){
	if(toGuess.indexOf(s)==-1)
	    return true;
	else  return false;
	}
function winner(){
	for(i=0;i<toGuess.length;i++){
		if(guessed.indexOf(toGuess.charAt(i))==-1)
		   return false;
		}
	       return true;
	}
function guess(s){
	if(guessed.indexOf(s)==-1)
	   guessed=s+guessed;
	if(badGuess(s))++guesses;
	displayHangman();
	displayToGuess();
	displayGuessed();
	if(guesses>=max){
		alert("You're dead.The word you missed was "+toGuess+".");
		startAgain();		
		}
	if(winner()){
		alert("You won!")
		startAgain();
		}
	}
//-->
</script>
</head>

<body>
<h1>Hangman</h1>
<form name="game">
<pre>
<textarea name="status" rows="7" cols="16" onfocus="stayAway()"></textarea>
</pre>
<p>
<input type="text" name="toGuess" onfocus="stayAway()" />Word to guess<br />
</p>
<p>
<input type="text" name="guessed" onfocus="stayAway()" />Letters guessed so far<br />
</p>
<p>Enter your next guess.</p>
<script language="javascript">
<!--
for(i=0;i<26;i++){
	var Initial=String.fromCharCode(i+65);
    window.document.write("<input type="+'button'+" value='"+Initial+"' onclick="+"guess('"+Initial+"')"+" />");
}
//-->
</script>
<input type="button" name="restart" value="----Start Again----" onclick="startAgain()" />
<script language="javascript">
<!--
startAgain();
	
//-->
</script>
</form>
</body>
</html>
 以下是运行结果:
javascript写的刽子手游戏_第1张图片

你可能感兴趣的:(javascript写的刽子手游戏)