ผลต่างระหว่างรุ่นของ "Se63/typescript/zombie"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 66: แถว 66:
  
 
ทำความเข้าใจกันก่อน
 
ทำความเข้าใจกันก่อน
 +
* <tt>gameBoard</tt> เป็น global ทำให้เราเรียก <tt>gameBoard.step()</tt> ใน console ให้เกมทำงานทีละขั้นได้
  
 
== แกะ player และ bots ==
 
== แกะ player และ bots ==

รุ่นแก้ไขเมื่อ 06:40, 10 กรกฎาคม 2563

โค้ดเริ่มต้น

ใส่ใน jsfiddle

ส่วน HTML

<html>
  <body>
    <pre id="gameBoard"></pre>
  </body>
</html>

ส่วน Typescript (เลือก Javascript เปลี่ยนภาษาเป็น Typescript)

const boardHeight = 30;
const boardWidth = 50;

class GameBoard {
  constructor() {
    this.pX = 25;
    this.pY = 15;
    this.botCount = 5;
    this.botXs = [10,20,30,40,50];
    this.botYs = [5,10,20,15,25,12]
  }

  showBoard() {
    let boardRows = [];
    for (let i = 0; i < boardHeight; i++) {
      let rowChars = [];
      for (let j = 0; j < boardWidth; j++) {
        rowChars.push(' ');
      }
      boardRows.push(rowChars);
    }
    
    boardRows[this.pY][this.pX] = '@';
    for (let i = 0; i < this.botCount; i++) {
      boardRows[this.botYs[i]][this.botXs[i]] = 'B';
    }
    
    let boardStr = boardRows.map((row) => {
      return row.join('') + "\n";
    }).join('');
    
    let boardElement = document.getElementById('gameBoard');
    boardElement.innerHTML = boardStr;
  }
  
  moveBots() {
    for (let i=0; i < this.botCount; i++) {
      this.botXs[i]--;
    }
  }
  
  step() {
    this.moveBots();
    this.showBoard();
  }
}

gameBoard = new GameBoard();
gameBoard.showBoard();

ทำความเข้าใจกันก่อน

  • gameBoard เป็น global ทำให้เราเรียก gameBoard.step() ใน console ให้เกมทำงานทีละขั้นได้

แกะ player และ bots

interface

Bots รูปแบบอื่น ๆ