当前位置:   article > 正文

用Java实现一个简易的植物大战僵尸游戏_植物大战僵尸java代码

植物大战僵尸java代码

今天给大家分享一个简易的植物大战僵尸游戏,用Java编写,还是挺好玩的。

一、设计思路
1.植物大战僵尸运用Java基础实现,通过滚轮上选择植物,随机生成各种类型僵尸,以及植物。
2.玩家可以安放各种植物到草坪上,僵尸通过重绘实现走步效果,以及僵尸死亡时效果。
实际运行效果如下:
在这里插入图片描述
二、代码实现
1.首先要考虑的就是将游戏涉及到的各种对象搞清,由于Java面向对象特性,我们可以将各个对象的共同特征抽象出来,比如僵尸,有多种类型的僵尸,就可抽象出一个僵尸对象的超类,植物也是如此。

public abstract class Zombie {
	
	protected int width;
	protected int height;
	protected int x;
	protected int y;
	protected int live;
	protected int xSpeed;
	protected int deadTm;
	private static BufferedImage[] imgs;
	static {
		imgs=new BufferedImage[25];
		for(int i=0;i<imgs.length;i++) {
			imgs[i]= i<=13 ? loadImage("/ZombieDie/Frame"+i+".png") : loadImage("/ZombieHead/Frame"+(i-14)+".png");
		}
	}
	int index=0;
	int index1=0;
	public BufferedImage getDeadImg() {
		if(index==13) {
			return imgs[13];
		}
		return imgs[index++%14];
	}
	public BufferedImage getDeadHeadImg() {
		if(index1==10) {
			return imgs[24];
		}
		return imgs[index1++%11+14];
	}
	public Zombie(int width,int height) {
		this.width=width;
		this.height=height;
		Random rand=new Random();
		this.x=GamePlay.WIDTH-100;
		//this.y=rand.nextInt(GamePlay.HEIGHT-this.height);
		this.y=rand.nextInt(5)*100+46;
		this.deadTm=80;
	}
	public int getDeadTm() {
		return deadTm;
	}
	public void loseDeadTm() {
		deadTm--;
	}
	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public int getSpeed() {
		return xSpeed;
	}
	public int getLive() {
		return live;
	}
	public void loseLive() {
		live--;
	}
	public void goSlowDown() {
		xSpeed=1;
	}
	public void goOut() {
		xSpeed=2;
	}
	// 移动方式
	public abstract void step();
	
	public boolean zombieHit(Plant p) {
		int x1=this.x-p.getWidth()+50;
		int x2=this.x+this.width-15;
		int y1=this.y-p.getHeight()+30;
		int y2=this.y+this.height-20;
		int x=p.getX();
		int y=p.getY();
		return x>=x1 && x<=x2 && y>=y1 && y<=y2;
	}
	public boolean outOfBounds() {
		return this.x<=0;
	}
	public static final int LIFE=0;
	public static final int ATTACK=1;
	public static final int DEAD=2;
	
	protected int state=LIFE;
	
	public boolean isLife() {
		return state==LIFE;
	}
	public boolean isAttack() {
		return state==ATTACK;
	}
	public boolean isDead() {
		return state==DEAD;
	}
	public void goLife() {
		state=LIFE;
	}
	public void goAttack() {
		state=ATTACK;
	}
	public void goDead() {
		state=DEAD;
	}
	public void goStop() {
		xSpeed=0;
	}
	public abstract void goRun();
	
