1. ์๊ฐ
๊ฒ์ ์์ง์ ํ๋ฉด์ ๋ฌด์ธ๊ฐ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฐ๋ง ์ฌ์ฉํ ์ ์๋ค๋ฉด ๊ฑฐ์ ์ธ๋ชจ๊ฐ ์์ ๊ฒ์ ๋๋ค. ๋ณธ๊ฒฉ์ ์ธ ๊ฒ์์ ์์ฑํ๋ ค๋ฉด ์ฌ์ฉ์์ ์ํธ ์์ฉํด์ผ ํฉ๋๋ค! ๋ ์ ํํ ๋งํ๋ฉด ์ฐ๋ฆฌ ํ๋ก๊ทธ๋จ์ ์ฌ์ฉ์ ํ๋์ ์ถ์ ํ๊ณ ์ด์ ์๋ตํด์ผ ํฉ๋๋ค.
์ด๋ฅผ ์ํด CodeGym ๊ฒ์ ์์ง์๋ ์ฌ์ฉ์๊ฐ ๋ง์ฐ์ค ๋ฒํผ์ด๋ ํค๋ณด๋ ํค๋ฅผ ํด๋ฆญํ ๋ ํธ์ถํ๋ ํน์ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
์์ ์ ์์ ์ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ง์ ์์ฑํ๋ฉด CodeGym ๊ฒ์ ์์ง์ด ํด๋น ๋ฉ์๋ ํธ์ถ์ ๋ด๋นํ๋ค๋ ๊ฒ์ ๋๋ค. ์ฝ๋์์ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ ์ธํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ์ด๊ฒ์ ์๋ฆฌ๋ณด๋ค ํ๊ธฐ ์ฝ์ต๋๋ค.
2. ๋ง์ฐ์ค๋ก ์์ ํ๊ธฐ
๊ฒ์ ์์ง์๋ ๋ง์ฐ์ค๋ก ์์ ํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
void onMouseLeftClick(int x, int y)
void onMouseRightClick(int x, int y)
ํด๋์ค๋ฅผ ์์ํ๋ ์์ ์ ํด๋์ค์์ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ ์ธํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค Game
. ๊ทธ๋ฐ ๋ค์ ์ด๋ฌํ ๋ฉ์๋์ ์ํ๋ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค. ๊ฒ์ ์์ง์ ์ฌ์ฉ์๊ฐ ๋ง์ฐ์ค ๋ฒํผ์ ํด๋ฆญํ ๋ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
onMouseLeftClick(int x, int y)
๋ง์ฐ์ค ์ผ์ชฝ ๋ฒํผ์ ํด๋ฆญ ํ๋ฉด ์์ง์ ์ํด ํธ์ถ๋ฉ๋๋ค . ํด๋น ๋งค๊ฐ๋ณ์๋ ํด๋ฆญ์ด ๋ฐ์ํ ๊ฒฝ๊ธฐ์ฅ ์
์ ์ขํ์
๋๋ค. ์ผ์ชฝ ์๋จ ์
์ ์ขํ๋ ์
๋๋ค (0,0)
. @Override
์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ฉ์๋ ์ด๋ฆ ์์ ์ฃผ์์ ๋ฐฐ์นํ์ฌ ์ฌ์ ์ํด์ผ ํฉ๋๋ค .
onMouseRightClick(int x, int y)
๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ํธ์ถ๋ฉ๋๋ค . ๋ฐฉ๋ฒ ๊ณผ ์ ์ฌํ๊ฒ ์๋ํฉ๋๋ค onMouseLeftClick(int x, int y)
.
์ด๋ฌํ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ์:
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) {
// Put a "X" in the cell that was clicked with the left mouse button
setCellValue(x, y, "X");
}
@Override
public void onMouseRightClick(int x, int y) {
// Clear the cell that was clicked with the right mouse button
setCellValue(x, y, "");
}
}
์์ ์์์ ์ฐ๋ฆฌ๋ ๋ ๋ฉ์๋๋ฅผ ๋ชจ๋ ์ ์ธํ์ต๋๋ค: onMouseLeftClick()
๋ฐ onMouseRightClick()
. ์ฒซ ๋ฒ์งธ ๋ฉ์๋๋ ์ฌ์ฉ์๊ฐ ๊ฒฝ๊ธฐ์ฅ์ ์
์์ ๋ง์ฐ์ค ์ผ์ชฝ ๋ฒํผ์ ํด๋ฆญํ ๋ ํธ์ถ๋ฉ๋๋ค. ๋ ๋ฒ์งธ ๋ฉ์๋๋ ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ํธ์ถ๋ฉ๋๋ค.
์ธ์๋ก CodeGym ๊ฒ์ ์์ง์ (x, y)
๋ง์ฐ์ค๋ฅผ ํด๋ฆญํ ๊ฒฝ๊ธฐ์ฅ ์
์ ์ขํ๋ฅผ ์ ๋ฌํฉ๋๋ค.
3. ํค๋ณด๋๋ก ์์ ํ๊ธฐ
๊ฒ์ ์์ง์๋ ํค๋ณด๋ ์์ ์ ์ํ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
void onKeyPress(Key key);
void onKeyReleased(Key key);
์ฌ์ฉ์๊ฐ ํค๋ฅผ ๋๋ฅผ ๋ ๋ฌด์ธ๊ฐ๋ฅผ ์ํํ๋ ค๋ฉด ํด๋์ค๋ฅผ ์์ํ๋ ํด๋์ค์์ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ ์ธํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค Game
.
์ด๋ฌํ ๋ฉ์๋์ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ฌ์ฉ์๊ฐ ํค๋ฅผ ๋๋ฅด๊ฑฐ๋ ๋์ ๋ ๊ฒ์ ์์ง์ด ์ด๋ฅผ ํธ์ถํฉ๋๋ค.
onKeyPress(Key key)
์๋ฌด ํค๋ ๋๋ฅผ ๋ ํธ์ถ๋ฉ๋๋ค . ๋๋ฅธ ํค(๋๋ )์ ๊ฐ์ด ๋ฉ์๋์ ๋งค๊ฐ ๋ณ์๋ก ์ ๋ฌ๋ฉ๋๋ค . ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ฉ์๋ ์ด๋ฆ ์์ ์ฃผ์์ ๋ฐฐ์นํ์ฌ ์ฌ์ ์ํด์ผ ํฉ๋๋ค .Key.UNKNOWN
key
@Override
onKeyReleased(Key key)
ํค๋ฅผ ๋์ผ๋ฉด ํธ์ถ ๋ฉ๋๋ค . ํด๋น ํค(๋๋ )์ ๊ฐ์ด ๋งค๊ฐ๋ณ์์ ํ ๋น๋ฉ๋๋ค . ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ฉ์๋ ์ด๋ฆ ์์ ์ฃผ์์ ๋ฐฐ์นํ์ฌ ์ฌ์ ์ํด์ผ ํฉ๋๋ค .Key.UNKNOWN
key
@Override
์ด๋ฌํ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ์:
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 spacebar is pressed, the center cell turns yellow
if (key == Key.SPACE) {
setCellColor(1, 1, Color.YELLOW);
}
}
@Override
public void onKeyReleased(Key key) {
// When the spacebar is released, the center cell returns to white
if (key == Key.SPACE) {
setCellColor(1, 1, Color.WHITE);
}
}
}
4. ์ง์๋๋ ๋ชจ๋ ํค ๋ชฉ๋ก
CodeGym ์์ง์ onKeyPress()
๋ฐ onKeyReleased()
๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋๋ฆฐ(ํด์ ๋) ํค์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ฌํฉ๋๋ค. ์ด๋ฅผ ์ํด CodeGym ์์ง์๋ Key
.
์์ง์ ํ์ฌ ๋ฒ์ ์์ Key
์ ํ์ ์ ํ๋ 9๊ฐ ๊ฐ ์ธํธ๋ง ์ง์ํฉ๋๋ค.
๊ฐ | ์ฌ์ฉ์๊ฐ ๋๋ฅธ ๊ฒ |
---|---|
|
์ฌ์ฉ์๊ฐ Enter ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ Esc ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์ผ์ ์ค์ง ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์คํ์ด์ค ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์ผ์ชฝ ํ์ดํ ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์ค๋ฅธ์ชฝ ํ์ดํ ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์์ชฝ ํ์ดํ ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์๋์ชฝ ํ์ดํ ํค๋ฅผ ๋๋ ์ต๋๋ค. |
|
์์ ๋์ด๋ ๊ฒ ์ด์ธ์ ๋ชจ๋ ํค |
ํ๋ ์ด์ด๊ฐ ์คํ์ด์ค๋ฐ๋ฅผ ๋๋ฅด๋ฉด ๋ฉ์๋๊ฐ ๋งค๊ฐ๋ณ์๋ก onKeyPress()
ํธ์ถ๋ฉ๋๋ค . Key.SPACE
์ฌ์ฉ์๊ฐ ์ผ์ชฝ ํ์ดํ๋ฅผ ๋๋ฅด๋ฉด ๋งค๊ฐ๋ณ์๋ ๊ฐ ๋ฉ๋๋ค Key.LEFT
. ์ฌ์ฉ์๊ฐ ์ ๋ชฉ๋ก์ ์๋ ํค๋ฅผ ๋๋ฅด๋ฉด ๋ฉ์๋๊ฐ ๋งค๊ฐ ๋ณ์๋ก onKeyPress()
ํธ์ถ๋ฉ๋๋ค .Key.UNKNOWN
์ฌ๊ธฐ์ ๋ฌธ์ ๋ ๊ธฐ์กด CodeGym ๊ฒ์์ด ํด๋ํฐ์์ ์คํ๋๋๋ก ์ค๊ณ๋์๋ค๋ ๊ฒ์ ๋๋ค. ํ์ค ํค๋ณด๋ ๋์ 8๊ฐ์ ๊ฐ์ ๋ฒํผ์ด ์์ต๋๋ค.
5. ํ์ด๋จธ๋ก ์์ ํ๊ธฐ
๋ง์ ๊ฒ์์ด ์ค์๊ฐ์ผ๋ก ์งํ๋ฉ๋๋ค. ์ฆ, ์ฌ์ฉ์๊ฐ ์๋ฌด๊ฒ๋ ํ์ง ์์๋ ํน์ ์ด๋ฒคํธ๊ฐ ๊ฒ์์์ ๊ณ์ ๋ฐ์ํฉ๋๋ค. ์ด๋ฌํ ๊ฒ์์ ๊ตฌํํ ์ ์๋๋ก ๊ฒ์ ์์ง์ ํ์ด๋จธ๋ฅผ ์ถ๊ฐํ์ต๋๋ค.
์๋ ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ํ์ด๋จธ๋ฅผ ์ผ๊ณ ํธ๋ฆฌ๊ฑฐ๋์ด์ผ ํ๋ ์๊ฐ์ ์ค์ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด 500ms์
๋๋ค. ๊ทธ๋ฐ ๋ค์ 0.5์ด๋ง๋ค CodeGym ๊ฒ์ ์์ง์ด ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค onTurn()
. ๋ฌดํํ ํ์ โ ํ์ด๋จธ๊ฐ ๊บผ์ง ๋๊น์ง.
ํ์ด๋จธ๋ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์?
1. ํ์ด๋จธ ์ผ๊ธฐ
ํ์ด๋จธ๋ฅผ ์ผ๋ void setTurnTimer(int timeMs)
๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ด ๋ฉ์๋๋ ์ฝ๋ฐฑ ์ฌ์ด์ ๊ฐ๊ฒฉ ๊ธฐ๊ฐ(๋ฐ๋ฆฌ์ด ๋๋ 1/1000์ด)์ ์ธ์๋ก ์ฌ์ฉํฉ๋๋ค. ์ด ๋ฉ์๋๋ฅผ ํ ๋ฒ๋ง ํธ์ถํ๋ฉด ๊ฒ์ ์์ง์ด ๋ฐ๋ฆฌ์ด onTurn()
๋ง๋ค ๋ฉ์๋ ํธ์ถ์ ์์ํฉ๋๋ค timeMs
.
2. onTurn(int)
๋ฉ์๋ ์ฌ์ ์
์ด๋ ๊ฒ ํ๋ ค๋ฉด ํด๋์ค void onTurn(int step)
๋ฅผ ์์ํ๋ ํด๋์ค์์ ๋ฉ์๋๋ฅผ ์ ์ธํด์ผ ํฉ๋๋ค Game
. ๊ฒ์ ์์ง์ ์ด ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค. ํธ์ถํ ๋๋ง๋ค ์์ง์ ๋ฉ์๋ ํธ์ถ์ ์๋ณํ๋ ์ผ๋ จ ๋ฒํธ( 1
, 2
, 3
, ...)๋ฅผ ์ ๋ฌํฉ๋๋ค.
3. ํ์ด๋จธ๋ฅผ ๋๋๋ค.
์๋ฅผ ๋ค์ด ์ฌ์ฉ์๊ฐ ๊ฒ์์ ๋๋๊ธฐ ๋๋ฌธ์ ํ์ด๋จธ๊ฐ ๋ ์ด์ ํ์ํ์ง ์์ผ๋ฉด ํ์ด๋จธ๋ฅผ ๋ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ ค๋ฉด stopTurnTimer()
๋ฉ์๋๋ฅผ ํธ์ถํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
4 ํ์ด๋จธ ๊ฐ์/๋ณ๊ฒฝ
์ผ๋ถ ๊ฒ์์์๋ ์ด๋ฒคํธ ๋น๋๊ฐ ์ ๊ธฐ์ ์ผ๋ก ์ฆ๊ฐํ๋ฏ๋ก ํ์ด๋จธ ์๋๋ฅผ ๋์ด๋ ๊ฒ, ์ฆ ์ฝ๋ฐฑ ์ฌ์ด์ ์๊ฐ์ ์ค์ด๋ ๊ฒ์ด ํธ๋ฆฌํ ๊ฒ์
๋๋ค. ์ด๋ณด๋ค ๋ ์ฌ์ธ ์๋ ์์ต๋๋ค. setTurnTimer(int timeMs)
์ ๊ฐ์ผ๋ก ๋ค์ ํธ์ถํ๋ฉด ์ ๋ํ ํธ์ถ ์ฌ์ด์ ์๊ฐ์ด onTurn()
๋ณ๊ฒฝ๋ฉ๋๋ค.
์:
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
public class MySuperGame extends Game {
...
@Override
public void initialize() {
// Create a 3x3 playing field
setScreenSize(3, 3);
showGrid(false);
setCellValueEx(1, 1, Color.BLUE, "X", Color.ORANGE, 50);
setTurnTimer(500); // Turn on the timer. The interval between callbacks is 500ms.
}
@Override
public void onTurn(int step) {
if (step == 100) {
stopTurnTimer(); // If this is the 100th callback, then turn off the timer
}
if (step % 2 == 1) {
// If this is an odd calllback, then set the cell background to red
setCellColor(1, 1, Color.RED);
} else {
// If this is an even callback, then set the cell background to blue
setCellColor(1, 1, Color.BLUE);
}
}
...
}
์ด ๊ฐ๋จํ ์์์๋ 3์
x 3์
์ธ ํ๋๋ฅผ ๋ง๋ค์์ต๋๋ค. onTurn()
๊ทธ๋ฐ ๋ค์ 0.5์ด๋ง๋ค ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ํ์ด๋จธ๋ฅผ ์ผญ๋๋ค .
์ฌ๊ธฐ์์ ์ค์ ์ ์ ์์์ 0.5์ด๋ง๋ค ๋ณ๊ฒฝ๋ฉ๋๋ค. ์ ์ ํ ์คํธ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. 50์ด(์ฝ๋ฐฑ 100ํ) ํ ์์ ๋ณ๊ฒฝ์ด ์ค์ง๋๊ณ ํ์ด๋จธ๊ฐ ๊บผ์ง๋๋ค.
GO TO FULL VERSION