"야, 아미고!"
"안녕하세요, 다람쥐 선장입니다!"
"새로운 일급 비밀 임무를 수행할 준비가 되셨습니까?"
"준비되었습니다!"
"그럼 첫 번째 비밀 임무에 대한 지침은 다음과 같습니다."
"이제 «Snake» 게임을 작성해 봅시다."
"예, 선생님! «Snake» 게임을 작성하세요!"
"소리치지 마, 병사. 그들이 이것이 극비 임무라고 말하지 않았느냐."
"당신은 IntelliJ IDEA 요원과 협력하여 이 임무를 완수하게 될 것입니다. 그는 당신이 속도를 낼 수 있도록 도와줄 것입니다."
"그는 또한 모든 추가 지침을 제공할 것입니다."
"계속해도 될까요, 선생님?"
"진행하다."
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 1)
Now let's write the "Snake" game.
To do this, we need:
a) the snake itself (Snake class);
b) the room where it slithers around (Room class);
c) the mouse that the snake will eat (Mouse class).
Create the Snake, Room, and Mouse classes.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 2)
Now the most interesting thing is how the snake will be constructed.
Here's what we can say about the snake:
a) In fact, the snake is a set of sections or pieces. But we do know that one of them is the head.
b) The snake has a direction of movement.
This means we also need the following classes.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 3)
Great. I also added the KeyboardObserver class, which we'll use later.
Study it carefully. It will come in handy.
Let's start with the simplest thing: the Mouse class.
The mouse won't move. It will have only coordinates and nothing else.
Add two private fields to the Mouse class: int x and int y.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 4)
You've already declared coordinates x and y in the Mouse class, but how do you access them?
Create proper getters for these fields and a constructor with two int parameters (x and y).
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 5)
Now finish the SnakeDirection enum so that it contains the possible directions of the snake's movement.
Add 4 values: UP, DOWN, LEFT, RIGHT.
It should look something like this:
public enum SnakeDirection
{
UP,
RIGHT,
DOWN,
LEFT
}
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 6)
Now let's tackle the SnakeSection class.
It will describe one piece of the snake.
And what does each piece have?
Right! Coordinates.
Add two fields to the SnakeSection class: int x and int y.
Add getters for these fields and a constructor with two int parameters.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 7)
We've described the "pieces of the snake". Now we'll deal with the snake itself.
The snake must have a list of SnakeSections and a head.
Let the head just be the very first piece (index 0).
The snake also has a direction in which it moves by default.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 8)
It's the Snake class constructor's turn.
Initially, the snake consists of one piece: the head.
And what does it require?
What needs to be passed to the constructor?
The coordinates of the snake, of course.
Here's what we need.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 9)
The most important class remains: Room.
What do we need to describe it?
One, the dimensions of the room (width and height).
Two, a Snake.
Three, a Mouse.
Here's what we need:
a) create int width and height variables in the Room class.
b) create a Snake snake variables in the Room class.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 10)
Let's tackle the main method.
First, you need to create a static Room game field in the Room class. The variable should be accessible from all classes.
Now in the main method:
a) Create the snake, a Snake object.
b) Create the room, a Room object, and pass the width, height, and snake to it.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 11)
Now the logic for controlling the mouse.
Two things happen with the mouse.
First, the snake eats the mouse.
Second, a new mouse appears at a random place in the room.
You must write and implement the createMouse() method in the Room class.
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 12)
We have a snake, so let's have a mouse.
Add the following to the main method:
a) a call the createMouse() method.
b) a call the run() method.
Nothing will happen without it. It has all the main logic.
Not bad, but I'll still make a couple of changes.
For example, how about writing a sleep method?
12
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 13)
This time I'll invite you to write a special sleep() method that will pause
depending on the length of the snake (the number of elements in sections).
Think of some slick algorithm. Make the delay 500 milliseconds on the first level,
and gradually drop it to 300 by Level 11. And to 200 by Level 15.
24
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 14)
Now let's work on the print() method.
Here's what we need:
a) display a rectangle of dots, whose dimensions is width x height;
b) represent the body of the snake with an "x";
c) draw the snake's head with an "X".
12
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 15)
Now we need to finish the snake.
Here's my suggestion regarding the snake's motion:
The snake consists of pieces. Each turn, let's just add one piece at the head,
and delete the last one. This will make the snake appear to slither.
12
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 16)
Nothing is forever... That's why the snake must die if it hits a wall or itself.
There's a very simple check to determine whether a snake intersects itself:
does the sections list contain a "new snake's head"?
This is what the code will look like:
if (sections.contains(head)).
24
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 17)
Now we'll finish the Snake class.
The snake consists of pieces. Each turn, let's just add one piece at the head,
and delete the last one. This will make the snake appear to slither.
You need to add the piece next to the current head (index 0).
6
과제
Java Multithreading, 레벨 2, 레슨 18
Snake (part 18)
Great!
Start up Snake and enjoy it.