HTML5应用开发课后习题题库期末考试试卷及答案 (4).docx
《HTML5应用开发课后习题题库期末考试试卷及答案 (4).docx》由会员分享,可在线阅读,更多相关《HTML5应用开发课后习题题库期末考试试卷及答案 (4).docx(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实操题:1.编写跳一跳游戏,效果如图7-5所示,绘制一个圆与长方形当棋子,绘制一个圆柱形作为跳 板五子一小 am图7-5习题1界面效果 it le 跳一跳 /title)*padding: 0;margin: 0;) bodymargin: 50px;)hl color: orange;text-shadow: lpx lpx 3px rgba(0) 0, 0, .6); text-align: center;.gameposition: relative;width: 800px;height: 400px;border: 3px double orange;margin: 0 auto;)
2、.game .contentposition: absolute;width: 700px;height: 400px;left: 0;top: 0;/绘制棋子function drawChess(xJ y, color) context.beginPath();context.arc(x y, 15, 0, Math.PI * 2, false);context.closePath();context.fillstyle = color; context/下棋子(canvas点击响应函数)canvas .addE vent Listener (Mc lickfunction(e) /判断游戏
3、是否结束if(flag) alert (游戏结束”); return;)/判断点击范围是否越出棋盘if(e.offsetX 420 | | e.offsetY 420) return;)van dx = Math.floor(e.offsetX + 15) / 30) * 30;van dy = Mathfloor(e.offsetY + 15) / 30) * 30;if(chessMapArrdx / 30 - 1dy / 30 - 1 = 0) drawChess(dxJ dy, chessColor step % 2); 下棋 chessMapArrdx / 30 - 1dy / 30
4、 - 1= chessColorstep % 2;for(var i = 0; i 4; i+) checkWin(dx / 3。- 1, dy / 30 - 1, chessColorstep % 2, checkModei);) step+;);/输赢判断函数function checkWin(x, y, color, mode) var count = 1; /记录分数for(var i = 1; i 5; i+) if(chessMapArrx + i * mode0) if(chessMapArrx + i * mode0y + i * model=color) count+;els
5、e break;for(var j = 1; j = 5) alert(游戏结束“);flag = true;) 3 ,贪吃蛇小游戏,初始化设计游戏界面如下,蛇用三个方块表示,食物用随机色方块表示贪电卧翔t开始游戏参考代码:Documentbody margin: 50px;padding: 0;).main width: 1200px;height: 600px;margin: 50px auto;hlcolor: orange;text-shadow: lpx lpx 3px rgba(00,0J .6); text-align: center;.btn width: 100px;heig
6、ht: 40px;display: block;margin: auto;font-size: 16px;color: #fff;text-shadow: lpx lpx 3px #2D9900;background: orange;border-radius: 5px;).map position: relative;width: 1200px;height: 600px;margin: 50px;border: 2px dotted red;)贪吃蛇小游戏开始游戏/buttonvar map = document.getElementById(ap1);/使用构造方法创立蛇,functio
7、n Snake()(this.width = 15;this.height = 15;this.direction = 1 right1;/初始为3个方块为一个蛇,this.body =x:0, y:2, /蛇头位置x:0, y:l,x:0, y:0 /蛇尾位置;/显示蛇this.display = function() /蛇for (var i=0; i0; i-) this.bodyi.x = this.bodyi-1.x; this.bodyi.y = this.bodyi-1.y;)/根据方向处理蛇头switch(this.direction)(case left:this.body0
8、.x -= 1;break;case “right”:this.body0.x += 1;break;case “up”:this.body0.y -= 1;break;case down”:this.body0.y += 1; break;)/判断是否出界,出界就gameover,回到初态if (this.body0.x 79 | | this.body0.y 39) clearlnterval(timer);/删除旧地for (var i=0; ithis.body.length; i+) if (this.bodyi.flag != null) map.removeChild(this
9、bodyi.flag);)this.body = / 回到初状x:2, y:0,x:l, y:0,x:0j y:0;this.direction = 1 right1;this.display(); / 显示 return false; / 结束 )/判断蛇头吃到食物if (this.body0.x = food.x & this.body0.y = food.y) this.body.push(x:nully:null flag: null);map removechild(food flag); food.display();)for (var i=4; ithis.body.length
10、; i+) if (this.body。.x = this.bodyi.x & this.body0.y = this.bodyi.y) clearlnterval(timer);for (var i=0; ithis.body.length; i+) if (this.bodyi.flag != null) map.removeChild(this bodyi.flag);this.body =/位置x:2, y:0),x:l, y:0, x:0, y:0);this.direction = 1 right1;this.display();return false;)for (var i=0
11、; ithis.body.length; i+) if (this.bodyi.flag != null) map.removeChild(this.bodyi.flag);)/刷新显示 this.display();)构造食物 function Food() this.width = 20;this.height = 20;this.display = function() var f = document.createElement(iv*); this.flag = f;f. style.vjidth = this .width + 1 px1;f.style.height = this
12、.height + 1px1;f.style.background = Mrgb(n + Math.floor(Math.random()*256) + Math. floor (Math. random () *256) + “J +Math.floor(Math.random()*256) + );f.style.borderRadius = 50%;f.style.position = absolute;this.x = Math.floor(Math.random()*40);this.y = Math.floor(Math.random()*20);f.style.left = th
13、is.x * this.width + 1px1;f.style.top = this.y * this.height + 1px1; map.appendChild(f);) )var snake = new Snake();var food = new Food();snake.display();food, display。;/上下左右地方向键控制document.body.onkeydown = function(e) var ev = e | window.event;switch(ev.keyCode) ( case 38:if (snake.direction != down)
14、snake.direction = up;) break; case 40:if (snake.direction != up) snake.direction = down;) break; case 37:if (snake.direction != right) snake.direction = left”;)break;case 39:if (snake.direction != left) snake.direction = right;)break;);/点击开始游戏时启动var begin = document.getElementByld(1 begin*);var time
15、r;begin.onclick = function() clearlnterval(timer);timer = setInterval(snake.run()500);.game #chessposition: absolute;width: 40px;height: 48px;overflow: hidden;z-index:99;transition-property: all;).cylinderposition: relative;width: 70px;height: 101px;float: left;transform-origin:center bottom;).cylin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- HTML5应用开发课后习题题库期末考试试卷及答案 4 HTML5 应用 开发 课后 习题 题库 期末考试 试卷 答案
限制150内