HTML5方向键控制会奔跑的马里奥大叔

<!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>HTML5会奔跑的马里奥大叔丨石家庄幼师|河北叉车|河北品牌策划公司</title>
    </head>
    <body>
    <div align="center">
    <canvas id="demo" style="border:1px solid #ddd;">您的浏览器不支持canvas标签,推荐您更换谷歌游览器,全面兼容html5。</canvas>
    <script  type="text/javascript">
    var eventjs = {
    //添加事件
    bind: function(elem, type, handler){
    if (elem.addEventListener) {
    elem.addEventListener(type, handler, false);
    } else if(elem.attachEvent){
    //IE
    elem.attachEvent("on" + type, handler);
    }else{
    elem["on" + type] = handler;
    };
    },
    //删除事件
    unbind: function(elem, type ,handler){
    if (elem.removeEventListener) {
    elem.removeEventListener(type, handler, false);
    } else if(elem.detachEvent){
    //IE
    elem.detachEvent("on" + type, handler)
    }else{
    elem["on" + type] = handler;
    };
    },
    //获取事件
    getEvent: function(event){
    return event ? event : window.event;
    },
    //获取事件目标
    getTarget: function(event){
    return event.target || event.srcElement;
    },
    //相关元素
    relatedTarget: function(event){
    if (event.relatedTarget) {
    return event.relatedTarget;
    } else if (event.toElement){
    return event.toElement;
    } else if (event.formElement) {
    return event.formElement;
    } else {
    return null;
    };
    },
    //阻止默认行为
    preventDefault: function(event){
    if(event.preventDefault){
    event.preventDefault();
    }else{
    event.returnValue = false;
    }
    },
    //取消捕获/冒泡
    cancelBubble: function(event){
    if (event.stopPropagation) {
    event.stopPropagation();
    } else{
    event.cancelBubble = true;
    };
    },
    //获取鼠标按钮
    getMouseButton: function(event){
    if (document.implementation.hasFeature("MouseEvents", "2.0")) {
    return event.button;
    } else{
    switch (event.button){
    case 0:
    case 1:
    case 3:
    case 5:
    case 7:
    return 0;
    case 2:
    case 6:
    return 2;
    case 4:
    return 1;
    }
    };
    },
    //获取键盘code
    getCharCode: function(event){
    if (typeof event.charCode == "number") {
    console.log(‘char code‘);
    return event.charCode;
    } else{
    return event.keyCode;
    };
    },
    getDirection: function(event){
    var keyCode = event.which || event.keyCode;
    switch(keyCode){
    case 1:
    case 38:
    case 269: //up
    return ‘up‘;
    break;
    case 2:
    case 40:
    case 270:
    return ‘down‘;
    break;
    case 3:
    case 37:
    case 271:
    return ‘left‘;
    break;
    case 4:
    case 39:
    case 272:
    return ‘right‘;
    break;
    case 339: //exit
    case 240: //back
    return ‘back‘;
    break;
    }
    }
    };
    </script>
    <script type="text/javascript">
    (function(){
    // canvas
    var canvas = document.getElementById(‘demo‘);
    var ctx = canvas.getContext(‘2d‘);
    //player base
    var playerImage = new Image();
    playerImage.src = ‘http://www.csrcode.cn/imagesforcode/201209/go.png‘;
    var player = {
    x: 50,       // 在canvas中的坐标
    y: 0,
    imageX: 0,  // 图像定位坐标
    imageY: 0,
    width: 50,  // 图像显示区域大小
    height: 60,
    stop: true  // 是否停止
    };
    function clean () {
    // clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
    function playerInit () {
    // init player
    playerImage.onload = _drawPlayer;
    }
    function playerMove (value) {
    if(‘right‘ === value){
    // move right
    if (player.stop) {
    player.imageX += 50;
    }else{
    player.imageX = 0;
    }
    if (player.imageX >= 150) {
    player.imageX = 0;
    };
    // player move speed
    player.x += 5;
    player.imageY = 0;
    } else if (‘left‘ === value){
    // move left
    if (player.stop) {
    player.imageX += 50;
    }else{
    player.imageX = 0;
    }
    if (player.imageX >= 150) {
    player.imageX = 0;
    };
    // player move speed
    player.x -= 5;
    player.imageY = 60;
    };
    clean();
    _drawPlayer();
    }
    function playerJump (value) {
    // player jump
    }
    function _drawPlayer () {
    // draw player image
    ctx.drawImage(playerImage, player.imageX, player.imageY, player.width, player.height, player.x, canvas.height - player.height, player.width, player.height);
    }
    function playerAction (value) {
    var posX = 0;
    var posY = 0;
    // console.log(player.x);
    switch(value){
    case ‘up‘:
    playerJump();
    break;
    case ‘right‘:
    posX = player.x + player.width;
    if (posX >= canvas.width) {
    return;
    };
    //player.x += 5;
    playerMove(value);
    break;
    case ‘down‘:
    break;
    case ‘left‘:
    if (player.x < 5) {
    return;
    };
    //player.x -= 5;
    playerMove(value);
    break;
    };
    // console.log(player.x);
    return {x: player.x, y: player.y};
    }
    //init canvas
    var init = (function(options){
    options = options || {};
    canvas.width = options.width || 500;
    canvas.height = options.height || 300;
    //init draw rect
    playerInit();
    //bind user event
    eventjs.bind(document, ‘keydown‘, function(event){
    var d = eventjs.getDirection(event);
    player.stop = true;
    playerAction(d);
    });
    eventjs.bind(document, ‘keyup‘, function(event){
    var d = eventjs.getDirection(event);
    player.stop = false;
    playerAction(d);
    });
    })({width:800, height:200});
    })();
    </script>
    <footer>&copy; 2012 <a target=‘_blank‘ href="#" title=</a></footer></body>
    </html>
    <br>用左右方向键控制小人奔跑方向<br><br><font color=black>▲ 目前IE8及以前版本不兼容CSS3,请使用IE9/火狐/Chrome浏览器运行本效果。<br><hr></font>
    
时间: 2024-10-05 20:48:57

HTML5方向键控制会奔跑的马里奥大叔的相关文章

canvas实现方向键控制人物行走动画

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style> #myCanvas { border: 1px solid #ccc; } </style> </head> <body> <canvas id="myCanvas" width="

使用HTML5技术控制电脑或手机上的摄像头

移动设备和桌面电脑上的客户端API起初并不是同步的.最初总是移动设备上先拥有某些功能和相应的API,但慢慢的,这些API会出现在桌面电脑上.其中一个应用接口技术就是getUserMedia API,它能让应用开发者访问用户的摄像头或内置相机.下面就让我展示一下如何通过浏览器来访问你的摄像头,并提取截屏图形. 下面的代码里写了一部分注释,请阅读: 1.  <!-- 2.      理想情况下我们应该先判断你的设备上是否 3.      有摄像头或相机,但简单起见,我们在这里直接 4.      写

使用HTML5技术控制电脑或手机上的摄像头(转载)

移动设备和桌面电脑上的客户端API起初并不是同步的.最初总是移动设备上先拥有某些功能和相应的API,但慢慢的,这些API会出现在桌面电脑上.其中一个应用接口技术就是getUserMedia API,它能让应用开发者访问用户的摄像头或内置相机.下面就让我展示一下如何通过浏览器来访问你的摄像头,并提取截屏图形. 观看演示 HTML代码 下面的代码里我写了一部分注释,请阅读: <!-- 理想情况下我们应该先判断你的设备上是否 有摄像头或相机,但简单起见,我们在这里直接 写出了HTML标记,而不是用Ja

html5 js控制音乐播放

<!DOCTYPE HTML><html><head><meta charset="UTF-8"><script language="javascript" type="text/javascript">function autoPlay(){var myAuto = document.getElementById('myaudio');myAuto.play();}</scrip

html5 触摸控制

http://www.360us.net/article/9.html <!DOCTYPE html> <html> <head><title>Touch Test</title></head> <body> <canvas id="canvas" width="600" height="600" style="border:solid black

关于方向键控制笑脸移动问题求解

gcp2vp特灰辟雌氨控<http://weibo.com/qLgJZp/230927983147615061282817> d9bvlw攘躺屎诳囱咆<http://weibo.com/20180414pp/230927983113456452378624> bi20ny洗顺仓滴四畔<http://weibo.com/wZGSdLBp/230927983023959484145664> qzhhz6赘烟腹巴泌紊<http://weibo.com/p/2309279

方向键控制圆球运动(简易)(js)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> *{padding:0px; margin:0px; } button{width:100px; height:100px; background:linear-gradient(to left,#FF0,#09

使用Html5+JS做的贪吃蛇小游戏

学习了Html5的Canvas的使用,和JS创建对象,做了一个贪吃蛇小游戏,来巩固JS面向对象编程和Canvas的用法. Node.js 1 /** 2 * 格子类,组成贪吃蛇和舞台 3 */ 4 function Node(x,y){ 5 this.x=x; 6 this.y=y; 7 /** 8 * 比较两个格子是否重合 9 */ 10 this.equals=function(x,y){ 11 return this.x==x&&this.y==y; 12 } 13 } Snake.

基于HTML5坦克大战游戏简化版

之前我们有分享过不少经典的HTML5游戏,有些还是很有意思的,比如HTML5版切水果游戏和HTML5中国象棋游戏.今天要分享的是一款简化版的HTML5坦克大战游戏,方向键控制坦克的行进方向,空格键发射子弹,命中敌方坦克后也会发出声音,效果还算可以.效果图如下: 在线预览   源码下载 实现的代码. javascript代码: window.addEventListener("load", canvasApp, false); //是否支持canvas function canvasSu