-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBoard.java
More file actions
176 lines (124 loc) · 4.1 KB
/
Board.java
File metadata and controls
176 lines (124 loc) · 4.1 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
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
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements KeyListener{
private BufferedImage blocks;
private final int blockSize = 30;
private final int boardWidth = 10, boardHeight = 20;
private int[][] board = new int[boardHeight][boardWidth];
private Shape[] shapes = new Shape[7];
private Shape currentShape;
private Timer timer;
private final int FPS = 60;
private final int delay = 1000/FPS;
private boolean gameOver = false;
public Board(){
try {
blocks = ImageIO.read(Board.class.getResource("/tiles.png"));
} catch (IOException e) {
e.printStackTrace();
}
timer = new Timer(delay, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
timer.start();
// shapes
shapes[0] = new Shape(blocks.getSubimage(0, 0, blockSize, blockSize), new int[][]{
{1, 1, 1, 1} // IShape
}, this, 1);
shapes[1] = new Shape(blocks.getSubimage(blockSize, 0, blockSize, blockSize), new int[][]{
{1, 1, 0},
{0, 1, 1} // ZShape
}, this, 2);
shapes[2] = new Shape(blocks.getSubimage(blockSize*2, 0, blockSize, blockSize), new int[][]{
{0, 1, 1},
{1, 1, 0} // S-Shape
}, this, 3);
shapes[3] = new Shape(blocks.getSubimage(blockSize*3, 0, blockSize, blockSize), new int[][]{
{1, 1, 1},
{0, 0, 1} // J-Shape
}, this, 4);
shapes[4] = new Shape(blocks.getSubimage(blockSize*4, 0, blockSize, blockSize), new int[][]{
{1, 1, 1},
{1, 0, 0} // L-Shape
}, this, 5);
shapes[5] = new Shape(blocks.getSubimage(blockSize*5, 0, blockSize, blockSize), new int[][]{
{1, 1, 1},
{0, 1, 0} // T-Shape
}, this, 6);
shapes[6] = new Shape(blocks.getSubimage(blockSize*6, 0, blockSize, blockSize), new int[][]{
{1, 1},
{1, 1} // O-Shape
}, this, 7);
setNextShape();
}
public void update(){
currentShape.update();
if(gameOver)
timer.stop();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
currentShape.render(g);
for(int row = 0; row < board.length; row++)
for(int col = 0; col < board[row].length; col++)
if(board[row][col] != 0)
g.drawImage(blocks.getSubimage((board[row][col]-1)*blockSize, 0, blockSize, blockSize),
col*blockSize, row*blockSize, null);
for(int i = 0; i < boardHeight; i++){
g.drawLine(0, i*blockSize, boardWidth*blockSize, i*blockSize);
}
for(int j = 0; j < boardWidth; j++){
g.drawLine(j*blockSize, 0, j*blockSize, boardHeight*blockSize);
}
}
public void setNextShape(){
int index = (int)(Math.random()*shapes.length);
Shape newShape = new Shape(shapes[index].getBlock(), shapes[index].getCoords(),
this, shapes[index].getColor());
currentShape = newShape;
for(int row = 0; row < currentShape.getCoords().length; row++)
for(int col = 0; col < currentShape.getCoords()[row].length; col++)
if(currentShape.getCoords()[row][col] != 0){
if(board[row][col + 3] != 0)
gameOver = true;
}
}
public int getBlockSize(){
return blockSize;
}
public int[][] getBoard(){
return board;
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT)
currentShape.setDeltaX(-1);
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
currentShape.setDeltaX(1);
if(e.getKeyCode() == KeyEvent.VK_DOWN)
currentShape.speedDown();
if(e.getKeyCode() == KeyEvent.VK_UP)
currentShape.rotate();
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_DOWN)
currentShape.normalSpeed();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}