当前位置:   article > 正文

Java版【植物大战僵尸+源码】

Java版【植物大战僵尸+源码】

上期回顾:

今天给大家推荐一个Gtihub开源项目:PythonPlantsVsZombies,翻译成中就是植物大战僵尸。
在这里插入图片描述

植物大战僵尸》是一款极富策略性的小游戏。可怕的僵尸即将入侵,每种僵尸都有不同的特点,例如铁桶僵尸拥有极强的抗击打能力,矿工僵尸可以挖地道绕过种植在土壤表面的植物等。玩家防御僵尸的方式就是栽种植物。49种植物每种都有不同的功能,例如樱桃炸弹可以和周围一定范围内的所有僵尸同归于尽,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以针对不同僵尸的弱点来合理地种植植物,这也是胜利的诀窍。游戏根据玩法不同分为五种游戏模式:冒险模式、迷你模式、解谜模式、生存模式、禅境花园。加之黑夜、屋顶、浓雾以及泳池之类的障碍增加了其挑战性。该游戏近乎永无止境。

文章地址:Python版【植物大战僵尸 +源码】

写在前面:

小伙伴儿们大家好,我们又见面啦。在上一篇文章中,我向大家介绍了如何使用Python编程语言来实现经典的游戏——植物大战僵尸,并且分享了完整的源代码。这篇文章发布后,反响热烈,受到了众多小伙伴的热情追捧和积极反馈。在众多留言中,我注意到有不少小伙伴对Java版本的实现表现出了浓厚的兴趣。为了满足大家的期待,今天我特别准备了一份精心制作的内容——深入剖析Java版“植物大战僵尸”的实现原理,并细致解读其代码结构。

今天,我就为大家带来了另一款同样精彩的游戏分享——Java版的植物大战僵尸。在这篇文章中,我们将深入探讨Java语言实现植物大战僵尸的原理,并对代码进行详细的分析和讲解。无论你是Java初学者,还是有一定基础的开发者,相信这篇文章都能给你带来不少启发和收获。

在这里插入图片描述

那么,废话不多说,让我们共同开启今天的游戏之旅,一起领略Java版植物大战僵尸的魅力吧!希望通过这篇文章,大家能够更加深入地了解Java编程语言的强大功能,以及如何将这些功能应用到实际项目中,创造出更多有趣、实用的作品。

一、环境准备:

为了准备Java版植物大战僵尸的开发环境,你需要以下具体工具和版本:

  1. JDK: Oracle JDK 8。确保安装了Java 8版本,因为它提供了广泛的库支持和稳定性。进行Java环境变量配置。

  2. IDE: IntelliJ IDEA Community Edition 2021-03 或 Eclipse IDE 。这些IDE提供了用户友好的界面和强大的开发工具,适合Java开发。

  3. 版本控制系统: Git。用于代码的版本管理和协作开发。

  4. 依赖管理: Maven 3.8.3。帮助你管理项目的依赖库和构建过程。

二、游戏内容:

在这里插入图片描述

2.1、 游戏目标
玩家的目标是在僵尸不断进攻的情况下,保护好房间不被僵尸闯入。玩家需要策略性地种植各种植物来抵御僵尸的进攻。

2.2 、植物卡牌系统

  • 游戏的左侧设有一个滚轮机会,它会不断地随机生成各种植物的卡牌。
  • 玩家可以通过鼠标点击来选中想要的植物卡牌,选中的植物卡牌会显示高亮或改变颜色,表示已被激活。
  • 当植物被选中后,鼠标指针会变成该植物的图标,并且可以移动到草地的任何位置进行放置。
  • 再次点击鼠标或空格键,玩家可以将选中的植物种植在指定的草地上,建立起防御阵线。

2.3 、植物功能介绍

在这里插入图片描述

  • 豌豆射手:基础攻击植物,可以发射豌豆攻击前方的僵尸。
  • 寒冰射手:发射冰冻豌豆,不仅能攻击僵尸,还能减缓僵尸的移动速度。
  • 三头豌豆射手:同时发射三颗豌豆,具有更强的攻击力。
  • 坚果:具有高耐久性,可以阻挡僵尸前进,为其他植物争取攻击时间。
  • 吹风草:具有特殊能力,可以一次性将所有屏幕上的僵尸吹出屏幕。
  • 地刺:放置在草地上后,会对经过的僵尸造成持续伤害。

2.4 、僵尸介绍

  • 游戏中有多种类型的僵尸,每种僵尸都有不同的血量和移动速度。
  • 击杀特定类型的僵尸,如足球僵尸,可以获得随机奖励,这些奖励会对僵尸产生特殊效果,如全屏僵尸死亡或全屏僵尸静止两秒等。

