以下式例是摘自一位国际友人的代码

用html作开发确实速度快,但程序跑在手机上,运行效果却很难让人接受,此游戏在手机上测试,move事件基本让人感觉不出来,倒让人感觉像是click事件,如果有高人在这方面有研究还请指点一下,

<!DOCTYPE>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>蘑菇动起来</title>
        <script type="text/javascript" src="./js/jquery-1.4.2.js">
        </script>
        <script type="text/javascript">
            
            //全局变量  
            var backgroundForestImg = new Image();//森林背景图 
            var mushroomImg = new Image();//蘑菇  
            var bearEyesClosedImg = new Image();//闭着眼睛的熊熊 
            var ctx;//2d画布  
            var screenWidth;//画布宽度 
            var screenHeight;//画布高度  
            var speed = 2;//不变常量,从新开始的速度 
            var horizontalSpeed = speed;//水平速度,随着熊的碰撞会发生改变  
            var verticalSpeed = -speed; //垂直速度,开始肯定是要向上飘,所以要负数,随着熊的碰撞会发生改变 
            var bearAngle = 2;//熊旋转的速度  
            var flowerImg = new Image();//奖品鲜花 
            var leafImg = new Image();//奖品叶子  
            var acornImg = new Image();//奖品橡子 
            var gameRunning = false;//游戏运行状态  
            var gameloopId;//记住循环的变量 
            var lives = 3;//3条生命数  
            var livesImages = new Array();//生命图片数组 
            var score = 0;//分数  
            var scoreImg = new Image();//分数板 
            //公用 定义一个游戏物体戏对象  
            function GameObject(){
                this.x = 0;
                this.y = 0;
                this.image = null;
            }
            
            //定义蘑菇Mushroom 继承游戏对象GameObject  
            function Mushroom(){
            };
            Mushroom.prototype = new GameObject();//游戏对象GameObject  
            //蘑菇实例 
            var mushroom = new Mushroom(); //循环描绘物体 
            //定义动物熊 Animal 继承 游戏对象GameObject  
            function Animal(){
            };
            Animal.prototype = new GameObject();//游戏对象GameObject  
            Animal.prototype.angle = 0;//动物的角度,目前中(即作为动物它在屏幕上旋转退回) 
            //定义熊实例  
            var animal = new Animal();
            //定义奖品数组Prizes和对象Prize,继承游戏对象GameObject 
            var prizes = new Array();
            function Prize(){
            };
            Prize.prototype = new GameObject();//继承游戏对象GameObject  
            Prize.prototype.row = 0;//奖品行位置 
            Prize.prototype.col = 0;//奖品列位置  
            Prize.prototype.hit = false;//是否被撞过 
            Prize.prototype.point = 0;//分数 
            function GameLoop(){
                //清除屏幕  
                ctx.clearRect(0, 0, screenWidth, screenHeight);
                ctx.save();
                //绘制背景 
                ctx.drawImage(backgroundForestImg, 0, 0);
                //绘制蘑菇 
                ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);
                //绘制奖品 
                DrawPrizes();
                //绘制生命条 
                DrawLives();
                //绘制分数板 
                DrawScore();
                //绘制熊 
                //改变移动动物X和Y位置  
                animal.x += horizontalSpeed;
                animal.y += verticalSpeed;
                //改变翻滚角度 
                animal.angle += bearAngle;
                //以当前熊的中心位置为基准 
                ctx.translate(animal.x + (animal.image.width / 2), animal.y + (animal.image.height / 2));
                //根据当前熊的角度轮换 
                ctx.rotate(animal.angle * Math.PI / 180);
                //描绘熊 
                ctx.drawImage(animal.image, -(animal.image.width / 2), -(animal.image.height / 2));
                ctx.restore();
                //检测是否碰到边界 
                HasAnimalHitEdge();
                //检测熊碰撞蘑菇 
                HasAnimalHitMushroom();
                //检测熊碰撞奖品 
                HasAnimalHitPrize();
            }
            
            //加载图片  
            function LoadImages(){
                mushroomImg.src = "images/mushroom.png";//蘑菇 
                backgroundForestImg.src = "images/forest1.jpg";//森林背景图  
                bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的 
                flowerImg.src = "images/flower.png";//奖品花  
                acornImg.src = "images/acorn.png";//奖品橡子 
                leafImg.src = "images/leaf.png";//奖品叶子  
                scoreImg.src = "images/score.png";//分数板 
                //生命图片因为只有6个,所以最多只能6个 
                for (var x = 0; x < 6; x++) {
                    livesImages[x] = new Image();
                    livesImages[x].src = "images/lives" + x + ".png";
                }
                mushroom.image = mushroomImg;
                animal.image = bearEyesClosedImg;
                backgroundForestImg.onload = function(){
                    GameLoop();
                };
            }
            
            //熊碰撞边界 
            function HasAnimalHitEdge(){
                //熊碰到右边边界  
                if (animal.x > screenWidth - animal.image.width) {
                    if (horizontalSpeed > 0)//假如向右移动 
                        horizontalSpeed = -horizontalSpeed;//改变水平速度方向  
                }
                //熊碰到左边边界 
                if (animal.x < -10) {
                    if (horizontalSpeed < 0)//假如向左移动 
                        horizontalSpeed = -horizontalSpeed;//改变水平速度方向  
                }
                //熊碰到下面边界  
                if (animal.y > screenHeight - animal.image.height) {
                    lives -= 1;//生命减1 
                    if (lives > 0) {
                        horizontalSpeed = speed;
                        verticalSpeed = -speed;
                        animal.x = parseInt(screenWidth / 2);
                        animal.y = parseInt(screenHeight / 2);
                        $("#BtnImgStart").show();
                        ToggleGameplay();
                        GameLoop();
                    }
                }
                //熊碰到上边边界 
                if (animal.y < 0) {
                    verticalSpeed = -verticalSpeed;
                }
                if (lives <= 0) 
                    GameOver();
            }
            
            //事件处理  
            function AddEventHandlers(){
                //鼠标移动则蘑菇跟着移动 
                $("#container").mousemove(function(e){
                    mushroom.x = e.pageX - (mushroom.image.width / 2);
                });
                //开始按钮 
                $("#BtnImgStart").click(function(){
                    ToggleGameplay();
                });
            }
            
            //检测2个物体是否碰撞  
            function CheckIntersect(object1, object2, overlap){
                //    x-轴                      x-轴 
                //  A1——>B1 C1              A2——>B2 C2  
                //  +——–+   ^              +——–+   ^ 
                //  | object1|   | y-轴         | object2|   | y-轴  
                //  |        |   |              |        |   | 
                //  +——–+  D1              +——–+  D2  
                // 
                //overlap是重叠的区域值  
                A1 = object1.x + overlap;
                B1 = object1.x + object1.image.width - overlap;
                C1 = object1.y + overlap;
                D1 = object1.y + object1.image.height - overlap;
                A2 = object2.x + overlap;
                B2 = object2.x + object2.image.width - overlap;
                C2 = object2.y + overlap;
                D2 = object2.y + object2.image.width - overlap;
                //假如他们在x-轴重叠 
                if (A1 > A2 && A1 < B2 ||
                B1 > A2 && B1 < B2) {
                    //判断y-轴重叠 
                    if (C1 > C2 && C1 < D1 ||
                    D1 > C2 && D1 < D2) {
                        //碰撞 
                        return true;
                    }
                }
                return false;
            }
            
            //动物碰撞蘑菇  
            function HasAnimalHitMushroom(){
                //假如碰撞 
                if (CheckIntersect(animal, mushroom, 5)) {
                    //假如碰撞的位置属于蘑菇的左下位置  
                    if ((animal.x + animal.image.width / 2) < (mushroom.x + mushroom.image.width * 0.25)) {
                        horizontalSpeed = -speed;//反弹 
                    }
                    //假如碰撞的位置属于蘑菇的左上位置 
                    else 
                        if ((animal.x + animal.image.width / 2) < (mushroom.x + mushroom.image.width * 0.5)) {
                            //反弹速度减半 
                            horizontalSpeed = -speed / 2;
                        }
                        //假如碰撞的位置属于蘑菇的右上位置  
                        else 
                            if ((animal.x + animal.image.width / 2) < (mushroom.x + mushroom.image.width * 0.75)) {
                                horizontalSpeed = speed / 2;
                            }
                            else {
                                horizontalSpeed = speed;
                            }
                    verticalSpeed = -speed;//改变垂直速度。也即动物向上移动  
                }
            }
            
            //创建奖品数组 
            function InitPrizes(){
                var count = 0;
                //一共3行 
                for (var x = 0; x < 3; x++) {
                    //一共23列 
                    for (var y = 0; y < 23; y++) {
                        prize = new Prize();
                        if (x == 0) {
                            prize.image = flowerImg;//鲜花放在第一行  
                            prize.point = 3;//3分 
                        }
                        if (x == 1) {
                            prize.image = acornImg;//豫子刚在第2行 
                            prize.point = 2;//2分  
                        }
                        if (x == 2) {
                            prize.image = leafImg;//叶子放在第3行  
                            prize.point = 1;//1分 
                        }
                        prize.row = x;
                        prize.col = y;
                        prize.x = 20 * prize.col + 10;//x轴位置  
                        prize.y = 20 * prize.row + 40;//y轴位置 
                        //装入奖品数组,用来描绘  
                        prizes[count] = prize;
                        count++;
                    }
                }
            }
            
            //绘制奖品,把奖品显示在画布上  
            function DrawPrizes(){
                for (var x = 0; x < prizes.length; x++) {
                    currentPrize = prizes[x];
                    //假如没有被撞击,则描绘  
                    if (!currentPrize.hit) {
                        ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);
                    }
                }
                if (AllPrizesHit()) {
                    GameOver();
                }
            }
            
            //撞到奖品  
            function HasAnimalHitPrize(){
                //取出所有奖品 
                for (var x = 0; x < prizes.length; x++) {
                    var prize = prizes[x];
                    //假如没有碰撞过  
                    if (!prize.hit) {
                        //判断碰撞 
                        if (CheckIntersect(prize, animal, 0)) {
                            prize.hit = true;
                            //熊反弹下沉 
                            verticalSpeed = speed;
                            //总分增加 
                            score += prize.point;
                        }
                    }
                }
            }
            
            //判断是否撞完奖品,假如其中有一个  
            function AllPrizesHit(){
                for (var c = 0; c < prizes.length; c++) {
                    checkPrize = prizes[c];
                    if (checkPrize.hit == false) 
                        return false;
                }
                return true;
            }
            
            //开始或者暂停游戏  
            function ToggleGameplay(){
                gameRunning = !gameRunning;
                if (gameRunning) {
                    $("#BtnImgStart").hide();
                    gameloopId = setInterval(GameLoop, 10);
                }
                else {
                    clearInterval(gameloopId);
                }
            }
            
            //结束游戏 
            function GameOver(){
                gameRunning = false;
                clearInterval(gameloopId);
                alert("游戏结束!");
            }
            
            //描绘生命条  
            function DrawLives(){
                ctx.drawImage(livesImages[lives], 0, 0);
            }
            
            //描绘分数 
            function DrawScore(){
                ctx.drawImage(scoreImg, screenWidth - (scoreImg.width), 0);//分数板  
                ctx.font = "12pt Arial";
                ctx.fillText("" + score, 425, 25); //得分  
            }
            
            //初始化  
            $(window).ready(function(){
                AddEventHandlers();//添加事件  
                LoadImages();
                ctx = document.getElementById('canvas').getContext('2d'); //获取2d画布  
                screenWidth = parseInt($("#canvas").attr("width")); //画布宽度 
                screenHeight = parseInt($("#canvas").attr("height"));
                //初始化蘑菇 
                mushroom.x = parseInt(screenWidth / 2);// 蘑菇X坐标  
                mushroom.y = screenHeight - 40;//蘑菇Y坐标 
                //初始化熊  
                animal.x = parseInt(screenWidth / 2);
                animal.y = parseInt(screenHeight / 2);
                //初始化奖品 
                InitPrizes();
            });
        </script>
    </head>
    <body>
        <div id="container" style="border:1px solid;  cursor:none;  width:480px;  height:300px;">
            <canvas id="canvas" width="480" height="300">
            </canvas>
        </div>
        <img id="BtnImgStart" style="position: absolute; left: 200px; top: 200px; cursor: pointer; float: left; display: block; " src="./images/start_button.png">
    </body>
