当前位置:首页 > VUE

vue实现打砖块

2026-03-08 05:24:30VUE

Vue 实现打砖块游戏

游戏初始化

创建 Vue 项目并安装依赖,使用 canvas 绘制游戏元素。初始化游戏画布、小球、挡板和砖块数组。

<template>
  <canvas ref="gameCanvas" width="800" height="600"></canvas>
</template>

<script>
export default {
  mounted() {
    this.initGame();
  },
  methods: {
    initGame() {
      const canvas = this.$refs.gameCanvas;
      this.ctx = canvas.getContext('2d');
      this.setupGameElements();
      this.gameLoop();
    }
  }
};
</script>

游戏元素设置

定义小球、挡板和砖块的初始属性。小球包括位置、速度和半径;挡板包括位置、宽度和高度;砖块生成二维数组。

vue实现打砖块

setupGameElements() {
  this.ball = { x: 400, y: 300, dx: 4, dy: -4, radius: 10 };
  this.paddle = { x: 350, y: 550, width: 100, height: 15 };
  this.bricks = [];
  const brickRowCount = 5;
  const brickColumnCount = 10;
  for (let i = 0; i < brickRowCount; i++) {
    this.bricks[i] = [];
    for (let j = 0; j < brickColumnCount; j++) {
      this.bricks[i][j] = { x: 0, y: 0, status: 1 };
    }
  }
}

绘制游戏元素

canvas 上绘制小球、挡板和砖块。砖块状态为 1 时绘制,为 0 时消失。

drawBall() {
  this.ctx.beginPath();
  this.ctx.arc(this.ball.x, this.ball.y, this.ball.radius, 0, Math.PI * 2);
  this.ctx.fillStyle = '#0095DD';
  this.ctx.fill();
  this.ctx.closePath();
}

drawPaddle() {
  this.ctx.beginPath();
  this.ctx.rect(this.paddle.x, this.paddle.y, this.paddle.width, this.paddle.height);
  this.ctx.fillStyle = '#0095DD';
  this.ctx.fill();
  this.ctx.closePath();
}

drawBricks() {
  for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 10; j++) {
      if (this.bricks[i][j].status === 1) {
        const brickX = j * 80 + 30;
        const brickY = i * 30 + 50;
        this.bricks[i][j].x = brickX;
        this.bricks[i][j].y = brickY;
        this.ctx.beginPath();
        this.ctx.rect(brickX, brickY, 75, 20);
        this.ctx.fillStyle = '#0095DD';
        this.ctx.fill();
        this.ctx.closePath();
      }
    }
  }
}

游戏循环与碰撞检测

通过 requestAnimationFrame 实现游戏循环。检测小球与墙壁、挡板及砖块的碰撞,更新游戏状态。

vue实现打砖块

gameLoop() {
  this.ctx.clearRect(0, 0, 800, 600);
  this.drawBricks();
  this.drawBall();
  this.drawPaddle();
  this.collisionDetection();

  if (this.ball.x + this.ball.dx > 800 - this.ball.radius || this.ball.x + this.ball.dx < this.ball.radius) {
    this.ball.dx = -this.ball.dx;
  }
  if (this.ball.y + this.ball.dy < this.ball.radius) {
    this.ball.dy = -this.ball.dy;
  } else if (this.ball.y + this.ball.dy > 600 - this.ball.radius) {
    if (this.ball.x > this.paddle.x && this.ball.x < this.paddle.x + this.paddle.width) {
      this.ball.dy = -this.ball.dy;
    } else {
      alert('Game Over');
      document.location.reload();
    }
  }

  this.ball.x += this.ball.dx;
  this.ball.y += this.ball.dy;
  requestAnimationFrame(this.gameLoop);
}

collisionDetection() {
  for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 10; j++) {
      const brick = this.bricks[i][j];
      if (brick.status === 1) {
        if (
          this.ball.x > brick.x &&
          this.ball.x < brick.x + 75 &&
          this.ball.y > brick.y &&
          this.ball.y < brick.y + 20
        ) {
          this.ball.dy = -this.ball.dy;
          brick.status = 0;
        }
      }
    }
  }
}

挡板控制

监听键盘事件控制挡板移动,限制挡板不超出画布边界。

mounted() {
  this.initGame();
  document.addEventListener('keydown', this.keyDownHandler);
  document.addEventListener('keyup', this.keyUpHandler);
},
methods: {
  keyDownHandler(e) {
    if (e.key === 'Right' || e.key === 'ArrowRight') {
      this.rightPressed = true;
    } else if (e.key === 'Left' || e.key === 'ArrowLeft') {
      this.leftPressed = true;
    }
  },
  keyUpHandler(e) {
    if (e.key === 'Right' || e.key === 'ArrowRight') {
      this.rightPressed = false;
    } else if (e.key === 'Left' || e.key === 'ArrowLeft') {
      this.leftPressed = false;
    }
  }
}

游戏状态更新

在游戏循环中更新挡板位置,并检测游戏胜利条件。

gameLoop() {
  if (this.rightPressed && this.paddle.x < 800 - this.paddle.width) {
    this.paddle.x += 7;
  } else if (this.leftPressed && this.paddle.x > 0) {
    this.paddle.x -= 7;
  }

  let bricksLeft = 0;
  for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 10; j++) {
      if (this.bricks[i][j].status === 1) {
        bricksLeft++;
      }
    }
  }
  if (bricksLeft === 0) {
    alert('You Win!');
    document.location.reload();
  }
}

标签: 打砖块vue
分享给朋友:

相关文章

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现导航栏

vue实现导航栏

使用 Vue 实现导航栏 基础导航栏结构 在 Vue 中实现导航栏通常使用 <router-link> 或自定义组件。以下是一个基础示例: <template> <…

vue怎么实现滑动

vue怎么实现滑动

Vue实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的过渡系统和CSS可以实现平滑的滑动效果。定义一个CSS类,利用transf…