java编写坦克大战

微信公众号:Java成长录

感兴趣可以关注下哦,Java知识点,学习路线规划,Java相关电子书,一起学习呀!。

简介:纯java面向对象编程,用到多线程,GUI,异常处理

1.我方坦克粉色,敌方坦克黄色
2.wsad 上下左右运动,j发射子弹。
3.子弹匀速发射,控制子弹数量一次最多不能超过10个,子弹发射有间隔,追求真实感
4.敌方坦克与我方坦克速度基本一致,并且敌方坦克随机发射子弹,随机运动
5.开始关卡界面
6.点击左上角游戏按钮选择重新开始,新游戏按钮

java编写坦克大战_第1张图片

java编写坦克大战_第2张图片

没有全部完成,只写了第一关新游戏和重新开始,希望感兴趣的小伙伴能继续写下去!

package tanke;//坦克类(包括子弹)

import java.util.Vector;

import org.omg.CORBA.INTERNAL;
import org.omg.CORBA.PUBLIC_MEMBER;

public class TK {
	int x=0,y=0,fngxiang=0,sudu=4,leixing=0;
	Boolean shengming=true;
	Vector dftk = new Vector();
	public int getFngxaing() {
		return fngxiang;
	}
	public void setFngxaing(int fngxiang) {
		this.fngxiang = fngxiang;
	}
	public int getSudu() {
		return sudu;
	}
	public void setSudu(int sudu) {
		this.sudu = sudu;
	}
	public int getLeixing() {
		return leixing;
	}
	public void setLeixing(int leixing) {
		this.leixing = leixing;
	}
	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 TK(int x,int y) {
		this.x = x;
		this.y = y;
	}
}
class wotk extends TK{
	Vector zdVector = new Vector(); 
	
	zidan zd = null;
	
	public wotk(int x,int y) {
		super(x, y);
	}
	public void xs() {
		if (y>0) {
			y-=sudu;
		}	
	}
	public void xx() {
		if (y<545) {
			y+=sudu;
		}

	}
	public void xz() {
		if (x>0) {
			x-=sudu;
		}

	}
	public void xy() {
		if (x<560) {
			x+=sudu;
		}

	}
	public void fszd() {

		switch (this.fngxiang) {
		case 0:
			zd = new zidan(x+20,y-10,0);
			zdVector.add(zd);
			break;
		case 1:
			zd = new zidan(x+20,y+50,1);
			zdVector.add(zd);
			break;
		case 2:
			zd = new zidan(x-10,y+20,2);
			zdVector.add(zd);
			break;
		case 3:
			zd = new zidan(x+50,y+20,3);
			zdVector.add(zd);
			break;
		default:
			break;
		}
		Thread zdThread = new Thread(zd);
		zdThread.start();
	}
}

class drtk extends TK implements Runnable{
	int sudu = 3;
	int time = 0;
	int fangxiang;
	Vector dfzdvVector = new Vector();
	public void dtjhl(Vector dftk) {
		this.dftk = dftk;
	}
	public drtk(int x,int y) {
		super(x, y);
	}
	
