随机骰子卷

时间:2015-12-13 11:15:51

标签: javascript

我对javascript很新,我正在创建一个骰子游戏。我想制作一个骰子随机出现的Javascript函数,我想在每次点击按钮时随机改变骰子。 这是javascript和html代码:

<!DOCTYPE html>
<html>
<head>
	<title>Lancio del dado</title>
<script type="text/javascript">
var numero_dado = 1;
var tempo =1000;
setTimeout(function(){lancio_dado(); }, tempo);
</script>
<style>
table{
 *border-collapse: collapse; /* IE7 and lower */
  border-spacing: 0;  
	background:#ffffff !important; /* For IE*/ 
}
.bordered {
  border: solid #ccc 10px;
	-moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
	behavior: url(border-radius.htc);
  -webkit-box-shadow: 0 1px 1px #ccc; 
  -moz-box-shadow: 0 1px 1px #ccc; 
  box-shadow: 0 1px 1px #ccc;         
}
</style>
</head>
<body>
<script type="text/javascript">

function lancio_dado(){
	if(numero_dado == 1){
		document.getElementById('primacella').innerHTML = "";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "";
		document.getElementById('quartacella').innerHTML = "";
		document.getElementById('quintacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('sestacella').innerHTML = "";
		document.getElementById('settimacella').innerHTML = "";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "";
		numero_dado = 2;
		setTimeout(function(){lancio_dado(); }, tempo);
		breake();
	}
	if(numero_dado == 2){
		document.getElementById('primacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "";
		document.getElementById('quartacella').innerHTML = "";
		document.getElementById('quintacella').innerHTML = "";
		document.getElementById('sestacella').innerHTML = "";
		document.getElementById('settimacella').innerHTML = "";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		numero_dado = 3;
		 setTimeout(function(){lancio_dado(); }, tempo);
		 breake();
	}
	if(numero_dado == 3){
		document.getElementById('primacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "";
		document.getElementById('quartacella').innerHTML = "";
		document.getElementById('quintacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('sestacella').innerHTML = "";
		document.getElementById('settimacella').innerHTML = "";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		numero_dado = 4;
		 setTimeout(function(){lancio_dado(); }, tempo);
		 breake();
	}
	if(numero_dado == 4){
		document.getElementById('primacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('quartacella').innerHTML = "";
		document.getElementById('quintacella').innerHTML = "";
		document.getElementById('sestacella').innerHTML = "";
		document.getElementById('settimacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		numero_dado = 5;
		 setTimeout(function(){lancio_dado(); }, tempo);
		 breake();
	}
	if(numero_dado == 5){
		document.getElementById('primacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('quartacella').innerHTML = "";
		document.getElementById('quintacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('sestacella').innerHTML = "";
		document.getElementById('settimacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		numero_dado = 6;
		 setTimeout(function(){lancio_dado(); }, tempo);
		 breake();
		
	}
	if(numero_dado == 6){
		document.getElementById('primacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('secondacella').innerHTML = "";
		document.getElementById('terzacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('quartacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('quintacella').innerHTML = "";
		document.getElementById('sestacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('settimacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		document.getElementById('ottavacella').innerHTML = "";
		document.getElementById('nonacella').innerHTML = "<img src='./img/punto.jpg'  width='50' height='50'>";
		numero_dado = 1;
		 setTimeout(function(){lancio_dado(); }, tempo);
		 breake();
		
	}
}
</script>
</script>
<button onclick="throwdice">Lancia il dado</button>
<center>
<table  class= "bordered" width ="300" height="300">
	<tr>
		<td align="center" valign="middle" width="100" height="100"><span id="primacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="secondacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="terzacella"></span></td>
	</tr>
	<tr>
		<td align="center" valign="middle" width="100" height="100"><span id="quartacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="quintacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="sestacella"></span></td>
	</tr>
	<tr>
		<td align="center" valign="middle" width="100" height="100"><span id="settimacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="ottavacella"></span></td>
		<td align="center" valign="middle" width="100" height="100"><span id="nonacella"></span></td>
	</tr>
</table>
</center>
</body>
</html> 

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

numero_dado = Math.floor((Math.random() * 6) + 1);

该语句生成[1,6]范围内的随机数并将其分配给numero_dado。