2.5 、游戏互动

  • 如果玩家对放置的植物不满意,可以使用铲子图标移除已种植的植物,为重新布局防御提供灵活性。
  • 游戏过程中,玩家需要注意植物的阳光产出,阳光是种植植物的货币,合理管理阳光资源对于建立有效的防御至关重要。

2.6 、游戏结束与重新开始

  • 如果有僵尸成功闯入房间,游戏即宣告结束。玩家可以选择点击重新开始游戏,进入新的一轮挑战。
  • 游戏可以设定不同的难度级别,随着游戏的进行,僵尸的数量和强度会逐渐增加,为玩家带来更大的挑战。

三、实现思路:

3.1 游戏架构设计:

采用面向对象的设计原则,将游戏中的各个元素(如植物、僵尸、子弹等)设计为不同的对象类。
使用MVC(Model-View-Controller)设计模式分离游戏的数据模型、用户界面和控制逻辑,以提高代码的可维护性和可扩展性。

3.2 游戏元素实现:

  • 植物:定义不同类型的植物类,如向日葵、豌豆射手等,每种植物都有其特定的属性和行为,例如生成阳光、攻击僵尸等。
  • 僵尸:创建僵尸类,具有不同的类型和行为模式,例如普通僵尸、路障僵尸等,它们会沿着特定路径移动并向植物发起攻击。

铁桶僵尸:

package core.zombies;

import java.awt.image.BufferedImage;

public class Zombie0 extends Zombie{
	
	// 铁桶僵尸
	// 加载图片
	private static BufferedImage imgs[];
	static {
		imgs = new BufferedImage[10];
		for(int i=0;i<imgs.length;i++) {
			imgs[i] = loadImage("zombie0"+i+".png");
		}
	}
	
	// 设置图片,0-4为生存,5-9为攻击
	int index = 0;
	public BufferedImage getImage() {
		if(isLife()) {
			return imgs[index++%5];//0-4
		}else if(isAttack()) {
			return imgs[index++%5+5];//5-9
		}else{
			return null;
		}
	}
	
	// 构造器
	public Zombie0() {
		super(166,144);
		live = 20;
		xSpeed = 2;
	}
	
	// 僵尸移动
	public void step() {
		this.x-= xSpeed;
	}
	
	// 僵尸的停止状态结束
	public void goRun() {
		xSpeed = 2;
	}
	
}
  • 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
  • 子弹:子弹类用于表示植物发射的攻击项目,需要实现子弹的移动轨迹和碰撞检测。

普通豌豆子弹:

package core.bullets;

import java.awt.image.BufferedImage;

public class PeaBullet extends Bullet{
	
	// 豌豆子弹
	// 加载图片
	private static BufferedImage imgs[];
	static {
		imgs = new BufferedImage[4];
		for(int i=0;i<imgs.length;i++) {
			imgs[i] = loadImage("PeaBullet"+i+".png");
		}
	}
	
	// 获取图片,0-3为子弹旋转移动
	int index = 0;
	public BufferedImage getImage() {
		if(isLife()) {
			return imgs[index++%4];//0-3
		}else {
			return null;
		}
	}
	
	// 构造器
	public PeaBullet(int x,int y) {
		super(x,y,24,24);
		xSpeed = 3;
	}

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

寒冰射手子弹:

package core.bullets;

import java.awt.image.BufferedImage;

public class SnowBullet extends Bullet{

	// 冰冻子弹
	// 加载图片
	private static BufferedImage imgs[];
	static {
		imgs = new BufferedImage[4];
		for(int i=0;i<imgs.length;i++) {
			imgs[i] = loadImage("SnowBullet"+i+".png");
		}
	}

	// 获取图片,0-3为子弹旋转移动
	int index = 0;
	public BufferedImage getImage() {
		if(isLife()) {
			return imgs[index++%4];//0-3
		}else {
			return null;
		}
	}

	// 构造器
	public SnowBullet(int x,int y) {
		super(x,y,24,24);
		xSpeed = 3;
	}

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

3.3 游戏逻辑处理:

