"अरे, अमीगो!"
"नमस्कार, कप्तान गिलहरी, सर!"
"एक नए, शीर्ष गुप्त मिशन के लिए तैयार हैं?"
"तैयार, सर!"
"फिर आपके पहले गुप्त कार्य के लिए निर्देश यहां दिए गए हैं:"
"अब हम «साँप» खेल लिखते हैं।"

"हाँ, श्रीमान! «साँप» खेल लिखो!"
"चिल्लाओ मत, सैनिक। क्या उन्होंने तुम्हें नहीं बताया कि यह एक शीर्ष गुप्त मिशन है।"
"आप इस मिशन को पूरा करने के लिए 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.