• 复刻一个羊了个羊掘金商城版


    游戏逻辑

    羊了个羊逻辑一致,不再赘述

    游戏实现

    • 盛放元素的容器box,临时存储的容器temp,多余元素的容器source与source1,结果元素result
    1. <div id="box"></div>
    2. <div id="temp"></div>
    3. <div id="source"></div>
    4. <div id="source1"></div>
    5. <div class="result">
    6. <div>游戏结束,别灰心,你能行的!</div>
    7. <div class="restart">重新开始</div>
    8. </div>
    • 样式基于定位和flex布局
    1. <style>
    2. * {
    3. margin: 0;
    4. padding: 0;
    5. list-style: none;
    6. box-sizing: border-box;
    7. }
    8. html,
    9. body {
    10. width: 100%;
    11. height: 100%;
    12. overflow: auto;
    13. background-image: url('https://pic.qy566.com/snake/bg.jpeg');
    14. background-size: cover;
    15. }
    16. #box{
    17. width: 600px;
    18. height: 600px;
    19. position: absolute;
    20. top: 0;
    21. left: 0;
    22. right: 0;
    23. bottom: 0;
    24. margin: auto;
    25. }
    26. #temp{
    27. width: 840px;
    28. height: 120px;
    29. position: absolute;
    30. left: 0;
    31. right: 0;
    32. margin: auto;
    33. bottom: 0;
    34. background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.92sucai.com%2Fimage%2F20180626%2F1529979993472449.jpg&refer=http%3A%2F%2Fup.92sucai.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665795593&t=0c74935a1fa75d5861903e8249c15bbb');
    35. background-size: contain;
    36. border-radius: 16px;
    37. display: flex;
    38. }
    39. .item{
    40. width: 120px;
    41. height: 120px;
    42. border-radius: 16px;
    43. background-size: contain;
    44. background-position: center;
    45. background-repeat: no-repeat;
    46. border: 1px solid #333;
    47. cursor: pointer;
    48. }
    49. .shadow{
    50. box-shadow: 0 0 50px 10px #333 inset;
    51. }
    52. #source{
    53. width: 260px;
    54. height: 120px;
    55. position: absolute;
    56. top: 0;
    57. left: 0;
    58. }
    59. #source1{
    60. width: 260px;
    61. height: 120px;
    62. position: absolute;
    63. top: 0;
    64. right: 0;
    65. }
    66. .result{
    67. width: 200px;
    68. height: 120px;
    69. border: 4px dashed cyan;
    70. position: absolute;
    71. top: 0;
    72. left: 0;
    73. right: 0;
    74. bottom: 0;
    75. margin: auto;
    76. background-color: #fff;
    77. padding: 10px;
    78. display: flex;
    79. flex-direction: column;
    80. justify-content: space-around;
    81. align-items: center;
    82. display: none;
    83. }
    84. .restart{
    85. width: 120px;
    86. height: 40px;
    87. line-height: 40px;
    88. border: 1px solid #333;
    89. text-align: center;
    90. cursor: pointer;
    91. margin: auto;
    92. }
    93. style>

    生成元素的逻辑

    • 首先会有一个维护好的图片的数组imgSrc

     

    • 生成12*14的随机乱序待消除元素
    1. // 随机乱序
    2. function randomArr (array) {
    3. const arr = [].concat(array)
    4. for (let i = arr.length - 1; i >= 0; i--) {
    5. const inx = Math.floor(Math.random() * i + 1)
    6. const next = arr[inx]
    7. arr[inx] = arr[i]
    8. arr[i] = next
    9. }
    10. return arr
    11. }
    12. // 生成 12*14个待消除元素
    13. for (let i =0;i<12;i++) {
    14. imgSrc.map((v,m)=>{
    15. console.log(m)
    16. source.push({src: v, inx: m})
    17. })
    18. }
    • 分为5层,进行排列,其中层数从5至0递减,然后内部双层循环进行5*层数递减页面布局,元素依次由source.slice(0)来得到,维护图片索引 inx
    1. for(let k =5;k>0;k--) {
    2. for (let i=0;i<5;i++) {
    3. for (let j=0;j<k;j++) {
    4. let div = $('
      ')
    5. div.addClass('item')
    6. div.addClass(`${i}-${j}-${k}`)
    7. div.attr('x', i)
    8. div.attr('y', j)
    9. div.attr('z', k)
    10. div.css('position', 'absolute')
    11. let img = source.splice(0,1)
    12. div.css('left', 120*j-Math.random()*10*k+'px')
    13. div.css('top', 120*i-Math.random()*10*k+'px')
    14. div.attr('inx', img[0].inx)
    15. div.css('backgroundImage', `url(${img[0].src})`)
    16. ....
    17. }
    18. }
    19. }
    • 判断是否被覆盖,被覆盖时添加shadow类
    1. $('.item').each((i,v) => {
    2. let x = $(v).attr('x')
    3. let y = $(v).attr('y')
    4. let z = $(v).attr('z')
    5. let ele = $(`.${x}-${y}-${z-1}`)
    6. let ele1 = $(`.${x+1}-${y+1}-${z-1}`)
    7. if (ele.length || ele1.length) {
    8. $(v).addClass('shadow')
    9. }
    10. })
    • 添加点击事件,通过detach方式向temp中转移
    • 判断是否包含shadow类
    • 判断是否存在,维护temp对象
    • 删除后更新shadow覆盖状态
    • 判断是否大于7触发结束条件
    1. div.click(function () {
    2. if(!$(this).hasClass('shadow')) {
    3. if (temp[$(this).attr('inx')]) {
    4. temp[$(this).attr('inx')] += 1
    5. } else {
    6. temp[$(this).attr('inx')] = 1
    7. }
    8. let e = $(this).detach()
    9. e.css('position', 'unset')
    10. $('#temp').append(e)
    11. $('.item').each((i,v) => {
    12. $(v).removeClass('shadow')
    13. let x = $(v).attr('x')
    14. let y = $(v).attr('y')
    15. let z = $(v).attr('z')
    16. let ele = $(`.${x}-${y}-${z-1}`)
    17. let ele1 = $(`.${x+1}-${y+1}-${z-1}`)
    18. if (ele.length || ele1.length) {
    19. $(v).addClass('shadow')
    20. }
    21. })
    22. if (temp[$(this).attr('inx')]===3) {
    23. $(`#temp div[inx=${$(this).attr('inx')}]`).remove()
    24. temp[$(this).attr('inx')] = 0
    25. }
    26. let num = 0
    27. for (let i in temp) {
    28. num+=temp[i]
    29. }
    30. if (num>=7) {
    31. $('.item').off('click')
    32. $('.result').fadeIn()
    33. }
    34. }
    35. })
    • 将剩余的待消除元素均分至source和source1
    1. let len = Math.ceil(source.length /2)
    2. source.forEach((v,i) => {
    3. let div = $('
      ')
    4. div.addClass('item')
    5. div.attr('inx', v.inx)
    6. div.css('backgroundImage', `url(${v.src})`)
    7. div.css('position', 'absolute')
    8. div.css('top', 0)
    9. if (i>len) {
    10. div.css('right', `${5*(i-len)}px`)
    11. $('#source1').append(div)
    12. } else {
    13. div.css('left', `${5*i}px`)
    14. $('#source').append(div)
    15. }
    16. ...
    • 同样添加点击事件处理

    • 初始化处理清空容器的相关逻辑

    1. $('#box').empty()
    2. $('#temp').empty()
    3. $('#source').empty()
    4. $('#source1').empty()
    5. $('.result').fadeOut()

     

  • 相关阅读:
    网络安全考研院校推荐
    合肥工业大学人工智能原理设计报告
    基于DJYOS的图形界面编程--DJYGUI系列教程
    【鸿蒙学习笔记】鸿蒙ArkTS学习笔记
    【uboot】uboot添加自定义命令
    jquary
    LeetCode //C - 114. Flatten Binary Tree to Linked List
    DP(动态规划)【2】 最大连续子列和 最长不降子序列
    第k小的数
    【车载开发系列】自动驾驶技术--HUD技术
  • 原文地址:https://blog.csdn.net/xhbzl/article/details/126893747