新手学java 问题求教!!

问题 专栏收录该内容
0 篇文章 0 订阅
  最近学习java 尝试写了个坦克大战游戏 但是问题多多 请指教
1.防止敌人坦克重叠运动  代码不知道在哪里出错了                   无法实现此功能 
2.当我的坦克击中敌人坦克时,敌人就消失(爆炸的效果)               第一辆爆炸的坦克没有爆炸效果,后面的则有爆炸效果
/**
 * 功能:坦克游戏
 * 1.画出坦克.
 * 2.我的坦克可以上下左右移动
 * 3.可以发射子弹,子弹连发(最多5)
 * 4.当我的坦克击中敌人坦克时,敌人就消失(爆炸的效果)
 * 5.我被击中后,显示爆炸效果
 * 6.防止敌人坦克重叠运动(*)
 */


import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;


public class MyTankGame extends JFrame{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyTankGame mtg=new MyTankGame();

	}
	public MyTankGame()
	{
		MyPanel mp=new MyPanel();
		
		//开始线程
		Thread t1=new Thread(mp);
		t1.start();
		//初始化JFrame
		this.setSize(900, 700);
		this.setLocation(200, 50);
		Image img=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/TankIco.gif"));
		this.setIconImage(img);
		this.setTitle("坦克大战");
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.add(mp);
		this.addKeyListener(mp);
	}

}

//坦克大战主战场 在JPanel上进行
class MyPanel extends JPanel implements KeyListener,Runnable
{
	Image img1=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_1.gif"));
	Image img2=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_2.gif"));
	Image img3=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_3.gif"));
	
	Vector<EnemyTank> ets=new Vector<EnemyTank>();
	Vector<Bomb> bombs=new Vector<Bomb>();
	MyTank mt=null;
	EnemyTank et=null;
	public MyPanel()
	{
		mt=new MyTank(400, 585, 0);
		et=new EnemyTank(100, 15, 2);
//		et.build();
		//创建坦克
//		public void build(){
		for (int i=0;i<et.enemyTankNum;i++)
			{
		
				EnemyTank et=new EnemyTank((i+1)*50, 15, 2);
				ets.add(et);
			
			}
		//}
		et.setEts(ets);
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		g.fillRect(0, 0, 800, 600);

//		g.drawImage(img1, 50, 50, 30, 30, this);
//		g.drawImage(img2, 100, 100, 30, 30, this);
//		g.drawImage(img3, 150, 150, 30, 30, this);
		//将敌人的坦克告诉我的坦克 用于判断子弹是否命中
//		mt.setEts(ets);
		
		
		//画敌人的坦克
		for (int i=0;i<ets.size();i++)
		{
			EnemyTank et=ets.get(i);
			if(et.isLive)
			{
				this.drawTank(et.x, et.y, g, et.direct, 1);
			}
		}
		for (int i=0;i<ets.size();i++)
				{
					EnemyTank et=ets.get(i);
					if(!et.isLive)
					{
						ets.remove(et);
					}
			}
		
		//画我的坦克
		if(mt.isLive){
		this.drawTank(mt.getX(), mt.getY(), g, mt.direct, 0);
		}
		
		//画子弹(我的)
		for (int i=0;i<mt.mbs.size();i++)
		{
			Bullet b=mt.mbs.get(i);
			if (b.isLive)
			{
			g.drawOval(b.x, b.y, 1, 1);
			}
		}
		g.setColor(Color.cyan);
		//画子弹(敌人的)
		for (int j=0;j<ets.size();j++)
		{
			EnemyTank et=ets.get(j);
		for (int i=0;i<et.ebs.size();i++)
		{
			Bullet b=et.ebs.get(i);
			this.isShottedMe(b);
			if (b.isLive)
			{
			g.drawOval(b.x, b.y, 1, 1);
			}
		}
		}
		
		for (int i=0;i<mt.mbs.size();i++)
		{
			Bullet b=mt.mbs.get(i);
			this.isShottedEnemy(b);
		}
		for (int i=0;i<mt.mbs.size();i++)
		{
			Bullet b=mt.mbs.get(i);
			if(!b.isLive)
			{
				mt.mbs.remove(i);
			}
		}
		
		//画炸弹
		for(int i=0;i<bombs.size();i++)
		{
			//System.out.println("################################");
			//System.out.println("bomb"+bombs.size());
			Bomb bomb=bombs.get(i);
			if(bomb.isLive)
			{
				
				if(bomb.life>6){
					//System.out.println("################################");
					//System.out.println("bomb"+bomb.life);
				g.drawImage(img1, bomb.x-15, bomb.y-15, 30, 30, this);
				
				}
				else if(bomb.life>3){
					
					g.drawImage(img2, bomb.x-15, bomb.y-15, 30, 30, this);
					
					}
				else if(bomb.life>0){
					
					g.drawImage(img3, bomb.x-15, bomb.y-15, 30, 30, this);
					
					}
				bomb.life--;
				if(bomb.life==0)
				{
					bomb.isLive=false;
				}
			}
			if(!bomb.isLive)
			{
				bombs.remove(bomb);
			}
		}
		
		
		
		
	}
	
