zoukankan      html  css  js  c++  java
  • Html5 小球键盘移动

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body onkeydown="test()">
        <h1>小球上下移动</h1>
        <canvas id="test" width="400px" height="300px" style="background-color: black"></canvas>
        <script type="text/javascript">
            //得到画布
            var canvas1 = document.getElementById("test");
            //取得画布画笔对象
            var cxt = canvas1.getContext("2d");
            //画出红色圆球
            cxt.beginPath();
            cxt.fillStyle = "#FF0000";
            cxt.arc(30, 30, 10, 0, 360, true);
            cxt.closePath();
            cxt.fill();
            //键盘发生事件 让小球移动 w d s a
            //按下一个键,实际上触发一个onkeydow事件
            var ballX = 30;
            var bally = 30;
    
            function test() {
    
                cxt.clearRect(0,0,400,300);
                var code = event.keyCode;
                switch (code) {
                    case 87:
                        bally--;
                        break;
                    case 68:
                        ballX++;
                        break;
                    case 83:
                        bally++;
                        break;
                    case 65:
                        ballX--;
                        break;
                }
                drawBall();
            }
    
    
            //重新绘制
            function drawBall() {
                cxt.beginPath();
                cxt.fillStyle = "#FF0000";
                cxt.arc(ballX, bally, 10, 0, 360, true);
                cxt.closePath();
                cxt.fill();
            }
    
        </script>
    </body>
    </html>
  • 相关阅读:
    poj2388-Who's in the Middle(排序)
    poj1543-Perfect Cubes(暴力)
    poj1664-放苹果(递归)
    快速幂
    poj2389-Bull Math(大整数乘法)
    HDU2608-0 or 1(数论+找规律)
    poj1131-Octal Fractions(进制转换)
    [noip2011 d1t2]选择客栈
    [周记]8.7~8.16
    [noip2012d1t2] 国王游戏
  • 原文地址:https://www.cnblogs.com/yzenet/p/3866572.html
Copyright © 2011-2022 走看看