"Hey, Amigo!"

"Hello, Captain Squirrels, sir!"

"Ready for a new, top secret mission?"

"Ready, sir!"

"Then here are the instructions for your first secret assignment:"

"Now let's write the «Snake» game."

Big task: The Snake game - 1

"Yes, sir! Write the «Snake» game!"

"Don't shout, soldier. Didn't they tell you this is a top secret mission."

"You'll work with Agent IntelliJ IDEA to complete this mission. He'll bring you up to speed."

"He'll also provide all further instructions."

"May I proceed, sir?"

"Proceed."

undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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).
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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 }
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
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?
undefined
12
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
24
Task
Java Multithreading, level 2, lesson 18
Locked
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".
undefined
12
Task
Java Multithreading, level 2, lesson 18
Locked
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.
undefined
12
Task
Java Multithreading, level 2, lesson 18
Locked
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)).
undefined
24
Task
Java Multithreading, level 2, lesson 18
Locked
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).
undefined
6
Task
Java Multithreading, level 2, lesson 18
Locked
Snake (part 18)
Great! Start up Snake and enjoy it.