【问题标题】:Choosing x and y coordinates for a player in a javascript platform game在 JavaScript 平台游戏中为玩家选择 x 和 y 坐标
【发布时间】:2020-08-13 08:21:48
【问题描述】:

所以游戏关卡数组是这样的,其中"@"是玩家

[".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."]
[".", ".", "#", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "#", ".", "."]
[".", ".", "#", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "=", ".", "#", ".", "."]
[".", ".", "#", ".", ".", ".", ".", ".", ".", ".", ".", ".", "o", ".", "o", ".", ".", ".", ".", "#", ".", "."]
[".", ".", "#", ".", "@", ".", ".", ".", ".", ".", ".", "#", "#", "#", "#", "#", ".", ".", ".", "#", ".", "."]
[".", ".", "#", "#", "#", "#", "#", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "#", ".", "."]
[".", ".", ".", ".", ".", ".", "#", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "#", ".", "."]
[".", ".", ".", ".", ".", ".", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", ".", "."]
[".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."]
class Vec {
  constructor(x, y) {
    this.x = x
    this.y = y
  }

  plus(other) {
    return new Vec(this.x + other.x, this.y + other.y)
  }

  times(factor) {
    return new Vec(this.x * factor, this.y * factor)
  }
}
class Player {
  constructor(pos, speed) {
    this.pos = pos;
    this.speed = speed;
  }

  get type() { return "player"; }

  static create(pos) {
    return new Player(pos.plus(new Vec(0, -0.5)),
                      new Vec(0, 0));
  }
}

Player.prototype.size = new Vec(0.8, 1.5);

我知道这是一个数学问题,而不是一个 js 问题,但是谁能告诉我这里的 x 和 y 坐标是如何选择的?我的意思是(0, -0.5)、(0,0) 和(0.8, 1.5)

  static create(pos) {
    return new Player(pos.plus(new Vec(0, -0.5)),
                      new Vec(0, 0));
  }

Player.prototype.size = new Vec(0.8, 1.5);

Eloquent JavaScript 声明:

因为玩家的高度是一个半方格,所以它的初始位置被设置为比@字符出现的位置高半个方格。这样,它的底部与它出现的正方形的底部对齐。

有人可以为我澄清这个疑问吗?谢谢。

【问题讨论】:

    标签: javascript oop object math game-development


    【解决方案1】:

    发生的事情是玩家有 1.5 格高。因此,如果您希望玩家的脚为 8,那么您的玩家的头必须为 6.5(8 - 1.5)。当您运行 Player.create(pos) //Pos is a vector 时,它会返回一个新的 Player 对象,其中 Player 的脚位于传递给 Player.create(pos) 的原始位置。

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多