HTML5游戏一步一步爬(一)

骰子游戏:两点数之和大于7则赢。初始金额:100

<!DOCTYPE HTML>
<head>
    <title>this is a dice game!!!</title>
    <style>
        .btn-cls{
            position:absolute;
            top:400px;
            left:250px;
        }
        #mesg{
            position:absolute;
            top:350px;
            left:250px;
            color:red;
        }
        #discr{
            position:absolute;
            top:300px;
            left:250px;
            color:green;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="600" height="600">
        your browser doesn't support the HTML5 element canvas.
    </canvas>
    <p id="mesg"></p>
    <p id="discr">金钱:100</p>
    <button id="btn" class="btn-cls" onclick="Throw()" >开始投掷</button>
    <script>

        var ctx; // canvas上下文
        var discr = document.getElementById('discr');
        var mesg = document.getElementById('mesg');
        var point1,point2;  //  点数1,2
        var money = 100;// 金额

        ctx = document.getElementById('canvas').getContext('2d');
        // 绘制画布
        ctx.strokeRect(0,0,600,600);
        // 绘制两个骰子
        ctx.strokeRect(100,100,150,150);
        ctx.strokeRect(350,100,150,150);

        function Throw(){   // 投掷按钮
            point1 = Math.ceil(Math.random() * 6);
            point2 = Math.ceil(Math.random() * 6);
            DrawPoint(point1,point2);
            pointOperation();
        }

        function DrawPoint(drawPoint1,drawPoint2){ // 绘制骰子上面的点
            ctx.clearRect(110,110,130,130);
            ctx.clearRect(360,110,130,130);
            ctx.beginPath();
            <!-- alert(drawPoint1); -->
            switch(drawPoint1){
                case 1: 
                        arcs(175,175,5,0,2 * Math.PI);
                        break;
                case 2: 
                        arcs(135,135,5,0,2 * Math.PI);
                        arcs(215,215,5,0,2 * Math.PI);
                        break;
                case 3: 
                        arcs(135,135,5,0,2 * Math.PI);
                        arcs(175,175,5,0,2 * Math.PI);
                        arcs(215,215,5,0,2 * Math.PI);
                        break;
                case 4: 
                        arcs(135,135,5,0,2 * Math.PI);
                        arcs(135,215,5,0,2 * Math.PI);
                        arcs(215,215,5,0,2 * Math.PI);
                        arcs(215,135,5,0,2 * Math.PI);
                        break;
                case 5: 
                        arcs(135,135,5,0,2 * Math.PI);
                        arcs(135,215,5,0,2 * Math.PI);
                        arcs(175,175,5,0,2 * Math.PI);
                        arcs(215,215,5,0,2 * Math.PI);
                        arcs(215,135,5,0,2 * Math.PI);
                        break;
                case 6: 
                        arcs(135,135,5,0,2 * Math.PI);
                        arcs(135,175,5,0,2 * Math.PI);
                        arcs(135,215,5,0,2 * Math.PI);
                        arcs(215,135,5,0,2 * Math.PI);
                        arcs(215,175,5,0,2 * Math.PI);
                        arcs(215,215,5,0,2 * Math.PI);
            }
            switch(drawPoint2){
                case 1: 
                        arcs(425,175,5,0,2 * Math.PI);
                        break;
                case 2: 
                        arcs(385,135,5,0,2 * Math.PI);
                        arcs(465,215,5,0,2 * Math.PI);
                        break;
                case 3: 
                        arcs(385,135,5,0,2 * Math.PI);
                        arcs(425,175,5,0,2 * Math.PI);
                        arcs(465,215,5,0,2 * Math.PI);
                        break;
                case 4: 
                        arcs(385,135,5,0,2 * Math.PI);
                        arcs(385,215,5,0,2 * Math.PI);
                        arcs(465,215,5,0,2 * Math.PI);
                        arcs(465,135,5,0,2 * Math.PI);
                        break;
                case 5: 
                        arcs(385,135,5,0,2 * Math.PI);
                        arcs(385,215,5,0,2 * Math.PI);
                        arcs(425,175,5,0,2 * Math.PI);
                        arcs(465,215,5,0,2 * Math.PI);
                        arcs(465,135,5,0,2 * Math.PI);
                        break;
                case 6: 
                        arcs(385,135,5,0,2 * Math.PI);
                        arcs(385,175,5,0,2 * Math.PI);
                        arcs(385,215,5,0,2 * Math.PI);
                        arcs(465,135,5,0,2 * Math.PI);
                        arcs(465,175,5,0,2 * Math.PI);
                        arcs(465,215,5,0,2 * Math.PI);
            }
            ctx.fillStyle = "block";
            ctx.closePath();
            ctx.fill();
        }
        function arcs(point_x,point_y,redius,begin,end){
            ctx.moveTo(point_x + redius,point_y);
            ctx.arc(point_x,point_y,redius,0,2 * Math.PI);
        }
        function pointOperation(){
            var pointCount = point1+point2;
            if(pointCount>7){
                money+=10;
                mesg.innerHTML = "你赢了!获得金钱10";
            }else if(pointCount == 7){
                mesg.innerHTML = "平局!重新开始";
            }else{
                money-=10;
                mesg.innerHTML= "你输了!失去金钱10";
            }
            discr.innerHTML = "金钱:"+ money;
        }
    </script>
</body>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值