Question
Objectives: Graphics, interfaces, anonymous classes, event handling Tetris is a game where players need to match falling tetrominos in a 10x20 grid.1 . A player
Objectives: Graphics, interfaces, anonymous classes, event handling Tetris is a game where players need to match falling tetrominos in a 10x20 grid.1 . A player can navigate the falling tetromino with four keys. Arrows left and right, to move a tetromino left or right, arrow up to rotate a tetromino, and arrow down (this is optional) to let the teromino fall in its current orientation and column. Implement a tetris game based on Java graphics and event handling. You can either choose to implement a complete game, or a short version. If the problem statement is unclear, use the Canvas forum to ask for clarifications. Turn in a zip file named blazerid hw6.zip. The file should contain an exported Eclipse project2 with the following items. All files needed to compile and run your solution. Your tests. A document (or text file) that describes your design decisions, your tests, any difficulties you had. If you received help from somebody else in class, please give credit to those students. If you would like to get a graded version on paper, add a note at the top of the report saying paper copy requested. (10pts) Lab attendance (Week of Oct 22) (10pts) Assignment report (see paragraph above) (10pts) Turned in classes compile (10pts) Code quality (e.g., class design, use of anonymous or inner classes and/or lambda functions), and documentation quality (e.g., javadoc). (10pts) A tetromino can be rotated and moved until it hits the ground. A falling tetromino can be moved to the border of the grid but not beyond. Rotations and moves that are invalid should be ignored. (10pts) Short version: the game ends after the first tetromino has reached the bottom. (10pts) Full version: when the tetromino hits the bottom, its blocks are integrated into the grid and a new tetromino enters the game. The game continues until no more tetromino can enter the grid structure. 1 https://en.wikipedia.org/wiki/Tetris 2 If you do not use Eclipse, the turned in file also needs to have a file readme.txt in its top directory that explains how to compile and test your code. 1 Design Suggestions Similar to the Tetromino assignment, the playing field can be implemented by deriving a class TetrisComponent from JComponent that contains the game items (grid and tetromino). The tetromino is controlled by user input with the four arrow keys. To this end you can install a KeyListener in a JComponent, then set the components focusability to true and request the focus3 . comp.addKeyListener(. . . ); comp.setFocusable(true); comp.requestFocus(); The KeyListener will record which buttons were pressed. A KeyListener provides three methods keyPressed, keyReleased, keyTyped. keyReleased is the recommended method to use. A KeyListener object can be created with an anonymous class. comp.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) { / record key press /} public void keyTyped(KeyEvent e) {} }); When a key was pressed, record which key was pressed. To this end you can query the parameter e and the constants VK UP, VK DOWN, VK LEFT, and VK RIGHT. The constants are associated with the non numeric keypad arrow keys. if (e.getKeyCode() == KeyEvent.VK UP) . . . When the state of the tetromino changes, repaint the component. The falling of the tetromino can be controlled by timers. Start with 500ms and choose a suitable timer speed. When the timer expires: calculate the new position of the tetromino. If the new position is valid within the grid, then the tetromino should fall by one row; otherwise the tetromino is embedded into the grid, and a new tetromino is introduced.4 When the new position is set, repaint the component. Tetromino rotation: When the arrow up key is pressed, the tetromino should rotate. However, the rotation can only be performed, if the resulting tetromino fits within the grid. The grid can use a two dimensional array as internal representation. e.g., Block[10][20]. Consider to use a grid that is larger than what is displayed on the screen (e.g., Block[16][25]. This will help to test whether a rotated or falling tetromino fits within the grid. For example, three pseudo columns on each side, and three pseudo rows at the bottom could be filled with pseudo blocks. This way you can avoid testing whether a point in the tetromino is within the grid bounds.) 2 Consider to use two empty pseudo rows on top for the same reason. The short version ends, after the first tetromino has hit the floor. The complete game ends when no more tetromino can be placed into the grid. Sketch of the grid representation
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started