	//判断子弹是否击中我机
	public void isShottedMe (Bullet b)
	{
		for (int i=0;i<ets.size();i++)
		{
			EnemyTank et=ets.get(i);
			for(int j=0;j<et.ebs.size();j++)
			{
			switch (et.direct)
			{
			case 0:
			case 2:
				if(b.x>mt.x-10&&b.x<mt.x+10&&b.y>mt.y-15&&b.y<mt.y+15)
				{
					if(mt.isLive){
					Bomb bomb=new Bomb(mt.x, mt.y);
					bomb.isLive=true;
					bombs.add(bomb);
					}
					b.isLive=false;
					mt.isLive=false;
				}
				break;
			case 3:
			case 1:
				if(b.x>mt.x-15&&b.x<mt.x+15&&b.y>mt.y-10&&b.y<mt.y+10)
				{
					if(mt.isLive){
						Bomb bomb=new Bomb(mt.x, mt.y);
						bomb.isLive=true;
						bombs.add(bomb);
						}
					b.isLive=false;
					mt.isLive=false;
				}
				break;
			}
			}
		}
	}
	
	//判断子弹是否击中敌机
	public void isShottedEnemy (Bullet b)
	{
		for (int i=0;i<ets.size();i++)
		{
			EnemyTank et=ets.get(i);
			switch (et.direct)
			{
			case 0:
			case 2:
				if(b.x>et.x-10&&b.x<et.x+10&&b.y>et.y-15&&b.y<et.y+15)
				{
					if(et.isLive){
						Bomb bomb=new Bomb(et.x, et.y);
						bomb.isLive=true;
						bombs.add(bomb);
						}
					b.isLive=false;
					ets.get(i).isLive=false;
				}
				break;
			case 3:
			case 1:
				if(b.x>et.x-15&&b.x<et.x+15&&b.y>et.y-10&&b.y<et.y+10)
				{
					if(et.isLive){
						Bomb bomb=new Bomb(et.x, et.y);
						bomb.isLive=true;
						bombs.add(bomb);
						}
					b.isLive=false;
					ets.get(i).isLive=false;
				}
				break;
			}
		}
	}
	
