基于java的Swing游戏植物大战僵尸(java+swing)

运行环境

Java≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

032123382011

082123382011

基于java的Swing游戏植物大战僵尸(java+swing)

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
private Map<Integer, String> bulletList=new HashMap<Integer, String>();
private SunAutoProduceThread sunAutoProduceThread;
private Map<Integer, Plant> plantList=new HashMap<Integer, Plant>();
private Map<Integer,CarThread> carList=new HashMap<Integer, CarThread>();
private Map<String,CorpseMoveThread> corpseList=new HashMap<String, CorpseMoveThread>();
private Map<Integer,ProduceBullet> produceBulletTimer=new HashMap<Integer,ProduceBullet>();
public Controller(){
indexwin=new IndexWin(this);

}
public void start(){
SunAdd.stop=false;
this.x=15;
this.y=80;
grid=new Grid(this.x,this.y);
sunValue=50;
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
win.toShow();
putSunValue(0);
timer.start();
sunAutoProduceThread.start();
moneyEnoughThread.start();
grid.getBoard()[1][0]="31111";
grid.getBoard()[4][0]="31111";
grid.getBoard()[7][0]="31111";
grid.getBoard()[10][0]="31111";
grid.getBoard()[13][0]="31111";
corpseNum=grid.getNum();

}
public void newCorpseThread(){
for (int i = 0; i < grid.getBoard().length; i++) {
if("11,12,13,14".contains(grid.getBoard()[i][length-1].substring(0,2))&&
corpseList.get(grid.getBoard()[i][length-1])==null){
corpseList.put(grid.getBoard()[i][length-1], new CorpseMoveThread(this,i ,length-1));
corpseList.get(grid.getBoard()[i][length-1]).start();
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
				return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
!("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
grid.getBoard()[x][y+1]=grid.getBoard()[x][y];
grid.getBoard()[x][y]="00111";
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
if (isHit(grid.getBoard()[x][y+1])){
grid.getBoard()[x][y+1] = "00111";

return -1;
}
grid.getBoard()[x][y]="00111";
return y+1;
}
return -1;
}
public int moveCorpse(int x,int y){
if (y==0&&("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
fail();
return -1;
}
if (y - 1 != -1
&& !("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
grid.getBoard()[x][y-1] = grid.getBoard()[x][y ];
grid.getBoard()[x][y] = "00111";
return y-1;
}
if (y - 1 != -1
&& ("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y ].substring(0, 2)))) {
plantList.get(x * 100 + y-1).setHp(plantList.get(x * 100 + y-1).getHp() - 1);
if (plantList.get(x * 100 + y-1).getHp() == 0) {
grid.getBoard()[x][y-1] = "00111";
}
return y;
}
return -1;
}
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
//			boardlbl[i][j].setIcon(wood);
// }
for (int i = 0; i < boardlbl.length; i++) {
for (int j = 0; j < boardlbl[i].length; j++) {
board.add(boardlbl[i][j]);
}
}
board.setBounds(40, 85,width-100,height-100);
width=width-100;
height=height-100;
mainwin.add(board,JLayeredPane.DEFAULT_LAYER);
//mainwin.add(board,JLayeredPane.PALETTE_LAYER);
createCard();
return mainwin;
}
private void createCard() {//卡片框
ImageIcon cardboard=new ImageIcon(Win.class.getResource("/plantvsplant/card/cardboard.png"));
JLabel cardboardl=new JLabel(cardboard);
cardboardl.setBounds(0, 2, 650, 98);
mainwin.add(cardboardl,JLayeredPane.MODAL_LAYER);
ImageIcon pic0=new ImageIcon(IndexWin.class.getResource("/plantvsplant/card/2.jpg"));
ImageIcon pic1=new ImageIcon(IndexWin.class.getResource("/plantvsplant/card/3.jpg"));
ImageIcon pic2=new ImageIcon(IndexWin.class.getResource("/plantvsplant/card/1.jpg"));
ImageIcon pic3=new ImageIcon(IndexWin.class.getResource("/plantvsplant/card/4.jpg"));
ImageIcon pic4=new ImageIcon(IndexWin.class.getResource("/plantvsplant/card/6.jpg"));
//阳光
sunnum=new JLabel(Integer.toString(controller.getSunValue()));
sunnum.setBounds(30, 60, 100, 50);
mainwin.add(sunnum,JLayeredPane.POPUP_LAYER);
//卡片框
card0=new JLabel(pic0);
card1=new JLabel(pic1);
card2=new JLabel(pic2);
card3=new JLabel(pic3);
card4=new JLabel(pic4);
card0.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e) {
if(card0.isEnabled()&& 0==plantid){
plantid=1;
controller.putSunValue(-50);
}else{
if(plantid==1){
controller.putSunValue(50);
plantid=0;
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
			b+=3;
if(a<200)
a+=2;
sun.setBounds(a, b, 80, 80);
if(b>=650)
{
sun.setVisible(false);
break;
}
}
}
}


public class Cell {
private int x;
private int y;

public Cell() {
}

public Cell(int x, int y) {
this.x=x;
this.y=y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

@Override
public String toString() {
return "Cell [x=" + x + ", y=" + y + "]";
}

@Override
public int hashCode() {
final int prime = 31;
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
main.add(namer1, 0);
// 游戏模式选项层
final ImageIcon img1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Adventure_highlight.png"));
final ImageIcon img11 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_StartAdventure_Highlight.png"));
ImageIcon img2 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Vasebreaker_button.png"));
ImageIcon img3 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Challenges_button.png"));
final JLabel lbl1 = new JLabel(img1);
lbl1.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {
controller.start();

}

public void mouseEntered(MouseEvent e) {
lbl1.setIcon(img11);

}

public void mouseExited(MouseEvent e) {
lbl1.setIcon(img1);

}
});
JLabel lbl2 = new JLabel(img2);
JLabel lbl3 = new JLabel(img3);
lbl1.setBounds(330, 50, img1.getIconWidth(), img1.getIconHeight());
lbl2.setBounds(340, 150, img2.getIconWidth(), img2.getIconHeight());
lbl3.setBounds(340, 250, img3.getIconWidth(), img3.getIconHeight());
main.add(lbl1, 0);
main.add(lbl2, 0);
main.add(lbl3, 0);
// 选项,帮助,退出
final ImageIcon option1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Options1.png"));
final ImageIcon help1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Help1.png"));
final ImageIcon quit1 = new ImageIcon(
IndexWin.class
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
		//corpse12.setImage(corpse12.getImage().getScaledInstance(80, 80, 1));
//corpse13.setImage(corpse13.getImage().getScaledInstance(80, 80, 1));
setHorizontalAlignment(JLabel.CENTER);
setIcon(null);
}
public MyLabel(int i,int j,final Controller controller){
this(controller);
this.x = i;
this.y = j;
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(controller.isUproot()){
controller.uprootPlant(x, y);
}
else{
controller.addPlant(x,y);
}
}
});

}
public void setlable(int x) {
switch (x) {
case 21:
setIcon(bb);
break;
// case 31:
// setIcon(bb);
// break;
case 1:
setIcon(sunfllower);
break;
case 3:
setIcon(pease);
break;
case 5:
setIcon(snowpease);
break;
case 2:
setIcon(bomb);
break;
case 4:
setIcon(potato);
break;
case 11:
setIcon(corpse11);
break;
case 12:
setIcon(corpse12);
break;
case 13:
setIcon(corpse13);


项目链接:
https://javayms.github.io?id=262123392011201gi
https://javayms.pages.dev?id=262123392011201gi