ล่าสุดการพัฒนาเว็บบทเรียน
 

อุปสรรคเกม HTML


กดปุ่มเพื่อย้ายสี่เหลี่ยมสีแดง:







เพิ่มอุปสรรคบางอย่าง

ตอนนี้เราต้องการที่จะเพิ่มอุปสรรค Som ที่จะเล่นเกมของเรา

เพิ่มองค์ประกอบใหม่ไปยังพื้นที่การเล่นเกม ทำให้มันเป็นสีเขียวกว้าง 10px, 200px สูงและวางไว้ 300px ไปทางขวาและ 120px ลง

นอกจากนี้ยังปรับปรุงคอมโพเนนต์อุปสรรคในทุกกรอบ:

ตัวอย่าง

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();
}
ลองตัวเอง»

ตีฝ่าอุปสรรค = มากกว่าเกม

ในตัวอย่างข้างต้นไม่มีอะไรเกิดขึ้นเมื่อคุณกดอุปสรรค ในเกมที่ไม่ได้เป็นที่น่าพอใจมาก

เรารู้ได้อย่างไรถ้าสี่เหลี่ยมสีแดงของเราฮิตอุปสรรค?

สร้างวิธีการใหม่ในตัวสร้างองค์ประกอบที่ chekcs ถ้าเกิดปัญหากับองค์ประกอบองค์ประกอบอื่น วิธีนี้ควรจะเรียกว่าทุกครั้งที่มีการอัปเดตกรอบ 50 ครั้งต่อวินาที

นอกจากนี้ยังเพิ่มการ stop() วิธีการที่จะ myGameArea วัตถุข้ามช่วงเวลา 20 มิลลิวินาที

ตัวอย่าง

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();
    }
}
ลองตัวเอง»

อุปสรรคการย้าย

อุปสรรคที่เป็นอันตรายเมื่อมันเป็นแบบคงที่ไม่ดังนั้นเราต้องการที่จะย้าย

เปลี่ยนค่าทรัพย์สินของ myObstacle.x ทุก UPDATE:

ตัวอย่าง

function updateGameArea() {
    if (myGamePiece.crashWith(myObstacle)) {
        myGameArea.stop();
    } else {
        myGameArea.clear();
        myObstacle.x += -1;
        myObstacle.update();
        myGamePiece.newPos();
        myGamePiece.update();
    }
}
ลองตัวเอง»

อุปสรรคหลาย

วิธีการเกี่ยวกับการเพิ่มอุปสรรคหลาย

เพื่อที่เราต้องการสถานที่ให้บริการสำหรับการนับเฟรมและวิธีการในการดำเนินการบางสิ่งบางอย่างที่อัตราเฟรมที่กำหนด

ตัวอย่าง

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

ฟังก์ชั่น everyinterval ผลตอบแทนจริงถ้า framenumber ปัจจุบันสอดคล้องกับช่วงเวลาที่กำหนด

เพื่อกำหนดอุปสรรคหลายแรกประกาศตัวแปรอุปสรรคเป็นอาร์เรย์

ประการที่สองเราจำเป็นต้องทำการเปลี่ยนแปลงบางอย่างในการทำงาน updateGameArea

ตัวอย่าง

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();
}
ลองตัวเอง»

ใน updateGameArea ฟังก์ชั่นที่เราจะต้องห่วงผ่านทุกอุปสรรคเพื่อดูว่ามีความผิดพลาด หากมีความผิดพลาดที่ updateGameArea ฟังก์ชั่นจะหยุดและไม่มีการวาดภาพมากขึ้นจะทำ

updateGameArea ฟังก์ชั่นนับเฟรมและเพิ่มเป็นอุปสรรคสำหรับกรอบทุกปีที่ 150


อุปสรรคขนาดสุ่ม

ที่จะทำให้เกมยากขึ้นเล็กน้อยและความสนุกสนานแล้วเราจะส่งในอุปสรรคขนาดสุ่มเพื่อให้ตารางสีแดงจะต้องย้ายขึ้นและลงเพื่อไม่ผิดพลาด

ตัวอย่าง

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();
}
ลองตัวเอง»