-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisDisplay.java
More file actions
234 lines (197 loc) · 6.77 KB
/
Copy pathTetrisDisplay.java
File metadata and controls
234 lines (197 loc) · 6.77 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* This class creates the graphic display that shows the status of
* the game by extending a JPanel
*
*/
/**
* @authors P. Cochran and N. Guidry
* @version 1.000
* last modified 10/18/2022
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;
public class TetrisDisplay extends JPanel
{
private TetrisGame game;
private int cellSize = 30;
private int start_x = 380;
private int start_y = 80;
private int speed;
private int delay = 300;
private Timer timer;
private boolean pause = false;
private Color[] colors = {Color.WHITE, Color.MAGENTA, Color.YELLOW
,Color.ORANGE, Color.BLUE, Color.GREEN, Color.RED, Color.BLACK, Color.CYAN};
public TetrisDisplay(TetrisGame gam)
{
game = gam;
this.addKeyListener( new KeyAdapter(){
public void keyPressed(KeyEvent ke)
{
translateKey(ke);
}
});
this.setFocusable(true);
this.setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cycleMove();
}
});
timer.start();
if(pause == true)
{
timer.stop();
}
if(game.isGameOver() == true)
{
timer.stop();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawBackground(g);
drawWell(g);
drawBrick(g);
drawScoreBox(g);
if(game.isGameOver())
{
drawGameOver(g);
}
}
private void drawGameOver(Graphics g)
{
int initialY = (game.getCols() / 2 + 3) * cellSize;
int width = game.getCols() * cellSize;
int height = (game.getRows() / 3) * cellSize;
int xText = start_x + 30;
int yText = initialY + 110;
int strokeWeight = 10;
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(strokeWeight));
g.setColor(colors[0]);
g.fillRect(start_x, initialY, width, height);
g.setColor(colors[4]);
g.drawRect(start_x, initialY, width, height);
g.setColor(colors[4]);
g.drawRect(start_x, initialY, width, height);
g.setColor(colors[4]);
g.drawString("GAME OVER",xText , yText);
}
private void drawWell(Graphics g)
{
int rect2_x = start_x +(cellSize * game.getCols());
int rect3_y = start_y + (cellSize * game.getRows());
int rect1n2Width = cellSize;
int rect1n2Hei = cellSize * game.getRows();
int rect3Width = cellSize * game.getCols() + cellSize*2;
g.setColor(colors[7]);
g.drawRect(start_x - cellSize, start_y,rect3Width, rect1n2Hei + cellSize);
g.fillRect(start_x - cellSize, start_y, rect1n2Width, rect1n2Hei);
g.fillRect(rect2_x,start_y, rect1n2Width, rect1n2Hei);
g.fillRect(start_x - cellSize, rect3_y, rect3Width, cellSize);
}
private void drawScoreBox(Graphics g)
{
int xSpacing = start_x - (cellSize * 12);
int ySpacing = start_y + (cellSize * 7);
int width = (cellSize * (game.getRows()/2));
int height = cellSize * 3;
int xSpacing2 = xSpacing + 10;
int ySpacing2 = ySpacing + 10;
int newWidth = width - 20;
int newHeight = height - 20;
int xSpacing3 = xSpacing2 + 100;
int ySpacing3 = ySpacing2 + 50;
int fontSize = 50;
int labelX = xSpacing;
int labelY = ySpacing - 10;
String label = "Score:";
g.setColor(colors[7]);
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g.drawString(label, labelX, labelY);
g.fillRect(xSpacing, ySpacing, width, height);
g.setColor(colors[0]);
g.fillRect(xSpacing2, ySpacing2, newWidth, newHeight);
g.setColor(colors[7]);
g.drawString(game.getScore(), xSpacing3, ySpacing3);
}
private void drawBackground(Graphics g)
{
for(int index = 0; index < (game.getRows()); index++)
{
for(int ind = 0; ind < (game.getCols()); ind++)
{
g.setColor(colors[game.fetchBoardPosition(index, ind)]);
g.fillRect(start_x + (cellSize * ind), start_y + (cellSize * index), cellSize, cellSize);
if(game.fetchBoardPosition(index, ind) != 0)
{
g.setColor(colors[7]);
g.drawRect(start_x + (cellSize * ind), start_y + (cellSize * index), cellSize, cellSize);
}
}
}
}
private void drawBrick(Graphics g)
{
int counter = 0;
while(counter < game.getNumSegs())
{
g.setColor(colors[game.getFallingBrickColor()]);
g.fillRect(start_x + (cellSize * game.getSegCol(counter) + 1),start_y + (cellSize * game.getSegRow(counter) + 1), cellSize, cellSize);
g.setColor(colors[7]);
g.drawRect(start_x + (cellSize * game.getSegCol(counter) + 1),start_y + (cellSize * game.getSegRow(counter) + 1), cellSize, cellSize);
counter ++;
}
}
private void cycleMove()
{
game.makeMove('D');
repaint();
}
private void translateKey(KeyEvent ke)
{
int code = ke.getKeyCode();
switch(code)
{
case KeyEvent.VK_UP:
game.makeMove('T');
repaint();
break;
case KeyEvent.VK_DOWN:
game.makeMove('D');
repaint();
break;
case KeyEvent.VK_RIGHT:
game.makeMove('R');
repaint();
break;
case KeyEvent.VK_LEFT:
game.makeMove('L');
repaint();
break;
case KeyEvent.VK_SPACE:
if(pause == false)
{
timer.stop();
pause = true;
}
else
{
timer.start();
pause = false;
}
break;
case KeyEvent.VK_N:
game.makeMove('N');
repaint();
break;
}
}
}