当前位置:首页 > 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中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…

vue实现回复

vue实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及前端表单交互、数据绑定和后端通信。以下是实现回复功能的详细步骤: 数据绑定与表单设计 使用 Vue 的 v-model 绑定回复框的输入内容,…