Herní obrázky


Stisknutím tlačítek přesunete smajlíka:








Jak používat obrázky?

Chcete-li přidat obrázky na plátno, objekt getContext("2d") má vestavěné vlastnosti a metody obrázku.

V naší hře, chcete-li vytvořit hru jako obrázek, použijte konstruktor komponent, ale místo odkazování na barvu musíte odkazovat na adresu URL obrázku. A musíte konstruktoru říci, že tato komponenta je typu "image":

Příklad

function startGame() {
  myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image");
  myGameArea.start();
}

V konstruktoru komponenty otestujeme, zda je komponenta typu „image“ a vytvoříme objekt obrázku pomocí vestavěného konstruktoru objektu „new Image()“. Když jsme připraveni nakreslit obrázek, použijeme metodu drawImage místo metody fillRect:

Příklad

function component(width, height, color, x, y, type) {
  this.type = type;
  if (type == "image") {
    this.image = new Image();
    this.image.src = color;
  }
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    if (type == "image") {
      ctx.drawImage(this.image,
        this.x,
        this.y,
        this.width, this.height);
    } else {
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }
  }
}


Změnit obrázky

Obrázek můžete kdykoli změnit změnou srcvlastnosti imageobjektu vaší komponenty.

Pokud chcete změnit smajlíka při každém jeho pohybu, změňte zdroj obrázku, když uživatel klikne na tlačítko, a vraťte se do normálního stavu, když na tlačítko neklikne:

Příklad

function move(dir) {
  myGamePiece.image.src = "angry.gif";
  if (dir == "up") {myGamePiece.speedY = -1; }
  if (dir == "down") {myGamePiece.speedY = 1; }
  if (dir == "left") {myGamePiece.speedX = -1; }
  if (dir == "right") {myGamePiece.speedX = 1; }
}

function clearmove() {
  myGamePiece.image.src = "smiley.gif";
  myGamePiece.speedX = 0;
  myGamePiece.speedY = 0;
}

Obrázky na pozadí

Přidejte obrázek na pozadí do své herní oblasti tím, že jej přidáte jako součást, a také aktualizujte pozadí v každém snímku:

Příklad

var myGamePiece;
var myBackground;

function startGame() {
  myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image");
  myBackground = new component(656, 270, "citymarket.jpg", 0, 0, "image");
  myGameArea.start();
}

function updateGameArea() {
  myGameArea.clear();
  myBackground.newPos();
  myBackground.update();
  myGamePiece.newPos();
  myGamePiece.update();
}

Pohybující se pozadí

Změňte vlastnost komponenty pozadí, speedXaby se pozadí posunulo:

Příklad

function updateGameArea() {
  myGameArea.clear();
  myBackground.speedX = -1;
  myBackground.newPos();
  myBackground.update();
  myGamePiece.newPos();
  myGamePiece.update();
}

Smyčka na pozadí

Abychom vytvořili stejnou smyčku na pozadí navždy, musíme použít specifickou techniku.

Začněte tím, že konstruktoru komponenty řeknete, že se jedná o pozadí . Konstruktor komponenty pak přidá obrázek dvakrát, přičemž druhý obrázek umístí hned za první obrázek.

V newPos()metodě zkontrolujte, zda xpozice komponenty dosáhla konce obrázku, pokud ano, nastavte xpozici komponenty na 0:

Příklad

function component(width, height, color, x, y, type) {
  this.type = type;
  if (type == "image" || type == "background") {
    this.image = new Image();
    this.image.src = color;
  }
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    if (type == "image" || type == "background") {
      ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
      if (type == "background") {
        ctx.drawImage(this.image, this.x + this.width, this.y, this.width, this.height);
      }
    } else {
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }
  }
  this.newPos = function() {
    this.x += this.speedX;
    this.y += this.speedY;
    if (this.type == "background") {
      if (this.x == -(this.width)) {
        this.x = 0;
      }
    }
  }
}