HTML5 打地鼠简化版

1、canvas 画图的时候相对的坐标是画板‘

2、浏览器兼容性问题,在IE下面是clientX clientY

3、修改小球的运动轨迹可以在addX和addY上面下功夫 ~

4、修改容人值可以修改 catch_ball函数 代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var ballX,ballY;
        var addX,addY;
        var intervalId ;

        var context ;
        var canvas ;
        var whdth;
        var height = 500;
        
        function catch_ball(ev){
            alert('catch');
            var x = ev.pageX;
            var y = ev.pageY;
            // get position relative to canvas .
            
            var relativeX = x - document.getElementById("mycanvas").offsetLeft;
            var relativeY = y - document.getElementById("mycanvas").offsetTop;
            
            //judge  whether  clicked  the ball
            if(-5<(relativeX - ballX)&&(relativeX - ballX)<=5){
                 if(-5<(relativeY - ballY)&&(relativeY - ballY)<=5){
                     alert('congratulation  to  you  ~~~ ,you are win .');
                     clearInterval(intervalId);
                     document.getElementById("start_btn").disabled = "";
                }
            }
        }
        
        function windowLoad(){
            document.getElementById("mycanvas").onclick = catch_ball;
        }
        
        function jump(){
           context.clearRect(0,0,width,height);
           context.save();
           //draw  ball table .
           context.fillStyle = "lightgreen";
           context.strokeStyle = "black";
           context.lineWidth = 3;
           context.fillRect(3,3,width -6,height-6);
           context.strokeRect(3,3,width-6,height-6);
           
           //draw ball .
           context.beginPath();
           context.fillStyle = "blue";
           context.arc(ballX,ballY,5,0,2*Math.PI,false);
           ballX += addX;
           ballY += addY;
           //alert(ballX+":"+ballY);
           //modify position .
           if(ballX<5){
               ballX = 5;
               addX = -addX;
           }
           
           if(ballY <5){
               ballY = 5;
               addY = -addY;
           }
           
           if(ballX>width-6){
               ballX = width-6;
               addX = -addX;
           }
           
           if(ballY>height-6){
               ballY = height-6;
               addY = -addY;
           }
           
           context.closePath();
           context.fill();
           context.restore(); 
        }
        
        function startclick(){
           // alert('game  start ');
            canvas = document.getElementById("mycanvas");
            context = canvas.getContext("2d");
            
            width = canvas.width;
            height = canvas.height;
            //alert(height);
            //init  ball  init position .
            ballX = parseInt(Math.random()*width);
            ballY = parseInt(Math.random()*height);
            
            addX = -5;
            addY = -5;
            
            jump();
            
            //test
            //context.fillStyle = "red";
            //context.fillRect(5,5,20,20);
            intervalId = setInterval("jump()",200);
            document.getElementById("start_btn").disabled = "disabled";
        }

    </script>
  </head>
  <body οnlοad="windowLoad();">
      <h1> jump  game  </h1>
      <H2>click  the  start button  , then   click  jumping  ball .</H2>
      <input type="button"  value="start game" id="start_btn" οnclick="startclick();"/>
      <div style="width:600px;height:550px;border:blue solid 2px;">
          <canvas   id="mycanvas" width="500px" height="500px"> broswer is not support !</canvas>
      </div>
  </body>
</html>

  

转载于:https://www.cnblogs.com/meiyitian/archive/2012/08/12/2634301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值