Gioco Ostacoli


Premi i pulsanti per spostare il quadrato rosso:







Aggiungi alcuni ostacoli

Ora vogliamo aggiungere alcuni ostacoli al nostro gioco.

Aggiungi un nuovo componente all'area di gioco. Rendilo verde, largo 10px, alto 200px e posizionalo 300px a destra e 120px in basso.

Aggiorna anche il componente ostacolo in ogni frame:

Esempio

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


Colpisci l'ostacolo = Game Over

Nell'esempio sopra, non succede nulla quando colpisci l'ostacolo. In un gioco, non è molto soddisfacente.

Come facciamo a sapere se il nostro quadrato rosso colpisce l'ostacolo?

Crea un nuovo metodo nel costruttore del componente, che controlla se il componente si arresta in modo anomalo con un altro componente. Questo metodo dovrebbe essere chiamato ogni volta che i frame si aggiornano, 50 volte al secondo.

Aggiungi anche un stop()metodo myGameAreaall'oggetto, che cancella l'intervallo di 20 millisecondi.

Esempio

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

Ostacolo in movimento

L'ostacolo non è pericoloso quando è statico, quindi vogliamo che si muova.

Modificare il valore della proprietà di myObstacle.xogni aggiornamento:

Esempio

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

Ostacoli multipli

Che ne dici di aggiungere più ostacoli?

Per questo abbiamo bisogno di una proprietà per il conteggio dei frame e di un metodo per eseguire qualcosa a un determinato frame rate.

Esempio

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

La funzione everyinterval restituisce true se il framenumber corrente corrisponde all'intervallo specificato.

Per definire più ostacoli, dichiarare prima la variabile ostacolo come un array.

In secondo luogo, dobbiamo apportare alcune modifiche alla funzione updateGameArea.

Esempio

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

Nella updateGameAreafunzione dobbiamo attraversare ogni ostacolo per vedere se c'è un incidente. Se si verifica un arresto anomalo, la updateGameAreafunzione si interrompe e non viene più eseguito il disegno.

La updateGameAreafunzione conta i frame e aggiunge un ostacolo ogni 150 frame.


Ostacoli di dimensioni casuali

Per rendere il gioco un po' più difficile e divertente, invieremo ostacoli di dimensioni casuali, in modo che il quadrato rosso si muova su e giù per non schiantarsi.

Esempio

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