-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.java
160 lines (137 loc) · 4.17 KB
/
Tetris.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
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JPanel {
private static boolean isMultiplayer;
private static boolean isGameActive;
//creates the board objects
private static Board board = new Board(1);
private static Board board2 = new Board(2);
public static void setMultiplayerStatus(boolean multiplayerStatus) {
isMultiplayer = multiplayerStatus;
}
public static boolean getMultiplayerStatus() {
return isMultiplayer;
}
public static void gameOver() {
isGameActive = false;
}
@Override
public void paintComponent(Graphics g) {
board.paintComponent(g);
board2.paintComponent(g);
}
public static void runTetris() {
JFrame gameScreen = new JFrame();
Music music = new Music("TetrisMusic.wav");
Music click = new Music("ClickSound.wav");
gameScreen.setLocationRelativeTo(null);
gameScreen.setLocation(0, 0);
gameScreen.setResizable(false);
gameScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameScreen.setVisible(true);
music.start();
music.loop();
isGameActive = true;
if (isMultiplayer) {
board2.resetBoard();
gameScreen.setSize(Const.screenWidth * 2, Const.screenHeight);
} else {
gameScreen.setSize(Const.screenWidth, Const.screenHeight);
}
board.resetBoard();
Tetris game = new Tetris();
gameScreen.add(game);
// Keyboard controls
gameScreen.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W){
board.rotate(+1);
game.repaint();
}
if (key == KeyEvent.VK_S){
board.softDrop(1);
game.repaint();
}
if (key == KeyEvent.VK_A){
board.move(-1);
game.repaint();
}
if (key == KeyEvent.VK_D){
board.move(1);
game.repaint();
}
if (key == KeyEvent.VK_SPACE){
board.hardDrop();
click.loop(1);
game.repaint();
}
if (key == KeyEvent.VK_Q){
board.rotate(-1);
game.repaint();
}
if (key == KeyEvent.VK_E){
board.holdPiece();
game.repaint();
}
if (isMultiplayer) {
if (key == KeyEvent.VK_UP){
board2.rotate(+1);
game.repaint();
}
if (key == KeyEvent.VK_DOWN){
board2.softDrop(1);
game.repaint();
}
if (key == KeyEvent.VK_LEFT){
board2.move(-1);
game.repaint();
}
if (key == KeyEvent.VK_RIGHT){
board2.move(1);
game.repaint();
}
if (key == KeyEvent.VK_CONTROL){
board2.hardDrop();
game.repaint();
}
if (key == KeyEvent.VK_SHIFT){
board2.rotate(-1);
game.repaint();
}
if (key == KeyEvent.VK_ENTER){
board2.holdPiece();
game.repaint();
}
}
}
public void keyReleased(KeyEvent e) {}
});
new Thread() {
@Override public void run() {
while (true) {
try {
Thread.sleep(1000);
//drops the piece everysecond to force player to take action
board.softDrop(1);;
if (isMultiplayer) {
board2.softDrop(1);;
}
game.repaint();
if (!isGameActive) {
gameScreen.setVisible(false);
gameScreen.remove(game);
music.stop();
Credits.runCredits();
return;
}
} catch (InterruptedException e) {}
}
}
}.start();
}
}