-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameOrchestrator.java
More file actions
163 lines (143 loc) · 2.92 KB
/
GameOrchestrator.java
File metadata and controls
163 lines (143 loc) · 2.92 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
package tetris;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Orchestrator to run and keep track of progress in
* the game.
*
* @author dev
*
*/
public class GameOrchestrator {
int level, score;
Tetrimino current, next;
boolean lost, paused;
Board gameBoard;
int clockTick;
JPanel infoPanel;
JTextField infoBox;
JFrame parent;
int rows;
public GameOrchestrator(int level, int clockTick, Board gameBoard,
JPanel infoPanel, JFrame parent) {
this.gameBoard = gameBoard;
this.infoPanel = infoPanel;
this.parent = parent;
lost = false;
paused = false;
score = 0;
rows = 0;
this.level = level;
this.clockTick = clockTick;
}
public int play() {
long lastClockTick = System.currentTimeMillis();
current = Tetrimino.getNext();
next = Tetrimino.getNext();
gameBoard.setNewTetrimino(current);
addNextToInfo();
while (!lost) {
while (paused);
if (lastClockTick + clockTick < System.currentTimeMillis()) {
if (current.canShiftDown()) {
current.shiftDown();
} else {
updateTetrimino();
}
lastClockTick = System.currentTimeMillis();
}
}
return score;
}
private void increaseLevel() {
this.level++;
clockTick *= .85;
}
public void togglePause() {
paused = !paused;
}
public void shiftRight() {
current.shiftRight();
}
public void shiftLeft() {
current.shiftLeft();
}
public void rotateRight() {
current.rotateRight();
}
public void rotateLeft() {
current.rotateLeft();
}
public void softDrop() {
current.shiftDown();
}
public void hardDrop() {
while (current.canShiftDown())
current.shiftDown();
updateTetrimino();
}
public void updateTetrimino() {
infoPanel.remove(next);
gameBoard.addToBoard(current);
eliminateLines();
current = next;
current.setLocation(((Board.WIDTH/2)-1)*Board.BLOCKSIZE, 0);
if(!gameBoard.setNewTetrimino(current)){
lost = true;
}
next = Tetrimino.getNext();
addNextToInfo();
gameBoard.revalidate();
gameBoard.repaint();
}
public void addNextToInfo(){
infoPanel.add(next);
next.setLocation(0, 0);
infoPanel.revalidate();
infoPanel.repaint();
}
public void eliminateLines() {
Block[][] blocks = gameBoard.blocks;
int numRows = 0;
for (int i = 0; i < Board.HEIGHT; i++) {
int count = 0;
for (int j = 0; j < Board.WIDTH; j++) {
if (blocks[j][i] != null)
count++;
}
if (count == 10) {
numRows++;
gameBoard.shiftDownFrom(i--);
}
}
updateScore(numRows);
}
public void updateScore(int numRows) {
rows += numRows;
switch (numRows) {
case 0:
return;
case 1:
score += level*100;
break;
case 2:
score += level*300;
break;
case 3:
score += level*800;
break;
case 4:
score += level*2000;
break;
}
if(rows >= 10){
rows %= 10;
increaseLevel();
}
updateInfo();
}
public void updateInfo(){
parent.setTitle("Score: " + score + " Level: " + level);
}
}