当前位置:   article > 正文

【小5聊】前端开发经典游戏俄罗斯方块(前端开发领域)_俄罗斯方块前端项目

俄罗斯方块前端项目

一年一度的博客之星来了,在此通过前端开发方式实现经典游戏俄罗斯方块

俄罗斯方块游戏,大家都玩过,今天就用自己熟悉的jQuery来实现TA

可提供完整源码,如果小伙伴们需要的话(步骤里其实也是完整的源码了)

一、功能分析

1、设置一个高宽度范围

长=20格,宽=10格(标准设置)

2、方块类型

  • 七种类型

O形、I形、S形、Z形、L形、J形、T形 

不管是什么形状,都是由4个小格子拼接而成

  • O形

一种旋转形态

  • I形、S形、Z形

两种形态,就是两种旋转类型

  • L形、J形、T形

四种形态,就是四个旋转方向,上右下左

3、控制方块方向

可以使用键盘的左右下来控制方块的移动方向

4、定时器

通过定时器控制方块向下移动,可以先设置每一秒种向下移动一个单位,比如设置好的30px一个移动单位

5、边界判断

主要有三个边界判断

1)到达底部边界判断

2)到达左右两边边界判断

3)到达某一方块上方边界判断

6、方块旋转

通过left和top移动标志,保持left和top值不变,根据每个形态的每个小方块进行公式计算即可

7、方块消除分析

每个格子都是独立,移动到一定位置后,如果10个小方块都是同在一行,那么就直接对当前行的小方块进行移除即可

二、实现流程

1、页面范围设计

长宽2:1,div高宽设计为600px:300px

  • 代码
  1. <div style="background:#099dff;position:absolute;top:150px;left:50%;height:600px;width:300px;box-shadow:0 0 13px #ccc;margin-left:-150px;">
  2. <!--游戏面板-->
  3. <div class="playDiv" style="background: #099dff; position: relative; height: 600px; width: 300px;">
  4. </div>
  5. <!--开始按钮-->
  6. <div class="btnStartDiv" style="position:absolute;top:-45px;left:0px;width:60px;height:35px;line-height:35px;text-align:center;background:#099dff;color:#fff;cursor:pointer;">
  7. <span>开始</span>
  8. </div>
  9. <!--测试-->
  10. <div class="testDiv" style="position:absolute;top:0px;right:-150px;">
  11. <div class="one" style="height:35px;line-height:35px;"></div>
  12. <div class="two" style="height:35px;line-height:35px;"></div>
  13. <div class="three" style="height:35px;line-height:35px;"></div>
  14. </div>
  15. </div>
  16. <script type="text/template" id="shapeTemplate">
  17. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  18. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  19. </div>
  20. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  21. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  22. </div>
  23. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  24. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  25. </div>
  26. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  27. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  28. </div>
  29. </script>

 2、方块形状设计

上一步骤设计了宽度为300px,那么一个小格子就是30px,并且每个形状都有四个方向的

1)O形

 2)I形

 3)S形

4)Z形

5)L形

 6)J形

7)T形

 

3、全局参数设置

1)方块模板

模板里有4个小方块,然后根据随机以及公式对4个方块进行absolute相对定位,组成一个方块形状以及方块方向

  1. <script type="text/template" id="shapeTemplate">
  2. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  3. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  4. </div>
  5. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  6. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  7. </div>
  8. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  9. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  10. </div>
  11. <div class="shapeDiv shapeMoveDiv" style="width: 30px;height: 30px;background: #3e3cd0;position:absolute;top:0px;left:0px;">
  12. <div style="width:100%;height:100%;box-sizing:border-box;border:2px solid #000;"></div>
  13. </div>
  14. </script>

2)全局变量如下

  1. var shapeUnitPx = 30; //格子单元像素
  2. var leftMaxValue = 30 * 10; //300px,小方块最大的移动宽度
  3. var topMaxValue = 30 * 20; //600px,小方块最大的移动高度
  4. var leftArr = [0, 0, 0, 0]; //单元格的left值(每个方块都有4个小方块)
  5. var topArr = [0, 0, 0, 0]; //单元格的top值(每个方块都有4个小方块)
  6. var nextIndex = 0; //下一个切换方向下标
  7. var currentIndex = 0; //当前小方块下标
  8. var moveLeft = 0; //方块移动的left倍数
  9. var moveTop = 0; //方块移动的top倍数
  10. var htmlTemplate = $("#shapeTemplate").html(); //单元格模板
  11. moveLeft = 5; //每个小方块的left值乘以5
  12. moveTop = -2; //每个小方块的top值乘以-2