  • 实现游戏循环,不断更新游戏状态,包括植物的生长、僵尸的移动、子弹的发射等。
  • 设计事件处理机制,响应用户输入和游戏内的事件(如植物被吃掉、僵尸被消灭)。
  • 实现碰撞检测算法,用于判断子弹和僵尸、植物和僵尸之间的交互。
package core.game;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

import core.bullets.Bullet;
import core.bullets.SnowBullet;
import core.plants.Blover;
import core.plants.Plant;
import core.plants.Repeater;
import core.plants.Shoot;
import core.plants.SnowPea;
import core.plants.Spikerock;
import core.plants.ThreePeater;
import core.plants.WallNut;
import core.zombies.Award;
import core.zombies.Zombie;
import core.zombies.Zombie0;
import core.zombies.Zombie1;
import core.zombies.Zombie2;
import core.zombies.Zombie3;

/**
 * 植物大战僵尸
 * 游戏内容:
 * 		左边的滚轮机会自动生成植物的卡牌,鼠标单击可以选中植物,
 * 被选中的植物将跟随鼠标移动,再次点击后,可在草地中放置植物。
 * 豌豆射手可以发射豌豆,寒冰射手发射的冰冻豌豆可以使僵尸减速,
 * 三头豌豆射手可以发射三颗豌豆,坚果可以阻止僵尸前进,吹风草
 * 可以将所有的僵尸吹到屏幕外。地刺会持续对僵尸进行攻击。若对
 * 放置的植物不满意,可以使用铲子,将草地上的植物移除。
 * 		不同的僵尸血量不同,移动速度不同。击杀足球僵尸后将获得
 * 随机奖励,奖励类型有:全屏僵尸死亡,全屏僵尸静止两秒等。
 * 		当有僵尸闯入房间,游戏结束。点击图标可重新开始游戏。
 * 
 */


public class GamePlay extends JPanel{
	
	// 游戏窗口大小,游戏状态
	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;

	// 背景
	private Background start = new Background(800,533,300,50);
	private Background running = new Background(WIDTH,HEIGHT,0,0);
	private Background gameOver = new Background(WIDTH,HEIGHT,0,0);

	// 游戏对象
	// 僵尸集合
	private List<Zombie> zombies = new ArrayList<Zombie>();
	// 植物集合
	// 滚轮机上的植物,状态为stop和wait
	private List<Plant> plants = new ArrayList<Plant>();
	// 战场上的植物,状态为life和move
	private List<Plant> plantsLife = new ArrayList<Plant>();
	// 子弹集合
	private List<Bullet> bullets = new ArrayList<Bullet>();
	// 草地集合
	private List<Glass> glasses = new ArrayList<Glass>();
	// 铲子
	private List<Shovel> shovels = new ArrayList<Shovel>();

	// 铲子入场
	public void shovelEnterAction() {
		// 铲子只有一把
		if(shovels.size()==0) {
			shovels.add(new Shovel());
		}
	}

	// 草地入场
	// 第一块草地的坐标
	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();
			// 遍历所有植物,如果草地上有植物,将草地状态改为被占有
			for(Plant p:plantsLife) {
				if(p.isLife()) {
					int x1 = g.getX();
					int y1 = g.getY();
					int x = p.getX();
					int y = p.getY();
					// 共点问题
					if(x==x1&&y==y1) {						
						g.goHold();						
						break;
					}
				}
			}
		}
	}

