-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetrisListener.java
122 lines (102 loc) · 2.45 KB
/
TetrisListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package animation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
* A listener to move the tetromini around given the grid, state, and timer.
*
* @author johnsonhsiung
*
*/
public class TetrisListener implements ActionListener
{
JLabel label;
Tetromini mini;
Timer t;
TetroState state;
Grid grid;
Random generator;
/**
* creates a TetrisListener with a random Tetris block that will move down the
* grid for each action performed.
*
* @param grid The grid it will draw on.
* @param state The state of the blocks already drawn.
* @param t The timer used to count down.
*/
public TetrisListener(Grid grid, TetroState state, Timer t)
{
this.grid = grid;
this.state = state;
this.t = t;
generator = new Random();
int whichTetro = generator.nextInt(6);
switch (whichTetro)
{
case 0:
this.mini = new TMini();
break;
case 1:
this.mini = new IMini();
break;
case 2:
this.mini = new JMini();
break;
case 3:
this.mini = new ZMini();
break;
case 4:
this.mini = new OMini();
break;
case 5:
this.mini = new LMini();
break;
}
ShapeIcon icon = new ShapeIcon(mini, 300, 660);
this.label = new JLabel(icon);
label.setBounds(0, 0, 300, 660);
grid.add(label);
}
/**
* Translates the tetromini down by 1 pixel unit. Can rotate the block, move the
* block to the right or left 1 grid unit. Also checks to see if the game is
* over. If it is, then it stops. Uses a random generator
*/
@Override
public void actionPerformed(ActionEvent e)
{
mini.translate(0, 1);
int randomNumber = generator.nextInt(100);
if (state.canRotate(mini) && randomNumber == ROTATE_NUMBER)
{
mini.rotate();
}
if (state.canTranslateLeft(mini) && randomNumber == TRANSLATE_LEFT_NUMBER)
{
mini.translate(Grid.getPixelUnit(-1), 0);
}
if (state.canTranslateRight(mini) && randomNumber == TRANSLATE_RIGHT_NUMBER)
{
mini.translate(Grid.getPixelUnit(1), 0);
}
label.repaint();
if (state.shouldStop(mini))
{
state.addTetro(mini);
state.repaint();
if (state.isGameOver())
{
t.stop();
} else
{
t.removeActionListener(this);
t.addActionListener(new TetrisListener(this.grid, state, t));
}
}
}
private static final int ROTATE_NUMBER = 5;
private static final int TRANSLATE_LEFT_NUMBER = 6;
private static final int TRANSLATE_RIGHT_NUMBER = 7;
}