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;
/**
* 植物大战僵尸
* 游戏内容:
* 左边的滚轮机会自动生成植物的卡牌,鼠标单击可以选中植物,
* 被选中的植物将跟随鼠标移动,再次点击后,可在草地中放置植物。
* 豌豆射手可以发射豌豆,寒冰射手发射的冰冻豌豆可以使僵尸减速,
* 三头豌豆射手可以发射三颗豌豆,坚果可以阻止僵尸前进,吹风草
* 可以将所有的僵尸吹到屏幕外。地刺会持续对僵尸进行攻击。若对
* 放置的植物不满意,可以使用铲子,将草地上的植物移除。
* 不同的僵尸血量不同,移动速度不同。击杀足球僵尸后将获得
* 随机奖励,奖励类型有:全屏僵尸死亡,全屏僵尸静止两秒等。
* 当有僵尸闯入房间,游戏结束。点击图标可重新开始游戏。
*
* @author Lychee
*
*/
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++) {
// 如果第一个�
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
JAVA小游戏,植物大战僵尸,完美运行版,无BUG (233个子文件)
GamePlay.class 12KB
GamePlay$1.class 5KB
Zombie.class 3KB
Plant.class 3KB
Bullet.class 3KB
Shovel.class 2KB
zombieAubio.class 2KB
Background.class 2KB
Zombie3.class 2KB
ThreePeater.class 2KB
Repeater.class 1KB
SnowPea.class 1KB
Blover.class 1KB
GamePlay$2.class 1KB
Zombie0.class 1KB
Zombie1.class 1KB
Zombie2.class 1KB
Glass.class 1KB
Spikerock.class 1KB
WallNut.class 1KB
SnowBullet.class 1KB
PeaBullet.class 1KB
Award.class 210B
Shoot.class 146B
.classpath 295B
plant_vs_zombie_simple.iml 437B
GamePlay.java 16KB
Plant.java 3KB
Zombie.java 3KB
Bullet.java 2KB
Shovel.java 2KB
zombieAubio.java 1KB
Background.java 1KB
ThreePeater.java 1011B
Zombie3.java 973B
Glass.java 888B
Repeater.java 865B
SnowPea.java 864B
Blover.java 841B
Zombie1.java 765B
Zombie0.java 763B
Zombie2.java 761B
Spikerock.java 623B
WallNut.java 615B
SnowBullet.java 569B
PeaBullet.java 569B
Award.java 177B
Shoot.java 133B
bg0.jpg 198KB
bg0.jpg 198KB
bg1.jpg 95KB
bg1.jpg 95KB
bg2.jpg 36KB
bg2.jpg 36KB
zombie37.png 16KB
zombie37.png 16KB
zombie310.png 16KB
zombie310.png 16KB
zombie34.png 16KB
zombie34.png 16KB
zombie31.png 16KB
zombie31.png 16KB
zombie36.png 16KB
zombie36.png 16KB
zombie30.png 15KB
zombie30.png 15KB
zombie33.png 15KB
zombie33.png 15KB
zombie35.png 15KB
zombie35.png 15KB
zombie21.png 15KB
zombie21.png 15KB
zombie39.png 15KB
zombie39.png 15KB
zombie20.png 15KB
zombie20.png 15KB
zombie32.png 15KB
zombie32.png 15KB
zombie38.png 15KB
zombie38.png 15KB
zombie22.png 15KB
zombie22.png 15KB
zombie24.png 15KB
zombie24.png 15KB
zombie23.png 15KB
zombie23.png 15KB
zombie28.png 14KB
zombie28.png 14KB
zombie27.png 14KB
zombie27.png 14KB
zombie26.png 14KB
zombie26.png 14KB
zombie29.png 14KB
zombie29.png 14KB
zombie25.png 14KB
zombie25.png 14KB
zombie07.png 13KB
zombie07.png 13KB
zombie08.png 13KB
zombie08.png 13KB
共 233 条
- 1
- 2
- 3
SLT的编程之路
- 粉丝: 42
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5