4、开始游戏按钮

1)点击开始游戏,默认方块的初始位置为left值的5倍,top的-2倍

2)随机从19种方块里获得1个方块

3)将随机得到的方块下标保存到全局当前方块下标里

4)调用创建方块方法或者移动方块方法

5)同时开启定时器,设置初始状态每隔1秒向下移动一个单元

  1. //===开始===
  2. $(".btnStartDiv").click(function () {
  3. moveLeft = 5;
  4. moveTop = -2;
  5. var shapeIndex = Math.ceil(Math.random() * 19); //1~19随机出现数字
  6. currentIndex = shapeIndex;
  7. createShapeOrChangeHtml(shapeIndex);
  8. moveTime = setInterval(autoMoveShape, 1 * 1000); //开启定时器
  9. });

5、创建方块或移动方块

1)根据方块下标,定位到不同的小方块的left和top值计算公式里

2)设置好方块的4个小方块的left和top值后,即可追加方块到游戏面板里

3)或者修改方块在面板的移动位置

  1. //===生成方块形状===
  2. function createShapeOrChangeHtml(shapeIndex) {
  3. //方块形状格子叠加原则:从下到上,从左到右
  4. //这个规则的目的:为消除方块作为基础条件
  5. //公式创建
  6. if (shapeIndex == 1) { //O
  7. nextIndex = 1;
  8. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  9. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  10. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  11. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  12. topArr[3] = (moveTop - 1) * shapeUnitPx;
  13. topArr[2] = (moveTop - 1) * shapeUnitPx;
  14. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  15. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  16. }
  17. else if (shapeIndex == 2) { //I - 竖线
  18. nextIndex = 3;
  19. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  20. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  21. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  22. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  23. topArr[3] = (moveTop - 1) * shapeUnitPx;
  24. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  25. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  26. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 3;
  27. }
  28. else if (shapeIndex == 3) { //I - 横线
  29. nextIndex = 2;
  30. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  31. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  32. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  33. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 3;
  34. topArr[3] = (moveTop - 1) * shapeUnitPx;
  35. topArr[2] = (moveTop - 1) * shapeUnitPx;
  36. topArr[1] = (moveTop - 1) * shapeUnitPx;
  37. topArr[0] = (moveTop - 1) * shapeUnitPx;
  38. }
  39. else if (shapeIndex == 4) { //S形 - 向右
  40. /*
  41. 0 30 60
  42. 0 3 4
  43. 30 1 2
  44. 60
  45. */
  46. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  47. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  48. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  49. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  50. topArr[3] = (moveTop - 1) * shapeUnitPx;
  51. topArr[2] = (moveTop - 1) * shapeUnitPx;
  52. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  53. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  54. nextIndex = 5;
  55. }
  56. else if (shapeIndex == 5) { //S形 - 向下
  57. /*
  58. 0 30 60
  59. 0 4
  60. 30 2 3
  61. 60 1
  62. */
  63. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  64. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  65. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  66. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  67. topArr[3] = (moveTop - 1) * shapeUnitPx;
  68. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  69. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  70. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  71. nextIndex = 4;
  72. }
  73. else if (shapeIndex == 6) { //Z形 - 向左
  74. /*
  75. 34
  76. 12
  77. */
  78. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  79. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  80. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  81. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  82. topArr[3] = (moveTop - 1) * shapeUnitPx;
  83. topArr[2] = (moveTop - 1) * shapeUnitPx;
  84. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  85. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  86. nextIndex = 7;
  87. }
  88. else if (shapeIndex == 7) { //Z形 - 向上
  89. /*
  90. 4
  91. 23
  92. 1
  93. */
  94. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  95. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  96. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  97. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  98. topArr[3] = (moveTop - 1) * shapeUnitPx;
  99. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  100. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  101. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  102. nextIndex = 6;
  103. }
  104. else if (shapeIndex == 8) { //L形 - 向上
  105. /*
  106. 0 30 60
  107. 0 4
  108. 30 3
  109. 60 1 2
  110. */
  111. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  112. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  113. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  114. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  115. topArr[3] = (moveTop - 1) * shapeUnitPx;
  116. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  117. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  118. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  119. nextIndex = 9;
  120. }
  121. else if (shapeIndex == 9) { //L形 - 向右
  122. /*
  123. 0 30 60
  124. 0 1 2 3
  125. 30 4
  126. 60
  127. */
  128. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  129. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  130. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  131. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  132. topArr[3] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  133. topArr[2] = (moveTop - 1) * shapeUnitPx;
  134. topArr[1] = (moveTop - 1) * shapeUnitPx;
  135. topArr[0] = (moveTop - 1) * shapeUnitPx;
  136. nextIndex = 10;
  137. }
  138. else if (shapeIndex == 10) { //L形 - 向下
  139. /*
  140. 0 30 60
  141. 0 3 4
  142. 30 2
  143. 60 1
  144. */
  145. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  146. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  147. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  148. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  149. topArr[3] = (moveTop - 1) * shapeUnitPx;
  150. topArr[2] = (moveTop - 1) * shapeUnitPx;
  151. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  152. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  153. nextIndex = 11;
  154. }
  155. else if (shapeIndex == 11) { //L形 - 向左
  156. /*
  157. 0 30 60
  158. 0 4
  159. 30 1 2 3
  160. 60
  161. */
  162. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  163. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  164. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  165. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  166. topArr[3] = (moveTop - 1) * shapeUnitPx;
  167. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  168. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  169. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  170. nextIndex = 8;
  171. }
  172. else if (shapeIndex == 12) { //J形 - 向上
  173. /*
  174. 4
  175. 3
  176. 12
  177. */
  178. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  179. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  180. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  181. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  182. topArr[3] = (moveTop - 1) * shapeUnitPx;
  183. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  184. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  185. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  186. nextIndex = 13;
  187. }
  188. else if (shapeIndex == 13) { //J形 - 向右
  189. /*
  190. 0 30 60
  191. 0 4
  192. 30 1 2 3
  193. 60
  194. */
  195. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  196. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  197. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  198. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  199. topArr[3] = (moveTop - 1) * shapeUnitPx;
  200. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  201. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  202. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  203. nextIndex = 14;
  204. }
  205. else if (shapeIndex == 14) { //J形 - 向下
  206. /*
  207. 0 30 60
  208. 0 3 4
  209. 30 2
  210. 60 1
  211. */
  212. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  213. leftArr[2] = (moveLeft - 1) * shapeUnitPx;
  214. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  215. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  216. topArr[3] = (moveTop - 1) * shapeUnitPx;
  217. topArr[2] = (moveTop - 1) * shapeUnitPx;
  218. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  219. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  220. nextIndex = 15;
  221. }
  222. else if (shapeIndex == 15) { //J形 - 向左
  223. /*
  224. 0 30 60
  225. 0 2 3 4
  226. 30 1
  227. 60
  228. */
  229. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  230. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  231. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  232. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  233. topArr[3] = (moveTop - 1) * shapeUnitPx;
  234. topArr[2] = (moveTop - 1) * shapeUnitPx;
  235. topArr[1] = (moveTop - 1) * shapeUnitPx;
  236. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  237. nextIndex = 12;
  238. }
  239. else if (shapeIndex == 16) { //T形 - 向下
  240. /*
  241. 0 30 60
  242. 0 2 3 4
  243. 30 1
  244. 60
  245. */
  246. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  247. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  248. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  249. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  250. topArr[3] = (moveTop - 1) * shapeUnitPx;
  251. topArr[2] = (moveTop - 1) * shapeUnitPx;
  252. topArr[1] = (moveTop - 1) * shapeUnitPx;
  253. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  254. nextIndex = 17;
  255. }
  256. else if (shapeIndex == 17) { //T形 - 向左
  257. /*
  258. 0 30 60
  259. 0 4
  260. 30 2 3
  261. 60 1
  262. */
  263. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  264. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  265. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  266. leftArr[0] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  267. topArr[3] = (moveTop - 1) * shapeUnitPx;
  268. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  269. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  270. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  271. nextIndex = 18;
  272. }
  273. else if (shapeIndex == 18) { //T形 - 向上
  274. /*
  275. 0 30 60
  276. 0 4
  277. 30 1 2 3
  278. 60
  279. */
  280. leftArr[3] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  281. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx * 2;
  282. leftArr[1] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  283. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  284. topArr[3] = (moveTop - 1) * shapeUnitPx;
  285. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  286. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  287. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  288. nextIndex = 19;
  289. }
  290. else if (shapeIndex == 19) { //T形 - 向右
  291. /*
  292. 0 30
  293. 0 4
  294. 30 2 3
  295. 60 1
  296. */
  297. leftArr[3] = (moveLeft - 1) * shapeUnitPx;
  298. leftArr[2] = (moveLeft - 1) * shapeUnitPx + shapeUnitPx;
  299. leftArr[1] = (moveLeft - 1) * shapeUnitPx;
  300. leftArr[0] = (moveLeft - 1) * shapeUnitPx;
  301. topArr[3] = (moveTop - 1) * shapeUnitPx;
  302. topArr[2] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  303. topArr[1] = (moveTop - 1) * shapeUnitPx + shapeUnitPx;
  304. topArr[0] = (moveTop - 1) * shapeUnitPx + shapeUnitPx * 2;
  305. nextIndex = 16;
  306. }
  307. //===方块是否可移动判断===
  308. IsMove(function () {
  309. if ($(".shapeMoveDiv").html() == undefined) { //下一个方法(新增方块到游戏面板)
  310. var html = '';
  311. for (var i = 0; i < leftArr.length; i++) {
  312. var html1 = htmlTemplate;
  313. html += $(html1).css({ "left": leftArr[i] + "px", "top": topArr[i] + "px" }).prop('outerHTML');
  314. }
  315. $(".playDiv").append(html);
  316. }
  317. else { //移动当前方块
  318. for (var i = 0; i < leftArr.length; i++) {
  319. $(".shapeMoveDiv").eq(i).css({ "left": leftArr[i] + "px", "top": topArr[i] + "px" });
  320. }
  321. }
  322. tempDisplay();
  323. });
  324. //===/方块是否可移动判断===
  325. }

