-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGamePanel.java
292 lines (235 loc) · 10.3 KB
/
GamePanel.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
public class GamePanel extends JLayeredPane implements MouseMotionListener {
Image bgImage;
Image peashooterImage;
Image freezePeashooterImage;
Image sunflowerImage;
Image peaImage;
Image freezePeaImage;
Image cherrybombImage;
Image normalZombieImage;
Image coneHeadZombieImage;
Image RunZombieImage;
Image NutImage;
Collider[] colliders;
ArrayList<ArrayList<Zombie>> laneZombies;
ArrayList<ArrayList<Pea>> lanePeas;
ArrayList<Sun> activeSuns;
Timer redrawTimer;
Timer advancerTimer;
Timer sunProducer;
Timer zombieProducer;
JLabel sunScoreboard;
GameWindow.PlantType activePlantingBrush = GameWindow.PlantType.None;
int mouseX , mouseY;
private int sunScore;
public int getSunScore() {
return sunScore;
}
public void setSunScore(int sunScore) {
this.sunScore = sunScore;
sunScoreboard.setText(String.valueOf(sunScore)); // hiển thị bảng điểm số
}
public GamePanel(JLabel sunScoreboard){
setSize(1000,752);
setLayout(null);
addMouseMotionListener(this);
this.sunScoreboard = sunScoreboard;
setSunScore(150); //thiết lập điểm số ban đầu
bgImage = new ImageIcon(this.getClass().getResource("images/mainBG(1).png")).getImage();
peashooterImage = new ImageIcon(this.getClass().getResource("images/plants/2.gif")).getImage();
freezePeashooterImage = new ImageIcon(this.getClass().getResource("images/plants/3.gif")).getImage();
sunflowerImage = new ImageIcon(this.getClass().getResource("images/plants/1(1).gif")).getImage();
peaImage = new ImageIcon(this.getClass().getResource("images/1.png")).getImage();
freezePeaImage = new ImageIcon(this.getClass().getResource("images/2.png")).getImage();
NutImage = new ImageIcon(this.getClass().getResource("images/plants/5.gif")).getImage();
normalZombieImage = new ImageIcon(this.getClass().getResource("images/zombies/1.gif")).getImage();
coneHeadZombieImage = new ImageIcon(this.getClass().getResource("images/zombies/2.gif")).getImage();
RunZombieImage = new ImageIcon(this.getClass().getResource("images/zombies/5.gif")).getImage();
laneZombies = new ArrayList<>();
laneZombies.add(new ArrayList<>()); //line 1
laneZombies.add(new ArrayList<>()); //line 2
laneZombies.add(new ArrayList<>()); //line 3
laneZombies.add(new ArrayList<>()); //line 4
laneZombies.add(new ArrayList<>()); //line 5
lanePeas = new ArrayList<>();
lanePeas.add(new ArrayList<>()); //line 1
lanePeas.add(new ArrayList<>()); //line 2
lanePeas.add(new ArrayList<>()); //line 3
lanePeas.add(new ArrayList<>()); //line 4
lanePeas.add(new ArrayList<>()); //line 5
colliders = new Collider[45];
for (int i = 0; i < 45; i++) {
Collider a = new Collider();
a.setLocation(44 + (i%9)*100,109+(i/9)*120);
a.setAction(new PlantActionListener((i%9),(i/9)));
colliders[i] = a;
add(a, Integer.valueOf(0));
}
//colliders[0].setPlant(new FreezePeashooter(this,0,0));
/*
colliders[9].setPlant(new Peashooter(this,0,1));
laneZombies.get(1).add(new NormalZombie(this,1));*/
redrawTimer = new Timer(25,(ActionEvent e) -> {
repaint(); // làm mới hình ảnh (FPS): tốc độ làm mới khung hình
});
redrawTimer.start();
advancerTimer = new Timer(60,(ActionEvent e) -> advance());
advancerTimer.start();
activeSuns = new ArrayList<>();
sunProducer = new Timer(5000,(ActionEvent e) -> {
Random rnd = new Random();
Sun sta = new Sun(this,rnd.nextInt(800)+100,0,rnd.nextInt(300)+200);
activeSuns.add(sta);
add(sta, Integer.valueOf(1));
});
sunProducer.start();
zombieProducer = new Timer(7000,(ActionEvent e) -> {
Random rnd = new Random();
LevelData lvl = new LevelData();
String [] Level = lvl.Level[Integer.parseInt(lvl.Lvl)-1];
int [][] LevelValue = lvl.LevelValue[Integer.parseInt(lvl.Lvl)-1];
int l = rnd.nextInt(5);
int t = rnd.nextInt(100);
Zombie z = null;
for(int i = 0;i<LevelValue.length;i++) {
if(t>=LevelValue[i][0]&&t<=LevelValue[i][1]) {
z = Zombie.getZombie(Level[i],GamePanel.this,l);
}
}
laneZombies.get(l).add(z);
});
zombieProducer.start();
}
private void advance(){
for (int i = 0; i < 5 ; i++) {
for(Zombie z : laneZombies.get(i)){ // trả danh sách zombie trong làn i
z.advance();
}
for (int j = 0; j < lanePeas.get(i).size(); j++) { // trả ra các hạt pea bắn zombie
Pea p = lanePeas.get(i).get(j);
p.advance();
}
}
for (int i = 0; i < activeSuns.size() ; i++) {
activeSuns.get(i).advance();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage,0,0,null);
//Draw Plants
for (int i = 0; i < 45; i++) {
Collider c = colliders[i];
if(c.assignedPlant != null){
Plant p = c.assignedPlant;
if(p instanceof Peashooter){
g.drawImage(peashooterImage,20 + (i%9)*100,80 + (i/9)*120,null);// tọa độ để vẽ cây bắn bth
}
if(p instanceof FreezePeashooter){
g.drawImage(freezePeashooterImage,20 + (i%9)*100,80 + (i/9)*120,null);// tọa độ vẽ cây bắn frezze
}
if(p instanceof Sunflower){
g.drawImage(sunflowerImage,15 + (i%9)*100,70 + (i/9)*120,null); // tọa độ vẽ bông mặt trời
}
if(p instanceof Nut){
g.drawImage(NutImage,20 + (i%9)*100,80 + (i/9)*120,null );
}
}
}
for (int i = 0; i < 5 ; i++) {
for(Zombie z : laneZombies.get(i)){ // trả danh sách zombie trên làn i
if(z instanceof NormalZombie){
g.drawImage(normalZombieImage,z.posX,125+(i*120),null); // tọa độ vẽ normal zombie
}else if(z instanceof ConeHeadZombie){
g.drawImage(coneHeadZombieImage,z.posX,85+(i*120),null); // tọa độ vẽ coneheadzombie
}else{
g.drawImage(RunZombieImage, z.posX, 90+(i*120), null);
}
}
for (int j = 0; j < lanePeas.get(i).size(); j++) {
Pea p = lanePeas.get(i).get(j);
if(p instanceof FreezePea){
g.drawImage(freezePeaImage, p.posX, 130 + (i * 120), null);
}else {
g.drawImage(peaImage, p.posX, 130 + (i * 120), null);
}
}
}
//if(!"".equals(activePlantingBrush)){
//System.out.println(activePlantingBrush);
/*if(activePlantingBrush == GameWindow.PlantType.Sunflower) {
g.drawImage(sunflowerImage,mouseX,mouseY,null);
}*/
//}
}
class PlantActionListener implements ActionListener { // thực hiện hành động trồng cây
int x,y;
public PlantActionListener(int x, int y){
this.x = x;
this.y = y;
}
@Override
public void actionPerformed(ActionEvent e) {
if(activePlantingBrush == GameWindow.PlantType.Sunflower){
if(getSunScore()>=50) {
colliders[x + y * 9].setPlant(new Sunflower(GamePanel.this, x, y));
setSunScore(getSunScore()-50);
}
}
if(activePlantingBrush == GameWindow.PlantType.Peashooter){
if(getSunScore() >= 100) {
colliders[x + y * 9].setPlant(new Peashooter(GamePanel.this, x, y));
setSunScore(getSunScore()-100);
}
}// đoạn mã đảm bảo người chơi chọn cây trồng đủ tiền để trồng
if(activePlantingBrush == GameWindow.PlantType.FreezePeashooter){
if(getSunScore() >= 175) {
colliders[x + y * 9].setPlant(new FreezePeashooter(GamePanel.this, x, y));
setSunScore(getSunScore()-175);
}
}
if(activePlantingBrush == GameWindow.PlantType.Nut){
if(getSunScore() >= 75) {
colliders[x + y * 9].setPlant(new Nut(GamePanel.this, x, y));
setSunScore(getSunScore()-75);
}
}
activePlantingBrush = GameWindow.PlantType.None; // về trạng thái không có loại cây nào được chồng
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) { // cập nhật vị trí chuột trên giao diện
mouseX = e.getX();
mouseY = e.getY();
}
static int progress = 0;
public static void setProgress(int num) { // điều kiện để kết thúc game
progress = progress + num;
System.out.println(progress);
if(progress>=150) {
if("1".equals(LevelData.Lvl)) {
JOptionPane.showMessageDialog(null,"Level Completed !!!" + '\n' + "Starting next Level");
GameWindow.gw.dispose();
LevelData.write("2");
GameWindow.gw = new GameWindow();
} else {
JOptionPane.showMessageDialog(null,"Level Completed !!!" + '\n' + "More Levels will come soon !!!" + '\n' + "Resetting data");
LevelData.write("1");
System.exit(0);
}
progress = 0;
}
}
}