vue-2048小游戏

2048 小游戏前端开发

技术栈选取

vue3+vite+ts+tailwind

游戏规则

游戏界面如下:
在这里插入图片描述

​ 游戏界面由 4*4 的方格组成,每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块(2 或者 4),相同数字的方块在靠拢、相撞时会相加。不断的叠加最终拼凑出 2048 这个数字就算成功,如果格子满了不能再滑动了则失败。

设计思路

布局

分为三个部分,头部的 title,中间的游戏操作界面,底部的操作按钮(重新开始)以及得分。主要代码如下:

<div class="w-full h-full p-4 flex flex-col items-center">
  <header class="text-3xl">2048小游戏</header>
  <footer ref="target" class="m-4 w-[90%] max-w-[460px] h-[50%] max-h-[460px] p-[10px] bg-slate-300 flex flex-wrap justify-between items-center">
    <div
      v-for="item in arr"
      :key="item.index"
      :style="{ 'background-color': refreshColor(item.value) }"
      class="w-[23%] h-[23%] text-2xl flex items-center justify-center"
    >
      {{ item.value }}
    </div>
  </footer>
  <div>
    <button class="w-20 h-10 bg-orange-50" style="border: 1px solid red; border-radius: 5px" @click="init"> 重新开始 </button>
    当前得分:<span class="text-lg">{{ score }}</span>
  </div>
</div>

设计思路

  1. 4*4 的小正方形设计为一个数组,初始值设计为 0
  2. 空白的地方随机生成 2 或者 4:从数组中获取值为 0 的对象生成一个新的数组,从中随机选择一个,赋予值 2 或者 4。
  3. 监听操作:监听键盘的上下左右按钮;监听页面的滑动。
  4. 核心:数值的和并,把大数组分成 4 个小数组,每个小数组先把 0 去除,根据操作方向(上下左右),去合并数值,最后把数组补充 0(恢复成原本长度)。
  5. 判断输赢:赢,如果数组出现 2048 则胜利;输:格子满了,且上下左右没有可以合并的值。
for (let i = 0; i < 4; i++) {
  // 分解大数组为四个小数组,同时把值为0的去掉
  let itemArr: Array<number> = [arr.value[i * 4].value, arr.value[i * 4 + 1].value, arr.value[i * 4 + 2].value, arr.value[i * 4 + 3].value].filter(
    (item: number) => item != 0
  )
  const length = itemArr.length
  if (length > 1) {
    if (direction == 'right') {
      for (let j = length; j > 1; j--) {
        if (!itemArr[j - 1]) {
          continue
        }
        // 判断值是否相等,相等则合并
        if (itemArr[j - 1] == itemArr[j - 2]) {
          itemArr[j - 1] = 2 * itemArr[j - 1]
          score.value += itemArr[j - 1]
          if (itemArr[j - 1] === 2048) isWin = true
          itemArr[j - 2] = 0
        }
      }
    } else {
      for (let j = 0; j < length - 1; j++) {
        if (!itemArr[j]) {
          continue
        }
        if (itemArr[j] == itemArr[j + 1]) {
          itemArr[j] = 2 * itemArr[j]
          score.value += itemArr[j]
          if (itemArr[j] === 2048) isWin = true
          itemArr[j + 1] = 0
        }
      }
    }
  }
  itemArr = itemArr.filter((item: number) => item != 0)
  const finalLength = itemArr.length
  if (finalLength != 4) {
    for (let k = 0; k < 4 - finalLength; k++) {
      if (direction == 'right') {
        itemArr.unshift(0)
      } else {
        itemArr.push(0)
      }
    }
  }
  arr.value[i * 4].value = itemArr[0]
  arr.value[i * 4 + 1].value = itemArr[1]
  arr.value[i * 4 + 2].value = itemArr[2]
  arr.value[i * 4 + 3].value = itemArr[3]
}

源码地址

源码地址

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个用TypeScript和Vue 3编写的简单游戏页面的示例: 首先,确保你已经安装了Vue CLI,并创建了一个Vue项目。然后,在你的项目中创建一个名为Game.vue的文件,用于编写游戏页面的逻辑和界面。 Game.vue文件的内容如下: ```typescript <template> <div> <h1>{{ message }}</h1> <button @click="startGame">Start Game</button> <div v-if="gameStarted"> <!-- 游戏界面的内容 --> </div> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ data() { return { message: 'Welcome to the Game', gameStarted: false, }; }, methods: { startGame() { this.gameStarted = true; // 在这里编写游戏开始时的逻辑 }, }, }); </script> ``` 这只是一个简单的示例,你可以根据你的实际需求进行扩展和修改。在这个示例中,我们使用了Vue 3的Composition API编写了一个组件。 你可以在data方法中定义组件的数据,这里我们定义了一个message和gameStarted变量。message用于显示欢迎信息,gameStarted用于控制游戏是否已经开始。 在methods中,我们定义了一个startGame方法,当点击"Start Game"按钮时,会将gameStarted设置为true,然后可以在该方法中编写游戏开始时的逻辑。 请注意,这只是一个简单的示例,你可能还需要添加其他的组件、样式和逻辑来完善你的游戏页面。希望这能帮到你!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值