</html>
</body>
</html>

转载于:https://www.cnblogs.com/retacn-yue/archive/2012/09/07/2761302.html

html5 游戏学习相关推荐

  1. html5游戏引擎-Pharse.js学习笔记(一)

    2019独角兽企业重金招聘Python工程师标准>>> 1.前言 前几天随着flappy bird这样的小游戏的火爆,使我这种也曾了解过html5技术的js业余爱好者也开始关注游戏开 ...

  2. HTML5游戏化互动学习平台,h5游戏平台_触摸型互动slg黄油手游

    乐趣H5游戏平台是目前中国最大的H5手机网页游戏平台,提供在线玩的H5手机小游戏,最火爆的微信小游戏,最好玩的手机小游戏排行榜,让您能结识到H5游戏玩家和H5小游戏里的朋友,无需下载,点击马上玩!开心 ...

  3. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月25日-7月1日)

    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月25日-7月1日) 本周Silverlight学习资源更新 用Silverlight做淘宝应用 往事如锋 基于 Si ...

  4. HTML5游戏开发(四):飞机大战之显示场景和元素

    <HTML5游戏开发>系列文章的目的有:一.以最小的成本去入门egret小项目开发,官方的教程一直都是面向中重型:二.egret可以非常轻量:三.egret相比PIXI.js和sprite ...

  5. javascript+HTMl5游戏下载,开发一个都能月薪上万!舅服你

    HTML5时代已经到来许久了,你是否已经掌握了那么一点呢?今天小编给大家讲讲h5的折叠多设备.跨平台特性, 即用HTML5制作游戏.相比flash,HTML5更加灵活方便,随着浏览器技术的不断升级,H ...

  6. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月24日-9月30日)

    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月24日-9月30日) 本周Silverlight学习资源更新 解决"Chrome提示:Silverligh ...

  7. 数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇

    HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心目中的产品方向,接着就朝这个方向慢慢打 ...

  8. Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月1日-1月6日)

    Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月1日-1月6日) 本周Windows 8开发学习资源更新 浅析微软Win 8系统激活的各种问题 cometwo Wi ...

  9. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(7月30日-8月5日)

    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(7月30日-8月5日) 本周Silverlight学习资源更新 Silverlight 之Control and Use ...

