一个变量是破坏我的简单程序,为什么?

时间:2017-04-07 02:03:57

标签: javascript html5-canvas

我正在关注教程并尝试修改一些代码。正如目前的代码所示,一切都处于正常工作状态,球在一个方块周围反弹并从底部的球拍上移开,我想要做的就是当球从球拍上反弹时使球变大。球的大小从15开始,我可以将它增加1-4,但是在5和更高的位置,有一个错误导致球停留在位置并长到屏幕的一半,然后就在那里。我认为随着它的增长它会夹回到桨的击球盒中,然后激活另一个增长并重复。但是,我完全不确定。任何人都能解释一下吗?我将对代码中途负责的变量进行大写评论。非常感谢您的帮助!

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

var x = canvas.width / 2; //starting coordinates
var y = canvas.height - 30;

var dx = 2; //rate of movement for ball
var dy = -2;

var ballRadius = 15;

var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width - paddleWidth) / 2;

var rightPressed = false;
var leftPressed = false;

function drawBall() {
  ctx.beginPath();
  ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
  ctx.fillStyle = "#ff0000";
  ctx.fill();
  ctx.closePath();
}

function drawPaddle() {
  ctx.beginPath();
  ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
  ctx.fillStyle = "#0095DD";
  ctx.fill();
  ctx.closePath();
}

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawBall();
  drawPaddle();
  x += dx;
  y += dy;

  if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) { //these ifs cause the ball to bounce off walls
    dx = -dx;
  }

  if (y + dy < ballRadius) {
    dy = -dy;
  } else if (y + dy > canvas.height - ballRadius) {
    if (x > paddleX && x < paddleX + paddleWidth) { //detects paddlebox detection
      dy = -dy;
      ballRadius += 7; //TROUBLESOME VARIABLE! Work with 1-4 but breaks beyond that.

    } else {
      alert("GAME OVER");
      document.location.reload();
    }
  }
  if (rightPressed && paddleX < canvas.width - paddleWidth) {
    paddleX += 7;
  } else if (leftPressed && paddleX > 0) {
    paddleX -= 7;
  }
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);

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;
  }
}

setInterval(draw, 10);
* {
  padding: 0;
  margin: 0;
}

canvas {
  background: #eee;
  display: block;
  margin: 0 auto;
}
<canvas id="myCanvas" width="480" height="320"></canvas>

1 个答案:

答案 0 :(得分:2)

问题是你的绘图功能每10毫秒就会超过一次。所以当你的球击中球拍时,它实际上会触发球多次生长。我的解决方案是在你增加球的大小时如何超时/延迟,以便在下次绘制功能触发之前有时间移开。这是代码。

注意:全屏打开代码段以更好地查看游戏

&#13;
&#13;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

var x = canvas.width / 2; //starting coordinates
var y = canvas.height - 30;

var dx = 2; //rate of movement for ball
var dy = -2;

var ballRadius = 15;

var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width - paddleWidth) / 2;

var rightPressed = false;
var leftPressed = false;

function drawBall() {
  ctx.beginPath();
  ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
  ctx.fillStyle = "#ff0000";
  ctx.fill();
  ctx.closePath();
}

function drawPaddle() {
  ctx.beginPath();
  ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
  ctx.fillStyle = "#0095DD";
  ctx.fill();
  ctx.closePath();
}

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawBall();
  drawPaddle();
  x += dx;
  y += dy;

  if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) { //these ifs cause the ball to bounce off walls
    dx = -dx;
  }

  if (y + dy < ballRadius) {
    dy = -dy;
  } else if (y + dy > canvas.height - ballRadius) {
    if (x > paddleX && x < paddleX + paddleWidth) { //detects paddlebox detection
      dy = -dy;
      setTimeout(function(){ ballRadius += 7; }, 100);
       

    } else {
      alert("GAME OVER");
      document.location.reload();
    }
  }
  if (rightPressed && paddleX < canvas.width - paddleWidth) {
    paddleX += 7;
  } else if (leftPressed && paddleX > 0) {
    paddleX -= 7;
  }
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);

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;
  }
}

setInterval(draw, 10);
&#13;
* {
  padding: 0;
  margin: 0;
}

canvas {
  background: #eee;
  display: block;
  margin: 0 auto;
}
&#13;
<canvas id="myCanvas" width="480" height="320"></canvas>
&#13;
&#13;
&#13;