„Здрасти, Амиго!“
„Здравейте, капитан Катерици, сър!“
„Имам нова мисия за вас. Нека напишем играта Tetris. “
„Това е секретна операция. Всички допълнителни инструкции ще бъдат предоставени чрез IntelliJ IDEA.“
"Да, сър! Започвам моята тайна мисия..."
7
Задача
Java Collections, ниво, урок
Tetris (part 1)
Let's write Tetris!
Our Tetris game consists of two things: a game fields and game pieces that fall from above.
So to start, create three classes: Field, GamePiece, and Tetris.
We will also need a method main() in the Tetris class.
7
Задача
Java Collections, ниво, урок
Tetris (part 2)
Great!
Now add the following two fields to the Tetris class: Field field and GamePiece gamePiece.
7
Задача
Java Collections, ниво, урок
Tetris (part 3)
There's a field and a game piece, but how do we get them?
Add getters for the field and gamePiece fields.
7
Задача
Java Collections, ниво, урок
Tetris (part 4)
We'll also need a couple of methods.
Add run() and step() methods to the Tetris class:
run() will be responsible for the entire game.
The step() method will be responsible for a single step in the game.
The return type is void for both methods.
7
Задача
Java Collections, ниво, урок
Tetris (part 5)
Now you need to create the Tetris object itself.
Add a static Tetris field to the Tetris class.
The game field must NOT be private.
Then in the main() method, create a Tetris object and save it in this variable.
Then add a call to the run() method.
7
Задача
Java Collections, ниво, урок
Tetris (part 6)
Now let's move on to the Field class.
It will be responsible for storing information about which cells of the game field are currently occupied and which are empty.
Add the following two fields to the Field class: int width and int height.
We also need a matrix, i.e. a two-dimensional array.
7
Задача
Java Collections, ниво, урок
Tetris (part 7)
We'll need 4 more methods in the Field class:
a) void print() - The object draws its current state on the screen;
b) void removeFullLines() - Completely full rows are removed from the matrix, and higher rows are moved down;
c) Integer getValue(int x, int y) - Returns the matrix value with coordinates (x, y).
7
Задача
Java Collections, ниво, урок
Tetris (part 8)
Now create the backbone of the GamePiece class.
This class will describe the falling game pieces.
We need to know the coordinates and shape.
The two variables x and y will be responsible for the coordinates.
The matrix is responsible for the shape.
7
Задача
Java Collections, ниво, урок
Tetris (part 9)
You may have noticed that we're writing the program "top down".
First, we decide what classes we need. Then we decide about the methods.
And then we start writing the code for the methods.
We're breaking a big task into many small ones.
7
Задача
Java Collections, ниво, урок
Tetris (part 10)
We also need methods to control the game piece.
Add the following methods to the GamePiece class: left() - For moving game pieces to the left. right() - For moving game pieces to the right. down() - For moving game pieces down. up() - For moving game pieces up.
7
Задача
Java Collections, ниво, урок
Tetris (part 11)
Now create a GamePieceFactory class.
We'll use it to create game pieces of various shapes.
For now, it will have only one static createRandomGamePiece() method:
static GamePiece createRandomGamePiece(int x, int y).
14
Задача
Java Collections, ниво, урок
Tetris (part 12)
In Tetris, we control the movement of the game piece using the keyboard.
There are 4 actions:
move left (left arrow key),
move right (right arrow key),
rotate the game piece (number 5 on the numeric keypad),
drop the game piece (space).
14
Задача
Java Collections, ниво, урок
Tetris (part 13)
Now we'll start to implement the methods we've created.
Implement the print() method in the Field class:
a) The method should display a rectangle of '.' characters and 'X' characters.
b) The rectangle's height is equal to height, and its width is equal to width.
28
Задача
Java Collections, ниво, урок
Tetris (part 14)
Implement the removeFullLines() method in the Field class.
Here's what we need to do:
a) delete all lines from the matrix that are completely full (consist of only ones);
b) move the remaining lines down;
c) create new lines to replace missing ones.
14
Задача
Java Collections, ниво, урок
Tetris (part 15)
Implement the step() method in the Tetris class.
This method should drop the game piece down by one step.
If the game piece cannot be placed in the new location, then:
a) put it back (bring it back up);
b) land it (the game piece "lands");
c) delete all full lines in the field object.
7
Задача
Java Collections, ниво, урок
Tetris (part 16)
Write your own implementation of the left(), right(), up(), and down() methods in the GamePiece class.
What do you think these methods should do?
7
Задача
Java Collections, ниво, урок
Tetris (part 17)
I've corrected the code a little bit.
But overall, well done.
Enjoy playing Tetris!
P.S.
Just don't forget to adjust the height of the console.