	// 生成僵尸
	public Zombie nextOneZombie() {
		Random rand = new Random();
		// 控制不同种类僵尸出现的概率
		int type = rand.nextInt(20);
		if(type<5) {
			return new Zombie0();
		}else if(type<10) {
			return new Zombie1();
		}else if(type<15) {
			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++%3==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) {
					for(Zombie z: zombies) {
						int x1 = p.getX();
						int x2 = p.getX()+p.getWidth();
						int y1 = p.getY();
						int y2 = p.getY()+p.getHeight();
						int x = z.getX();
						int y = z.getY();
						// 如果僵尸在地刺上就扣血
						if(x>x1&&x<x2&&y>y1&&y<y2&&p.isLife()&&(z.isLife()||z.isAttack())) {
							z.loseLive();
						}
					}
				}
			}
		}
	}

	// 僵尸攻击
	// 设置攻击间隔
	int zombieHitTime = 0;
	public void zombieHitAction() {
		if(zombieHitTime++%100==0) {
			for(Zombie z:zombies) {
				// 如果战场上没有植物,则把所有僵尸的状态改为life
				if(!z.isDead()) {
					z.goLife();
				}
				for(Plant p:plantsLife) {
					// 如果僵尸是活的,并且植物是活的,并且僵尸进入攻击植物的范围
					if(z.isLife()&&!p.isDead()&&z.zombieHit(p)&&!(p instanceof Spikerock)) {
						// 僵尸状态改为攻击状态
						z.goAttack();
						// 植物掉血
						p.loseLive();
					}
				}
			}
		}
	}

	// 检测僵尸状态
	public void checkZombieAction() {
		// 迭代器
		Iterator<Zombie> it = zombies.iterator();
		while(it.hasNext()) {
			Zombie z = it.next();
			// 僵尸血量小于0则死亡,死亡的僵尸从集合中删除
			if(z.getLive()<=0) {
				// 判断僵尸是否有奖励的接口
				if(z instanceof Award) {
					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 zom:zombies) {
							zom.goStop();
							timeStop = 1;
							//zombieGoLife();
						}
						break;
					}
				}
				z.goDead();
				it.remove();
			}
			// 僵尸跑进房子,而游戏生命减一,并删除僵尸
			if(z.OutOfBound()) {
				gameLife--;
				it.remove();
			}
		}
	}

	// 僵尸静止2秒后继续移动
	int timeStop = 1;
	public void zombieGoLife() {
		if(timeStop++%200==0) {
			for(Zombie z:zombies) {
				z.goRun();
			}
		}
	}

	// 检测游戏状态
	// 初始游戏生命值
	int gameLife = 1;
	public void checkGameAction() {
		if(gameLife<=0) {
			state = GAME_OVER;
			// 游戏结束清空所有集合
			plants.clear();
			plantsLife.clear();
			zombies.clear();
			bullets.clear();
			shovels.clear();
		}
	}

	// 生成植物
	public Plant nextOnePlant() {
		Random rand = new Random();
		int type = rand.nextInt(30);
		// 控制植物的出场概率
		if(type<5) {
			return new Repeater();
		}else if(type<10) {
			return new SnowPea();
		}else if(type<15) {
			return new ThreePeater();
		}else if(type<20) {
			return new Spikerock();
		}else if(type<25) {
			return new Blover();
		}else {
			return new WallNut();
		}
	}

	// 植物入场
	// 设置进场间隔
	int plantTime = 0;
	public void plantEnterAction() {
		plantTime++;
		if(plantTime%300==0) {
			// 添加到滚轮机集合中
			plants.add(nextOnePlant());
		}
	}

	// 植物移动
	public void plantStepAction() {
		for(Plant p:plants) {
			// 只有滚轮机集合中的wait状态的植物会移动
			if(p.isWait()) {
				p.step();
			}
		}
	}

	// 植物在滚轮机上的碰撞判定
	public void plantBangAction() {
		// 遍历滚轮机上植物集合,从第二个开始
		for(int i=1;i<plants.size();i++) {
			// 如果第一个植物的y大于0,并且是stop状态,则状态改为wait
			if(plants.get(0).getY()>0&&plants.get(0).isStop()) {
				plants.get(0).goWait();
			}
			// 如果第i个植物y小于i-1个植物的y+height,则说明碰到了,改变i的状态为stop
			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();
			}
			/*
			 * 如果第i个植物y大于于i-1个植物的y+height,则说明还没碰到或者第i-1个
			 * 植物被移走了,改变i的状态为wait,可以继续往上走
			 */
			if(plants.get(i).isStop()&& 
					plants.get(i).getY()>plants.get(i-1).getY()+plants.get(i-1).getHeight()) {
				plants.get(i).goWait();
			}
		}
	}

	// 检测滚轮机上的植物状态
	public void checkPlantAction1() {
		// 迭代器
		Iterator<Plant> it = plants.iterator();
		while(it.hasNext()) {
			Plant p = it.next();
			/*
			 * 如果滚轮机集合里有move或者life状态的植物
			 * 则添加到战场植物的集合中,并从原数组中删除
			 */
			if(p.isMove()||p.isLife()) {
				plantsLife.add(p);
				it.remove();
			}
		}
	}
	// 检测战场上的植物状态
	public void checkPlantAction2() {
		// 迭代器
		Iterator<Plant> it = plantsLife.iterator();
		while(it.hasNext()) {
			Plant p = it.next();
			// 植物生命小于0死亡,死亡状态的植物从集合中移出
			if(p.getLive()<=0) {
				p.goDead();
				it.remove();
			}
		}
	}
	// 检测吹风草的状态
	int bloverTime = 1;
	public void checkBloverAction() {
		if(bloverTime++%200==0) {
			for(Plant p :plantsLife) {
				if(p instanceof Blover &&p.isLife()) {
					((Blover) p).goClick();
				}
			}
		}
	}

	// 子弹入场
	// 控制子弹进场的间隔时间
	int BulletTime = 0;
	public void BulletShootAction() {
		if(BulletTime++%50==0) {
			for(Plant p : plantsLife) {
				if(p.isLife()) {
					if(p instanceof Shoot) {
						// 如果植物有射击的接口
						Shoot s = (Shoot)p;
						// 射击
						//s.shoot();
						// 把射击的子弹装进子弹数组
						bullets.addAll(Arrays.asList(s.shoot()));
					}
				}
			}
		}
	}

	// 子弹移动
	public void BulletStepAction() {
		for(Bullet b:bullets) {
			b.step();
		}
	}

	// 子弹与僵尸的碰撞
	public void hitAction() {
		// 遍历僵尸和子弹数组
		for(Zombie z:zombies) {
			for(Bullet b:bullets) {
				// 满足条件则僵尸扣血,子弹去死
				if((z.isAttack()||z.isLife())&&b.isLife()&&b.hit(z)&&z.getX()<GamePlay.WIDTH) {
					if(b instanceof SnowBullet) {
						z.goSlowDown();
					}
					z.loseLive();
					b.goDead();
				}
			}
		}
	}

	// 检测子弹状态
	public void bulletCheckAction() {
		Iterator<Bullet> it = bullets.iterator();
		while(it.hasNext()) {
			Bullet b = it.next();
			// 如果子弹死亡或者越界则删除
			if(b.isDead()||b.isOutOfBound()) {
				it.remove();
			}
		}
	}

	// 运行代码
	// 鼠标上是否有植物和铲子的判定
	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();
				//System.out.println(Mx+"-"+My);

				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) {
										bloverTime = 0;
									}
									break f;
								}
							}			
						}
					}
				// 使用铲子
				Iterator<Shovel> it = shovels.iterator();
				Iterator<Plant> it2 = plantsLife.iterator();
				while(it.hasNext()) {
					Shovel s = it.next();
					// 如果铲子是移动状态,就遍历植物集合
					if(s.isMove()) {
						while(it2.hasNext()) {
							Plant p  = it2.next();
							int x1 = p.getX();
							int x2 = p.getX()+p.getWidth();
							int y1 = p.getY();
							int y2 = p.getY()+p.getHeight();
							if((p.isLife()||((Blover) p).isClick())&&Mx>x1&&Mx<x2&&My>y1&&My<y2&&shovelCheck) {
								// 移除植物
								it2.remove();
								// 移除铲子
								it.remove();
								shovelCheck = false;
							}
						}
					}
				}
				// 鼠标单击后,植物将改变状态,随鼠标移动
				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> it3 = shovels.iterator();
				if(plantsLife.size()>0) {
					while(it3.hasNext()) {
						Shovel s = it3.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() {
			public void run() {
				if(state==RUNNING) {
					shovelEnterAction();
					zombieEnterAction();
					zombieStepAction();
					zombieMoveToSpikerockAction();
					zombieHitAction();
					plantEnterAction();
					plantStepAction();
					plantBangAction();
					zombieGoLife();
					BulletShootAction();
					BulletStepAction();
					hitAction();
					checkBloverAction();
					checkPlantAction1();
					checkPlantAction2();
					checkZombieAction();
					bulletCheckAction();
					glassCheckAction();
					checkGameAction();
				}
				repaint();
			}
		},interval,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);
		}
		// 画植物
		for(Plant p:plants) {
			p.paintObject(g);
		}
		for(Plant p:plantsLife) {
			p.paintObject(g);
		}
		// 画僵尸
		for(Zombie z:zombies) {
			z.paintObject(g);
		}
		// 画子弹
		for(Bullet b:bullets) {
			b.paintObject(g);
		}		
		// 画铲子
		for(Shovel s:shovels) {
			s.paintObject(g);
		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		GamePlay game= new GamePlay();
		frame.add(game);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT);
		frame.setLocationRelativeTo(null); 
		frame.setVisible(true); 

		game.action();
		
		// 启动线程加载音乐
		Runnable r = new zombieAubio("bgm.wav");
		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
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689

3.4 图形和动画:

  • 使用Java的Swing或JavaFX库来绘制游戏界面和动画效果。
  • 为游戏元素创建图像资源,并在界面上动态显示和更新这些图像。

3.5 音效和背景音乐:

  • 集成音效和背景音乐,增强游戏的沉浸感和娱乐性。
  • 在适当的时机播放攻击、受伤、胜利或失败等音效。

四、项目获取:

添加wx,备注:植物大战僵尸获取


在这里插入图片描述

欢迎添加微信,加入我的核心小队,请备注来意

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