โค้ดยิม/หลักสูตรจาวา/All lectures for TH purposes/ดำดิ่งสู่เอ็นจิ้นของเกม

ดำดิ่งสู่เอ็นจิ้นของเกม

มีอยู่

1. ทำงานกับเซลล์ของสนามเด็กเล่น

เป็นเรื่องดีที่เราสามารถแบ่งพื้นที่เล่นออกเป็นเซลล์ได้ แต่เราจะทำอะไรกับเซลล์เองได้บ้าง?

สำหรับแต่ละเซลล์ของสนามเด็กเล่น เราสามารถตั้งค่า:

  • สีของเซลล์ (สีพื้นหลังของเซลล์);
  • ข้อความ (อาจเป็นข้อความหรือตัวเลขก็ได้);
  • สีข้อความ
  • ขนาดข้อความเป็นเปอร์เซ็นต์ของขนาดเซลล์

ลองพิจารณาวิธีการทำงานกับเซลล์ของสนามเด็กเล่น:

void setCellColor(int x, int y, Color color)กำหนดสีของเซลล์ด้วยพิกัด(x, y)เท่ากับcolor

ตัวอย่าง:

setCellColor(0, 0, Color.RED);
setCellColor(3, 6, Color.BLACK);
setCellColor(6, 8, Color.NONE);

Color getCellColor(int x, int y)ส่งกลับสีของเซลล์ด้วย(x, y)พิกัด

ตัวอย่าง:

Color myColor = getCellColor(2, 0);

void setCellValue(int x, int y, String value)กำหนดข้อความในString valueเซลล์ด้วย(x, y)พิกัด

ตัวอย่าง:

setCellValue(3, 3, "text");
setCellValue(0, 8, "W");
setCellValue(4, 1, "2222");
setCellValue(6, 6, "");

String getCellValue(int x, int y)ส่งคืนข้อความที่อยู่ในเซลล์พร้อม(x, y)พิกัด

ตัวอย่าง:

String s = getCellValue(3, 3);
System.out.println(getCellValue(4, 1));

void setCellTextSize(int x, int y, int size)กำหนดขนาดของข้อความในเซลล์ด้วยพิกัด(x, y)โดยที่sizeความสูงของข้อความเป็นเปอร์เซ็นต์ของความสูงของเซลล์

ตัวอย่าง:

setCellTextSize(2, 0, 70); // 70% of the cell height

int getCellTextSize(int x, int y)ส่งกลับขนาดของเนื้อหาในเซลล์พร้อม(x, y)พิกัด

ตัวอย่าง:

int size = getCellTextSize(2 , 0);

void setCellNumber(int x, int y, int value)กำหนดตัวเลขint valueให้กับเซลล์ด้วย(x, y)พิกัด

ตัวอย่าง:

setCellNumber(3, 3, 40);
setCellNumber(0, 8, -8);
setCellNumber(4, 1, 2222);
setCellNumber(6, 6, 0);

int getCellNumber(int x, int y)ส่งกลับจำนวนที่มีอยู่ในเซลล์ที่มี(x, y)พิกัด ถ้าเซลล์ไม่มีตัวเลข จะส่งกลับ 0

ตัวอย่าง:

int i = getCellNumber(3, 3);
System.out.println(getCellNumber(4, 1));

void setCellTextColor(int x, int y, Color color)กำหนดสีของเนื้อหา (ข้อความ) ของเซลล์ด้วย(x, y)พิกัด

ตัวอย่าง:

setCellTextColor(2, 1, Color.GREEN);
setCellTextColor(0, 1, Color.NONE);

Color getCellTextColor(int x, int y)ส่งกลับสีของเนื้อหา (ข้อความ) ของเซลล์ด้วย(x, y)พิกัด

ตัวอย่าง:

Color textColor = getCellTextColor(1, 3);

เพื่อความสะดวกของคุณ มีหลายsetCellValueEx()วิธีที่มีชุดพารามิเตอร์ต่างกัน:

void setCellValueEx(int x, int y, Color cellColor, String value)กำหนดสีพื้นหลังและข้อความของเซลล์ด้วยพิกัด(x, y)เท่ากับcellColorและvalueตามลำดับ

ตัวอย่าง:

setCellValueEx(0, 2, Color.BLUE, "56");

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor)กำหนดสีพื้นหลัง ข้อความ และสีข้อความของเซลล์ที่มีพิกัด(x, y)เท่ากับcellColor, value, และtextColorตามลำดับ

ตัวอย่าง:

setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN);

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor, int textSize);กำหนดสีพื้นหลัง ข้อความ สีข้อความ และขนาดข้อความของเซลล์ที่มีพิกัด(x, y)เท่ากับcellColor, value, textColor, และtextSizeตามลำดับ

