CodeGym /Blog Jawa /Acak /bagean Games ing CodeGym: Penanganan acara
John Squirrels
tingkat
San Francisco

bagean Games ing CodeGym: Penanganan acara

Diterbitake ing grup
"Game" saka CodeGym minangka bagean anyar kanthi tugas menarik sing gedhe ing ngendi sampeyan nggawe versi game populer sampeyan dhewe. Iku prasaja: saben project game dipérang dadi subtasks. Rampungake siji-siji lan game rampung. Sampeyan bisa enggo bareng ing jaringan sosial lan ngajak kanca-kanca kanggo muter. Bagean "Game" ing CodeGym: Penanganan acara - 1Ing artikel iki, kita bakal njlèntrèhaké kanthi rinci cara nangani acara sing digunakake kanggo nulis game.

1. Nggarap mouse

Mesin game duwe rong cara kanggo nggarap mouse:
  • void onMouseLeftClick(int x, int y);

  • void onMouseRightClick(int x, int y);

Sampeyan mung ngumumake metode kasebut ing kelas sampeyan sing duwe warisan Game, lan tambahake kode sing dikarepake. Mesin game bakal nelpon nalika pangguna ngeklik tombol mouse.
  1. 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.

  2. onMouseRightClick(int x, int y)- disebut nalika tombol mouse tengen diklik. Cara iki dianggo kaya onMouseLeftClick(int x, int y)metode.

Mangkene conto nggunakake metode iki:

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);

Yen sampeyan pengin soko kelakon nalika pangguna mencet tombol, wara-wara cara iki ing kelas sing oleh warisan Game, lan nambah kode kanggo wong-wong mau. Mesin game bakal nelpon nalika pangguna menet utawa ngeculake tombol.
  1. onKeyPress(Key key)— disebut nalika sembarang tombol dipencet. Parameter tombol yaiku tombol sing dipencet (utawa Key.UNKNOWN).

  2. onKeyReleased(Key key)- disebut nalika sembarang tombol dirilis. Parameter tombol yaiku kunci sing cocog (utawa Key.UNKNOWN).

Mangkene conto nggunakake metode iki:

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 nelpon onTurnTimer()cara. Maneh lan maneh ing salawas-lawase - nganti wektu dipateni. Dadi carane sampeyan nggunakake timer?
  1. 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 nelpon onTurn()cara saben milliseconds Ms.

  2. Ganti metode onTurn(int).

    Kanggo nindakake iki, sampeyan kudu ngumumake void onTurn(int step)metode ing kelas sing duwe warisan Game. Cara iki bakal disebut dening mesin game. Apa maneh, kanthi saben telpon, mesin game bakal ngliwati metode pengenal urutan kanggo telpon (1, 2, 3, ...).

  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.

  4. 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 antarane onTurn()telpon bakal ganti.

Tuladhane:

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:
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION