-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraw.java
214 lines (181 loc) · 7.08 KB
/
Draw.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
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
package view;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;
import java.io.IOException;
public class Draw {
private Screen screen;
private TextGraphics tg;
/**
* Poll input
* @return key
* @throws IOException
*/
public KeyStroke pollInput() throws IOException {
KeyStroke key = screen.pollInput();
return key;
}
/**
* Draw map
* @param map
* @throws IOException
*/
public void DrawMap(Map map) throws IOException {
screen.clear();
map.Draw(tg);
screen.refresh(Screen.RefreshType.DELTA);
}
/**
* Draw initialization
*/
public void drawInitialize(){
try{
Terminal terminal = new DefaultTerminalFactory().createTerminal();
screen = new TerminalScreen(terminal);
screen.setCursorPosition(null);
screen.startScreen();
screen.doResizeIfNecessary();
tg = screen.newTextGraphics();
} catch(IOException e){
e.printStackTrace();
}
}
/**
* Method to display GameOver screen at the end
* @throws IOException
*/
public void drawGameOver(int score) throws IOException {
screen.clear();
tg.setForegroundColor(TextColor.ANSI.RED_BRIGHT);
tg.putString(10, 10, "GAME OVER");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(10, 14, String.valueOf(score) + " POINTS");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
screen.refresh();
KeyStroke keyStroke = screen.readInput();
if (keyStroke.getKeyType() == KeyType.Character && (keyStroke.getCharacter() == 'q' || keyStroke.getCharacter() == ' ')) {
screen.close();
}
screen.refresh();
}
/**
* Method which draws menu
* @throws IOException
*/
public void drawMenu() throws IOException {
screen.clear();
int option = 0;
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(10, 4, "MAIN MENU");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.GREEN_BRIGHT);
tg.putString(10, 8, "NEW GAME");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.YELLOW_BRIGHT);
tg.putString(10, 12, "INSTRUCTIONS");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.RED_BRIGHT);
tg.putString(10, 16, "EXIT");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(6, 8, "->");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.BLACK_BRIGHT);
tg.putString(27, 12, "<Press Space key to choose>");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
screen.refresh();
KeyStroke keyStroke = screen.readInput();
while(!(keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ')){
screen.clear();
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(10, 4, "MAIN MENU");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.GREEN_BRIGHT);
tg.putString(10, 8, "NEW GAME");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.YELLOW_BRIGHT);
tg.putString(10, 12, "INSTRUCTIONS");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.RED_BRIGHT);
tg.putString(10, 16, "EXIT");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
tg.setForegroundColor(TextColor.ANSI.BLACK_BRIGHT);
tg.putString(27, 12, "<Press Space key to choose>");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
switch (keyStroke.getKeyType()) {
case ArrowUp -> {
if(option != 0){
option--;
}
}
case ArrowDown -> {
if(option!=2){
option++;
}
}
}
switch (option){
case 0 -> {
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(6, 8, "->");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
}
case 1 ->{
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(6, 12, "->");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
}
case 2 ->{
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(6, 16, "->");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
}
}
screen.refresh(Screen.RefreshType.DELTA);
keyStroke = screen.readInput();
}
switch (option) {
case 0 -> {
return;
}
case 1 -> {
drawInstructions();
}
case 2 -> {
screen.close();
}
}
}
/**
* Method which displays the instructions of Tetris game
* @throws IOException
*/
public void drawInstructions() throws IOException {
tg.setForegroundColor(TextColor.ANSI.YELLOW_BRIGHT);
tg.putString(10, 4, "Instructions");
tg.setForegroundColor(TextColor.ANSI.WHITE_BRIGHT);
tg.putString(5, 8, "Use the left and right arrow keys to move the falling piece left or right");
tg.putString(5, 10, "Use the up arrow key to rotate the falling piece.");
tg.putString(5, 12, "Use the down arrow key to make the piece fall faster");
tg.putString(5, 14, "Use the spacebar to drop the piece instantly.");
tg.putString(5, 16, "Use the 'c' key to hold the current piece for later use.");
tg.putString(5, 18, "Press the 'q' to close the game.");
tg.setForegroundColor(TextColor.ANSI.BLACK_BRIGHT);
tg.putString(10, 20, "<Space key to continue>");
tg.setForegroundColor(TextColor.ANSI.DEFAULT);
screen.refresh();
KeyStroke keyStroke = screen.readInput();
if (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == 'q') {
screen.close();
} else if (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' '){
screen.clear();
drawMenu();
}
}
}