Java项目:植物大战僵尸(java+swing)

功能简介:

植物大战僵尸、冒险模式、生存模式、解谜模式

 

 

僵尸移动线程:

package plantvsplant.tool;

import javax.swing.JLabel;

import plantvsplant.Controller;

public class CorpseMoveThread extends Thread{

	private int x;
	private int y;
	private JLabel corpse;
	private boolean flag=true;
	private int second=1500;
	private Controller controller;
	public CorpseMoveThread(Controller controller,int x,int y){
		this.x=x;
		this.y=y;
		this.controller=controller;
		controller.getWin().putCorpse(x, y,Integer.parseInt(controller.getGrid().getBoard()[x][y].substring(0,2)));
		corpse=controller.getWin().getCorpseMap().get(100*x+y);
	}
	public void run() {
		
		while(flag){
			int aa=controller.moveCorpse(x, y);
//			for (int i = 0; i < 15; i++) {
//				for (int j = controller.getLength()-3; j < controller.getLength(); j++) {
//					System.out.print(controller.getGrid().getBoard()[i][j]+"\t");
//				}
//				System.out.println();
//			}
//			System.out.println();
//			System.out.println();
//			System.out.println();
//			System.out.println();
				corpse.setVisible(false);
				if(aa==-1){
					flag=false;
				}else{
					//controller.getWin().getCorpseMap().remove(100*x+y);
					controller.getWin().putCorpse(x, aa,Integer.parseInt(controller.getGrid().getBoard()[x][aa].substring(0,2)));
					corpse=controller.getWin().getCorpseMap().get(100*x+aa);
					y=aa;
				}
			try {
				sleep(second);
				
			} catch (Exception e) {
			}
		}
		if(flag==false){
			corpse.setVisible(false);
		}
	}
	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 boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public int getSecond() {
		return second;
	}
	public void setSecond(int second) {
		this.second = second;
	}
	public Controller getController() {
		return controller;
	}
	public void setController(Controller controller) {
		this.controller = controller;
	}
	public JLabel getCorpse() {
		return corpse;
	}
	public void setCorpse(JLabel corpse) {
		this.corpse = corpse;
	}
	
}

阳光收集器:

package plantvsplant.tool;

import plantvsplant.Controller;

public class MoneyEnoughThread extends Thread {
	private Controller controller;
	private boolean goon = true;
	private int sunvalue = 0;
	public MoneyEnoughThread(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void run() {
		
		while (goon) {
			try {
					sleep(300);
			} catch (InterruptedException e) {
			}
			sunvalue = controller.getSunValue();
			canSun();
		}
	}
	public void canSun(){
		if(sunvalue>=50){	
			if(controller.getWin().getPsunflower().isTrainFinish()==true
				&& !controller.getWin().getCard0().isEnabled()){					
				controller.getWin().getCard0().setEnabled(true);
				}
			if(controller.getWin().getPpotato().isTrainFinish()==true
					&& !controller.getWin().getCard3().isEnabled()){
				controller.getWin().getCard3().setEnabled(true);	
		}
	}
	if(sunvalue>=100){
		if(controller.getWin().getPpease().isTrainFinish()==true
				&& !controller.getWin().getCard2().isEnabled()){					
				controller.getWin().getCard2().setEnabled(true);
				}
	}
	if(sunvalue>=150){
		if(controller.getWin().getPbomb().isTrainFinish()
				&& !controller.getWin().getCard1().isEnabled()){					
				controller.getWin().getCard1().setEnabled(true);
				}
	}
	if(sunvalue>=175){
		if( controller.getWin().getPsnowpease().isTrainFinish()
				&&!controller.getWin().getCard4().isEnabled()){					
			controller.getWin().getCard4().setEnabled(true);
			}
	}	
	if(sunvalue<175){
		if(controller.getWin().getCard4().isEnabled()){
			controller.getWin().getCard4().setEnabled(false);
		}
	}
	if(sunvalue<150){
		if(controller.getWin().getCard1().isEnabled()){
			controller.getWin().getCard1().setEnabled(false);
		}
	}
	if(sunvalue<100){
		if(controller.getWin().getCard2().isEnabled()){
			controller.getWin().getCard2().setEnabled(false);
		}
	}
	if(sunvalue<50){
		if(controller.getWin().getCard0().isEnabled()){
			controller.getWin().getCard0().setEnabled(false);
		}
		if(controller.getWin().getCard3().isEnabled()){
			controller.getWin().getCard3().setEnabled(false);
		}
	}
	}
}

//package plantvsplant;
//
//public class MoneyEnoughThread extends Thread {
//	private Controller controller;
//	private boolean goon = true;
//	private int sunvalue = 0;
//	public MoneyEnoughThread(Controller controller) {
//		this.controller = controller;
//	}
//
//	@Override
//	public void run() {
//		
//		while (goon) {
//			try {
//					sleep(300);
//			} catch (InterruptedException e) {
//			}
//			sunvalue = controller.getSunValue();
//			for (int j = 0; j < 5; j++) {
//				isEnable(j);
//			}
//		}
//	}
//	public void isEnable(int j){
//		if (sunvalue < controller.getWin().getPlant(j).getPrice()
//				|| controller.getWin().getPlant(j).isTrainFinish() == false) {
//			controller.getWin().setCardsEnable(j, false);
//		}
//		if (sunvalue >= controller.getWin().getPlant(j).getPrice()
//				&& controller.getWin().getPlant(j).isTrainFinish() == true) {
//			controller.getWin().setCardsEnable(j, true);
//		}
//	}
//}

攻击移动路线:

package plantvsplant.tool;

import javax.swing.JLabel;

import plantvsplant.Controller;

public class BulletMoveThread extends Thread{
	private boolean flag=true;
	private int x;
	private int y;
	private JLabel bullet;
	private Controller controller;
	public BulletMoveThread(int x,Controller controller,int y){
		this.controller=controller;
		this.x=x;
		this.y=y;
		controller.getWin().putBomp(x, y, 0);
		bullet=controller.getWin().getBulletMap().get(100*x+y);
	}
	public void run() {
		while(flag){
			int aa=controller.moveBullet(x, y);
			bullet.setVisible(false);
			if(aa!=-1){			
				//controller.getWin().getBulletMap().remove(100*x+y);
				controller.getWin().putBomp(x, aa, 0);
				bullet=controller.getWin().getBulletMap().get(100*x+aa);
				if(bullet==null){
					controller.fail();
				}
				y=aa;
			}else{
				flag=false;
			}
			try {
				sleep(300);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}	
			if(bullet!=null){
				bullet.setVisible(false);
		}
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public JLabel getBullet() {
		return bullet;
	}
	public void setBullet(JLabel bullet) {
		this.bullet = bullet;
	}
	
}

猜你喜欢

转载自blog.csdn.net/m0_59687645/article/details/121725823
今日推荐