-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFull Script
336 lines (283 loc) · 9.46 KB
/
Full Script
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Falling_Game implements KeyListener{
float gameSpeed = 750f;
boolean gameOver = false;
JFrame gameWindow = new JFrame();
int[][] matrix = new int[12][5];
JPanel[][] jPanelmatrix = new JPanel[12][5];
JLabel gameOverLabel = new JLabel("Game Over");
JPanel gameBoard = new JPanel();
JPanel gameOverPanel = new JPanel();
JButton button = new JButton("Exit");
int playerLocation = 2;
boolean firstOpen = true;
private long startTime = System.currentTimeMillis();
ArrayList<Coord> coords = new ArrayList<Coord>();//This one holds the coords of the falling blocks
public void CreateGameBoard() {
// initializing the 2D array
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = 0;
}
}
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
//System.out.print(matrix[i][j]);
}
//System.out.println();
}
for (int i = 0; i < jPanelmatrix.length; i++) {
for (int j = 0; j < jPanelmatrix[i].length; j++) {
jPanelmatrix[i][j] = new JPanel();
}
}
}
public void JFrameBuilder() {
gameWindow.setVisible(true);
gameWindow.setSize(new Dimension(300, 600));
gameWindow.setLocationRelativeTo(null);
gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstOpen = false;
gameWindow.addKeyListener(this);
gameBoard.setLayout(new GridLayout(12, 5)); // set the layout to 4 rows and 4 columns
gameOverLabel.setHorizontalAlignment(JLabel.CENTER);
gameOverLabel.setVerticalAlignment(JLabel.CENTER);
gameOverLabel.setFont(new Font("Arial", Font.BOLD, 36));
button.setHorizontalAlignment(JLabel.CENTER);
button.setVerticalAlignment(JLabel.CENTER);
button.setFont(new Font("Arial", Font.BOLD, 36));
button.setSize(50, 10);
gameOverPanel.setLayout(new BoxLayout(gameOverPanel, BoxLayout.PAGE_AXIS));
gameOverPanel.setLayout(new BoxLayout(gameOverPanel, BoxLayout.PAGE_AXIS));
gameOverPanel.add(Box.createVerticalGlue());
gameOverPanel.add(gameOverLabel);
long endTime = System.currentTimeMillis();
long runTime = endTime - this.startTime;
JLabel score = new JLabel("Score: " + runTime);
score.setFont(new Font("Arial", Font.BOLD, 26));
gameOverPanel.add(score);
gameOverPanel.add(button);
gameOverPanel.add(Box.createVerticalGlue());
gameOverLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
score.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add glue to the left and right to center the components horizontally
gameOverPanel.add(Box.createHorizontalGlue());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gameOver = false;
System.exit(0);
//RestartGame();
}
});
RecreateWindow();
}
public void RecreateWindow() {
// create and add tiles to the panel
for (int i = 0; i < matrix.length; i++) {
for(int x = 0; x < matrix[1].length; x++) {
//System.out.print(x + "" + i);
jPanelmatrix[i][x].setPreferredSize(new Dimension(60, 50));
jPanelmatrix[i][x].setBackground(Color.white); // alternate tile colors
if(matrix[i][x] == 1) {
jPanelmatrix[i][x].setBackground(Color.MAGENTA);
}
gameBoard.add(jPanelmatrix[i][x]);
}
}
gameWindow.add(gameBoard);
gameWindow.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) { // check if the button was clicked
//System.out.println("Restarting");
System.exit(0);
}
}
public void RandomizeDrop() {
Random random = new Random();
int spawns = random.nextInt(4);
for(int i = 0; i < spawns; i++) {
//System.out.println("Spawning at " + random.nextInt(5));
matrix[0][random.nextInt(5)] = 2;
}
}
public void UpdateGame() {
for (int i = matrix.length - 1; i > -1; i--) {
for(int x = matrix[1].length - 1; x > -1; x--) {
//*/
//System.out.println();
jPanelmatrix[i][x].setPreferredSize(new Dimension(60, 50)); // set the tile size to 50x50 pixels
Color color = Color.white;
//System.out.println(i);
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
if(matrix[i][x] == 2) {
color = Color.black;
if(i == 11 && x == playerLocation) {
gameOver = true;
gameBoard.setVisible(false);
//System.out.println("Game playing: " + !gameOver);
gameWindow.add(gameOverPanel);
}
//System.out.print(" = Black");
//This adds gravity to the block
if(i <= 11 && !(i == 11 && x == playerLocation)) {
//System.out.print("This is where it fell: " + x + ", " + i);
coords.add(new Coord(i, x));
}
if(i == 0 && !(i == 11 && x == playerLocation)) {//This is to get rid of the block at the end
//matrix[i][x] = 0;
jPanelmatrix[i][x].setBackground(Color.white);
}
}
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
jPanelmatrix[i][x].setBackground(color); // alternate tile colors
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
gameBoard.add(jPanelmatrix[i][x]);
//System.out.println();
}
}
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
for(int i = 0; i < coords.size(); i++) {
int x = coords.get(i).x + 1;
int y = coords.get(i).y;
Color color = Color.black;
//System.out.println(x + "-------------------");
if(x == 11) {
//System.out.println(playerLocation + ", " + y);
if(jPanelmatrix[11][playerLocation].getBackground() != Color.black && y == playerLocation) {
}
/*
if(y == playerLocation && jPanelmatrix[x][y].getBackground() == Color.black) {
System.out.println("Killed by: " + x + ", " + y);
gameOver = true;
gameBoard.setVisible(false);
System.out.println("Game playing: " + !gameOver);
gameWindow.add(gameOverPanel);
}
*/
}
if(x == 12) {
jPanelmatrix[x - 1][y].setBackground(Color.white);
matrix[x - 1][y] = 0;
}else {
matrix[x][y] = 2;
matrix[x - 1][y] = 0;
jPanelmatrix[x][y].setBackground(color);
}
jPanelmatrix[x - 1][y].setBackground(Color.white);
}
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
}
@Override
public void keyPressed(KeyEvent e) {
// handle key press event
int keyCode = e.getKeyCode();
//System.out.println("Key pressed: " + KeyEvent.getKeyText(keyCode));
if(keyCode == KeyEvent.VK_A) {
if(playerLocation > 0) {
jPanelmatrix[11][playerLocation].setBackground(Color.white);
playerLocation = playerLocation - 1;
jPanelmatrix[11][playerLocation].setBackground(Color.magenta);
}
}
if(keyCode == KeyEvent.VK_D) {
if(playerLocation < 4) {
jPanelmatrix[11][playerLocation].setBackground(Color.white);
playerLocation = playerLocation + 1;
jPanelmatrix[11][playerLocation].setBackground(Color.magenta);
}
}
//System.out.print(playerLocation);
}
@Override
public void keyReleased(KeyEvent e) {
// handle key release event
}
@Override
public void keyTyped(KeyEvent e) {
// handle key typed event
}
public void RestartGame() {
Random random = new Random();
if(firstOpen) {
CreateGameBoard();
}else {
RecreateWindow();
}
JFrameBuilder();
while(!gameOver) {
RandomizeDrop();
for(int i = 0; i < random.nextInt(3) + 1; i++) {
try {
// wait for 1 second (1000 milliseconds)
Thread.sleep((long) gameSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
UpdateGame();
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
}
}
}
public Falling_Game(){
Random random = new Random();
if(firstOpen) {
CreateGameBoard();
}else {
RecreateWindow();
}
JFrameBuilder();
while(!gameOver) {
RandomizeDrop();
for(int i = 0; i < random.nextInt(3) + 1; i++) {
try {
// wait for 1 second (1000 milliseconds)
Thread.sleep((long) gameSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
UpdateGame();
jPanelmatrix[11][playerLocation].setBackground(Color.MAGENTA);
if(gameSpeed > 200) {
gameSpeed = gameSpeed - 5;
//System.out.println("GS: " + gameSpeed);
}
}
}
}
public static void main(String[] args) {
new Falling_Game();
}
}
class Coord {
int x;
int y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
}