-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunTetris.java
99 lines (84 loc) · 3.7 KB
/
RunTetris.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
package org.cis120.tetris;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
/**
* Game Main class that specifies the frame and widgets of the GUI
*/
public class RunTetris implements Runnable {
public void run() {
// NOTE : recall that the 'final' keyword notes immutability even for
// local variables.
// Top-level frame in which game components live.
final JFrame frame = new JFrame("Tetris");
frame.setLocation(300, 100);
// JOptionPane.showMessageDialog(frame, "test");
String instructions = "The goal is to last as long as possible by clearing rows "
+ "by moving pieces. \n"
+ "Rows are cleared when they are filled up. \n"
+ "Use your arrow keys to move the pieces. Up arrow rotates the piece. Space bar drops the piece"
+ " straight down. \n"
+ "Have fun!";
JOptionPane.showMessageDialog(frame, instructions, "How To Play", 1);
// Status panel
final JPanel status_panel = new JPanel();
frame.add(status_panel, BorderLayout.SOUTH);
final JLabel status = new JLabel("Running...");
status_panel.add(status);
// Main playing area
final Board court = new Board(status, 10, 20);
frame.add(court, BorderLayout.CENTER);
// Reset button
final JPanel control_panel = new JPanel();
frame.add(control_panel, BorderLayout.LINE_END);
// control_panel.setLayout(new BoxLayout(control_panel, BoxLayout.Y_AXIS));
control_panel.setPreferredSize(new Dimension(200, 600));
// Note here that when we add an action listener to the reset button, we
// define it as an anonymous inner class that is an instance of
// ActionListener with its actionPerformed() method overridden. When the
// button is pressed, actionPerformed() will be called.
final JButton reset = new JButton("Start Over");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.startOver();
}
});
//Button that pauses or unpauses the game
final JButton pausePlay = new JButton("Pause");
pausePlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.pausePlay();
String pp = pausePlay.getText();
if (pp.equals("Pause")) {
pausePlay.setText("Play");
} else {
pausePlay.setText("Pause");
}
}
});
pausePlay.setSize(reset.getSize());
NextDisplay upcoming = new NextDisplay();
upcoming.setSize(150, 240);
upcoming.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Next"));
//Adding a listener so the NextDisplay can be updated
court.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
upcoming.update(court.getNextPieces());
upcoming.repaint();
}
});
//Adding the buttons, and NextDisplay
control_panel.add(pausePlay);
control_panel.add(reset);
control_panel.add(upcoming);
// Put the frame on the screen
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Start game
court.reset();
}
}