-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.java
181 lines (135 loc) · 4.84 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package Tetris;
import java.awt.*;
import javax.sound.sampled.AudioSystem;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.*;
public class Tetris extends JFrame {
private JLabel statusbar;
private JLabel statusbar1;
static JPanel mainPanel = new JPanel();
JButton playAgain;
int player;
static MainMenu a = new MainMenu();
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
/* SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
while (a.getA() != -1) {
int numberOfPlayer = a.getA();
a.MainMenuFr.setVisible(false);
if (numberOfPlayer == 1 || numberOfPlayer == 2) {
Tetris game = new Tetris(numberOfPlayer);
game.setVisible(true);
System.out.print("da chay");
}
else {
System.out.print("khong chay");
}
}
}
}); */
}
public void playSound() {{
new Thread(new Runnable() {
public void run() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("music.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
}).start();
}
}
public Tetris(int numberOfPlayer) {
playSound();
mainPanel = new JPanel();
player = numberOfPlayer;
playAgain = new JButton("Back To Menu?");
playAgain.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
a.MainMenuFr.setVisible(true);
}
});
add(playAgain);
playAgain.setVisible(false);
if (numberOfPlayer == 1) {
//System.out.println("so 1");
initUI();
}
else if (numberOfPlayer == 2) {
//System.out.println("so 2");
initUI2Players();
}
}
int count = 0;
public void gameOver() {
count++;
if(player == 1) {
playAgain.setBounds(110 ,650, 180, 60);
playAgain.setVisible(true);
} else {
if(count == 2){
playAgain.setBounds(310 ,650, 180, 60);
playAgain.setVisible(true);
}
}
}
private void initUI2Players() {
TAdapter adapter = new TAdapter();
statusbar = new JLabel(" 0");
Board board = new Board(this, statusbar, adapter);
board.add(statusbar, BorderLayout.PAGE_END);
board.start();
statusbar1 = new JLabel(" 0");
Board board1 = new Board(this, statusbar1, adapter);
board1.add(statusbar1, BorderLayout.PAGE_END);
mainPanel.add(board1);
mainPanel.add(board);
board1.start();
mainPanel.setLayout(new GridLayout(0, 2,5,2));
mainPanel.setSize(800,800);
setBackground(Color.black);
add(mainPanel);
setResizable(false);
setSize(800, 800);
setTitle("Tetris");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
//set mau nen
board.setBackground(Color.DARK_GRAY);
//board1.setBackground(Color.white);
}
public void initUI() {
TAdapter adapter = new TAdapter();
statusbar = new JLabel(" 0");
add(statusbar, BorderLayout.SOUTH);
Board board = new Board(this, statusbar, adapter);
add(board);
board.start();
setSize(400, 800);
setTitle("Tetris");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
//set mau nen
board.setBackground(Color.black);
}
// public JLabel getStatusBar() {
// return statusbar;
// }
}