لعبة الصور
اضغط على الأزرار لتحريك الابتسامة:
كيف تستخدم الصور؟
لإضافة صور على لوحة قماشية ، يحتوي كائن getContext ("2d") على خصائص وأساليب صورة مضمنة.
في لعبتنا ، لإنشاء لوحة اللعب كصورة ، استخدم مُنشئ المكوِّن ، ولكن بدلاً من الإشارة إلى اللون ، يجب عليك الرجوع إلى عنوان url للصورة. ويجب أن تخبر المنشئ أن هذا المكون من نوع "image":
مثال
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") {
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);
}
}
}
تغيير الصور
يمكنك تغيير الصورة متى شئت عن طريق تغيير src
خاصية image
كائن المكون الخاص بك.
إذا كنت ترغب في تغيير الوجوه الضاحكة في كل مرة يتحرك فيها ، فقم بتغيير مصدر الصورة عندما ينقر المستخدم على زر ، والعودة إلى الوضع الطبيعي عند عدم النقر فوق الزر:
مثال
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;
}
صور الخلفية
أضف صورة خلفية إلى منطقة لعبتك عن طريق إضافتها كمكون ، وقم أيضًا بتحديث الخلفية في كل إطار:
مثال
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 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;
}
}
}
}