forked from jannie601H/OOP_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAimLabGame.java
More file actions
224 lines (190 loc) · 7.28 KB
/
AimLabGame.java
File metadata and controls
224 lines (190 loc) · 7.28 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
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
package example1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.util.concurrent.ExecutionException;
public class AimLabGame extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int BUTTON_WIDTH = 100;
private static final int BUTTON_HEIGHT = 30;
private static final int CIRCLE_RADIUS = 30;
//게임 화면 크기 및 버튼과 게임에서 사용될 원의 크기를 정의
private int lives = 3;
private int score = 0;
private Timer timer;
private Random random = new Random();
private Circle targetCircle;
//목숨 수, 점수, 타이머, 난수 생성과 원 변수 선언
private JLabel scoreLabel = new JLabel(Integer.toString(score));
private JLabel livesLabel = new JLabel(Integer.toString(lives));
private Partition partition = new Partition(WIDTH - 100, 0, WIDTH - 100, HEIGHT); // 세로선 좌표
// 점수, 목숨을 출력할 JLabel 정의 및 게임화면 분할 Partition 직선 정의
public AimLabGame() {
setTitle("Aim Lab Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//게임 창의 제목 및 크기 지정과 창 종료 설정
setLayout(null);
//게임 시작 버튼 생성
JButton startButton = new JButton("Game Start");
startButton.setBounds((WIDTH - BUTTON_WIDTH) / 2, (HEIGHT - BUTTON_HEIGHT) / 2, BUTTON_WIDTH, BUTTON_HEIGHT);
//버튼의 위치를 중앙정렬
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startGame();
//startGame 메서드를 호출하여 게임을 시작한다.
}
});
// 레이아웃 매니저를 사용하지 않음
setLayout(null);
add(startButton);
//버튼 표시
}
private void startGame() {
getContentPane().removeAll(); // 버튼 제거
// init score Label
scoreLabel.setSize(100, 200);
scoreLabel.setLocation(WIDTH - 50, HEIGHT - 150);
add(scoreLabel);
// init lives Label
livesLabel.setSize(100, 200);
livesLabel.setLocation(WIDTH - 70, HEIGHT - 150);
add(livesLabel);
// 게임 컴포넌트 초기화
initGame();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
checkHit(e.getX(), e.getY());
}
});
// 게임이 시작되면 타이머 생성
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateGame();
//메서드 호출
repaint();
//화면을 다시 그리기
}
});
timer.start();
generateRandomCircle();
}
private void initGame() {
generateRandomCircle();
}
private void updateGame() {
targetCircle.shrink();
//원의 크기를 줄이는 shrink()메서드 호출
if (targetCircle.getRadius() <= 0) {
generateRandomCircle(); // 새로운 원 생성
lives--;
livesLabel.setText(Integer.toString(lives));
// 클릭 실패시 목숨 1감소
if (lives == 0) {
gameOver();
//크기가 0이하가 되면 새로운 원을 생성하고 목숨을 감소
//목숨이 0이 되면 gameOver 메소드를 호출
}
}
}
private void checkHit(int x, int y) {
if (targetCircle.contains(x, y)) {
generateRandomCircle();
//클릭이 원 안에서 이루어 졌는지 확인하고 새로운 원을 생성
score += 1;
scoreLabel.setText(Integer.toString(score));
// 클릭 성공시 점수 1증가
}
}
private void gameOver() {
timer.stop();
//타이머 정지 게임 루프를 중단시키기 위해 사용
JOptionPane.showMessageDialog(this, "Game Over");
//다이얼로그 창을 통해 Game Over 메세지를 표시
System.exit(0);
//프로그램 종료
}
//게임이 종료되었을 때 호출되는 메서드
@Override
public void paint(Graphics g) {
super.paint(g);
targetCircle.draw(g);
partition.draw(g);
}
//페인트 메소드를 오버리아드하여 화면을 그림
private void generateRandomCircle() {
SwingWorker<Circle, Void> worker = new SwingWorker<Circle, Void>(){
//Override
protected Circle doInBackground() {
int x = random.nextInt(WIDTH - 100 - CIRCLE_RADIUS * 2) + CIRCLE_RADIUS;
int y = random.nextInt(HEIGHT - 30 - CIRCLE_RADIUS * 2) + CIRCLE_RADIUS + 30;
// circle 좌표 random 생성, 원이 화면 밖으로 나가지 않도록 범위 조정
return new Circle(x, y, CIRCLE_RADIUS);
}
//Override
protected void done() {
try {
targetCircle = get();
repaint();
}
catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
};
worker.execute();;
}
//화면 내에 랜덤한 위치에 원을 생성한다.
//화면 넓이에서 원의 넓이 만큼 뺜 범위 내에서 랜덤한 값을 얻고 원의 반지름 만큼 이동해서 원이 생성되도록 설정한다.
private class Circle {
private int x;
private int y;
private int radius;
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void shrink() {
radius -= 0.1;
}
//원의 크기를 감소시키는 메소드
public boolean contains(int px, int py) {
return Math.sqrt((px - x) * (px - x) + (py - y) * (py - y)) <= radius;
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
}
}
private class Partition {
private int x1;
private int y1;
private int x2;
private int y2;
public Partition(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(x1, y1, x2, y2);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AimLabGame().setVisible(true));
}
}