最新文章

  1. [蓝桥杯]2018年第九届省赛真题C/C++ B组 填空+大题
  2. javascript 方法总结(Array篇)
  3. vos3000v2.1.6.0客户端 vos3000 6.0下载
  4. 神经网络控制基本原理,神经网络理论及其应用
  5. mimetype知识点备忘
  6. Super Iservice 发布地图三维服务
  7. c++中使用orm关系对象模型
  8. linux怎么查服务器的ip地址查询,Linux操作系统查看当前服务器网卡的IP地址
  9. DS18B20温度传感器单片机C语言驱动程序
  10. MES的发展历程及功能模块
  11. 【javaScript案例】之搜索的数据显示
  12. 【RK按键】按键切换
  13. 详解STM32中的ADC
  14. ArcGIS中根据DEM提取等高线和高程点(附练习数据)
  15. paessler公司PRTG 可以监控您的整个 IT 基础设施官方免费下载试用
  16. 华为手机便捷好用的原因,终于被我找到了
  17. python3通过itchat登录微信给好友发送消息
  18. Android测试音高app,安卓版AudioTools频谱声场测试软件音响师手机调音啸叫频率软件...
  19. 人不是被事物本身困扰是什么理论_心理学:人不是被事情本身所困扰,而是被其对事情的看法所困扰!...
  20. 2023高频经典前端面试题(vue2+vue3+typescript+nodejs下篇,含答案)

热门文章

  1. java构造方法嵌套,laravel查询构建器中的嵌套查询
  2. python入门之控制结构-循环结构_(一)Python入门-4控制语句:05while循环结构-死循环处理...
  3. 信令风暴研究现状总结
  4. ubuntu dig timeout解决方法,dnscat执行失败也是这个原因
  5. python3 如何让字典保持有序
  6. rem是如何实现自适应布局的
  7. 移动vue项目,启动错误:Module build failed: Error: No PostCSS Config found in:
  8. bzoj千题计划128:bzoj4552: [Tjoi2016Heoi2016]排序
  9. kindeditor-网页文字编辑
  10. 聊聊JS与设计模式之(工厂Factory)篇------(麦当劳的故事)