CodeGym /Java Course /Java Collections /Big task: Tetris

Big task: Tetris

Java Collections
Level 2 , Lesson 15
Available
Big task: Tetris - 1

"Hi, Amigo!"

"Hello, Captain Squirrels, sir!"

"I have a new mission for you. Let's write the game Tetris."

"This is a secret operation. All further instructions will be provided via IntelliJ IDEA."

"Yes, sir! Beginning my secret mission..."

7
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
Tetris (part 2)
Great! Now add the following two fields to the Tetris class: Field field and GamePiece gamePiece.
7
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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
Task
Java Collections, level 2, lesson 15
Locked
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.
Comments (9)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 35, San Diego, United States
8 January 2024
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. Requirements: • Your big task for Level 32 is done!
Justin Smith Level 41, Greenfield, USA, United States
26 September 2022
This whole big task is far easier than any of regular tasks. Parts 1 through 11 should have been all one long (but still easy) task, the orange ones are easy, and the only red task is probably equal to a regular orange task. Overall, all of the real work is provided for you. Which means you don't really learn much doing this task, but I guess it's a nice break. My suggestion for the CG team to make this one a lot better: 1. Use a graphical JFrame implementation, not the wonky console one. This is the perfect opportunity to introduce working with graphics. 2. Actually have people write the code for setting up JFrames and stuff. I get that it hasn't been taught, but you could still have easy-rated tasks where you literally just tell students what to type, and it would be more educational than this.
UltraGameCoder Level 41, Groningen, Netherlands
10 August 2022
When they ask you to create methods, I thought they also had to be implemented unless explicitly specified not to. But now that I get to the next assignments, my existing implementation of these methods got overridden and I had to redo the implementation from scratch. print, removelines etc. So for whoever reads this in time, don't work on things they aren't explicitly telling you to in the making of Tetris.
Ibrahim Level 41, Sheffield, United Kingdom
2 June 2022
Pretty easy task but not structured well. I found the games in the game section had better structure and allowed you to do more. Here a lot of the code is written for you.
MaGaby2280 Level 41, Guatemala City, Guatemala
1 June 2021
Finally a big task you can really enjoy! 😀
Elvis Lee Level 41, Aurora, United States
1 February 2020
It's not difficult to apply 2048 tactics to extend this task. Instead of displaying characters in console, Modify the JPanel in View class of 2048, different colors of GamePieces can be shown like the diagram shown above. Just replace the "1" of gamePieces with various int index which represents different colors. Of course, it needs some modifications on various classes logic. But it's worth to spend several hours to make it a real playable game. Have fun!
yz Level 37, Jakarta, Indonesia
11 December 2019
for those who didnt understand part 13 google how to display multidimensional array
ted Level 46, Indonesia Expert
25 March 2023
wow, that helped a lot!!!
Senned Level 41, Azov, Russia
4 December 2019
Too easy after Multithreading quests grand tasks. Sadly we don't write all code(