javascript - 如何在 Javascript 中检查和更改砖块的状态?
问题描述
我制作破砖游戏。制作砖的行 = 3,列 = 5。
我想在行 [0] 中制作砖块(它们的状态 = 2)并改变它们的状态,如果它们与球碰撞则颜色。(如果他们的状态 = 2,改变他们的状态 = 1 & 颜色 = “灰色调”)
但我的代码无法正常工作。
我正在尝试修复它,但这并不容易。
这是我的代码:(省略变量声明和无关函数。)
function init(){
canvas = document.getElementById("myCanvas");
canvas.width = 500;
canvas.height = 480;
if (canvas == null || canvas.getContext == null) return;
ctx = canvas.getContext("2d");
ballColor = "black";
ballRadius = "30";
changeBallColor();
changeBallRadius();
x = canvas.width/2;
y = canvas.height-30;
dx = 3;
dy = -3;
paddleHeight = ballRadius;
paddleWidth = 300;
paddleX = (canvas.width-paddleWidth)/2;
rightPressed = false;
leftPressed = false;
brickRowCount = 3;
brickColumnCount = 5;
brickWidth = 80;
brickHeight = 40;
brickPadding = 13;
brickOffsetTop = 10;
brickOffsetLeft = 20;
bricks = [];
for(var c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(var r=0; r<brickRowCount; r++) {
bricks[c][r] = { x : 0, y : 0, status : 1 };
if(r==0) {
bricks[c][r] = { x: 0, y : 0, status : 2 };
}
}
}
s = 0; //score.
lives = 2;
}
function collisionDetection() {
for(var c=0; c<brickColumnCount; c++) {
for(var r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.status == 2) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 1;
}
}
else if(b.status == 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 0;
s++;
document.getElementById("score").innerHTML=s;
if (s == 15) {
level2();
}
if (s == 30) {
level3();
}
if (s == 45) {
gameClear();
}
}
}
}
}
}
function drawBricks() {
for(var c=0; c<brickColumnCount; c++) {
for(var r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.status == 2) {
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 = "red";
ctx.fill();
ctx.closePath();
}
else if(b.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 = "EAEAEA";
ctx.fill();
ctx.closePath();
}
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
drawBricks();
collisionDetection();
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("GAME OVER.");
document.location.reload();
}
else {
x = canvas.width/2;
y = canvas.height-30;
dx = 3;
dy = -3;
paddleX = (canvas.width-paddleWidth)/2;
}
}
}
if(rightPressed && paddleX < canvas.width-paddleWidth) {
paddleX += 5;
}
else if(leftPressed && paddleX > 0) {
paddleX -= 5;
}
x += dx;
y += dy;
}
ball = setInterval(draw, 10);
解决方案
推荐阅读
- sql-server - 删除触发器不起作用,而添加触发器正常工作?
- prawn - 如何使用 Prawn 中的 column_boxes 中断新页面插入
- postgresql - 从 PostgreSQL 中的不同格式中提取日期
- flutter - 带开关颤振的未来建设者
- javascript - 如何在 WooCommerce 单一产品页面中将增强型电子商务数据添加到数据层?
- java - IDEA导入项目后,yml文件无法识别
- bash - bwa 示例的 bash 循环中的多个变量
- javascript - ANULGAR => 尝试获取在 ngFor 中生成的模板值,以由 onlick 事件发送
- r - 如何在R中制作描述性统计表
- r - 使用 ggplot for mapsError in unit(x, default.units) 出现此错误:'x' 和 'units' 的长度必须 > 0