forked from lchi/java-tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetris.java
More file actions
130 lines (108 loc) · 2.63 KB
/
Tetris.java
File metadata and controls
130 lines (108 loc) · 2.63 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
package tetris;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
/**
* Swing frame and main class, mostly used to setup
* the UI.
* @author dev
*
*/
@SuppressWarnings("serial")
public class Tetris extends JFrame {
JPanel panel;
GameOrchestrator orchestrator;
public Tetris() {
setAndSizeComponents();
initKeyListeners();
}
private void setAndSizeComponents() {
this.setLayout(null);
Insets insets = this.getInsets();
Board gameBoard = new Board();
gameBoard.setBounds(insets.left + 5, insets.top + 5, Board.BLOCKSIZE
* Board.WIDTH, Board.BLOCKSIZE * Board.HEIGHT);
gameBoard.setVisible(true);
gameBoard.setBorder(new BevelBorder(BevelBorder.LOWERED));
gameBoard.setOpaque(false);
JPanel infoPanel = new JPanel();
infoPanel.setName("Info");
infoPanel.setLayout(null);
infoPanel.setBounds(35 + Board.BLOCKSIZE * Board.WIDTH,
insets.top + 35, 100, 100);
infoPanel.setVisible(true);
orchestrator = new GameOrchestrator(1, 1000, gameBoard, infoPanel, this);
this.add(gameBoard);
this.add(infoPanel);
this.setBounds(50, 50, 400, 593);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Score: 0 Level: 1");
}
private void initKeyListeners() {
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// nothing, handle in keyPressed()
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case 27: // esc
quit();
break;
case 32: // space
orchestrator.hardDrop();
break;
case 37: // <-
orchestrator.shiftLeft();
break;
case 38: // up
orchestrator.rotateRight();
break;
case 39: // ->
orchestrator.shiftRight();
break;
case 40: // dwn arrow
orchestrator.softDrop();
break;
case 65: // a
orchestrator.rotateLeft();
break;
case 68: // d
orchestrator.rotateRight();
break;
case 80: // p
togglePause();
break;
default:
e.consume();
return;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public static void main(String[] args) {
Tetris tetris = new Tetris();
int score = tetris.play();
JOptionPane.showMessageDialog(tetris, "You lose! Score=" + score);
System.exit(0);
}
public int play() {
return orchestrator.play();
}
public void quit() {
System.exit(0);
}
private void togglePause() {
orchestrator.togglePause();
}
}