గేమ్ ఇమేజ్

బటన్ ను నొక్కడం ద్వారా స్మైలీ ను కదించవచ్చు:






చిత్రాన్ని ఎలా ఉపయోగించాలి?

కాంవాస్ పైన చిత్రాన్ని జోడించడానికి, getContext("2d") ఆప్లికేషన్ లోని చిత్రం అంశాలు మరియు పద్ధతులను ఉపయోగించండి.

మా గేమ్ లో గేమ్ పోస్ట్ ను చిత్రంగా సృష్టించడానికి, కాంపోనెంట్ కాస్ట్ కంట్రక్టర్ ను ఉపయోగించండి, కానీ రంగును పరిచయం చేయకుండా చిత్రం యూరిల్ ను పరిచయం చేయండి. మరియు కాంపోనెంట్ రకం "image" గా కాస్ట్ కంట్రక్టర్ కు చెప్పండి:

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

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

కాంపోనెంట్ కాస్ట్ కంట్రక్టర్ లో మేము "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();
  myBackground.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;
      }
    }
  }
}

亲自试一试