	//画坦克
	public void drawTank(int x,int y,Graphics g,int direct,int type)
	{
		if(type==0)
		{
			g.setColor(Color.yellow);
		}else if (type==1)
		{
			g.setColor(Color.cyan);
		}
		switch(direct)
		{
			case 0:
				g.fill3DRect(x-10, y-15, 5, 30, false);
				g.fill3DRect(x+5, y-15, 5, 30, false);
				g.fill3DRect(x-5, y-10, 10, 20, false);
				g.drawOval(x-6, y-5, 10, 10);
				g.drawLine(x, y, x, y-15);
				break;
			case 1:
				g.fill3DRect(x-15, y-10, 30, 5, false);
				g.fill3DRect(x-15, y+5, 30, 5, false);
				g.fill3DRect(x-10, y-5, 20, 10, false);
				g.drawOval(x-5, y-6, 10, 10);
				g.drawLine(x, y, x+15, y);
				break;
			case 2:
				g.fill3DRect(x-10, y-15, 5, 30, false);
				g.fill3DRect(x+5, y-15, 5, 30, false);
				g.fill3DRect(x-5, y-10, 10, 20, false);
				g.drawOval(x-6, y-5, 10, 10);
				g.drawLine(x, y, x, y+15);
				break;
			case 3:
				g.fill3DRect(x-15, y-10, 30, 5, false);
				g.fill3DRect(x-15, y+5, 30, 5, false);
				g.fill3DRect(x-10, y-5, 20, 10, false);
				g.drawOval(x-5, y-6, 10, 10);
				g.drawLine(x, y, x-15, y);
				break;
		}
	}
	//public void drawMyTank(int x,int y,Graphcs g,int )

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_W)
		{
			mt.setDirect(0);
			mt.moveUp();
		}
		else if(e.getKeyCode()==KeyEvent.VK_D)
		{
			mt.setDirect(1);
			mt.moveRight();
		}
		else if(e.getKeyCode()==KeyEvent.VK_S)
		{
			mt.setDirect(2);
			mt.moveDown();
		}
		else if(e.getKeyCode()==KeyEvent.VK_A)
		{
			mt.setDirect(3);
			mt.moveLeft();
		}
		if (e.getKeyCode()==KeyEvent.VK_J)
		{
			if(mt.mbs.size()<5){
			mt.shotEnemy();
			}
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true)
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();
		}
	}
}
//坦克主要属性:坐标、方向
class Tank
{
	boolean isLive=true;
	int x;
	int y;
	//方向 0:上 1:右 2:下 3:左
	int direct;
	int speed=1;
	
	public Tank(int x,int y,int direct)
	{
		this.x=x;
		this.y=y;
		this.direct=direct;
	}
	
	//移动
	public void moveUp()
	{
		y-=speed;
	}
	public void moveRight()
	{
		x+=speed;
	}
	public void moveDown()
	{
		y+=speed;
	}
	public void moveLeft()
	{
		x-=speed;
	}
	
	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 getDirect() {
		return direct;
	}
	public void setDirect(int direct) {
		this.direct = direct;
	}
}

//我的坦克
class MyTank extends Tank
{

//	Vector<EnemyTank> ets=new Vector<EnemyTank>();
//	public Vector<EnemyTank> getEts() {
//		return ets;
//	}
//	public void setEts(Vector<EnemyTank> ets) {
//		this.ets = ets;
//	}
	//子弹库
	Vector<Bullet> mbs=new Vector<Bullet>();
	
	public MyTank(int x, int y, int direct) {
		super(x, y, direct);
		// TODO Auto-generated constructor stub
	}
	//射击
	public void shotEnemy()
	{
		Bullet b=new Bullet(x, y, direct);
		Thread t=new Thread(b);
		t.start();
		mbs.add(b);
		b.setMbs(mbs);
	}

}

//敌人的坦克
class EnemyTank extends Tank implements Runnable
{
	boolean b=false;
	int enemyTankNum=10;
	//子弹库
	Vector<Bullet> ebs=new Vector<Bullet>();
	Vector<EnemyTank> ets=new Vector<EnemyTank>();
	
	
	public Vector<EnemyTank> getEts() {
		return ets;
	}

	public void setEts(Vector<EnemyTank> ets) {
		this.ets = ets;
	}

	public EnemyTank(int x, int y, int direct) {
		super(x, y, direct);
		Thread t=new Thread(this);
		t.start();
		// TODO Auto-generated constructor stub
	}
	
//	//创建坦克
//	public void build(){
//	for (int i=0;i<enemyTankNum;i++)
//		{
//	
//			EnemyTank et=new EnemyTank((i+1)*50, 15, 2);
//			ets.add(et);
//		
//		}
//	}
	//射击
	public void shotMe(int x,int y,int direct)
	{
		Bullet b=new Bullet(x, y, direct);
		Thread t=new Thread(b);
		t.start();
		ebs.add(b);
		b.setEbs(ebs);
	}
	