6、方块是否可移动判断

1)设置一个全局变量,保存已经在游戏面板占据空间的小方块的坐标

2)首先对方块进行右边界的判断,如果方块有一个小方块的left值大于等于了游戏面板的最大宽度,那么就停止向右边移动

3)到达底部判断,如果方块有一个小方块的top值超过了游戏面板的最大宽度,那么就停止向下移动,并移除当前方块移动标志的class值

4)如果游戏面板已经有方块存在,那么根据前面已经保存好的方块坐标来判断,如果存在相同坐标,那么停止方块移动,并将当前方块的坐标保存到全局变量里

5)如果通过上面步骤都不满足,那么就是可以移动,否则就进行下一步操作

6)如果方块触发了停止,那么就停止定时器向下移动方法

7)如果当前停止移动的方块的top值有出现负数,那么就结束游戏

  1. //===判断是否可以移动===
  2. var havePositionArr = [];
  3. function IsMove(call) {
  4. var flag = 1;
  5. //判断右边边界
  6. for (var i = 0; i < leftArr.length; i++) {
  7. var _v = leftArr[i];
  8. if (_v >= leftMaxValue) {
  9. flag = 0;
  10. moveLeft -= 1;
  11. }
  12. }
  13. if (!flag) {
  14. return;
  15. }
  16. //到达了底部,不可再移动
  17. for (var i = 0; i < leftArr.length; i++) {
  18. var _v = topArr[i];
  19. if (_v >= topMaxValue) {
  20. flag = 0;
  21. }
  22. }
  23. //再判断是否存在的单元格
  24. if (havePositionArr.length > 0) {
  25. for (var i = 0; i < leftArr.length; i++) {
  26. var currentPosition = leftArr[i] + "," + topArr[i];
  27. for (var j = 0; j < havePositionArr.length; j++) {
  28. var havaPosition = havePositionArr[j];
  29. if (currentPosition == havaPosition) {
  30. flag = 0;
  31. }
  32. }
  33. }
  34. }
  35. //判断
  36. if (flag) { //可以移动方块
  37. call();
  38. }
  39. else { //停止移动方块
  40. //===先停止定时器===
  41. clearInterval(moveTime);
  42. //===移除移动标志class===
  43. var isFinal = 0;
  44. for (var i = 0; i < $(".shapeMoveDiv").length; i++) {
  45. havePositionArr.push($(".shapeMoveDiv").eq(i).css("left").replace("px", "") + "," + $(".shapeMoveDiv").eq(i).css("top").replace("px", ""));
  46. if (parseInt($(".shapeMoveDiv").eq(i).css("top").replace("px", "")) < 0) {
  47. isFinal = 1;
  48. }
  49. }
  50. $(".shapeMoveDiv").removeClass("shapeMoveDiv");
  51. if (isFinal) {
  52. alert("游戏结束!");
  53. return;
  54. }
  55. //===/移除移动标志class===
  56. //===再检查是否有可移除的方块==
  57. checkRemoveFun();
  58. }
  59. }

