warganism
12-15-2010, 03:29 PM
Hey everyone!
I'm new to coding and I've been working on a hangman game, but I got one problem I just can't figure out. I want to be able to insert feedback at the end of the game where I give the player the definition of the word they were guessing.
Also I will gladly take any suggestions or commentary on my game.
Heres my code so far:
<html>
<head>
<script>
var canvas, ctx;
var dc=0;//draw counter
var it, dg, ds, swa;//input text, display guess div, display string
sw = ["Netherlands","world","zombies","ashtray","mouse", "asperagus", "wooden", "energy", "arial", "horseshoe", "foundation", "majestic", "titties", "coffee", "postman", "perfect", "yellow", "onomatopoeia"];//secret word
var sw = sw[Math.floor(Math.random()*sw.length)];
function init(){
canvas = document.getElementById("hungman");
ctx = canvas.getContext('2d');
ctx.lineWidth = 3;
it = document.getElementById("input_txt");
dg = document.getElementById("display_guess_div");
displayGuess("-");
}
function drawStuff(){
ctx.beginPath();
dc++;
switch(dc){
case 1:
gallows1();
break;
case 2:
gallows2();
break;
case 3:
gallows3();
break;
case 4:
gallows4();
break;
case 5:
drawHead();
break;
case 6:
drawBody();
break;
case 7:
drawLeftArm();
break;
case 8:
drawRightArm();
break;
case 9:
drawLeftLeg();
break;
case 10:
drawRightLeg();
break;
}
ctx.stroke();
}
function drawHead(){
//draw head
ctx.arc(150,175,50,0,Math.PI*2, true);//outer circle
ctx.moveTo(115,175);
ctx.arc(150,175,35,0, Math.PI, false);//mouth
ctx.moveTo(135,155);
ctx.arc(130,155,5,0,Math.PI*2,true);//left eye
ctx.moveTo(175,155)
ctx.arc(170,155,5,0,Math.PI*2,true);//right eye
}
function drawBody(){
//draw body
ctx.moveTo(150,225);
ctx.lineTo(150, 320);
}
function drawLeftArm(){
//draw left arm
ctx.moveTo(150,250);
ctx.lineTo(50,200);
}
function drawRightArm(){
//draw right arm
ctx.moveTo(150,250);
ctx.lineTo(250,200);
}
function drawLeftLeg(){
//draw left leg
ctx.moveTo(150,300);
ctx.lineTo(100,450);
}
function drawRightLeg(){
//draw right leg
ctx.moveTo(150,300);
ctx.lineTo(200,450);
}
function gallows1(){
//draw gallow1
ctx.moveTo(20,20);
ctx.lineTo(20,500);
}
function gallows2(){
//draw gallow2
ctx.moveTo(20,20);
ctx.lineTo(150,20);
}
function gallows3(){
//draw gallow3
ctx.moveTo(20,80);
ctx.lineTo(80,20);
}
function gallows4(){
//draw gallow4 - rope
ctx.moveTo(150,20);
ctx.lineTo(150,125);
}
function checkLetter(){
var len = it.value.length-1;
var guess = it.value.charAt(len);
if(sw.indexOf(guess)==-1){
drawStuff();
if (dc>=10){
alert("bad luck, you're hung! The word was " + sw + ".");
playAgain();
}
}else{
displayGuess(guess)
if(ds.indexOf("-")==-1){
alert("You win! The word was " + sw + "!");
playAgain();
}
}
}
function displayGuess(guess){
//first time
if(guess == "-"){
ds = "";
for(i=0; i<sw.length; i++){
ds+="-";
}
}else{
//display a correct letter
var newds ="";
for(i=0; i<sw.length; i++){
if(sw.charAt(i)==guess){
newds +=guess;
}else{
newds +=ds.charAt(i);
}
}
ds = newds;
}
dg.innerHTML= ds;
}
function playAgain() //Copyright Joey
{
var r=confirm("Play Again?");
if (r==true)
{
window.location.reload();
}
else
{
alert("Thanks For Playing!");
window.close();
}
}
function changeWord() {
var r=confirm("Change word?");
if (r==true)
{
window.location.reload();
}
}
function clear() {
document.text_form.input_text.value="";
return true;
}
//function numberCheck()
//{
// if ( isNaN ( it ) || it != ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz )
// {
// alert("Please Insert a Letter") ;
// return;
// }
//}
</script>
<style type="text/css">
canvas{border:1px solid black}
</style>
</head>
<body onLoad="init()">
<center>
<h1>Hangman</h1>
<p>Guess The Word or !</p>
<canvas id="hungman" width="300px" height="600px"> </canvas><br/>
<div id="input">
<div id="display_guess_div">display guessed letters here</div><br/>
<input type="text" id="input_txt" maxlength="1" size="19"> </input> <br>
<input type="button" value="Guess" onClick="checkLetter()" onSubmit="clear()"></input>
<input type="button" value="Change Word" onClick="changeWord()"></input>
</div>
</center>
</body>
</html>
I'm new to coding and I've been working on a hangman game, but I got one problem I just can't figure out. I want to be able to insert feedback at the end of the game where I give the player the definition of the word they were guessing.
Also I will gladly take any suggestions or commentary on my game.
Heres my code so far:
<html>
<head>
<script>
var canvas, ctx;
var dc=0;//draw counter
var it, dg, ds, swa;//input text, display guess div, display string
sw = ["Netherlands","world","zombies","ashtray","mouse", "asperagus", "wooden", "energy", "arial", "horseshoe", "foundation", "majestic", "titties", "coffee", "postman", "perfect", "yellow", "onomatopoeia"];//secret word
var sw = sw[Math.floor(Math.random()*sw.length)];
function init(){
canvas = document.getElementById("hungman");
ctx = canvas.getContext('2d');
ctx.lineWidth = 3;
it = document.getElementById("input_txt");
dg = document.getElementById("display_guess_div");
displayGuess("-");
}
function drawStuff(){
ctx.beginPath();
dc++;
switch(dc){
case 1:
gallows1();
break;
case 2:
gallows2();
break;
case 3:
gallows3();
break;
case 4:
gallows4();
break;
case 5:
drawHead();
break;
case 6:
drawBody();
break;
case 7:
drawLeftArm();
break;
case 8:
drawRightArm();
break;
case 9:
drawLeftLeg();
break;
case 10:
drawRightLeg();
break;
}
ctx.stroke();
}
function drawHead(){
//draw head
ctx.arc(150,175,50,0,Math.PI*2, true);//outer circle
ctx.moveTo(115,175);
ctx.arc(150,175,35,0, Math.PI, false);//mouth
ctx.moveTo(135,155);
ctx.arc(130,155,5,0,Math.PI*2,true);//left eye
ctx.moveTo(175,155)
ctx.arc(170,155,5,0,Math.PI*2,true);//right eye
}
function drawBody(){
//draw body
ctx.moveTo(150,225);
ctx.lineTo(150, 320);
}
function drawLeftArm(){
//draw left arm
ctx.moveTo(150,250);
ctx.lineTo(50,200);
}
function drawRightArm(){
//draw right arm
ctx.moveTo(150,250);
ctx.lineTo(250,200);
}
function drawLeftLeg(){
//draw left leg
ctx.moveTo(150,300);
ctx.lineTo(100,450);
}
function drawRightLeg(){
//draw right leg
ctx.moveTo(150,300);
ctx.lineTo(200,450);
}
function gallows1(){
//draw gallow1
ctx.moveTo(20,20);
ctx.lineTo(20,500);
}
function gallows2(){
//draw gallow2
ctx.moveTo(20,20);
ctx.lineTo(150,20);
}
function gallows3(){
//draw gallow3
ctx.moveTo(20,80);
ctx.lineTo(80,20);
}
function gallows4(){
//draw gallow4 - rope
ctx.moveTo(150,20);
ctx.lineTo(150,125);
}
function checkLetter(){
var len = it.value.length-1;
var guess = it.value.charAt(len);
if(sw.indexOf(guess)==-1){
drawStuff();
if (dc>=10){
alert("bad luck, you're hung! The word was " + sw + ".");
playAgain();
}
}else{
displayGuess(guess)
if(ds.indexOf("-")==-1){
alert("You win! The word was " + sw + "!");
playAgain();
}
}
}
function displayGuess(guess){
//first time
if(guess == "-"){
ds = "";
for(i=0; i<sw.length; i++){
ds+="-";
}
}else{
//display a correct letter
var newds ="";
for(i=0; i<sw.length; i++){
if(sw.charAt(i)==guess){
newds +=guess;
}else{
newds +=ds.charAt(i);
}
}
ds = newds;
}
dg.innerHTML= ds;
}
function playAgain() //Copyright Joey
{
var r=confirm("Play Again?");
if (r==true)
{
window.location.reload();
}
else
{
alert("Thanks For Playing!");
window.close();
}
}
function changeWord() {
var r=confirm("Change word?");
if (r==true)
{
window.location.reload();
}
}
function clear() {
document.text_form.input_text.value="";
return true;
}
//function numberCheck()
//{
// if ( isNaN ( it ) || it != ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz )
// {
// alert("Please Insert a Letter") ;
// return;
// }
//}
</script>
<style type="text/css">
canvas{border:1px solid black}
</style>
</head>
<body onLoad="init()">
<center>
<h1>Hangman</h1>
<p>Guess The Word or !</p>
<canvas id="hungman" width="300px" height="600px"> </canvas><br/>
<div id="input">
<div id="display_guess_div">display guessed letters here</div><br/>
<input type="text" id="input_txt" maxlength="1" size="19"> </input> <br>
<input type="button" value="Guess" onClick="checkLetter()" onSubmit="clear()"></input>
<input type="button" value="Change Word" onClick="changeWord()"></input>
</div>
</center>
</body>
</html>