HWE-Forum.de

Internet und Co. => HTML, PHP, XML, JavaScript u. Co. => Thema gestartet von: Mark am 23. April 2005, 21:54:22 Uhr

Titel: HTML Würfelspiel
Beitrag von: Mark am 23. April 2005, 21:54:22 Uhr
Hallo!

Ich möchte eine Website mit HTML und Javascript erstellen mit folgenden Eigenschaften:
Wenn man auf einen Button klickt, sollen zwei Würfel geworfen werden. Die zufälligen Augenzahlen sollen dann addiert und in einem Textfeld ausgegeben werden.

Das habe ich nun hinbekommen. Jetzt kommt das aber hinzu:
Wenn man nun mehrmals hintereinander auf den Button klickt sollen die neu geworfenen Zahlen mit den alten Zahlen addiert werden und in dem Textfeld ausgegeben werden.

Kann mir jemand einen Tipp geben, wie man das machen kann?


Mein HTML-Code sieht bis jetzt so aus:

<html>
<head>
<script language="JavaScript">

test = new Array(6);
for (i=1; i<=6; i++)

test="wurf"+i+".gif";

function rechnen()
{
zahl1=Math.floor(Math.random()*6+1);
zahl2=Math.floor(Math.random()*6+1);

if (zahl2 > zahl1)
{
jo = zahl1;
zahl1 = zahl2;
zahl2 = jo
}

document.images["Bilder1"].src=test[zahl1];
document.images["Bilder2"].src=test[zahl2];

Ergebnis = zahl1 + zahl2;
document.hallo.c.value=Ergebnis
}

//-->
</script>

<title>Wurfeldings</title>

</head>
<body>
<img src="wurfohne.gif" width= "200" name="Bilder1" alt="hier ist ein bild">
<img src="wurfohne.gif" width= "200" name="Bilder2" alt="hier ist noch ein bild">
<form name="hallo" action="">
<input type="button" name="a" value="Wuerfeln" OnClick="rechnen()">
<BR> <BR>

<input type="button" name="b" value="zusammengerechnet">
<input type="button" name="c" value="">
</form>
</body>
</html>



Gruß,
Frank
Titel: Re: HTML Würfelspiel
Beitrag von: Gudi am 29. April 2005, 17:47:25 Uhr
versuch's mal so:


<html>
<head>
<script language="JavaScript">

Ergebnis=0;
test = new Array(6);
for (i=1; i<=6; i++)

test[i]="wurf"+i+".gif";

function rechnen()
{
zahl1=Math.floor(Math.random()*6+1);
zahl2=Math.floor(Math.random()*6+1);

if (zahl2 > zahl1)
{
jo = zahl1;
zahl1 = zahl2;
zahl2 = jo
}

document.images["Bilder1"].src=test[zahl1];
document.images["Bilder2"].src=test[zahl2];

Ergebnis += zahl1 + zahl2;
document.hallo.c.value=Ergebnis
}

//-->
</script>

<title>Wurfeldings</title>

</head>
<body>
<img src="wurfohne.gif" width= "200" name="Bilder1" alt="hier ist ein bild">
<img src="wurfohne.gif" width= "200" name="Bilder2" alt="hier ist noch ein bild">
<form name="hallo" action="">
<input type="button" name="a" value="Wuerfeln" OnClick="rechnen()">
<BR> <BR>

<input type="button" name="b" value="zusammengerechnet">
<input type="button" name="c" value="">
</form>
</body>
</html>