ขวางเกม

กดปุ่มเพื่อย้ายชุดกล่องสีแดง:






เพิ่มประกาย

ตอนนี้เราต้องการเพิ่มประกายเข้าสู่เกม.

เพิ่มองค์ประกอบใหม่เข้าสู่พื้นที่เล่นเกม. ตั้งค่าเป็นสีเขียว กว้าง 10 พิกเซล สูง 200 พิกเซล และจัดให้ตั้งอยู่ที่ด้านขวา 300 พิกเซล และลงไป 120 พิกเซล.

ยังต้องปรับปรุงองค์ประกอบประกายในทุกฉากของการเล่นเกม:

ตัวอย่าง

var myGamePiece;
var myObstacle;
function startGame() {
  myGamePiece = new component(30, 30, "red", 10, 120);
  myObstacle = new component(10, 200, "green", 300, 120);
  myGameArea.start();
}
function updateGameArea() {
  myGameArea.clear();
  myObstacle.update();
  myGamePiece.newPos();
  myGamePiece.update();
}

ลองด้วยตัวเอง

撞到障碍 = 游戏结束

在上面的例子中,当您碰到障碍物时什么也不会发生。在游戏中,这并不令人满意。

我们如何知道我们的红色方块是否撞到了障碍物?

在组件构造函数中创建一个新方法,用于检查该组件是否与另一个组件相撞。每次帧更新时都应调用此方法,每秒 50 次。

还要向 myGameArea 对象添加 stop() 方法,该方法会清除 20 毫秒的间隔。

ตัวอย่าง

var myGameArea = {
  canvas : document.createElement("canvas"),
  start : function() {
    this.canvas.width = 480;
    this.canvas.height = 270;
    this.context = this.canvas.getContext("2d");
    document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    this.interval = setInterval(updateGameArea, 20);
  },
  clear : function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  },
  stop : function() {
    clearInterval(this.interval);
  }
}
function component(width, height, color, x, y) {
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.fillStyle = color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
  }
  this.newPos = function() {
    this.x += this.speedX;
    this.y += this.speedY;
  }
  this.crashWith = function(otherobj) {
    var myleft = this.x;
    var myright = this.x + (this.width);
    var mytop = this.y;
    var mybottom = this.y + (this.height);
    var otherleft = otherobj.x;
    var otherright = otherobj.x + (otherobj.width);
    var othertop = otherobj.y;
    var otherbottom = otherobj.y + (otherobj.height);
    var crash = true;
    if ((mybottom < othertop) ||
    (mytop > otherbottom) ||
    (myright < otherleft) ||
    (myleft > otherright)) {
      crash = false;
    }
    return crash;
  }
}
function updateGameArea() {
  if (myGamePiece.crashWith(myObstacle)) {
    myGameArea.stop();
  } else {
    myGameArea.clear();
    myObstacle.update();
    myGamePiece.newPos();
    myGamePiece.update();
  }
}

ลองด้วยตัวเอง

移动障碍物

障碍物在静止时没有危险,因此我们希望它动起来。

每次更新时更改 myObstacle.x 的属性值:

ตัวอย่าง

function updateGameArea() {
  if (myGamePiece.crashWith(myObstacle)) {
    myGameArea.stop();
  } else {
    myGameArea.clear();
    myObstacle.x += -1;
    myObstacle.update();
    myGamePiece.newPos();
    myGamePiece.update();
  }
}

ลองด้วยตัวเอง

多重障碍

添加多个障碍怎么样?

为此,我们需要一个用于计算帧数的属性,以及一个以给定帧速率执行某些操作的方法。

ตัวอย่าง

var myGameArea = {
  canvas : document.createElement("canvas"),
  start : function() {
    this.canvas.width = 480;
    this.canvas.height = 270;
    this.context = this.canvas.getContext("2d");
    document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    this.frameNo = 0;
    this.interval = setInterval(updateGameArea, 20);
  },
  clear : function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  },
  stop : function() {
    clearInterval(this.interval);
  }
}
function everyinterval(n) {
  if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  return false;
}

如果当前帧编号与给定间隔相对应,则 everyinterval 函数返回 true。

首先,如需定义多个障碍物,将障碍物变量声明为数组。

ต่อมา,เราต้องการที่จะเปลี่ยนแปลงฟังก์ชัน updateGameArea

ตัวอย่าง

var myGamePiece;
var myObstacles = [];
function updateGameArea() {
  var x, y;
  for (i = 0; i < myObstacles.length; i += 1) {
    if (myGamePiece.crashWith(myObstacles[i])) {
      myGameArea.stop();
      return;
    }
  }
  myGameArea.clear();
  myGameArea.frameNo += 1;
  if (myGameArea.frameNo == 1 || everyinterval(150)) {
    x = myGameArea.canvas.width;
    y = myGameArea.canvas.height - 200
    myObstacles.push(new component(10, 200, "green", x, y));
  }
  for (i = 0; i < myObstacles.length; i += 1) {
    myObstacles[i].x += -1;
    myObstacles[i].update();
  }
  myGamePiece.newPos();
  myGamePiece.update();
}

ลองด้วยตัวเอง

ใน updateGameArea ฟังก์ชันนี้ เราต้องวนรอบเพื่อตรวจสอบว่ามีการชนเข้ากันหรือไม่ ถ้ามีการชนเข้ากัน ฟังก์ชัน updateGameArea จะหยุดลงและไม่ทำการวาดต่อไป

updateGameArea ฟังก์ชันนี้นับเวลาของฟ레ม และเพิ่มอุปกรณ์ขัดขวางหนึ่งตัวทุก 150 ฟรีม

ขนาดของอุปกรณ์ขัดขวางสุ่ม

เพื่อเพิ่มความยากและความน่าสนใจของเกม เราจะส่งอุปกรณ์ขัดขวางขนาดสุ่ม ให้กล่องแดงต้องเคลื่อนตัวขึ้นลงเพื่อไม่ให้เกิดการชนเข้ากัน

ตัวอย่าง

function updateGameArea() {
  var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  for (i = 0; i < myObstacles.length; i += 1) {
    if (myGamePiece.crashWith(myObstacles[i])) {
      myGameArea.stop();
      return;
    }
  }
  myGameArea.clear();
  myGameArea.frameNo += 1;
  if (myGameArea.frameNo == 1 || everyinterval(150)) {
    x = myGameArea.canvas.width;
    minHeight = 20;
    maxHeight = 200;
    height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
    minGap = 50;
    maxGap = 200;
    gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
    myObstacles.push(new component(10, height, "green", x, 0));
    myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  }
  for (i = 0; i < myObstacles.length; i += 1) {
    myObstacles[i].x += -1;
    myObstacles[i].update();
  }
  myGamePiece.newPos();
  myGamePiece.update();
}

ลองด้วยตัวเอง