掷骰子游戏

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>骰子游戏</title>
 <style type="text/css">
  form{
    font-size: 40px;
    color: #ee5566;
   }
   input{
    width: 150px;
    text-align: center;
    font-size: 40px;
    color: #00eeee;
   }
   button{
    font-size: 35px;

   }
 </style>
 <script type="text/javascript">
window.οnlοad=function(){
  var timer=null;
  var num=0;
  var sum=0;
  var cwidth = 400;
  var cheight = 300;
  var dicex = 100;
  var dicey = 50;
  var dicewidth = 200;
  var diceheight = 200;
  var dotrad = 15;
  var ctx;
  var dx;
  var dy;
document.getElementById("throwDice").οnclick=function(){
  clearInterval(timer);
    timer=setInterval(function(){
      var ch = 1+Math.floor(Math.random()*6);
       sum = ch;
      dx = dicex;
      dy = dicey;
      drawface(ch);
     
      },50)
    }

document.getElementById("stopDice").οnclick=function(){
     clearInterval(timer);
     document.f.num.value="第"+(++num)+"次";
      
      document.f.all.value=parseInt(document.f.all.value)+sum;
      if(num>=6){
       alert("您已经摇了6次骰子");
        num=0;
        document.f.all.value="0";
      }
 
}
 
 function drawface(n) {
  ctx = document.getElementById('canvas').getContext('2d'); 
  ctx.lineWidth = 5;
  ctx.fillStyle="#00ee11";
  ctx.clearRect(dx,dy,dicewidth,diceheight);
  ctx.fillRect(dx,dy,dicewidth,diceheight)
  var dotx;
  var doty;
  ctx.fillStyle = "#ee0000";
  switch(n) {
    case 1:
     draw1();
     break;
    case 2:
     draw2();
     break;
    case 3:
     draw2();
     draw1();
     break;
    case 4:
     draw4();
     break;
    case 5:
     draw4();
     draw1();
     break;
    case 6:
     draw4();
     draw2mid();
     break;
   
  }
}
function draw1() {
  var dotx;
  var doty;
  ctx.beginPath();
  dotx = dx + .5*dicewidth;
  doty = dy + .5*diceheight;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
}
function draw2() {
  var dotx;
  var doty;
  ctx.beginPath();
  dotx = dx + 3*dotrad;
  doty = dy + 3*dotrad;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  dotx = dx+dicewidth-3*dotrad;
  doty = dy+diceheight-3*dotrad;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
}
function draw4() {
  var dotx;
  var doty;
  ctx.beginPath();
  dotx = dx + 3*dotrad;
  doty = dy + 3*dotrad;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  dotx = dx+dicewidth-3*dotrad;
  doty = dy+diceheight-3*dotrad;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
  ctx.beginPath();
  dotx = dx + 3*dotrad;
  doty = dy + diceheight-3*dotrad;  //no change
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  dotx = dx+dicewidth-3*dotrad;
  doty = dy+ 3*dotrad;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
}
function draw2mid() {
  var dotx;
  var doty;
  ctx.beginPath();
  dotx = dx + 3*dotrad;
  doty = dy + .5*diceheight;
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  dotx = dx+dicewidth-3*dotrad;
  doty = dy + .5*diceheight; //no change
  ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
}
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300" style="border:2px red solid">
Your browser doesn't support the HTML5 element canvas.
</canvas>
<br/>
<button id="throwDice">开始摇骰子</button><button id="stopDice">停止摇骰子</button>
<form name="f" id="f">
第几次摇骰子: <input name="num" value=" " readonly="readonly" />
总点数: <input name="all" value="0" readonly="readonly" /></form>

</body>
</html>

转载于:https://my.oschina.net/u/2604302/blog/597217

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值