
1. Nggarap mouse
Mesin game duwe rong cara kanggo nggarap mouse:-
void onMouseLeftClick(int x, int y);
-
void onMouseRightClick(int x, int y);
Game
, lan tambahake kode sing dikarepake. Mesin game bakal nelpon nalika pangguna ngeklik tombol mouse.
-
onMouseLeftClick(int x, int y)
- disebut dening engine nalika tombol mouse kiwa diklik. Parameter kasebut minangka koordinat sel ing lapangan dolanan ing ngendi klik ana. Sèl kiwa ndhuwur duwé koordinat (0, 0). Sampeyan kudu ngganti cara iki kanggo nggunakake. -
onMouseRightClick(int x, int y)
- disebut nalika tombol mouse tengen diklik. Cara iki dianggo kayaonMouseLeftClick(int x, int y)
metode.
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import com.codegym.engine.cell.Key;
public class MySuperGame extends Game {
@Override
public void initialize() {
// Set the size of the playing field to 3x3
setScreenSize(3, 3);
// Paint the playing field white
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
setCellColor(x, y, Color.WHITE);
}
}
}
@Override
public void onMouseLeftClick(int x, int y) {
// Set "X" in the cell where the left mouse click occurred
setCellValue(x, y, "X");
}
@Override
public void onMouseRightClick(int x, int y) {
// Clear the cell where the right mouse click occurred
setCellValue(x, y, "");
}
}
2. Nggarap keyboard
Mesin game duwe rong cara kanggo nggarap keyboard:-
void onKeyPress(Key key);
-
void onKeyReleased(Key key);
-
onKeyPress(Key key)
— disebut nalika sembarang tombol dipencet. Parameter tombol yaiku tombol sing dipencet (utawa Key.UNKNOWN). -
onKeyReleased(Key key)
- disebut nalika sembarang tombol dirilis. Parameter tombol yaiku kunci sing cocog (utawa Key.UNKNOWN).
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import com.codegym.engine.cell.Key;
public class MySuperGame extends Game {
@Override
public void initialize() {
// Set the size of the playing field to 3x3
setScreenSize(3, 3);
// Paint the playing field white
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
setCellColor(x, y, Color.WHITE);
}
}
}
@Override
public void onKeyPress(Key key) {
// When the space bar is pressed, the center cell turns yellow
if (key == Key.SPACE) {
setCellColor(1, 1, Color.YELLOW);
}
}
@Override
public void onKeyReleased(Key key) {
// When the space bar is released, the center cell changes back to white
if (key == Key.SPACE) {
setCellColor(1, 1, Color.WHITE);
}
}
}
Penting! Ing versi mesin game saiki, jinis Key nduweni set winates sangang nilai:Nilai | Tombol ditekan dening pangguna |
---|---|
Key.ENTER | Pangguna menet Enter |
Kunci.MULUNG | Pangguna menet Esc |
Kunci.JAGA | Pangguna menet Pause |
Kunci. SPACE | Pangguna menet Space |
Kunci.KIRI | Pangguna menet tombol panah kiwa |
Kunci. TEngen | Pangguna menet tombol panah tengen |
Kunci.UP | Pangguna menet tombol panah munggah |
Kunci. MUNDUR | Pangguna menet tombol mudhun |
Kunci.Ora ngerti | Pangguna menet tombol liyane saka ndhuwur |
3. Nggarap timer
Akeh game sing kedadeyan ing wektu nyata, yaiku sanajan pangguna ora nindakake apa-apa, acara kasebut isih ana ing game kasebut. Kanggo ngaktifake sampeyan ngleksanakake game kasebut, kita nambahake timer menyang mesin game. Kerjane kaya iki: sampeyan nguripake timer lan nyetel interval wektu. Contone, 500 milidetik. Banjur, saben setengah detik mesin game nelpononTurnTimer()
cara. Maneh lan maneh ing salawas-lawase - nganti wektu dipateni. Dadi carane sampeyan nggunakake timer?
-
Nguripake timer.
Kanggo nindakake iki, ana
void setTurnTimer(int timeMs)
cara khusus. Cara kasebut njupuk minangka argumen interval antarane panggilan ing milidetik (1 milidetik = 1/1000 detik). Sampeyan mung kudu nelpon sapisan, lan mesin game bakal miwiti nelpononTurn()
cara saben milliseconds Ms. -
Ganti metode onTurn(int).
Kanggo nindakake iki, sampeyan kudu ngumumake
void onTurn(int step)
metode ing kelas sing duwe warisanGame
. Cara iki bakal disebut dening mesin game. Apa maneh, kanthi saben telpon, mesin game bakal ngliwati metode pengenal urutan kanggo telpon (1, 2, 3, ...). -
Pateni timer.
Yen wektu ora dibutuhake maneh, contone, nalika pangguna ngrampungake game, sampeyan bisa mateni. Kanggo nindakake iki, sampeyan mung kudu nelpon
stopTurnTimer()
cara. -
Nyepetake utawa ngganti wektu.
Ing sawetara game, tingkat kedadeyan kedadeyan terus saya cepet, saengga luwih gampang nyepetake wektu (nyuda wektu ing antarane telpon). Ora ana sing luwih gampang: nelpon
setTurnTimer(int timeMs)
maneh kanthi nilai anyar, lan wektu ing antaraneonTurn()
telpon bakal ganti.
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
public class MySuperGame extends Game {
…
@Override
public void initialize() {
// Create a playing field that is 3 cells x 3 cells
setScreenSize(3, 3);
showGrid(false);
setCellValueEx(1, 1, Color.BLUE, "X", Color.ORANGE, 50);
setTurnTimer(500); // Turn on the timer, the interval between calls is 500ms.
}
@Override
public void onTurn(int step) {
if(step == 100) {
stopTurnTimer(); // If 100 calls have been made, turn off the timer
}
if (step % 2 == 1) {
// If this call is odd, set the cell background to red
setCellColor(1, 1, Color.RED);
}
else {
// If this call is even, set the cell background to blue
setCellColor(1, 1, Color.BLUE);
}
}
…
}
Ing conto prasaja iki, kita nggawe lapangan sing 3 sel x 3 sel. Banjur kita miwiti timer sing bakal nelpon onTurn()
cara saben setengah detik. Saben setengah detik, werna sel bakal ganti, nanging isine ora bakal owah. Sawise 50 detik, werna ora bakal diganti maneh. Sing kabeh kanggo saiki! Yen sampeyan pengin sinau luwih lengkap babagan bagean "Game", iki sawetara dokumentasi migunani sing bisa mbantu:
GO TO FULL VERSION