	public boolean hxpz() {	
		boolean b=false;
		switch (this.fangxiang) {
		case 0:
		for (int i = 0; i < dftk.size(); i++) {
			drtk dt = dftk.get(i);
			if (dt!=this) {
				if (dt.fangxiang==0||dt.fangxiang==1||dt.fangxiang==2||dt.fangxiang==3){
					if (dt.x<=this.x && dt.x+40>=this.x && dt.y<=this.y && dt.y+40>=this.y) {
						return true;
					}
					if (this.x+40>=dt.x&&this.x+40<=dt.x+40 && this.y>=dt.y && this.y<=dt.y+40) {
						return true;
					}
					/*if (this.x<=dt.x&&this.x+40>=dt.x&& this.y<=dt.y+40 && this.y+40>=dt.y+40) {
					return true;
				}
				if (dt.x+40>=this.x&&dt.x+40<=this.x+40&&dt.y+40>this.y&&dt.y+40<=this.y+40) {
					return true;
				}*/
					
			}	
			}
		}
			break;
		case 1:
			for (int i = 0; i < dftk.size(); i++) {
				drtk dt = dftk.get(i);
				if (dt!=this) {
					if (dt.fangxiang==0||dt.fangxiang==1||dt.fangxiang==2||dt.fangxiang==3){
						/*if (dt.x>=this.x && dt.x<=this.x+40 && dt.y>=this.y && dt.y<=this.y+40) {
							return true;
						}
						if (this.x<=dt.x+40&&this.x+40>=dt.x+40 && this.y<=dt.y && this.y+40>=dt.y) {
							return true;
						}*/
						if (this.x>=dt.x&&this.x<=dt.x+40&& this.y+40>=dt.y && this.y+40<=dt.y+40) {
						return true;
					}
					if (dt.x<=this.x+40&&dt.x+40>=this.x+40&&dt.y<=this.y+40&&dt.y+40>=this.y+40) {
						return true;
					}
				}	
				}
			}
				break;
		case 2:
			for (int i = 0; i < dftk.size(); i++) {
				drtk dt = dftk.get(i);
				if (dt!=this) {
					if (dt.fangxiang==0||dt.fangxiang==1||dt.fangxiang==2||dt.fangxiang==3){
						if (dt.x<=this.x && dt.x+40>=this.x && dt.y<=this.y && dt.y+40>=this.y) {
							return true;
						}
						/*if (this.x<=dt.x+40&&this.x+40>=dt.x+40 && this.y<=dt.y && this.y+40>=dt.y) {
							return true;
						}*/
						if (this.x>=dt.x&&this.x<=dt.x+40&& this.y+40>=dt.y && this.y+40<=dt.y+40) {
						return true;
					}
					/*if (dt.x+40>=this.x&&dt.x+40<=this.x+40&&dt.y+40>this.y&&dt.y+40<=this.y+40) {
						return true;
					}*/
				}	
				}
			}
				break;
		case 3:
			for (int i = 0; i < dftk.size(); i++) {
				drtk dt = dftk.get(i);
				if (dt!=this) {
					if (dt.fangxiang==0||dt.fangxiang==1||dt.fangxiang==2||dt.fangxiang==3){
						/*if (dt.x>=this.x && dt.x<=this.x+40 && dt.y>=this.y && dt.y<=this.y+40) {
							return true;
						}*/
						if (this.x+40>=dt.x&&this.x+40<=dt.x+40 && this.y>=dt.y && this.y<=dt.y+40) {
							return true;
						}
						/*if (this.x<=dt.x&&this.x+40>=dt.x&& this.y<=dt.y+40 && this.y+40>=dt.y+40) {
							return true;
						}*/
						if (dt.x<=this.x+40&&dt.x+40>=this.x+40&&dt.y<=this.y+40&&dt.y+40>=this.y+40) {
							return true;
						}
				}	
				}
			}
				break;

		default:
			break;
		}
		
		return b;
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(50);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			switch (this.fangxiang) {
			case 0:

				for (int i = 0; i < 30; i++) {
					if (y>0&&!hxpz()) {
						y-=sudu;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				break;
			case 1:

				for (int i = 0; i < 30; i++) {
					if (y<560&&!hxpz()) {
						y+=sudu;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}

				break;
			case 2:

				for (int i = 0; i < 30; i++) {
					if (x>0&&!hxpz()) {
						x-=sudu;
					}				
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}

				break;
			case 3:
				for (int i = 0; i < 30; i++) {
					if (x<560&&!hxpz()) {
						x+=sudu;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}

				break;
			default:
				break;
			}
			this.fangxiang=(int)(Math.random()*4);
			if (this.shengming==false) {
				break;
			}
			this.time++;
			if (time%2==0) {
				if (shengming) {
					if (dfzdvVector.size()<5) {
						zidan zd = null;
						switch (this.fangxiang) {
						case 0:
							zd = new zidan(x+20, y-5, 0);
							dfzdvVector.add(zd);
							break;
						case 1:
							zd = new zidan(x+20, y+45, 1);
							dfzdvVector.add(zd);
							break;
						case 2:
							zd = new zidan(x-5, y+20, 2);
							dfzdvVector.add(zd);
							break;
						case 3:
							zd = new zidan(x+45, y+20, 3);
							dfzdvVector.add(zd);
							break;
						default:
							break;
						}
						Thread dfzdtThread = new Thread(zd);
						dfzdtThread.start();
					}

				}
			}
		}
	}
}

class zidan implements Runnable{
	int x,y,fangxiang;
	int sudu = 4;
	boolean fanwei=true;
	public zidan(int x,int y,int fangxiang) {
		this.x = x;
		this.y = y;
		this.fangxiang = fangxiang;
	}
	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(50);
			} catch (Exception e) {
				// TODO: handle exception
			}
			switch (this.fangxiang) {
			case 0:
				y-=sudu;
				break;
			case 1:
				y+=sudu;
				break;
			case 2:
				x-=sudu;
				break;
			case 3:
				x+=sudu;
				break;
			default:
				break;
			}
			if (x<0||y<0||x>600||y>600) {
				this.fanwei=false;
				break;
			}
		}
	}
}
package tanke;//界面功能类
import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.parser.DTD;

