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. JDK 11+
IntelliJ IDEA에서 프로그램을 실행할 때 Game 클래스를 상속하는 클래스에서 다음 오류가 발생할 수 있습니다.
Error: JavaFX runtime components are missing, and are required to run this application
이 경우 이러한 각 클래스에 대해 다음 단계를 한 번 수행해야 합니다.
- 실행 열기 → 구성 편집
- VM 옵션 값으로 다음을 입력합니다.
--module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base
주목:
최신 버전의 IntelliJ IDEA에서는 "VM 옵션" 필드가 기본적으로 표시되지 않습니다. 표시하려면 ALT+V를 누르십시오.
- 누르기: 적용 → 확인
- 게임을 실행합니다.