รูปภาพเกม

กดปุ่มเพื่อย้ายตัวเล็กมาซ้าย:






จะใช้ภาพได้อย่างไร?

ถ้าต้องการเพิ่มภาพบนกระดานวาด โปรดใช้คุณสมบัติและวิธีของภาพที่ฝากตัวแทน getContext("2d") ไว้:

ในเกมของเรา ถ้าต้องการสร้างองค์ประกอบเป็นภาพ โปรดใช้ฟังก์ชันสร้างองค์ประกอบ แต่คุณต้องอ้างอิง URL ของภาพ ไม่ใช่อ้างอิงสี และต้องบอกให้ฟังก์ชันทราบว่าชนิดขององค์ประกอบคือ "image":

function component(width, height, color, x, y, type) {

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

ในฟังก์ชันสร้างองค์ประกอบ เราทดสอบว่าองค์ประกอบนั้นเป็นชนิด "image" และใช้ฟังก์ชันสร้างตัวแทน Image ภายในตัวแทน new Image() ที่สร้างตัวแทนภาพตัวเดียวกัน ในขณะที่เราเตรียมการวาดภาพ เราใช้วิธี drawImage แทนวิธี fillRect:

function component(width, height, color, x, y, type) {

this.type = type;
  if (type == "image" ||
  if (type == "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") {
    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);
    }
  }
}

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

更改圖像

您可以隨時通過更改组件的 image 對象的 src 屬性來更改圖像。

如果您想在每次移動時更改笑脸,請在用戶點擊按鈕時更改圖像源,并在未點擊按鈕時恢復正常:

function component(width, height, color, x, y, type) {

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

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

背景圖片

通过將背景圖像添加為组件,可將其添加到遊戲區域,并在每個畫面中更新背景:

function component(width, height, color, x, y, type) {

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

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

移動背景

更改背景组件的 speedX 属性可使背景移動:

function component(width, height, color, x, y, type) {

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

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

การวนภาพพื้นหลัง

เพื่อที่จะมีภาพพื้นหลังซ้ำกันเป็นเวลานานต้องใช้เทคนิคเฉพาะ

ในขณะที่เราบอกกับฟังก์ชันสร้างตัวอย่างว่านี้คือพื้นหลัง ฟังก์ชันสร้างตัวอย่างจะเพิ่มภาพเข้ามาสองครั้ง และจะนำภาพที่สองมาตั้งที่หลังภาพที่หนึ่งทันที

newPos() ในนาทีนี้ ตรวจสอบตำแหน่ง x ขององค์ประกอบ ว่าเข้าสู่ท้ายของภาพหรือไม่ ถ้าเข้าสู่ จะเคลื่อนองค์ประกอบไปตาม x ตำแหน่ง 0 ถูกตั้งเป็น: ตัวอย่าง

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

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