怎么把这个纯js代码改成使用Vue2实现,这是一个消除砖块的小游戏,我刚学Vue,不知道咋改,能不能麻烦各位会的帮我改一下。
html>
<html>
<head>
<meta charset="utf-8" />
<title>消除砖块小游戏title>
<style>
*{padding: 0;margin: 0;}
canvas{background: #eee;display: block;margin: 0;auto;}
style>
head>
<body>
<canvas id = "myCanvas" width="480" height="320">canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height - 30;
var dx = 2;
var dy = -2;
var ballRadius = 10;
//计算分数
var score = 0;
//定义一个球板来接球
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width - paddleWidth) / 2;
var rightPressed = false;
var leftPressed = false;
//定义砖变量
var brickRowCount = 3;
var brickColumnCount = 5;
var brickWidth = 75;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 30;
var bricks = [];
//存储其生命值
var lives = 3;
//定义二维数组容纳我们所有的砖
var bricks = [];
for (c = 0; c < brickColumnCount; c++) {
bricks[c] = [];
for (r = 0; r < brickRowCount; r++) {
bricks[c][r] = {
x: 0,
y: 0,
status: 1
};
}
}
//绘制小球
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
//绘制球拍
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
//创建一个函数来遍历数组中的所有砖块并在屏幕上绘制它们
function drawBricks() {
for (c = 0; c < brickColumnCount; c++) {
for (r = 0; r < brickRowCount; r++) {
if (bricks[c][r].status == 1) {
var brickX = (c * (brickWidth + brickPadding)) + brickOffsetLeft;
var brickY = (r * (brickHeight + brickPadding)) + brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
}
}
}
function draw() {
//小球移动逻辑
if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if (y + dy < ballRadius) {
dy = -dy;
} else if (y + dy > canvas.height - ballRadius) {
if (x > paddleX && x < paddleX + paddleWidth) {
dy = -dy;
} else {
lives--;
if (!lives) {
alert("你输了,游戏结束!");
document.location.reload();
//clearInterval(interval);
} else {
x = canvas.width / 2;
y = canvas.height - 30;
dx = 2;
dy = -2;
paddleX = (canvas.width - paddleWidth) / 2;
}
}
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawBricks();
drawPaddle();
drawScore();
drawLives();
collisionDetection();
x += dx;
y += dy;
//球拍移动逻辑
if (rightPressed && paddleX < canvas.width - paddleWidth) {
paddleX += 7;
} else if (leftPressed && paddleX > 0) {
paddleX -= 7;
}
//使 draw() 函数递归调用自身:
requestAnimationFrame(draw);
}
//添加两个监听器
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
//监听鼠标
document.addEventListener("mousemove", mouseMoveHandler, false);
function mouseMoveHandler(e) {
var relativeX = e.clientX - canvas.offsetLeft;
if (relativeX > 0 && relativeX < canvas.width) {
paddleX = relativeX - paddleWidth / 2;
}
}
function keyDownHandler(e) {
if (e.keyCode == 39) {
rightPressed = true;
} else if (e.keyCode == 37) {
leftPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 39) {
rightPressed = false;
} else if (e.keyCode == 37) {
leftPressed = false;
}
}
function collisionDetection() {
for (var c = 0; c < brickColumnCount; c++) {
for (var r = 0; r < brickRowCount; r++) {
var b = bricks[c][r];
if (b.status == 1) {
if (x > b.x && x < b.x + brickWidth && y > b.y && y < b.y + brickHeight) {
dy = -dy;
b.status = 0;
score++;
if (score == brickRowCount * brickColumnCount) {
alert("你赢了,恭喜你!");
document.location.reload();
}
}
}
}
}
}
//创建和更新分数显示
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("分数: " + score, 8, 20);
}
//绘制生命值计数
function drawLives() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("生命值: " + lives, canvas.width - 65, 20);
}
draw();
script>
body>
html>
创建一个vue项目,然后把HTML复制到templete,function放到methods,调用按照调用时机放到不同的钩子函数中。一些特殊获取改成vue的获取方式就可以了
你可能是想通过cdn的方式引入vue吧,这个可去官网学习怎么使用,楼上的方法不建议采纳。如果官网的示例看了还不会再提问
我下载了官网的vue.js ,通过
<script type="text/javascript" src="vue.js"></script>
引入,但后面不知道咋改
我觉得你还是稍微学学vue 吧 ,这样才会得心应手的。至少把前面基础的看了,到时候盖起来很容易了