碰撞检测2D游戏

时间:2015-10-28 13:08:17

标签: javascript collision-detection

我正在关注Mozilla 2D游戏教程。这不是我的原始代码,也不是我最初的想法。链接如下。

Mozilla Game

我已经陷入了游戏的一个特定方面:用砖碰撞检测球。程序运行正常,直到我调用了collisionDetection()函数。

这是功能:

function collisionDetection() {
    for(c=0; c<brickColumnCount; c++) {
        for(r=0; r<brickRowCount; r++) {
            var b = bricks[c][r];
            if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
                dy = -dy;
            }
        }
    }
}

在调用此功能之后,球在画布周围飞行,游戏机制完美无缺。但是,在将此调用添加到我的代码中时,球在其起点处保持不动,完全没有移动。

很明显,collisionDetection()函数存在问题,但这对我来说都是正确的!

我还搜索了球到墙和球到球碰撞检测'if statements'的问题,但一切似乎都是正确的。

整个代码如下。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Gamedev Canvas Workshop</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");

// Circle variables
var radiusCircle = 10;
var xCircle = canvas.width/2;
var yCircle = canvas.height - radiusCircle;
var dx = 2;
var dy = -2;

// Keyboard movement variables
var keyLeft = false;
var keyRight = false;

// Paddle variables
var paddleHeight = 10;
var paddleWidth = 75;
var xPaddle = (canvas.width - paddleWidth)/2;

// Brick variables
var brickRowCount = 3;
var brickColumnCount = 5;
var brickWidth = 75;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 30;

var bricks = [];
for(c=0; c<brickColumnCount; c++) {
    bricks[c] = [];
    for(r=0; r<brickRowCount; r++) {
        bricks[c][r] = { x: 0, y: 0 };
    }
}

document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);

function keyDownHandler(e) {
    if(e.keyCode == 39) {
        keyRight = true;
    }
    else if(e.keyCode == 37) {
        keyLeft = true;
    }
}

function keyUpHandler(e) {
    if(e.keyCode == 39) {
        keyRight = false;
    }
    else if(e.keyCode == 37) {
        keyLeft = false;
    }
}

function collisionDetection() {
    for(c=0; c<brickColumnCount; c++) {
        for(r=0; r<brickRowCount; r++) {
            var b = bricks[c][r];
            if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
                dy = -dy;
            }
        }
    }
}

function drawBall() {
    ctx.beginPath();
    ctx.arc(xCircle, yCircle, radiusCircle, 0, Math.PI * 2, false);
    ctx.fillStyle = "green";
    ctx.fill();
    ctx.closePath;
}

function drawPaddle() {
    ctx.beginPath();
    ctx.rect(xPaddle, canvas.height - paddleHeight, paddleWidth, paddleHeight);
    ctx.fillStyle = "green";
    ctx.fill();
    ctx.closePath();
}

function drawBricks() {
    for(c=0; c<brickColumnCount; c++) {
        for(r=0; r<brickRowCount; r++) {
            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 = "green";
            ctx.fill();
            ctx.closePath();
        }
    }
}

// Renders game
var draw = function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBricks();
drawBall();
drawPaddle();
collisionDetection();

// Collision detection for ball and paddle and game over mechanic
if (xCircle + dx > canvas.width - radiusCircle || xCircle < radiusCircle) {
    dx = -dx;
    }

if (yCircle + dy < radiusCircle) {
    dy = -dy;
    }

else if (yCircle + dy > canvas.height - radiusCircle) {


    if (xCircle > xPaddle && xCircle < xPaddle + paddleWidth) {
        if (yCircle = yCircle - paddleHeight) {
            dy = -dy;
            }
        }   
    else {
    alert("GAME OVER!");
    document.location.reload();
    }
}

// Paddle movement mechanics
if (keyRight && xPaddle < canvas.width - paddleWidth) {
    xPaddle += 5;
    }
else if (keyLeft && xPaddle > 0) {
    xPaddle += -5;
    }

// Makes the ball move
xCircle += dx;
yCircle += dy;
}

setInterval(draw, 10);

</script>

</body>
</html>

有人可以通过指出我的代码中的错误或者crashDetection()函数如何处理游戏机制的另一段代码来帮助我吗?即球运动或桨运动。

我还附上了当前非功能状态的游戏照片。

谢谢

Photo of game

1 个答案:

答案 0 :(得分:0)

您的代码变量xy未定义。我想你的意思是xCircleyCircle。您的collisionDetection函数必须如下所示:

function collisionDetection() {
    for (c=0; c<brickColumnCount; c++) {
        for (r=0; r<brickRowCount; r++) {
            var b = bricks[c][r];
            if (xCircle > b.x && xCircle < b.x+brickWidth && yCircle > b.y && yCircle < b.y+brickHeight) {
                dy = -dy;
            }
        }
    }
}