7、检查是否有方块可消除

判断的原则

1)通过每个小方块的top值为分组,如果top值相同,且达到10个方块,那么说明是同一行,即可移除方块

2)移除方块后,再根据移除后的方块的top值来判断,小于这个top的上面的所有小方块就需要增加top值往下叠加

3)最后就是触发点击开始按钮,即可下一个方块

  1. //===分段判断===
  2. function checkRemoveFun() {
  3. //检查是否可以消除了
  4. var length = $(".shapeDiv").length;
  5. var tempArr = [];
  6. var flag = '';
  7. var currentTop = '';
  8. for (var n = 0; n < length; n++) {
  9. currentTop = $(".shapeDiv").eq(n).css("top");
  10. if (tempArr.length <= 0) {
  11. flag += currentTop + "|";
  12. tempArr.push({ key: currentTop, value: 1 });
  13. }
  14. else {
  15. if (flag.indexOf(currentTop) < 0) {
  16. flag += currentTop + "|";
  17. tempArr.push({ key: currentTop, value: 1 });
  18. }
  19. else {
  20. for (var i = 0; i < tempArr.length; i++) {
  21. if (tempArr[i]['key'] == currentTop) {
  22. var _vvv = parseInt(tempArr[i]['value']) + 1;
  23. tempArr[i]['value'] = _vvv;
  24. }
  25. }
  26. }
  27. }
  28. }
  29. //循环移除10个单元格
  30. var removeTop = '';
  31. var removeRow = 0;
  32. for (var i = 0; i < tempArr.length; i++) {
  33. if (parseInt(tempArr[i]['value']) >= 10) {
  34. removeTop = tempArr[i]['key'];
  35. //先消除完再移动
  36. for (var c = 0; c < 10; c++) {
  37. for (var n = 0; n < length; n++) {
  38. currentTop = $(".shapeDiv").eq(n).css("top");
  39. if (currentTop == removeTop) {
  40. $(".shapeDiv").eq(n).remove();
  41. }
  42. }
  43. }
  44. removeRow += 1;
  45. }
  46. }
  47. //上一层的top值向下
  48. for (var n = 0; n < $(".shapeDiv").length; n++) {
  49. currentTop = $(".shapeDiv").eq(n).css("top");
  50. if (parseInt(currentTop.replace("px", "")) < parseInt(removeTop.replace("px", ""))) {
  51. $(".shapeDiv").eq(n).css("top", (parseInt(currentTop.replace("px", "")) + shapeUnitPx * removeRow) + "px");
  52. }
  53. }
  54. //再循环保存现有的单元格
  55. if (removeTop != '') {
  56. havePositionArr = [];
  57. for (var i = 0; i < $(".shapeDiv").length; i++) {
  58. havePositionArr.push($(".shapeDiv").eq(i).css("left").replace("px", "") + "," + $(".shapeDiv").eq(i).css("top").replace("px", ""));
  59. }
  60. }
  61. $(".btnStartDiv").trigger('click'); //触发开始,下一个方块
  62. }

8、到此游戏基础逻辑完成

1)方块随机出现

2)定时器自动将方块往下移动

3)边界判断

4)方块停止判断

5)方块消除判断

9、后续可在基础逻辑上发挥了

1)消除后,增加分数

2)方块动态变化颜色

3)加上消除方块的音效

4)加上背景音乐

5)更换背景皮肤等等可任意发挥的功能

总结

这里提供的是一个开发思路和方向,方法和算法不一定是最佳的,我相信大家通过自己的方式也能快速开发出来

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/862208
推荐阅读
相关标签
  

闽ICP备14008679号