CodeGym /Kursus Jawa /All lectures for JV purposes /Nyilem jero menyang mesin game

Nyilem jero menyang mesin game

All lectures for JV purposes
tingkat , wulangan
kasedhiya

1. Nggarap sel saka lapangan playing

Iku apik yen kita bisa dibagi lapangan playing menyang sel. Nanging apa sing bisa kita lakoni karo sel kasebut?

Kanggo saben sel ing lapangan dolanan, kita bisa nyetel:

  • werna sel (warna latar mburi sel);
  • teks (iki bisa dadi teks utawa nomer);
  • werna teks;
  • ukuran teks minangka persentasi saka ukuran sel.

Ayo nimbang cara kanggo nggarap sel ing lapangan dolanan:

void setCellColor(int x, int y, Color color)nyetel werna sel kanthi koordinat (x, y)sing padha karo color.

Tuladha:

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

Color getCellColor(int x, int y)ngasilake werna sel kanthi koordinat (x, y).

Tuladha:

Color myColor = getCellColor(2, 0);

void setCellValue(int x, int y, String value)menehi teks menyang String valuesel kanthi koordinat (x, y).

Tuladha:

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

String getCellValue(int x, int y)ngasilake teks sing ana ing sel kanthi koordinat (x, y).

Tuladha:

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

void setCellTextSize(int x, int y, int size)nyetel ukuran teks ing sel kanthi koordinat (x, y), ing ngendi sizedhuwur teks minangka persentasi saka dhuwur sel.

Tuladha:

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

int getCellTextSize(int x, int y)ngasilake ukuran isi ing sel kanthi koordinat (x, y).

Tuladha:

int size = getCellTextSize(2 , 0);

void setCellNumber(int x, int y, int value)menehi nomer int valuemenyang sel kanthi koordinat (x, y).

Tuladha:

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

int getCellNumber(int x, int y)ngasilake nomer sing ana ing sel kanthi koordinat (x, y). Yen sel ora ngemot nomer, sel bakal ngasilake 0.

Tuladha:

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

void setCellTextColor(int x, int y, Color color)nyetel warna isi (teks) sel kanthi koordinat (x, y).

Tuladha:

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

Color getCellTextColor(int x, int y)ngasilake warna isi (teks) sel kanthi koordinat (x, y).

Tuladha:

Color textColor = getCellTextColor(1, 3);

Kanggo penak, ana macem-macem setCellValueEx()cara kanthi paramèter sing beda:

void setCellValueEx(int x, int y, Color cellColor, String value)nyetel werna latar mburi lan teks sel kanthi koordinat (x, y)padha karo cellColorlan value, mungguh.

Tuladha:

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

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor)nyetel werna latar mburi, teks, lan werna teks sel kanthi koordinat (x, y)padha karo cellColor, value, lan textColor, mungguh.

Tuladha:

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

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor, int textSize);nyetel werna latar mburi, teks, werna teks, lan ukuran teks sel kanthi koordinat (x, y)padha karo cellColor, value, textColor, lan textSize, mungguh.

Tuladha:

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


2. Nggarap werna

Mesin game CodeGym nduweni Colorjinis khusus sing ngemot nilai unik kanggo 148 werna. Uga nduweni NONEnilai khusus sing nuduhake ora ana warna.

Conto nggarap warna

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.

Sampeyan bisa werna abang sel nggunakake printah:

setCellColor(0, 2, Color.RED);

Sampeyan bisa mriksa manawa sel minangka warna tartamtu kanthi prentah kaya:

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

Kadhangkala sampeyan kudu njaluk macem-macem warna sing bisa. Kanggo nindakake iki, gunakake values()metode kasebut.

Tuladha:

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

Entuk indeks warna ing palet warna gampang banget - mung gunakake ordinal()cara:

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

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

Sampeyan uga bisa kanthi gampang entuk warna kanthi indeks:

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


3. Kothak dialog

Ing pungkasan game, kita kudu menehi katrangan marang pemain yen dheweke menang utawa kalah. Kanggo iki lan kesempatan liyane, mesin game CodeGym duwe void showMessageDialog(Color cellColor, String message, Color textColor, int textSize)cara khusus, sing nampilake kothak dialog kanthi messagepesen kasebut.
Parameter metode iki yaiku:

  • cellColoryaiku werna latar mburi kothak dialog
  • messageyaiku teks pesen
  • textColoryaiku warna teks pesen
  • textSizeyaiku ukuran teks pesen

Kothak dialog nutup dhewe yen pangguna menet spasi utawa ngeklik kothak dialog nganggo mouse.

Conto nelpon cara iki:

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


4. Metode Utilitas

Nalika nulis game, sampeyan bakal kerep nggunakake nomer acak. Kanggo nggampangake entuk nomer acak, sampeyan bisa nggunakake cara sarana mesin game:

int getRandomNumber(int max)ngasilake nomer acak saka 0kanggo (max–1)klebu.

int getRandomNumber(int min, int max)ngasilake nomer acak saka minkanggo (max–1)klebu.


5. JDK 11+

Nalika mbukak program saka IntelliJ IDEA, kelas sing marisi kelas Game bisa nyebabake kesalahan ing ngisor iki:

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

Ing kasus iki, kanggo saben kelas kasebut, sampeyan kudu nindakake langkah-langkah iki sapisan:
  1. Bukak RunEditConfiguration
  2. Kanggo nilai pilihan VM , ketik ing ngisor iki:
    --module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base

    PERHATIAN:

    Ing versi anyar saka IntelliJ IDEA, kolom "pilihan VM" ora ditampilake kanthi gawan. Kanggo nampilake, penet ALT + V

  3. Pencet: AplikasiOK
  4. Mbukak game.
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION