趣味JAVA实现的植物大战僵尸源码

趣味JAVA实现的植物大战僵尸源码

项目结构:

趣味JAVA实现的植物大战僵尸源码_第1张图片
1.运行界面

*2.主要代码展示:
*
Background.java

package plantsGame;

import java.awt.image.BufferedImage;

public class Background extends Zombies{
	static BufferedImage image;
	static {
		image = loadImage("background.png");//背景图片
	}
	public Background() {
		super(0, 0, 1402, 637);
	}
	protected void step() {
	}
	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return null;
	}


}

2.Game.java

package plantsGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel{
	private int score;
	Background background = new Background();
	Hammer hammer = new Hammer();
	Zombies enemy[] = {
		new Ordinary(),
		new Roadblock(),
		new Iron()};
	int a = 0;
	//画笔
	public void paint(Graphics g) {
		g.drawImage(background.image, 0, 0, 1400, 600, null);
		for(int i=0;i<enemy.length;i++) {
			enemy[i].paintObject(g);
		}
		hammer.paintObject(g);
		g.setFont(new Font("Tahoma", Font.BOLD, 20));
		g.drawString("SCORE:"+score,1200,550);//画分数
		g.drawString("AUTHOR:"+"烧包哥哥",1200,570);//作者
		if(space==false) {
			g.setFont(new Font("Tahoma", Font.BOLD, 80));
			g.drawString("STOP",620,640/2);//画分数
		}
		
	}
	//随即生成僵尸
	public Zombies enemyRandom() {
		a = (int) (Math.random()*20);
		if(a<=12) {
			return new Ordinary();
		}else if(a>12&&a<=17) {
			return new Roadblock();
		}else {
			return new Iron();
		}
	}
	//生成僵尸
	int enterIndex=0;
	public void enterAction() {
		enterIndex++;
		if(enterIndex%200==0) {
			//多长时间走一次
			Zombies obj=enemyRandom();//获取敌人数组
			enemy=Arrays.copyOf(enemy, enemy.length+1);//扩容
			enemy[enemy.length-1]=obj;
		}
	}
	//僵尸走动
	public void step() {
		for(int a = 0;a<enemy.length;a++) {
			enemy[a].step();
			if(enemy.length<=10) {
				enterAction();
			}
		}
	}
	//判断生命为0的僵尸删除
	public void outOfBoundsAction() {
		int index = 0;
		Zombies[] flyingLives = new Zombies[enemy.length];
		for (int i = 0; i < enemy.length; i++) {
			Zombies f = enemy[i];
			if (f.life!=0) {
				flyingLives[index++] = f;
			}else {
				score+=1;
			}
		}
		enemy = Arrays.copyOf(flyingLives, index); 
	}
		/**启动程序的执行*/
	public void action() {	
		MouseAdapter l=new MouseAdapter() {//创建侦听器对象
			public void mouseMoved(MouseEvent e) {
				if(space==true) {
					if(true) {
						int x=e.getX();
						int y=e.getY();
						hammer.moveTo(x, y);
					}
				}
			}
			//鼠标左键单击事件
			public void mouseClicked(MouseEvent arg0) {
				if(space==true) {
				if(arg0.getButton() == MouseEvent.BUTTON1) {
			   	hammer.getButton(1);
				}
				//判断僵尸是否被打中,打中了减掉一血
				boolean duan = false;
				if(true) {
					int x=arg0.getX();
					int y=arg0.getY();
					for(int a = 0;a<enemy.length;a++) {
						duan = hammer.duang(enemy[a].x,enemy[a].width, enemy[a].y,enemy[a].height);
						if(duan==true) {
							duan = enemy[a].booleanfile();
						}
					}
				}
			}
			}
		};

		this.addMouseListener(l);
		this.addMouseMotionListener(l);	
		Timer timer=new Timer();//创建定时器
		int intervel=70;//时间间隔,以毫秒为单位,每10毫秒运行一次
		timer.schedule(new TimerTask() {
			public void run() {//定时干的事
				if(space==true) {
					step();//僵尸走动
					outOfBoundsAction();//判断僵尸生命并删除
					enterAction();//生成僵尸
				}

			}

		},intervel, intervel);//(定时干的事,几毫秒后开始,每几毫秒运行一次)
	}
	public void action1() {
		Timer timer=new Timer();//创建定时器
		int intervel=180;//时间间隔,以毫秒为单位,每10毫秒运行一次
		timer.schedule(new TimerTask() {
			public void run() {//定时干的事
				repaint();//重画,调用paint方法
			}

		},intervel, intervel);//(定时干的事,几毫秒后开始,每几毫秒运行一次)
	}
	private KeyboardPanel keyboardPanel = new KeyboardPanel();
	//键盘监控
	public Game(){
	  add(keyboardPanel);   
	  	keyboardPanel.setFocusable(true);       
  	}
	static boolean space = true;
	//主方法
	public static void main(String[] args) {
		JFrame jf = new JFrame();
		Game game = new Game();
		if(space==true) {
			game.action();
			game.action1();
		}
		jf.add(game);
		jf.setSize(1402,637);//宽高
		jf.setVisible(true);//窗口可见
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	 //键盘监控事件类
	static class KeyboardPanel extends JPanel{
		public KeyboardPanel(){
			addKeyListener( new KeyAdapter(){
				public void keyPressed(KeyEvent e){
					switch(e.getKeyCode()){
						case KeyEvent.VK_ENTER: 
							if(space==true) {
								space=false;
								System.out.println(space);
							}else if(space==false){
								space=true;
								System.out.println(space);
							}

						}
					}
			});
		}
	}
}

3.Hammer.java

package plantsGame;

import java.awt.image.BufferedImage;

/**
 * 锤子
 * @author soft01
 *	this.x = x-40;
 *	this.y = y-90;
 */
public class Hammer extends Zombies{
	private int mouse = 0;
	public static BufferedImage images[] = new BufferedImage[2];
	static {
		images[0] = loadImage("hammer.png");
		images[1] = loadImage("hammer_down.png");
	}
	public Hammer() {
		super(10, 10, 70, 110);
	}
	public BufferedImage getImage() {
		if(mouse==1) {
			mouse-=1;
			return images[1];
		}
		return images[0];
	}
	protected void step() {
	}
	/*
	 * 锤子跟着鼠标移动
	 */
	public void moveTo(int x,int y) {
		this.x = x-width;
		this.y = y-height;
	}
	public int getx() {
		return x;
	}
	public int gety() {
		return y;
	}
//	public boolean bang(int x,int x1,int y,int y1) {
//		System.out.println('b');
//		if(this.x>=x&&this.x<=x1&&this.y>=y&&this.y>=y1) {
//			System.out.println('a');
//			return true;
//		}else {
//			return false;
//		}
//	}
	/*
	 * 鼠标被左键单击
	 */
	public void getButton(int mouse) {
		this.mouse = mouse;
	}
	//判断是否击中僵尸
	public boolean duang(int x,int width,int y,int height) {
		if(this.x-20>=x-70&&this.x-20<=x&&this.y-50<=y-60&&this.y-30>=(y-60)-140) {
			return true;
		}else {
			return false;
		}
		
	}
}

4.Iron.java

package plantsGame;

import java.awt.image.BufferedImage;

public class Iron extends Zombies{
	public static BufferedImage images[] = new BufferedImage[8];
	static {
		for(int a=1;a<=8;a++) {
			images[a-1] = loadImage("z_02_0"+a+".png");
		}

	}
	public Iron() {
		super(1300, 8, 90,130,3,4);
		this.y = nextY();
		System.out.println(life);
	}
	

	protected void step() {
		x-=speed;
	}
	int index=0;
	public BufferedImage getImage() {
		if(index==8) {
			index=0;
		}
		return images[index++];	
	}
}

5.Ordinary.java

package plantsGame;

import java.awt.image.BufferedImage;

public class Ordinary extends Zombies{
	public static BufferedImage images[] = new BufferedImage[8];
	static {
		for(int a=1;a<=8;a++) {
			images[a-1] = loadImage("z_00_0"+a+".png");
		}

	}
	public Ordinary() {
		super(1300, 10, 90,130,1,1);
		this.y = nextY();
	}

	protected void step() {
		x-=speed;
	}
	int index=0;
	public BufferedImage getImage() {
		if(index==8) {
			index=0;
		}
		return images[index++];	
	}


}

6.Roadblock.java

package plantsGame;

import java.awt.image.BufferedImage;

public class Roadblock extends Zombies{
	public static BufferedImage images[] = new BufferedImage[8];
	static {
		for(int a=1;a<=8;a++) {
			images[a-1] = loadImage("z_01_0"+a+".png");
		}

	}
	public Roadblock() {
		super(1300, 10, 90,130,2,2);
		this.y = nextY();
	}

	protected void step() {
		x-=speed;
	}
	int index=0;
	public BufferedImage getImage() {
		if(index==8) {
			index=0;
		}
		return images[index++];	
	}
}

7.Zombies.java

package plantsGame;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
/**
 * 8
 * 100
 * 200
 * 300
 * 400
 */
abstract public class Zombies {
	protected int x;
	protected int y;
	protected int width;
	protected int height;
	protected int speed;
	protected int life;
	public Zombies(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.life = life;
	}
	public Zombies(int x, int y, int width, int height,int speed,int life) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.speed = speed;
		this.life = life;
	}
	public static BufferedImage loadImage(String fileName) {
		try {
			BufferedImage img=ImageIO.read(Game.class.getResource(fileName));
			return img;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	/**获取图片*/
	public abstract BufferedImage getImage();
	public void paintObject(Graphics g) {
		g.drawImage(getImage(),x,y,width,height,null);
			
	}

	abstract protected void step();
	int a = 1;
	//僵尸随即出现的路线
	public int nextY() {
		a = (int) (Math.random()*4);
		if(a==0) {
			return 8;
		}else if(a==1) {
			return 100;
		}else if(a==2) {
			return 200;
		}else if(a==3) {
			return 300;
		}else {
			return 400;
		}
	}
	public boolean booleanfile() {
		life-=1;
		return false;
	}
	
}

谢谢大家观看,有需要源码的同学请后台留言,博主等着你们!

你可能感兴趣的:(趣味JAVA)