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

目录


一、案例效果

二、实现思路

  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

                                                                

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发