ตัวอย่าง:

setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN, 70);


2. การทำงานกับสี

เอ็นจิ้นเกม CodeGym มีColorชนิดพิเศษที่มีค่าเฉพาะสำหรับ 148 สี นอกจากนี้ยังมีค่าพิเศษNONEที่แสดงถึงการไม่มีสี

ตัวอย่างการทำงานกับสี

Color myColor = Color.WHITE;  // The color white is assigned to the myColor variable.
Color redColor = Color.RED; // The color red is assigned to the redColor variable.
Color blueColor = Color.BLUE; // The color blue is assigned to the blueColor variable.

คุณสามารถระบายสีเซลล์เป็นสีแดงโดยใช้คำสั่ง:

setCellColor(0, 2, Color.RED);

คุณสามารถตรวจสอบว่าเซลล์มีสีใดสีหนึ่งหรือไม่โดยใช้คำสั่งเช่น:

if (getCellColor(0,2) == Color.GREEN)
{
}

บางครั้งคุณอาจต้องได้รับอาร์เรย์ของทุกสีที่เป็นไปได้ ในการทำเช่นนี้ให้ใช้values()วิธีการ

ตัวอย่าง:

// An array containing every available color is assigned to the colors variable.
Color[] colors = Color.values();

การรับดัชนีของสีในจานสีทำได้ง่ายมาก เพียงใช้ordinal()วิธีการ:

Color color = Color.RED;
int redIndex = color.ordinal(); // Index of the color red

int blueIndex = Color.BLUE.ordinal(); // Index of the color blue

คุณยังสามารถรับสีได้อย่างง่ายดายด้วยดัชนีของมัน:

// The color whose index is 10 in the Color enum is assigned to the color variable.
Color color = Color.values()[10];


3. กล่องโต้ตอบ

ในตอนท้ายของเกม เราต้องแจ้งให้ผู้เล่นทราบว่าเขาหรือเธอชนะหรือแพ้ สำหรับโอกาสนี้และโอกาสอื่นๆ เอ็นจิ้นเกม CodeGym มีvoid showMessageDialog(Color cellColor, String message, Color textColor, int textSize)วิธีการพิเศษ ซึ่งแสดงกล่องโต้ตอบพร้อมmessageข้อความ
พารามิเตอร์ของวิธีนี้คือ:

  • cellColorเป็นสีพื้นหลังของกล่องโต้ตอบ
  • messageเป็นเนื้อความของข้อความ
  • textColorเป็นสีของข้อความ
  • textSizeคือขนาดของข้อความ

กล่องโต้ตอบจะปิดเองหากผู้ใช้กดแป้นเว้นวรรคหรือคลิกบนกล่องโต้ตอบด้วยเมาส์

ตัวอย่างการเรียกเมธอดนี้:

// Display a dialog box with a message
showMessageDialog(Color.BLACK, "EPIC FAIL", Color.RED, 80);


4. วิธีการยูทิลิตี้

เมื่อเขียนเกม คุณจะใช้ตัวเลขสุ่มบ่อยๆ เพื่อให้ง่ายต่อการรับหมายเลขสุ่ม คุณสามารถใช้วิธีการอรรถประโยชน์ของโปรแกรมเกม:

int getRandomNumber(int max)ส่งคืนตัวเลขสุ่มจาก0ถึง(max–1)รวม

int getRandomNumber(int min, int max)ส่งคืนตัวเลขสุ่มจากminถึง(max–1)รวม


5. มจพ.11+

เมื่อเรียกใช้โปรแกรมของคุณจาก IntelliJ IDEA คลาสที่สืบทอดคลาส Game อาจสร้างข้อผิดพลาดต่อไปนี้:

Error: JavaFX runtime components are missing, and are required to run this application

ในกรณีนี้ สำหรับแต่ละคลาสดังกล่าว คุณต้องทำตามขั้นตอนเหล่านี้เพียงครั้งเดียว:
  1. เปิดRunEditConfiguration
  2. สำหรับค่าของตัวเลือก VMให้ป้อนข้อมูลต่อไปนี้:
    --module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base

    ความสนใจ:

    ใน IntelliJ IDEA เวอร์ชันล่าสุด ฟิลด์ "ตัวเลือก VM" จะไม่แสดงตามค่าเริ่มต้น หากต้องการแสดง ให้กดALT+V

  3. กด: ใช้ตกลง
  4. เรียกใช้เกม
ความคิดเห็น
  • เป็นที่นิยม
  • ใหม่
  • เก่า
คุณต้องลงชื่อเข้าใช้เพื่อแสดงความคิดเห็น
หน้านี้ยังไม่มีความคิดเห็นใด ๆ