	public static BufferedImage loadImage(String fileName) {
		try {
			BufferedImage img=ImageIO.read(Zombie.class.getResource(fileName));
			return img;
		}catch(IOException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	public abstract BufferedImage getImage();
	
	public void paintObject(Graphics g) {
		g.drawImage(getImage(), x, y, width, height, null);
	}
	public void paintHead(Graphics g) {
		g.drawImage(getDeadHeadImg(), x-10, y-30, null);
		
	}
}
  • 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

2.植物超类代码实现:

public abstract class Plant {
	
	protected int x;
	protected int y;
	protected int width;
	protected int height;
	protected int ySpeed;
	protected int live;
	
	protected int fixLive;
	
	public Plant(int width,int height) {
		this.width=width;
		this.height=height;
		this.x=0;
		this.y=GamePlay.HEIGHT;
		ySpeed=1;
	}

	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;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getLive() {
		return live;
	}
	public void loseLive() {
		this.live--;
	}
	public void moveTo(int x,int y) {
		this.x=x-this.width/2;
		this.y=y-this.height/2;
	}
	public void step() {
		y-=ySpeed;
		if(y<=0) {
			state=STOP;
		}
	}
	
	public static BufferedImage loadImage(String fileName) {
		try {
			BufferedImage img=ImageIO.read(Plant.class.getResource(fileName));
			return img;
		}catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	public abstract BufferedImage getImage();
	
	public void paintObject(Graphics g) {
		g.drawImage(getImage(), x, y, width,height,null);
	}
	public static final int WAIT=0;
	public static final int STOP=1;
	public static final int MOVE=2;
	
	public static final int LIFE=3;
	
	public static final int DEAD=4;
	
	protected int state=WAIT;
	
	public void setState(int state) {
		this.state=state;
	}
	public boolean isWait() {
		return state==WAIT;
	}
	public boolean isStop() {
		return state==STOP;
	}
	public boolean isMove() {
		return state==MOVE;
	}
	public boolean isLife() {
		return state==LIFE;
	}
	public boolean isDead() {
		return state==DEAD;
	}
	public void goWait() {
		state=WAIT;
	}
	public void goStop() {
		state=STOP;
	}
	public void goMove() {
		state=MOVE;
	}
	public void goLife() {
		state=LIFE;
	}
	public void goDead() {
		state=DEAD;
	}
}
  • 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

3.通过子类继承父类实现各个对象的创建,创建完各个对象后,接下来就可让他们动起来,以僵尸为例,僵尸入场并走步,如下:

//随机生成各类僵尸
public Zombie nextOneZombie() { 
		Random rand=new Random();
		int type=rand.nextInt(20);
		if(type<5) {
			return new Zombie0();
		}else if(type<11) {
			return new Zombie1();
		}else if(type<17) {
			return new Zombie2();
		}else {
			return new Zombie3();
		}
	}
	int zombieEnterTime=0;
	public void zombieEnterAction() {
		zombieEnterTime++;
		if(zombieEnterTime%300==0) {
			zombies.add(nextOneZombie());
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.在僵尸移动过程中,就涉及到僵尸与植物的碰撞问题,这其中就要理解两者间如何才算碰撞,实现如下:

	//判断是否碰撞
	public boolean zombieHit(Plant p) {
		int x1=this.x-p.getWidth()+50;
		int x2=this.x+this.width-15;
		int y1=this.y-p.getHeight()+30;
		int y2=this.y+this.height-20;
		int x=p.getX();
		int y=p.getY();
		return x>=x1 && x<=x2 && y>=y1 && y<=y2;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5.绘制界面,通过重绘实现,具体代码如下:

public class GamePlay extends JPanel{

	private static final long serialVersionUID = 1L;
	
	public static final int WIDTH=1400;
	public static final int HEIGHT=600;
	public static final int START=0;
	public static final int RUNNING=1;
	public static final int GAME_OVER=2;
	
	public static int state=RUNNING;
	
	Background start=new Background(800, 533, 300, 50);
	Background running=new Background(WIDTH, HEIGHT, 0, 0);
	Background gameOver=new Background(WIDTH, HEIGHT, 0, 0);
	
	Vector<Zombie> zombies=new Vector<Zombie>();
	
	Vector<Plant> plants=new Vector<>();
	Vector<Plant> plantsLife=new Vector<>();
	Vector<Bullet> bullets=new Vector<>();
	List<Glass> glasses=new ArrayList<>();
	
	List<Shovel> shovels=new ArrayList<>();
	
	public void shovelEnterAction() {
		if(shovels.size()==0) {
			shovels.add(new Shovel());
		}
	}
	public void checkShovelAction() {
		if(plantsLife.size()==0) {
			Iterator<Shovel> its=shovels.iterator();
			while(its.hasNext()) {
				Shovel s=its.next();
				if(s.isMove() && shovelCheck) {
					its.remove();
				}
			}
		}
	}
	int glassX=260;
	int glassY=80;
	public void glassEnterAction() {
		for(int i=0;i<9;i++) {
			int x=glassX+i*Glass.width;
			for(int j=0;j<5;j++) {
				int y=glassY+j*Glass.height;
				glasses.add(new Glass(x, y));
			}
		}
	}
	public void glassCheckAction() {
		for(Glass g : glasses) {
			g.goEmpty();
			int x1=g.getX();
			int y1=g.getY();
			for(Plant p : plantsLife) {
				if(p.isLife() || (p instanceof Blover && ((Blover)p).isClick())) {
					int x=p.getX();
					int y=p.getY();
					if(x1==x && y1==y) {
						g.goHold();
						break;
					}
				}
			}
		}
	}
	public Zombie nextOneZombie() {
		Random rand=new Random();
		int type=rand.nextInt(20);
		if(type<5) {
			return new Zombie0();
		}else if(type<11) {
			return new Zombie1();
		}else if(type<17) {
			return new Zombie2();
		}else {
			return new Zombie3();
		}
	}
	int zombieEnterTime=0;
	public void zombieEnterAction() {
		zombieEnterTime++;
		if(zombieEnterTime%300==0) {
			zombies.add(nextOneZombie());
		}
	}
	int zombieStepTime=0;
	public void zombieStepAction() {
		if(zombieStepTime++%6==0) {
			for(Zombie z : zombies) {
				if(z.isLife()) {
					z.step();
				}
			}
		}
	}
	int spikerockHitTime=0;
	public void zombieMoveToSpikerockAction() {
		if(spikerockHitTime++%20==0) {
			for(Plant p : plantsLife) {
				if(p instanceof Spikerock) {
					if(p.isLife()) {
						int x1=p.getX();
						int y1=p.getY();
						int x2=p.getX()+p.getWidth();
						int y2=p.getY()+p.getHeight();
						for(Zombie z : zombies) {
							if(z.isLife() || z.isAttack()) {
								int x=z.getX();
								int y=z.getY();
								if(x>x1 && y>y1 && x<x2 && y<y2) {
									z.loseLive();
								}
							}
						}
					}
				}
			}
		}
	}
	int zombieHitTime=0;
	public void zombieHitAction() {
		if(zombieHitTime++%100==0) {
			for(Zombie z : zombies) {
				if(!z.isDead()) {
					z.goLife();
				}
				for(Plant p : plantsLife) {
					if(z.isLife() && (p.isLife() || (p instanceof Blover && ((Blover)p).isClick())) && z.zombieHit(p) && !(p instanceof Spikerock)) {
						z.goAttack();
						p.loseLive();
					}
				}
			}
		}
	}
	int timeStop=1; //停止2秒
	public void checkZombieAction() {
		Iterator<Zombie> it=zombies.iterator();
		while(it.hasNext()) {
			Zombie z=it.next();
			if(z.getLive()<=0) {
				if(z instanceof Award && !z.isDead()) {
					Award a=(Award)z;
					int type=a.getAwardType();
					switch (type) {
					case Award.CLEAR:
						for(Zombie zo : zombies) {
							zo.goDead();
						}
						break;
					case Award.STOP:
						for(Zombie zo : zombies) {
							zo.goStop();
							timeStop=1;
						}
						break;
					}
				}
				z.goDead();
				//it.remove();
				z.loseDeadTm();
				
			}else {
				if(z.isDead()) {
					z.loseDeadTm();
				}
			}
			if(z.outOfBounds()) {
				gameLife--;
				it.remove();
			}
		}
	}
	public void checkDeadZombieRemoveAction() {
		Iterator<Zombie> it=zombies.iterator();
		while(it.hasNext()) {
			Zombie z=it.next();
			if(z.isDead() && z.getDeadTm()<=0) {
				it.remove();
			}
		}
	}
	public void zombieGoLife() {
		if(timeStop++%250==0) {
			for(Zombie zo : zombies) {
				zo.goRun();						
			}
		}
	}
	int gameLife=1;
	public void checkGameAction() {
		if(gameLife<=0) {
			state=GAME_OVER;
			plants.clear();
			zombies.clear();
			bullets.clear();
			plantsLife.clear();
			shovels.clear();
		}
	}
	public Plant nextOnePlant() {
		Random rand=new Random();
		int type=rand.nextInt(35);
		if(type<5) {
			return new Repeater();
		}else if(type<10) {
			return new ThreePeater();
		}else if(type<15) {
			return new SnowPea();
		}else if(type<20) {
			return new Blover();
		}else if(type<25) {
			return new Spikerock();
		}else if(type<30){
			return new WallNut();
		}else {
			return new Torchwood();
		}
	}
	int plantEnterTm=0;
	public void plantEnterAction() {
		plantEnterTm++;
		if(plantEnterTm%150==0) {
			plants.add(nextOnePlant());
		}
	}
	public void plantStepAction() {
		for(Plant p : plants) {
			if(p.isWait()) {
				p.step();
			}
		}
	}
	public void plantBangAction() {
		for(int i=1;i<plants.size();i++) {
			if(plants.get(0).getY()>0 && plants.get(0).isStop()) {
				plants.get(0).goWait();
			}
			if((plants.get(i).isStop() || plants.get(i).isWait()) && (plants.get(i-1).isStop() || plants.get(i-1).isWait())
				&& plants.get(i).getY()<=plants.get(i-1).getY()+plants.get(i-1).getHeight()) {
				plants.get(i).goStop();
			}
			if(plants.get(i).isStop()  && plants.get(i).getY()>plants.get(i-1).getY()+plants.get(i-1).getHeight()) {
				plants.get(i).goWait();
			}
		}
	}
	int bloverTm=1;
	public void checkBloverAction() {
		if(bloverTm++%200==0) {
			for(Plant p : plantsLife) {
				if(p instanceof Blover && p.isLife()) {
					((Blover)p).goClick();
				}
			}
		}
	}
	int bulletTm=0;
	public void bulletShootAction() {
		if(bulletTm++%80==0) {
			for(Plant p : plantsLife) {
				if(p instanceof Shoot && p.isLife()) {
					Shoot s=(Shoot) p;
					bullets.addAll(Arrays.asList(s.shoot()));
				}
			}
		}
	}
	public void bulletStepAction() {
		for(Bullet b : bullets) {
			b.step();
		}
	}
	public void bulletZombieHitAction() {
		for(Zombie z : zombies) {
			if(z.isAttack() || z.isLife()) {
				for(Bullet b : bullets) {
					if((b.isLife() || b.isFire()) && b.hit(z) && z.getX()<GamePlay.WIDTH) {
						if(b instanceof SnowBullet && b.isLife()) {
							z.goSlowDown();
						}
						if(b.isLife()) {
							z.loseLive();
						}
						z.loseLive();
						b.goDead();
					}
				}
			}
		}
	}
	public void bulletFireHitAction() {
		for(Plant p : plantsLife) {
			if(p instanceof Torchwood && p.isLife()) {
				Torchwood t=(Torchwood)p;
				for(Bullet b : bullets) {
					if(b.isLife() && b.hitFire(t)) {
						b.goFire();
					}
				}
			}
		}
	}
	boolean plantCheck=false;
	boolean shovelCheck=false;
	
	public void action() {
		glassEnterAction();
		
		MouseAdapter l=new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int mX=e.getX();
				int mY=e.getY();
				if(state==RUNNING) {
					f:for(Plant p : plantsLife) {
						if(p.isMove() && plantCheck) {
							for(Glass g : glasses) {
								int x1=g.getX();
								int x2=g.getX()+g.getWidth();
								int y1=g.getY();
								int y2=g.getY()+g.getHeight();
								if(mX>x1 && mX<x2 && mY>y1 && mY<y2 && g.isEmpty()) {
									p.setX(x1);
									p.setY(y1);
									g.goHold();
									p.goLife();
									plantCheck=false;
									if(p instanceof Blover) {
										bloverTm=0;
									}
									break f;
								}
							}
						}
					}
					Iterator<Shovel> it=shovels.iterator();
					Iterator<Plant> itp=plantsLife.iterator();
					while(it.hasNext()) {
						Shovel s=it.next();
						if(s.isMove() && shovelCheck) {
							while(itp.hasNext()) {
								Plant p=itp.next();
								int x1=p.getX();
								int x2=p.getX()+p.getWidth();
								int y1=p.getY();
								int y2=p.getY()+p.getHeight();
								if((p.isLife() || (p instanceof Blover && ((Blover)p).isClick())) && mX>x1 && mX<x2 && mY>y1 && mY<y2) {
									itp.remove();
									it.remove();
									shovelCheck=false;
									break;
								}
							}
						}
					}
					for(Plant p : plants) {
						if((p.isStop() || p.isWait()) && !plantCheck && !shovelCheck) {
							int x1=p.getX();
							int x2=p.getX()+p.getWidth();
							int y1=p.getY();
							int y2=p.getY()+p.getHeight();
							if(mX>x1 && mX<x2 && mY>y1 && mY<y2) {
								p.goMove();
								plantCheck=true;
								break;
							}
						}
					}
					Iterator<Shovel> its=shovels.iterator();
					if(plantsLife.size()>0) {
						while(its.hasNext()) {
							Shovel s = its.next();
							int x1 = s.getX();
							int x2 = s.getX()+s.getWidth();
							int y1 = s.getY();
							int y2 = s.getY()+s.getHeight();
							if(s.isWait() && mX>x1 && mX<x2 && mY>y1 && mY<y2 && !plantCheck) {
								s.goMove();
								shovelCheck=true;
							}
						}
					}
					for(Plant p : plantsLife) {
						if(p instanceof Blover) {
							int x1=p.getX();
							int x2=p.getX()+p.getWidth();
							int y1=p.getY();
							int y2=p.getY()+p.getHeight();
							if(((Blover)p).isClick() && mX>x1 && mX<x2 && mY>y1 && mY<y2 && !plantCheck && !shovelCheck) {
								p.goDead();
								for(Zombie z : zombies) {
									if(z.isAttack()) {
										z.goLife();
									}
									z:for(int i=0;i<10;i++) {
										z.goOut();
										if(z.getX()>=GamePlay.WIDTH-z.getWidth()) {
											z.goRun();
											break z;
										}
									}
								}
							}
						}
					}
				}
				if(state==START) {
					int x1 = 720;
					int x2 = 990;
					int y1 = 210;
					int y2 = 320;
					if(mX>=x1&&mX<=x2&&mY>=y1&&mY<=y2) {
						state = RUNNING;
					}
				}
				if(state==GAME_OVER) {
					int x1 = 480;
					int x2 = 950;
					int y1 = 100;
					int y2 = 540;
					if(mX>=x1&&mX<=x2&&mY>=y1&&mY<=y2) {
						state = START;
						gameLife = 1;
					}
				}
			}
			public void mouseMoved(MouseEvent e) {
				if(state==RUNNING) {
					for(Plant p : plantsLife) {
						if(p.isMove()) {
							int x=e.getX();
							int y=e.getY();
							p.moveTo(x, y);
							break;
						}
					}
					for(Shovel s : shovels) {
						if(s.isMove()) {
							int x=e.getX();
							int y=e.getY();
							s.moveTo(x, y);
							break;
						}
					}
				}
			}
		};
		this.addMouseListener(l);
		this.addMouseMotionListener(l);
		Timer timer=new Timer();
		int interval=10;
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				if(state==RUNNING) {
					shovelEnterAction();
					checkShovelAction();
					zombieEnterAction();
					zombieStepAction();
					zombieMoveToSpikerockAction();
					zombieHitAction();
					plantEnterAction();
					plantStepAction();
					plantBangAction();
					zombieGoLife();
					bulletShootAction();
					bulletStepAction();
					bulletZombieHitAction();
					checkBloverAction();
					checkPlantAction();
					checkPlantLifeAction();
					checkZombieAction();
					bulletCheckAction();
					glassCheckAction();
					checkGameAction();		
					checkDeadZombieRemoveAction();
					bulletFireHitAction();
				}				
			}
		}, interval, interval);
	}
	public void refreshAction() {
		Timer timer=new Timer();
		int interval=80;
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				repaint();		
			}
		}, 0,interval);
	}
	public void paint(Graphics g) {
		if(state==START) {
			start.paintObject(g);
		}else if(state==RUNNING) {
			running.paintObject(g);
		}else if(state==GAME_OVER) {
			gameOver.paintObject(g);
		}		
		synchronized (plants) {
			for(Plant p:plants) {
				p.paintObject(g);
			}
		}
		synchronized (plantsLife) {
			for(Plant p:plantsLife) {	
				p.paintObject(g);
			}
		}
		synchronized (zombies) {
			for(Zombie z:zombies) {
				z.paintObject(g);
				if(z.isDead()) {
					z.paintHead(g);
				}
			}
		}
		synchronized (bullets) {
			for(Bullet b:bullets) {
				if(b.isFire()) {
					b.paintObjectFire(g);
				}else if(b.isDead()){
					b.paintObjectDead(g);
				}else {
					b.paintObject(g);
				}
				
			}	
		}
		synchronized (shovels) {
			for(Shovel s:shovels) {
				s.paintObject(g);
			}
		}	
	}
	public static void main(String[] args) {
		JFrame frame=new JFrame();
		GamePlay play=new GamePlay();
		frame.add(play);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT+40);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null); 
		frame.setTitle("植物大战僵尸");
		play.action();
		play.refreshAction();
		Runnable r = new ZombieAubio("cuowei.mp3");
		Thread t = new Thread(r);
		t.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
  • 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
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558

接下来就可运行了:
在这里插入图片描述

有兴趣的可以试一试。

下载地址:
植物大战僵尸完整源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/770583
推荐阅读
相关标签
  

闽ICP备14008679号