กราฟิก HTML

กราฟิก หน้าแรก

Google Maps

แนะนำแผนที่ แผนที่พื้นฐาน การวางซ้อนแผนที่ กิจกรรมของแผนที่ การควบคุมแผนที่ ประเภทแผนที่ แผนที่อ้างอิง

กวดวิชา SVG

บทนำ SVG SVG ใน HTML SVG สี่เหลี่ยมผืนผ้า SVG Circle SVG วงรี สาย SVG รูปหลายเหลี่ยม SVG SVG Polyline เส้นทาง SVG ข้อความ SVG SVG ลากเส้น บทนำตัวกรอง SVG เอฟเฟกต์เบลอ SVG SVG Drop Shadows SVG ลิเนียร์ SVG Radial ตัวอย่าง SVG ข้อมูลอ้างอิง SVG

กวดวิชาผ้าใบ

แนะนำผ้าใบ ภาพวาดบนผืนผ้าใบ พิกัดผ้าใบ ผ้าใบไล่โทนสี ข้อความผ้าใบ ภาพแคนวาส อ้างอิงผ้าใบ

นาฬิกาผ้าใบ

แนะนำนาฬิกา หน้าปัดนาฬิกา ตัวเลขนาฬิกา เข็มนาฬิกา นาฬิกาเริ่ม

เกม HTML

แนะนำเกม เกมแคนวาส ส่วนประกอบของเกม ตัวควบคุมเกม อุปสรรคของเกม คะแนนเกม รูปภาพเกม เสียงเกม เกมแรงโน้มถ่วง เกมเด้ง การหมุนเกม การเคลื่อนไหวของเกม

อุปสรรคของเกม


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







เพิ่มอุปสรรค

ตอนนี้เราต้องการเพิ่มอุปสรรคให้กับเกมของเรา

เพิ่มส่วนประกอบใหม่ให้กับพื้นที่เล่นเกม ทำให้เป็นสีเขียว กว้าง 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();
}


ตีสิ่งกีดขวาง = จบเกม

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

เราจะรู้ได้อย่างไรว่าสี่เหลี่ยมสีแดงของเราชนสิ่งกีดขวางหรือไม่?

สร้างวิธีการใหม่ในคอนสตรัคเตอร์คอมโพเนนต์ ซึ่งจะตรวจสอบว่าคอมโพเนนต์ขัดข้องกับคอมโพเนนต์อื่นหรือไม่ ควรเรียกวิธีนี้ทุกครั้งที่อัปเดตเฟรม 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ทุกครั้งที่อัปเดต:

ตัวอย่าง

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 คืนค่า จริง หากหมายเลขเฟรมปัจจุบันสอดคล้องกับช่วงเวลาที่กำหนด

ในการกำหนดสิ่งกีดขวางหลายรายการ ขั้นแรกให้ประกาศตัวแปรสิ่งกีดขวางเป็นอาร์เรย์

ประการที่สอง เราจำเป็นต้องทำการเปลี่ยนแปลงบางอย่างในฟังก์ชัน 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();
}