仿遊戲《勇闖獨木橋》-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>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。