-
Notifications
You must be signed in to change notification settings - Fork 0
/
startGameScreen.java
434 lines (379 loc) · 17.3 KB
/
startGameScreen.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
public class startGameScreen {
// constant
private static int frameWidth = 1280;
private static int frameHeight = 720;
private static int timePerGame = 60; // 60
private static int targetWideh = 200;
private static int targetHeight = 120;
private static Rectangle TimeLabelPositionAndSize = new Rectangle(750, 5, 400,100);
private static Rectangle ScoreLabelPositionAndSize = new Rectangle(1050, 5, 400,100);
private static Rectangle pauseButtonPositionAndSize = new Rectangle(10, 10, 150,150);
private static Font labelFont = new Font("Microsoft YaHei", Font.BOLD, 40);
private static float lighteningFactoe = 0.5f;
private static float pauseIconScaleRate = 2.0f;
private static String pathIWin = "./src/img/iwin.png";
private static ImageIcon backgroundImage = new ImageIcon("./src/img/battle-background.png");
private static ImageIcon darkerBackgroundImage = new ImageIcon(decreaseBrightness(backgroundImage.getImage(), lighteningFactoe));
private static ImageIcon iWin = new ImageIcon(pathIWin);
private static ImageIcon pauseIcon = new ImageIcon("./src/img/pause.png");
private static ImageIcon biggerPauseIcon = getScaledImageIcon(pauseIcon, pauseIconScaleRate);
private static ImageIcon aronaIcon = new ImageIcon("./src/img/arona.png");
// Function to decrease brightness of an image
private static BufferedImage decreaseBrightness(Image image, float factor) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
// Apply brightness adjustment
RescaleOp op = new RescaleOp(1.0f - factor, 0, null);
BufferedImage darkerImage = op.filter(bufferedImage, null);
return darkerImage;
}
// function to scale icon
private static ImageIcon getScaledImageIcon(ImageIcon theImageIcon, float ScaledRate) {
Image theImage = theImageIcon.getImage();
int newWidth = theImageIcon.getIconWidth() * 2;
int newHeight = theImageIcon.getIconHeight() * 2;
Image scaledImage = theImage.getScaledInstance(newWidth,newHeight,Image.SCALE_DEFAULT);
return new ImageIcon(scaledImage);
}
// CALL to START
public static void startGame(){
NewGame newGame = new NewGame();
}
// Game frame
private static class NewGame extends JFrame
{
private boolean waitEnd = false;
private boolean pausing = false;
// objects in gameFrame
// JLayeredPane maybe?
private int timeLeft;
private int score;
private JLabel waitText = new JLabel();
private JLabel waitText2 = new JLabel();
private JLabel background = new JLabel();
private JLabel timeLabel = new JLabel();
private JLabel scoreLabel = new JLabel();
private JButton target = new JButton(iWin);
private JButton pauseButton = new JButton();
private JLabel arona = new JLabel();
private Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeLeft--;
timeLabel.setText("剩餘時間 : " + (timeLeft+9)/10);
if(timeLeft <= 0) {
pausing = true;
timer.stop();
background.setIcon(darkerBackgroundImage);
// change if you think not remove better better
background.remove(target);
background.remove(pauseButton);
background.remove(timeLabel);
background.remove(scoreLabel);
arona.setVisible(true);
showEndMenu();
}
}
});
public NewGame()
{
pre();
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set game icon
ImageIcon mainIcon = new ImageIcon("./src/img/main_icon.jpg");
this.setIconImage(mainIcon.getImage());
}
private void init()
{
// self
this.setTitle("Never Gonna Give You Up");
this.setSize(frameWidth, frameHeight);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
this.setLayout(null); // I don't wtf is this but can't work well without this
this.setVisible(true);
// init
timeLeft = timePerGame * 10;
score = 0;
// Background Image
background = new JLabel(darkerBackgroundImage);
background.setSize(frameWidth,frameHeight);
// this is weird I know
background.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(waitEnd == false)
{
waitEnd = true;
addComponents();
timer.start();
background.setIcon(backgroundImage);
background.remove(waitText);
background.remove(waitText2);
}
}
});
// wait Text
waitText.setText("點擊滑鼠左鍵");
waitText.setBounds(new Rectangle(460,60,500,500));
waitText.setFont(new Font("Microsoft YaHei", Font.BOLD, 65));
waitText.setForeground(Color.WHITE);
background.add(waitText, BorderLayout.PAGE_START);
waitText2.setText(" 開始遊戲 ");
waitText2.setBounds(new Rectangle(460,200,500,500));
waitText2.setFont(new Font("Microsoft YaHei", Font.BOLD, 65));
waitText2.setForeground(Color.WHITE);
background.add(waitText2, BorderLayout.PAGE_START);
this.add(background);
}
private void addComponents()
{
// time Label
timeLabel.setText("剩餘時間 : " + (timeLeft+9)/10);
timeLabel.setBounds(TimeLabelPositionAndSize);
timeLabel.setFont(labelFont);
timeLabel.setForeground(Color.WHITE);
background.add(timeLabel, BorderLayout.PAGE_START);
// Score Label
scoreLabel.setText("分數 : " + score);
scoreLabel.setBounds(ScoreLabelPositionAndSize);
scoreLabel.setFont(labelFont);
scoreLabel.setForeground(Color.WHITE);
background.add(scoreLabel, BorderLayout.PAGE_START);
// target
// JButton target = new JButton(iWin);
target.setSize(targetWideh, targetHeight);
target.setLocation((frameWidth - targetWideh) / 2, (frameHeight - targetHeight) / 2); // set to mid
target.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// 設置按鈕外框與背景不可見,只保留圖片
target.setBorderPainted(false);
target.setContentAreaFilled(false);
target.setFocusPainted(false);
target.setOpaque(false);
target.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// shoot audio
Tools.shootClick_SE();
if (timeLeft == 0 || pausing == true) return;
int x = (int) (Math.random() * (frameWidth - targetWideh));
int y = (int) (Math.random() * (frameHeight - targetHeight));
target.setLocation(x, y);
score++;
scoreLabel.setText("分數 : " + score);
}
});
background.add(target, BorderLayout.PAGE_START);
// Pause Button
pauseButton = new JButton(biggerPauseIcon);
pauseButton.setBounds(pauseButtonPositionAndSize);
pauseButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// 設置按鈕外框與背景不可見,只保留圖片
pauseButton.setBorderPainted(false);
pauseButton.setContentAreaFilled(false);
pauseButton.setFocusPainted(false);
pauseButton.setOpaque(false);
pauseButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Tools.basicClick_SE();
if(timeLeft == 0 || pausing ==true) return;
// 隱藏組件
timeLabel.setVisible(false);
scoreLabel.setVisible(false);
target.setVisible(false);
pauseButton.setVisible(false);
timer.stop();
pausing = true;
background.setIcon(darkerBackgroundImage);
showPauseMenu();
pausing = false;
// 顯示組件
timeLabel.setVisible(true);
scoreLabel.setVisible(true);
target.setVisible(true);
pauseButton.setVisible(true);
timer.start();
}
});
background.add(pauseButton, BorderLayout.PAGE_START);
// arona.png
arona.setIcon(aronaIcon);
arona.setBounds(200,50,300,300);
arona.setVisible(false);
background.add(arona, BorderLayout.PAGE_START);
}
//! ///////////////////////////////////////////////////////////////////////////////////////
JDialog pauseDialog = new JDialog();
JPanel pauseMenu = new JPanel();
JDialog scoreDialog = new JDialog();
JPanel scoreMenu = new JPanel();
// Set the preferred size for the pause menu
int menuWidth = 450;
int menuHeight = 450;
// Button dimensions
int buttonWidth = 300;
int buttonHeight = 70;
Dimension buttonSize = new Dimension(buttonWidth, buttonHeight);
private void pre()
{
// Set the layout manager to BoxLayout with vertical alignment
pauseMenu.setLayout(new BoxLayout(pauseMenu, BoxLayout.Y_AXIS));
pauseMenu.setPreferredSize(new Dimension(menuWidth, menuHeight));
// label
JLabel theLabel = new JLabel();
theLabel.setText("暫停");
theLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
theLabel.setFont(labelFont);
theLabel.setForeground(Color.BLACK);
// button 1
JButton button1 = new JButton("繼續遊戲");
Color customColor = new Color(0, 150, 255);
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setPreferredSize(buttonSize);
button1.setMaximumSize(buttonSize);
button1.setFont(labelFont);
button1.setForeground(Color.WHITE);
button1.setBackground(customColor);
button1.setBorder(BorderFactory.createLineBorder(Color.WHITE));
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tools.basicClick_SE();
background.setIcon(backgroundImage);
pauseDialog.setVisible(false);
timer.start();
pausing = false;
}
});
// button 2
JButton button2 = new JButton("回到標題");
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button2.setPreferredSize(buttonSize);
button2.setMaximumSize(buttonSize);
button2.setFont(labelFont);
button2.setForeground(Color.WHITE);
button2.setBackground(Color.DARK_GRAY);
button2.setBorder(BorderFactory.createLineBorder(Color.WHITE));
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tools.basicClick_SE();
//! this is to fix a weird bug if game.java use timer or sth
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {}
});
pauseDialog.setVisible(false);
gameEnd();
}
});
// Add components with glue for spacing
pauseMenu.add(Box.createVerticalGlue());
pauseMenu.add(theLabel);
pauseMenu.add(Box.createVerticalGlue());
pauseMenu.add(button1);
pauseMenu.add(Box.createVerticalGlue());
pauseMenu.add(button2);
pauseMenu.add(Box.createVerticalGlue());
// Add the pauseMenu to a dialog or another container to show it
pauseDialog.setUndecorated(true); // Remove the title bar
pauseDialog.setModal(true); // Block input to other windows
pauseDialog.getContentPane().add(pauseMenu);
pauseDialog.pack();
pauseDialog.setLocationRelativeTo(null); // Center the dialog
pauseDialog.setVisible(false);
}
private void build()
{
// Set the layout manager to BoxLayout with vertical alignment
scoreMenu.setLayout(new BoxLayout(scoreMenu, BoxLayout.Y_AXIS));
scoreMenu.setPreferredSize(new Dimension(menuWidth, menuHeight));
// label
JLabel theLabel = new JLabel("分數:" + score);
theLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
theLabel.setFont(labelFont);
theLabel.setForeground(Color.BLACK);
// button 1
JButton button1 = new JButton("再玩一次");
Color customColor = new Color(0, 150, 255);
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setPreferredSize(buttonSize);
button1.setMaximumSize(buttonSize);
button1.setFont(labelFont);
button1.setBackground(customColor);
button1.setForeground(Color.WHITE);
button1.setBorder(BorderFactory.createLineBorder(Color.WHITE));
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tools.basicClick_SE();
scoreDialog.dispose();
dispose();
startGameScreen.startGame();
}
});
// button 2
JButton button2 = new JButton("回到標題");
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button2.setPreferredSize(buttonSize);
button2.setMaximumSize(buttonSize);
button2.setFont(labelFont);
button2.setForeground(Color.WHITE);
button2.setBackground(Color.DARK_GRAY);
button2.setBorder(BorderFactory.createLineBorder(Color.WHITE));
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tools.basicClick_SE();
//! this is to fix a weird bug if game.java use timer or sth
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {}
});
scoreDialog.setVisible(false);
gameEnd();
}
});
// Add components with glue for spacing
scoreMenu.add(Box.createVerticalGlue());
scoreMenu.add(theLabel);
scoreMenu.add(Box.createVerticalGlue());
scoreMenu.add(button1);
scoreMenu.add(Box.createVerticalGlue());
scoreMenu.add(button2);
scoreMenu.add(Box.createVerticalGlue());
// Add the pauseMenu to a dialog or another container to show it
scoreDialog.setUndecorated(true); // Remove the title bar
scoreDialog.setModal(true); // Block input to other windows
scoreDialog.getContentPane().add(scoreMenu);
scoreDialog.pack();
scoreDialog.setLocationRelativeTo(null); // Center the dialog
scoreDialog.setVisible(false);
}
// show Pause menu
private void showPauseMenu() {
pauseDialog.setVisible(true);
}
//show Score
public void showEndMenu() {
build();
scoreDialog.setVisible(true);
}
private void gameEnd()
{
timer.stop();
Tools.stopBGM();
dispose();
Game game = new Game();
}
}
}