首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试编写一个程序,让球忽略击中绿色障碍物,但当红色障碍物表示游戏结束时结束游戏。

我正在尝试编写一个程序,让球忽略击中绿色障碍物,但当红色障碍物表示游戏结束时结束游戏。
EN

Stack Overflow用户
提问于 2020-05-16 14:47:08
回答 1查看 23关注 0票数 1

我正在尝试编写一个JavaScript程序,其中球忽略击中绿色障碍物,但当红色障碍物表示游戏结束时结束游戏。然而,在我的代码中,当球碰到任何颜色的障碍物时,游戏就结束了。

我对障碍的定义是:

代码语言:javascript
复制
function Obstacle(x, size, horizon, color) {

  this.x = x;
  this.y = horizon - size;

  this.size = size;
  this.color = color;

  this.onScreen = true;
}

/**
    *   handle x and onScreen values
    */
Obstacle.prototype.update = function(speed) {

    /* check if offscreen */
    this.onScreen = (this.x > -this.size);

    /* movement */
    this.x -= speed;
};

Obstacle.prototype.draw = function() {

    fill(this.color);
    stroke(255);
    strokeWeight(2);
    rect(this.x, this.y, this.size, this.size);
};


Obstacle.prototype.hits = function(ball) {

    var halfSize = this.size / 2;
    var minimumDistance = halfSize + (ball.radius); // closest before collision

    /* find center coordinates */
    var xCenter = this.x + halfSize;
    var yCenter = this.y + halfSize;

    var distance = dist(xCenter, yCenter, ball.x, ball.y); // calculate distance from centers

    return (distance < minimumDistance); // return result

}; 

这是游戏的代码:

代码语言:javascript
复制
function setup() {
  createCanvas(600, 200);
  textAlign(CENTER);
  horizon = height - 40;
  score = 0;
  obstacleSpeed = 6;
  var size = 20;
  ball = new bol(size * 2, height - horizon, size);
  textSize(20);
}

function draw() {
  background(51);
  drawHUD();
  handleLevel(frameCount);
  ball.update(horizon);
  handleObstacles();
  //handlepowerups();
}

/**
 * draws horizon & score
 */
function drawHUD() {
  /* draw horizon */
  stroke(255);
  strokeWeight(2);
  line(0, horizon, width, horizon);

  /* draw score */
  noStroke();
  text("Score: " + score, width / 2, 30);

ball.draw();
}

/**
 *  updates, draws, and cleans out the obstacles
 */
function handleObstacles() {
  for (var i = obstacles.length - 1; i >= 0; i--) {
    obstacles[i].update(obstacleSpeed);
    obstacles[i].draw();
    if (obstacles[i].hits(ball)) // if there's a collision
    {
      if (obstacles[i].color = color(225, 0, 0))
        endGame();
      else
        obstacles[i].color = color(0, 0, 225)
    }

    if (!obstacles[i].onScreen) // if it's no longer showing
      obstacles.splice(i, 1); // delete from array
  }
}

/**
 * speeds game up, pushes new obstacles, & handles score
 */
function handleLevel(n) {
  if (n % 100 === 0) { // every 0.5 seconds
    var n = noise(n);
    if (n > 0.5)
      newObstacle(n); // push new obstacle
    if (n % 120 === 0) // every 2 seconds
      obstacleSpeed *= 1.05; // speed up
  }
  score++;
}

/**
 * pushes random obstacle
 */
function newObstacle(n) {
  var col1 = color(225, 0, 0);
  var col2 = color(0, 225, 0);
  var size = random(25) + 20;
  var obs = new Obstacle(width + size, size, horizon, col2);
  var obs2 = new Obstacle(width + size, size, horizon, col1);
  var cases = random(1);
  if (cases < 0.5)
    obstacles.push(obs);
  else
    obstacles.push(obs2)
}

function keyPressed() {
  if ((keyCode === UP_ARROW || keyCode === 32) && ball.onGround) // jump if possible
    ball.jump();
  boingaudio.play()
}

function endGame() {
  noLoop();
  noStroke();
  textSize(40);
  text("GAME OVER", width / 2, height / 2);
  if (highscore !== null) {
    if (score > highscore) {
      localStorage.setItem("highscore", score);
    }
  } else {
    localStorage.setItem("highscore", score);
  }

  textSize(20);
  text("Highscore: " + highscore, width / 2, height / 2 + 20);
  gameoveraudio.play()
}

我不明白为什么不管出现什么颜色的球,球都会结束比赛。你能帮我找出哪里不对劲吗?

EN

回答 1

Stack Overflow用户

发布于 2020-05-16 15:02:40

您似乎是在为obstacles[i].color赋值,而不是测试它:

代码语言:javascript
复制
if (obstacles[i].color = color(225, 0, 0))
  endGame();
else
  obstacles[i].color = color(0, 0, 225)

这可能应该是这样的:

代码语言:javascript
复制
if (obstacles[i].color == color(225, 0, 0))
  endGame();

没有你的else,这似乎没有任何用处。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61832967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档