前端案例:像素鸟小游戏(js+dom操作,完整代码,附案例素材)

举报
敬 之 发表于 2022/04/14 02:28:58 2022/04/14
【摘要】 目录 一、案例效果 二、实现思路 三、完整代码+详细注释 四、案例素材 一、案例效果 二、实现思路 创建游戏背景板和小鸟,并分别设置相对定位与绝对定位;初始化背景图的位置;初始化小鸟的位置;设置游戏状态,游戏开始时背景和管道全部向左运动,游戏结束全部停止运动;使小鸟飞行,其实就是背景图在 X 轴方向的位置不断减小,实...

目录

一、案例效果

二、实现思路

三、完整代码+详细注释

四、案例素材


一、案例效果

二、实现思路

  1. 创建游戏背景板和小鸟,并分别设置相对定位与绝对定位;
  2. 初始化背景图的位置;
  3. 初始化小鸟的位置;
  4. 设置游戏状态,游戏开始时背景和管道全部向左运动,游戏结束全部停止运动;
  5. 使小鸟飞行,其实就是背景图在 X 轴方向的位置不断减小,实现小鸟向右飞行效果;
  6. 设置点击事件,每点击一次小鸟在Y轴的位置减小,实现向上飞的效果;
  7. 创建管道,X 方向上管道和下管道位置相同,Y 方向上上管道和下管道高度随机,但中间要空出200px;
  8. 实现管道向左运动,与背景图向左操作类似,也是在 X 轴方向的位置不断减小;
  9. 管道向左运动移出游戏面板最左侧时再回到原位重新执行,实现循环效果;
  10. 定义上下管道的临界值,也就是上下管道自身区域;
  11. 小鸟位置与上下管道位置重合(相碰撞)时游戏结束;
  12. 多次调用管道创建函数,产生多组管道。

三、完整代码+详细注释


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>小游戏:像素鸟</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #game {
  12. width: 800px;
  13. height: 600px;
  14. background: url('./img/sky.png');
  15. position: relative;
  16. margin: auto;
  17. overflow: hidden;
  18. }
  19. #bird {
  20. width: 34px;
  21. height: 25px;
  22. background: url(./img/birds.png) -10px -8px no-repeat;
  23. position: absolute;
  24. top: 100px;
  25. left: 100px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <!-- 游戏背景 -->
  31. <div id="game">
  32. <!-- 小鸟 -->
  33. <div id="bird"></div>
  34. </div>
  35. </body>
  36. <script>
  37. //获取游戏背景和小鸟
  38. var game = document.getElementById('game');
  39. var birdEle = document.getElementById('bird');
  40. //初始化背景图
  41. var sky = {
  42. x: 0 //背景图初始位置为0
  43. }
  44. //初始化小鸟
  45. var bird = {
  46. speedX: 5, //小鸟在X轴的速度
  47. SpeedY: 0, //小鸟在Y轴的速度
  48. //小鸟坐标
  49. x: birdEle.offsetLeft, //小鸟初始位置在绝对定位的位置
  50. y: birdEle.offsetTop,
  51. }
  52. var runing = true; //游戏状态
  53. setInterval(function () {
  54. if (runing) {
  55. //小鸟飞行(其实是背景在动)
  56. sky.x -= 5; //背景每次-5px,以实现向左运动的效果
  57. game.style.backgroundPositionX = sky.x + 'px';
  58. //小鸟上下运动
  59. bird.SpeedY += 1; //每一次点击小鸟向上10px后开始自增也就是再自动向下
  60. bird.y += bird.SpeedY; //小鸟自动不断向下运动
  61. //判断游戏状态
  62. if (bird.y < 0) { //超出游戏背景顶部时游戏结束
  63. runing = false;
  64. bird.y = 0;
  65. }
  66. if (bird.y + birdEle.offsetHeight > 600) { //超出游戏背景底部时游戏结束
  67. runing = false;
  68. bird.y = 600 - birdEle.offsetHeight;
  69. }
  70. birdEle.style.top = bird.y + 'px';
  71. }
  72. }, 30);
  73. //点击时小鸟向上运动
  74. document.onclick = function () {
  75. bird.SpeedY = -10; //点击一次向上运动10px
  76. }
  77. //创建管道
  78. function creatPipe(position) {
  79. var pipe = {};
  80. pipe.x = position;
  81. pipe.upHeight = 200 + parseInt(Math.random() * 100); //上管道高度为200 - 300px
  82. pipe.doHeight = 600 - pipe.upHeight - 200; //下管道高度
  83. pipe.doTop = pipe.upHeight + 200; //上下两管道之间200px
  84. //创建上管道
  85. var upPipe = document.createElement('div'); //新建div
  86. upPipe.style.width = '52px';
  87. upPipe.style.height = pipe.upHeight + 'px';
  88. upPipe.style.background = 'url(./img/pipe2.png) no-repeat center bottom';
  89. upPipe.style.position = 'absolute';
  90. upPipe.style.top = '0px';
  91. upPipe.style.left = pipe.x + 'px';
  92. game.appendChild(upPipe); //将上管道追加到游戏页面中
  93. //创建下管道
  94. var doPipe = document.createElement('div'); //新建div
  95. doPipe.style.width = '52px';
  96. doPipe.style.height = pipe.doHeight + 'px';
  97. doPipe.style.background = 'url(./img/pipe1.png) no-repeat center top';
  98. doPipe.style.position = 'absolute';
  99. doPipe.style.top = pipe.doTop + 'px';
  100. doPipe.style.left = pipe.x + 'px';
  101. game.appendChild(doPipe); //将下管道追加到游戏页面中
  102. //管道进行运动
  103. setInterval(function () {
  104. if (runing) { //游戏处于运行状态时管道再运动
  105. pipe.x -= 2; //x方向不断-2px,以实现管道向左运动的效果
  106. upPipe.style.left = pipe.x + 'px';
  107. doPipe.style.left = pipe.x + 'px';
  108. if (pipe.x < -52) { //管道移出最左侧时回到原位,实现不间断效果
  109. pipe.x = 800;
  110. }
  111. //上下管道临界值
  112. var uCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y < pipe.upHeight;
  113. var dCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y > pipe.upHeight + 200;
  114. if (uCheck || dCheck) { //碰到上管道或下管道临界值则游戏终止
  115. runing = false;
  116. }
  117. }
  118. }, 30)
  119. }
  120. creatPipe(400); //产生四组管道
  121. creatPipe(600);
  122. creatPipe(800);
  123. creatPipe(1000);
  124. </script>
  125. </html>

四、案例素材

sky.png

birds.png

pipe1.png

pipe2.png

                                                                

文章来源: majinjian.blog.csdn.net,作者:Developer 小马,版权归原作者所有,如需转载,请联系作者。

原文链接:majinjian.blog.csdn.net/article/details/123620175

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。