【问题标题】:Can't end the game properly in Javascript无法在 Javascript 中正确结束游戏
【发布时间】:2018-09-20 20:03:06
【问题描述】:

我是使用 JavaScript 进行游戏编程的初学者。这是我尝试制作的第一款游戏。游戏结束后我无法显示时间。

游戏详情:

  1. 鼠标用于移动与球碰撞的方块并转向。

  2. 当球通过画布的左端时,游戏结束并停止在画布上显示。

  3. 当上述事件发生时变量game over变为真,显示时间的代码如下。(但它不起作用)

var canvas;
var canvascontext;
var ballX = 50;
var ballY = 50;
var ballXspeed = 15;
var ballYspeed = 15;
var paddleY = 225;
var deltaY;
var gameover = false;

function calculateMousePos(evt) {
  var rect = canvas.getBoundingClientRect();
  var root = document.documentElement;
  var mouseX = evt.clientX - rect.left - root.scrollLeft;
  var mouseY = evt.clientY - rect.top - root.scrollTop;
  return {
    x: mouseX,
    y: mouseY
  };
}
window.onload = function() {
  canvas = document.getElementById('gameCanvas');
  canvasContext = canvas.getContext('2d');
  var fps = 30;
  setInterval(callBoth, 1000 / 30);
  canvas.addEventListener('mousemove', function(evt) {
    var mousePos = calculateMousePos(evt);
    paddleY = mousePos.y - 50;
  })
  var date1 = new Date();
}

function callBoth() {
  if (gameover == true) {
    return;
  } else {
    moveEverything();
    drawEverything();
  }
}

function moveEverything() {
  ballX = ballX + ballXspeed;
  ballY = ballY + ballYspeed;
  if (ballX > canvas.width) {
    ballXspeed = -ballXspeed;
  }
  if (ballY > canvas.height || ballY < 0) {
    ballYspeed = -ballYspeed;
  }
  if (ballX < 0) {
    if (ballY > paddleY && ballY < (paddleY + 150)) {
      ballXspeed = -(ballXspeed + ballXspeed * 0.1);
      deltaY = ballY - (75 + paddleY);
      ballYspeed = deltaY * 0.35;
    } else {
      gameover = true;
      var date2 = new Date();
      var diff = date2 - date1;
      document.getElementById("time").innerHTML = diff;
    }
  }
}

function drawEverything() {

  canvasContext.fillStyle = 'black';
  canvasContext.fillRect(0, 0, canvas.width, canvas.height);
  canvasContext.fillStyle = 'white';
  canvasContext.fillRect(0, paddleY, 10, 150)
  canvasContext.fillStyle = 'red';
  canvasContext.beginPath();
  canvasContext.arc(ballX, ballY, 15, 0, Math.PI * 2);
  canvasContext.fill();
}

function reset() {
  gameover = false;
  ballX = canvas.width;
  ballY = canvas.height / 2;
  ballYspeed = 15;
  ballXspeed = 15;
  date1 = new date();
}
<canvas id="gameCanvas" width="800" height="600"></canvas>
<br>
<span id="time"></span>
<button onclick="reset()"> RESET </button>

【问题讨论】:

  • 所以我给你做了一个 sn-p,当我点击重置时控制台给出了一个错误。 - 你需要`date1 = new Date();`

标签: javascript html5-canvas


【解决方案1】:

一些事情

  1. 启动 date1 onload - 它必须在全局范围内
  2. 正确更新 date1(新 date() 中的错字)
  3. 重置日期和文本

var canvas;
var canvascontext;
var ballX = 50;
var ballY = 50;
var ballXspeed = 15;
var ballYspeed = 15;
var paddleY = 225;
var deltaY;
var gameover = false;
var date1 = 0; // global variable

function calculateMousePos(evt) {
  var rect = canvas.getBoundingClientRect();
  var root = document.documentElement;
  var mouseX = evt.clientX - rect.left - root.scrollLeft;
  var mouseY = evt.clientY - rect.top - root.scrollTop;
  return {
    x: mouseX,
    y: mouseY
  };
}
window.onload = function() {
  canvas = document.getElementById('gameCanvas');
  canvasContext = canvas.getContext('2d');
  var fps = 30;
  setInterval(callBoth, 1000 / 30);
  canvas.addEventListener('mousemove', function(evt) {
    var mousePos = calculateMousePos(evt);
    paddleY = mousePos.y - 50;
  })
  date1 = new Date(); // update the global var
}

function callBoth() {
  if (gameover == true) {
    return;
  } else {
    moveEverything();
    drawEverything();
  }
}

function moveEverything() {
  ballX = ballX + ballXspeed;
  ballY = ballY + ballYspeed;
  if (ballX > canvas.width) {
    ballXspeed = -ballXspeed;
  }
  if (ballY > canvas.height || ballY < 0) {
    ballYspeed = -ballYspeed;
  }
  if (ballX < 0) {
    if (ballY > paddleY && ballY < (paddleY + 150)) {
      ballXspeed = -(ballXspeed + ballXspeed * 0.1);
      deltaY = ballY - (75 + paddleY);
      ballYspeed = deltaY * 0.35;
    } else {
      gameover = true;
      var date2 = new Date();
      var diff = date2 - date1;
      document.getElementById("time").innerHTML = parseInt(diff/1000)+" seconds"; // show seconds
    }
  }
}

function drawEverything() {

  canvasContext.fillStyle = 'black';
  canvasContext.fillRect(0, 0, canvas.width, canvas.height);
  canvasContext.fillStyle = 'white';
  canvasContext.fillRect(0, paddleY, 10, 150)
  canvasContext.fillStyle = 'red';
  canvasContext.beginPath();
  canvasContext.arc(ballX, ballY, 15, 0, Math.PI * 2);
  canvasContext.fill();
}

function reset() {
  gameover = false;
  ballX = canvas.width;
  ballY = canvas.height / 2;
  ballYspeed = 15;
  ballXspeed = 15;
  date1 = new Date(); // reset the date
  document.getElementById("time").innerHTML =""; // reset the field
}
<canvas id="gameCanvas" width="800" height="600"></canvas>
<br>
<span id="time"></span>
<button onclick="reset()"> RESET </button>

【讨论】:

    【解决方案2】:

    您在 window.onload 中本地声明了date1,这就是其他函数无法访问它的原因。从那里删除 var 并将 var date1 添加到顶部以使其全局化。

    【讨论】:

    • 还有更多问题。也有错别字
    • 拼写错误不会导致给定的问题。 (当该命令执行时会变得很明显。)
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多