第八回合中主要是完善游戏,给游戏加上开始按钮、生命数、得分

成都网站建设哪家好,找创新互联公司!专注于网页设计、成都网站建设、微信开发、微信平台小程序开发、集团成都企业网站建设等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:成都食品包装袋等众多领域,积累了大量丰富的经验,同时也获得了客户的一致赞扬!
预期达到的效果:http://www.html5china.com/html5games/mogu/index7.html
一、添加开始按钮
A、html代码中加入开始按钮,并定位他于画布的中间
开始图片下载地址:http://www.html5china.com/html5games/mogu/images/START-Button.png
B、全局变量
- var gameRunning = false;//游戏运行状态
- var gameloopId;//记住循环的变量
C、原来是游戏自己开始没有暂停的、写一个开始暂停的函数
- //开始或者暂停游戏
- function ToggleGameplay()
- {
- gameRunning = !gameRunning;
- if(gameRunning)
- {
- $("#BtnImgStart").hide();
- gameloopId = setInterval(GameLoop, 10);
- }else
- {
- clearInterval(gameloopId);
- }
- }
D、添加开始按钮事件
- //事件处理
- function AddEventHandlers()
- {
- //鼠标移动则蘑菇跟着移动
- $("#container").mousemove(function(e){
- mushroom.x = e.pageX - (mushroom.image.width/2);
- });
- //开始按钮
- $("#BtnImgStart").click(function (){
- ToggleGameplay();
- });
- }
注意,要把$(window).ready(function(){})函数中的setInterval(GameLoop, 10);去掉
#p#
二、添加生命数条
A、需要的全局变量
- var lives=3;//3条生命数
- var livesImages = new Array();//生命图片数组
B、生命图片初始化
- //生命图片因为只有6个,所以最多只能6个
- for(var x=0; x<6; x++)
- {
- livesImages[x] = new Image();
- livesImages[x].src = "images/lives" + x + ".png";
- }
C、绘制生命条
- //描绘生命条
- function DrawLives()
- {
- ctx.drawImage(livesImages[lives], 0, 0);
- }
D、当熊碰到底线时,减一条生命
- //熊碰到下面边界
- if(animal.y>screenHeight - animal.image.height)
- {
- lives -=1;//生命减1
- //当还有生命条时,暂停游戏,熊回到中间位置,显出开始按钮
- if(lives>0)
- {
- horizontalSpeed = speed; //初始化水平速度
- verticalSpeed = -speed; //初始化垂直速度
- animal.x = parseInt(screenWidth/2); //初始化熊的x坐标
- animal.y = parseInt(screenHeight/2); //初始化熊的y坐标
- $("#BtnImgStart").show(); //显示开始按钮
- ToggleGameplay(); //暂停游戏
- GameLoop(); //初始化绘图
- }
- }
E、当生命条数等于0或者奖品消灭完,游戏结束
- //结束游戏
- function GameOver()
- {
- gameRunning = false;
- clearInterval(gameloopId);
- alert("游戏结束!");
- }
在熊撞到底线的代码中,加入判断,当生命数=0时,游戏结束
- if(lives<=0)
- GameOver();
在绘制奖品函数中加入判断,当奖品被消灭完时,游戏结束
- //绘制奖品,把奖品显示在画布上
- function DrawPrizes()
- {
- for(var x=0; x
- {
- currentPrize = prizes[x];
- //假如没有被撞击,则描绘
- if(!currentPrize.hit)
- {
- ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);
- }
- }
- if(AllPrizesHit())
- {
- GameOver();
- }
- }
- //判断是否撞完奖品,假如其中有一个
- function AllPrizesHit()
- {
- for(var c=0; c
- {
- checkPrize = prizes[c];
- if(checkPrize.hit == false)
- return false;
-
- }
- return true;
- }
#p#
三、添加得分
A、定义全局变量
- var score = 0;//分数
- var scoreImg = new Image();//分数板
B、初始化分数板
- scoreImg.src = "images/score.png";//分数板
C、给奖品加一个分数属性。这样在撞奖品时才能知道得到多少分
- function Prize() {};
- Prize.prototype = new GameObject();//继承游戏对象GameObject
- Prize.prototype.row = 0;//奖品行位置
- Prize.prototype.col = 0;//奖品列位置
- Prize.prototype.hit = false;//是否被撞过
- Prize.prototype.point = 0;//分数
D、在初始化奖品数组中加入分数
- //创建奖品数组
- 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++;
- }
- }
- }
E、当熊撞到奖品时,根据碰撞奖品的分数增加总分
- //撞到奖品
- function HasAnimalHitPrize()
- {
- //取出所有奖品
- for(var x=0; x
- {
- var prize = prizes[x];
- //假如没有碰撞过
- if(!prize.hit)
- {
- //判断碰撞
- if(CheckIntersect(prize, animal, 0))
- {
- prize.hit = true;
- //熊反弹下沉
- verticalSpeed = speed;
- //总分增加
- score += prize.point;
- }
- }
- }
-
- }
F、绘制分数
- //描绘分数
- function DrawScore()
- {
- ctx.drawImage(scoreImg, screenWidth-(scoreImg.width),0);//分数板
- ctx.font = "12pt Arial";
- ctx.fillText("" + score, 425, 25); //得分
- }
#p#
综合上面的代码, 到此第八回的完整代码如下:
-
-
-
-
- 绘制奖品-html5中文网
-
-
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-01 15:05:56 HTTP/1.1 GET : /article/codcspc.html
- 运行时间 : 0.0652s ( Load:0.0033s Init:0.0006s Exec:0.0566s Template:0.0047s )
- 吞吐率 : 15.34req/s
- 内存开销 : 2,338.36 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=nl0p6ipti92jt0p9hp2fu71ad0
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000004s ]
- [ app_init ] --END-- [ RunTime:0.000024s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000190s ]
- [ app_begin ] --END-- [ RunTime:0.000206s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000051s ]
- [ template_filter ] --END-- [ RunTime:0.000068s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.003600s ]
- [ view_parse ] --END-- [ RunTime:0.003618s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000067s ]
- [ view_filter ] --END-- [ RunTime:0.000075s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/9131fb59a09396ce775ce0a5b1f828e8.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/4e2710f73e9bf855444a170345e79424.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.0652s
