Repository navigation
Expand file tree
/
Copy pathView.java
More file actions
132 lines (113 loc) · 4.2 KB
/
Copy pathView.java
File metadata and controls
132 lines (113 loc) · 4.2 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
131
132
package assignment_2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.Color;
/**
* Created by Intoleravel on 05/12/2016.
*/
public class View extends JFrame {
public static final Color BACKGROUND_COLOR = Color.BLACK;
public View() {
JFrame f = new JFrame("Tetris by Mario Oliveira (1502867)");
Container contentPane = f.getContentPane();
final Structure game = new Structure();
contentPane.add(game, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(470, 565);
f.setVisible(true);
f.getContentPane().setBackground(Color.pink);
// Start the game of Tetris
game.start();
// Mouse controls for the shape movement
f.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
switch (e.getButton()) {
// Right mouse button to move shape to the right
case KeyEvent.KEY_LOCATION_RIGHT:
game.moveSide(1);
game.dropShadow();
break;
// Left mouse button to move shape to the left
case KeyEvent.KEY_LOCATION_STANDARD:
game.moveSide(-1);
game.dropShadow();
break;
// Middle mouse button to rotate shape 90 degrees
case KeyEvent.KEY_LOCATION_LEFT:
game.rotate();
game.dropShadow();
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
// Key controls for the shape movement
f.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
// Arrow up key to rotate shape
case KeyEvent.VK_UP:
game.rotate();
game.dropShadow();
break;
// Arrow left key to move shape to the left
case KeyEvent.VK_LEFT:
game.moveSide(-1);
game.dropShadow();
break;
// Arrow right key to move shape to the right
case KeyEvent.VK_RIGHT:
game.moveSide(+1);
game.dropShadow();
break;
// Space bar to drop the shape to its final destination
case KeyEvent.VK_SPACE:
game.drop();
if (game.isGameOver()) {
System.out.println("Game has been terminated.");
}
break;
}
}
public void keyReleased(KeyEvent e) {
}
});
// Make the shape drop every second if the game is still running
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
if (game.run) {
game.moveDown();
if (game.isGameOver()) {
System.out.println("Game has been terminated.");
}
}
}
};
timer.scheduleAtFixedRate(task, 100, 1000);
}
public static void main(String[] args) {
new View();
}
}