import java.awt.event.*;
import java.util.Vector;
public class jieMian extends JFrame implements ActionListener {
	wdmb womb = null;
	gqmb wdgqmb = null;
	JMenuBar cdlBar;
	JMenu caidan;
	JMenuItem ejcdItem;
	JMenuItem ejcdItem2;
	public jieMian() {
		cdlBar = new JMenuBar();
		caidan = new JMenu("游戏");
		ejcdItem = new JMenuItem("新游戏");
		ejcdItem2 = new JMenuItem("重新开始");
		wdgqmb = new gqmb();
		caidan.add(ejcdItem);
		caidan.add(ejcdItem2);
		cdlBar.add(caidan);
		this.setJMenuBar(cdlBar);
		ejcdItem.addActionListener(this);
		ejcdItem.setActionCommand("1");
		ejcdItem2.addActionListener(this);
		ejcdItem2.setActionCommand("2");
		this.add(wdgqmb);
		Thread gq = new Thread(wdgqmb);
		gq.start();
		this.setLocation(200,200);
		this.setSize(800,800);
		this.setTitle("坦克大战");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);	
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("1")) {
		this.remove(wdgqmb);
		womb = new wdmb();
		this.add(womb);
		this.addKeyListener(womb);
		Thread mbThread = new Thread(womb);
		mbThread.start();
		this.setVisible(true);
		}
		if (e.getActionCommand().equals("2")) {
			this.remove(wdgqmb);
			this.remove(womb);
			womb = new wdmb();
			this.add(womb);
			this.addKeyListener(womb);
			Thread mbThread = new Thread(womb);
			mbThread.start();
			this.setVisible(true);
		}
		
	}
}
class gqmb extends JPanel implements Runnable{
	int time=0;
	public gqmb() {
		// TODO Auto-generated constructor stub	
	}
	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 600, 600);
		if (time%2==0) {
			g.setColor(Color.yellow);
			Font my = new Font("华文行楷",Font.BOLD,58);
			g.setFont(my);
			g.drawString("第一关", 200, 300);
		}
	}
	public void run() {
		while (true) {
			try {
				Thread.sleep(600);
			} catch (Exception e) {
				// TODO: handle exception
			}
			time++;
			this.repaint();
		}
		
	}
	
}

class wdmb extends JPanel implements KeyListener,Runnable{
	wotk wotk = null;
	int tksl=5;
	drtk dt = null;
	Vector dftk = new Vector();
	public wdmb() {
		wotk = new wotk(280, 550);
		for (int i = 0; i < tksl; i++) {
			drtk dt = new drtk(i*139, 5);
			dt.dtjhl(dftk);
			dftk.add(dt);
			Thread dtydThread = new Thread(dt);
			dtydThread.start();
			zidan zd = new zidan(dt.x+20, dt.y+45, dt.fangxiang);
			dt.dfzdvVector.add(zd);
			Thread dfzdtThread = new Thread(zd);
			dfzdtThread.start();
			
		}
	}

	public void paint(Graphics g) {
		super.paint(g);
		Font my = new Font("华文行楷", Font.BOLD, 38);
		g.setFont(my);
		g.drawString("吴大萌的小坦克,谁也打不过", 100, 650);
		g.fillRect(0,0, 600, 600);
		for (int i = 0; i dt2.x||dt.y-40>dt2.y) {
					return true;
				}
			}
		}
	    return false;
		
	}*/

	public void jbtk(zidan zd, TK tk) {
		switch (tk.fngxiang) {
		case 0:
		case 1:
			if (zd.x>tk.x && zd.y>tk.y && zd.xtk.x && zd.y>tk.y && zd.x
 
package tanke;//主函数

public class tanke {

	public static void main(String[] args) {
		jieMian jm = new jieMian();

	}

}

 

 

 

 

 

 

 

你可能感兴趣的:(Java小程序)