	//判断敌机是否碰撞 碰撞则返回true 否则返回false
	public boolean enemyTankTouched()
	{
		//System.out.println("*******************************");
		switch(this.direct)
		{
		case 0:
			//System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et0=ets.get(i);
				System.out.println("ets.size="+ets.size());
				if(this!=et0)
				{
					//System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
//					switch(et0.direct)
//					{
//					case 0:
//					case 2:
					if(et0.direct==0||et0.direct==2)
					{
						//boolean b1=(this.x-10>et0.x-10)&&(this.x-10<et0.x+10);
						//System.out.println("1111111111111111111111");
						if((this.x-10>et0.x-10)&&(this.x-10<et0.x+10))
								//&&(this.y+15>et0.y-15)&&(this.y+15<et0.y+15))
						{
							return true;
						}
						if((this.x+10>et0.x-10)&&(this.x+10<et0.x+10)
								&&(this.y+15>et0.y-15)&&(this.y+15<et0.y+15))
						{
							return true;
						}
					}
//						break;
//					case 1:
//					case 3:
					if(et0.direct==1||et0.direct==3)
					{
						//System.out.println("1111111111111111111111");
						if((this.x-10>et0.x-15)&&(this.x-10<et0.x+15)
								&&(this.y+15>et0.y-10)&&(this.y+15<et0.y+10))
						{
							return true;
						}
						if((this.x+10>et0.x-15)&&(this.x+10<et0.x+15)
								&&(this.y+15>et0.y-10)&&(this.y+15<et0.y+10))
						{
							return true;
						}
						//break;
					}
					//}
					
				}
			}
			break;
		case 1:
			//System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@");
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et0=ets.get(i);
				if(this!=et0)
				{
//					switch(et0.direct)
//					{
//					case 0:
//					case 2:
					if(et0.direct==0||et0.direct==2)
					{
						//System.out.println("222222222222222222");
						if((this.x+15>et0.x-10)&&(this.x+15<et0.x+10)
								&&(this.y+10>et0.y-15)&&(this.y+10<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了11111");
						}
						if((this.x+15>et0.x-10)&&(this.x+15<et0.x+10)
								&&(this.y-10>et0.y-15)&&(this.y-10<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了11111");
						}
					}
//						break;
//					case 1:
//					case 3:
					if(et0.direct==1||et0.direct==3)
					{
						//System.out.println("222222222222222222222");
						if((this.x+15>et0.x-15)&&(this.x+15<et0.x+15)
								&&(this.y+10>et0.y-10)&&(this.y+10<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了11111");
						}
						if((this.x+15>et0.x-15)&&(this.x+15<et0.x+15)
								&&(this.y-10>et0.y-10)&&(this.y-10<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了11111");
						}
					}
						//break;
					//}
					
				}
			}
			break;
		case 2:
			//System.out.println("##########################");
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et0=ets.get(i);
				if(this!=et0)
				{
//					switch(et0.direct)
//					{
//					case 0:
//					case 2:
					if(et0.direct==0||et0.direct==2)
					{
						if((this.x-10>et0.x-10)&&(this.x-10<et0.x+10)
								&&(this.y-15>et0.y-15)&&(this.y-15<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了222222");
						}
						if((this.x+10>et0.x-10)&&(this.x+10<et0.x+10)
								&&(this.y-15>et0.y-15)&&(this.y-15<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了222222");
						}
					}
//						break;
//					case 1:
//					case 3:
					if(et0.direct==1||et0.direct==3)
					{
						if((this.x-10>et0.x-15)&&(this.x-10<et0.x+15)
								&&(this.y-15>et0.y-10)&&(this.y-15<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了222222");
						}
						if((this.x+10>et0.x-15)&&(this.x+10<et0.x+15)
								&&(this.y-15>et0.y-10)&&(this.y-15<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了222222");
						}
						
						//break;
					}
					
				}
			}
			break;
		case 3:
			//System.out.println("$$$$$$$$$$$$${1}quot;);
			for(int i=0;i<ets.size();i++)
			{
				EnemyTank et0=ets.get(i);
				if(this!=et0)
				{
//					switch(et0.direct)
//					{
//					case 0:
//					case 2:
					if(et0.direct==0||et0.direct==2)
					{
						if((this.x-15>et0.x-10)&&(this.x-15<et0.x+10)
								&&(this.y+10>et0.y-15)&&(this.y+10<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了33333");
						}
						if((this.x-15>et0.x-10)&&(this.x-15<et0.x+10)
								&&(this.y-10>et0.y-15)&&(this.y-10<et0.y+15))
						{
							return true;
							//System.out.println("碰撞了3333333332");
						}
					}
//						break;
//					case 1:
//					case 3:
					if(et0.direct==1||et0.direct==3)
					{
						if((this.x-15>et0.x-15)&&(this.x-15<et0.x+15)
								&&(this.y+10>et0.y-10)&&(this.y+10<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了23333322");
						}
						if((this.x-15>et0.x-15)&&(this.x-15<et0.x+15)
								&&(this.y-10>et0.y-10)&&(this.y-10<et0.y+10))
						{
							return true;
							//System.out.println("碰撞了3333333332");
						}
						//break;
					}
					
				}
			}
			break;
		}
		return b;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		int times=0;
		while(isLive)
		{
			//System.out.println(this.enemyTankTouched());
//			if(!this.enemyTankTouched())
//			{
//			for(int i=0;i<ets.size();i++)
//			{
				//EnemyTank et=ets.get(i);
					switch (this.direct)
					{
					case 0:
						//System.out.println("=======00000000=========");
//						System.out.println("index="+i);
						for (int j=0;j<30;j++)
						{
							try {
								Thread.sleep(50);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if(this.y>15 && !this.enemyTankTouched()){
							
									this.moveUp();
								}
						}
						break;
					case 1:
						//System.out.println("=======111111111=========");
						for (int j=0;j<30;j++)
						{
							try {
								Thread.sleep(50);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if(this.x<785 && !this.enemyTankTouched()){
									
									this.moveRight();
									}
						}
						
						break;
					case 2:
						//System.out.println("=======22222222222222=========");
						for (int j=0;j<30;j++)
						{
							try {
								Thread.sleep(50);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if(this.y<585 && !this.enemyTankTouched()){
									
									this.moveDown();
									}
						}
						
						break;
					case 3:
						//System.out.println("=======3333333=========");
						for (int j=0;j<30;j++)
						{
							try {
								Thread.sleep(50);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if(this.x>15 && !this.enemyTankTouched()){
									
									this.moveLeft();
									}
								
								}
						break;
						}
		
					if(times%2==0){
					if (this.ebs.size()<3){
					this.shotMe(this.x, this.y, this.direct);
						}
					}else
					{
					this.direct=(int) (Math.random()*4);
					}
					times++;
						
				//}//for()
//			for(int i=0;i<ets.size();i++)
//			{	
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
			//}
			}//while()
			
		}//run()
}

//子弹类
class Bullet implements Runnable
{
	Vector<Bullet> ebs=new Vector<Bullet>();
	Vector<Bullet> mbs=new Vector<Bullet>();
	public Vector<Bullet> getEbs() {
		return ebs;
	}
	public void setEbs(Vector<Bullet> ebs) {
		this.ebs = ebs;
	}
	public Vector<Bullet> getMbs() {
		return mbs;
	}
	public void setMbs(Vector<Bullet> mbs) {
		this.mbs = mbs;
	}
	int x;
	int y;
	int direct;
	int speed=2;
	boolean isLive=true;
	public Bullet(int x,int y,int direct)
	{
		this.x=x;
		this.y=y;
		this.direct=direct;
		switch (direct)
		{
		case 0:
			this.y=y-15;
			break;
		case 1:
			this.x=x+15;
			break;
		case 2:
			this.y=y+15;
			break;
		case 3:
			this.x=x-15;
			break;
		}
		for(int i=0;i<ebs.size();i++)
		{
			Bullet b=ebs.get(i);
			if (!b.isLive)
			{
				ebs.remove(b);
			}
		}
		for(int i=0;i<mbs.size();i++)
		{
			Bullet b=mbs.get(i);
			if (!b.isLive)
			{
				mbs.remove(b);
			}
		}
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (isLive)
		{
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(x>0&&x<800&&y>0&&y<600){
				switch (direct)
				{
				case 0:
					y-=speed;
					break;
				case 1:
					x+=speed;
					break;
				case 2:
					y+=speed;
					break;
				case 3:
					x-=speed;
					break;
				}
			}else
			{
				this.isLive=false;
				ebs.remove(this);
			}
		}
			
			
		
	}
}

//爆炸效果
class Bomb
{
	int life=9;
	int x;
	int y;
	boolean isLive=false;
	public Bomb(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
}

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值