仿微信遊戲《勇闖獨木橋》-HTML5版本,html5仿微信聊天介面

來源:互聯網
上載者:User

仿遊戲《勇闖獨木橋》-HTML5版本,html5仿聊天介面

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>勇闖獨木橋</title></head><body>    <canvas id="canvas" width="400" height="400" style="border: 1px solid #000;"></canvas>    <script>        var canvas = document.getElementById('canvas');        var cxt = canvas.getContext('2d');        // 定時器        var timer;        // 遊戲結束控制開關        var iStop = false;        // 得分        var score = 1;        // 起點        var start = {x:0,y:300,w:100,h:100};        // 終點        var end = {x:200,y:300,w:100,h:100};        // 過橋人的位置        var manX = 90;        // 過橋速率        var manStep = 0;        // 橋的寬度        var loadWidth = 0;        // 橋變長的速率        var loadStep = 0;        // 橋倒下控制開關        var loadRun = false;        // 橋倒下的速率        var loadDeg = 0;        // 過橋是否成功        var isSuccess = false;        // 是否正在過橋        var isOn = false;        // 畫布移動速率        var canvasMove = 0;        // 終點的位置列表        var intervals = [];        for(var i=0; i<400; i+=10){            intervals.push(i);        }        // 終點的寬度列表        var blocks = [];        for(var i=30; i<100; i+=10){            blocks.push(i);        }        // 橋變長        function pave(){            loadWidth += loadStep;        }        // 人過橋        function step(){            manX += manStep;        }        // 繪製圖形        function draw(){            cxt.save();            cxt.fillRect(start.x, start.y, start.w, start.h);            cxt.fillRect(end.x, end.y, end.w, end.h);            cxt.restore();            cxt.save();            cxt.fillStyle = 'gray';            cxt.translate(start.w, 300);            cxt.rotate(loadDeg*Math.PI/180);            cxt.fillRect(0, 0, 10, -loadWidth);            cxt.restore();            cxt.save();            cxt.fillStyle = '#FF4500';            cxt.fillRect(manX, 290, 10, 10);            cxt.restore();        }        // 清除畫布        function erase() {            cxt.clearRect(0, 0, canvas.width, canvas.height)        }        // 隨機終點        function initEnd(){            var interval = intervals[Math.floor(Math.random()*intervals.length)];            var block = blocks[Math.floor(Math.random()*blocks.length)];            if(interval > start.w && interval + block < 400){                end.x = interval;                end.w = block;            }else{                initEnd();            }        }        // 畫得分        function drawScore() {            cxt.save();            cxt.font="40px Verdana";            cxt.fillStyle = 'black';            cxt.fillText('第' + score +'關', 130, 50);            cxt.restore();        }        // 畫結束        function drawEnd() {            cxt.save();            cxt.font="40px Verdana";            cxt.fillStyle = 'red';            cxt.fillText('遊戲失敗,繼續努力!', 10, 150);            cxt.restore();        }        // 控制橋的長度-空格鍵(space)        document.onkeydown = function(ev){            if(ev.keyCode == 32 && !isOn){                loadStep = 2;                isOn = true;            }else{                return;            }            document.onkeyup = function(ev){                if(ev.keyCode == 32 && loadDeg == 0){                    loadStep = 0;                    loadRun = true;                    if(loadWidth + start.w > end.x && loadWidth + start.w < end.x + end.w){                        isSuccess = true;                    }                }            }        };        window.requestAnimationFrame =                window.requestAnimationFrame ||                window.mozRequestAnimationFrame ||                window.webkitRequestAnimationFrame ||                window.msRequestAnimationFrame;        window.cancelRequestAnimationFrame =                window.cancelRequestAnimationFrame ||                window.mozCancelRequestAnimationFrame ||                window.webkitCancelRequestAnimationFrame ||                window.msCancelRequestAnimationFrame;        function animate() {            erase();            pave();            step();            draw();            drawScore();            // 橋開始倒下            if(loadRun){                loadDeg += 2;            }            // 橋倒下後,人開始過橋            if(loadDeg == 90){                loadRun = false;                manStep = 5;            }            // 過橋是否能成功            if(isSuccess){                // 到達目的地                if(manX >= (end.x + end.w - 10)){                    canvasMove = 5;                    manStep = 0;                    loadWidth = 0;                }                // 初始化,開始下一關過橋                if(end.x == 0){                    loadDeg = 0;                    isSuccess = false;                    isOn = false;                    canvasMove = 0;                    start.x = 0;                    start.w = end.w;                    manX = start.w - 10;                    initEnd();                    score ++;                }else{                    start.x -= canvasMove;                    end.x -= canvasMove;                    manX -= canvasMove;                }            }else{                // 過橋失敗,遊戲結束                if(manX >= start.w + loadWidth){                    manStep = 0;                    drawEnd();                    iStop = true;                }            }            if(iStop){                cancelRequestAnimationFrame(timer);            }else{                timer = requestAnimationFrame(animate);            }        }        animate();    </script></body></html>



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.