-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetris.java
More file actions
101 lines (78 loc) · 2.01 KB
/
Tetris.java
File metadata and controls
101 lines (78 loc) · 2.01 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
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Tetris extends JFrame {
private JLabel statusbar;
private TetrisBoard board;
protected static String name;
protected static Tetris tetris = null;
static Player thisPlayer;
protected Tetris(String name) {
this.name = name;
initUI();
}
protected Tetris(Player thisPlayer) {
this.thisPlayer = thisPlayer;
this.name = thisPlayer.getPlayerName();
initUI();
}
private void initUI() {
statusbar = new JLabel("Lines: -- Score:--");
add(statusbar, BorderLayout.SOUTH);
board = new TetrisBoard(this);
add(board);
board.start();
setResizable(false);
pack();
setTitle(name + "Tetris");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
board.addPropertyChangeListener("enabled",
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if (!board.isEnabled()) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
});
}
public JLabel getStatusBar() {
return statusbar;
}
public int findScore() {
return board.getScore();
}
public boolean isGameOver() {
return board.isGameOver();
}
// make Singleton so there can only be one game running at a time
protected static Tetris getInstance(Player thisPlayer) {
if (tetris == null) {
tetris = new Tetris(thisPlayer);
} else {
// JOptionPane.showMessageDialog(null, "Tetris already Running");
tetris = null;
tetris = new Tetris(thisPlayer);
}
return tetris;
}
/*
* public static void main(String[] args) {
*
* EventQueue.invokeLater(() -> {
*
* var game = new Tetris(); game.setVisible(true);
*
* });
*
* }
*/
}