⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.java

📁 坦克大战的小游戏。可以按方向键控制坦克的移动方向。按CTRL键击毙敌方坦克。吃移动小血块可以加血。按A键可以发超级炮弹。
💻 JAVA
字号:
package com.sun;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TanK {
	/**
	 * 坦克移动的速度
	 */
	public static final int XSPEED=5;
	public static final int YSPEED=5;
	/**
	 * 坦克的宽度
	 */
	public static final int WIDTH=30;
	/**
	 * 坦克的高度
	 */
	public static final int HEIGHT=30;
	private int x,y;
	private int oldX,oldY;
    private boolean bl=false,br=false,bu=false,bd=false;
    private boolean good;
    private static Random r=new Random();
    private boolean live=true;
    private int life=100;
    private BloodBar bb=new BloodBar();
    private int step=r.nextInt(12)+3;
    
    enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};
    private Direction dir=Direction.STOP;
    private Direction ptDir=Direction.D;
    Tanke tc=null;
   /**
    * 构造函数
    * @param x  坦克的位置 x坐标
    * @param y  坦克的位置 y坐标
    * @param good  敌我坦克的标志
    */
	public TanK(int x, int y,boolean good) {
		this.x = x;
		this.y = y;
		this.oldX=x;
		this.oldY=y;
		this.good=good;
	}
	public TanK(int x,int y,boolean good,Tanke tc){
		this(x,y,good);
		this.tc=tc;
	}
	
	public void draw(Graphics g){
		if(!live){
			if(!good){
				tc.tanks.remove(this);
			}
			return;
		}
			
		Color c = g.getColor();
				if(good){
					bb.draw(g);
					g.setColor(Color.red);
					
				}
					
			
		
		else 
			g.setColor(Color.BLACK);
		
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(Color.blue);
		/*
		 * 画炮筒
		 */
		switch(ptDir){
		case L:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x, y+TanK.HEIGHT/2);
			break;
		case LU:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x, y);
			break;
		case U:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x+TanK.WIDTH/2, y);
			break;
		case RU:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x+TanK.WIDTH, y);
			break;
		case R:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x+TanK.WIDTH, y+TanK.HEIGHT/2);
			break;
		case RD:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x+TanK.WIDTH,y+TanK.HEIGHT);
			break;
		case D:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x+TanK.WIDTH/2, y+TanK.HEIGHT);
			break;
		case LD:
			g.drawLine(x+TanK.WIDTH/2, y+TanK.HEIGHT/2, x, y+TanK.HEIGHT);
			break;
		
		}
		g.setColor(c);
		move();
		
	}
	/**
	 * 根据按键设定坦克的移动方向
	 */
	void move(){
		this.oldX=x;
		this.oldY=y;
		switch(dir){
		case L:
			x-=XSPEED;
			break;
		case LU:
			x-=XSPEED;
			y-=YSPEED;
			break;
		case U:
			y-=YSPEED;
			break;
		case RU:
			y-=YSPEED;
			x+=XSPEED;
			break;
		case R:
			x+=XSPEED;
			break;
		case RD:
			x+=XSPEED;
			y+=YSPEED;
			break;
		case D:
			y+=YSPEED;
			break;
		case LD:
			x-=XSPEED;
			y+=YSPEED;
			break;
		case STOP:	
			break;
		}
		if(dir!=Direction.STOP){
			this.ptDir=this.dir;
		}
		if(x<0)x=0;
		if(y<30)y=30;
		if(x+TanK.WIDTH>Tanke.WIDTH)x=Tanke.WIDTH-TanK.WIDTH;
		if(y+TanK.HEIGHT>Tanke.HEIGHT)y=Tanke.HEIGHT-TanK.HEIGHT ;
		if(!good){
			Direction[] dirs=Direction.values();
			if(step==0){
				step=r.nextInt(12)+3;
				dir=dirs[r.nextInt(dirs.length)];
			}
			step--;
			if(r.nextInt(40)>38) this.fire();
			
			
		}//子弹不能太密集,敌方坦克分布的更加均匀
	}
		public void keyPressed(KeyEvent e) {
			int k=e.getKeyCode();
			switch(k){
			case KeyEvent.VK_RIGHT:
				br=true;
			     break;
			case KeyEvent.VK_LEFT:
				bl=true;
			     break; 
			case KeyEvent.VK_UP:
				bu=true;
			     break;
			case KeyEvent.VK_DOWN:
			    bd=true;
			     break;	
						
			
				
		}
			locateDirection();

		
   }
		/**
		 * 坦克打出一颗子弹
		 * @return  一颗子弹
		 */
		public Missile fire(){
			if(!live)return null;
			Missile m=new Missile(x+TanK.WIDTH/2-Missile.WIDTH/2,y+TanK.HEIGHT/2-Missile.HEIGHT/2,this.good,ptDir,this.tc);
			
			tc.missiles.add(m);
			return m;
			
		}
		/**
		 * 坦克打出具有特效的子弹
		 * @param dir 子弹射出的方向
		 * @return 一颗子弹
		 */
		public Missile fire(Direction dir){
			if(!live)return null;
			Missile m=new Missile(x+TanK.WIDTH/2-Missile.WIDTH/2,y+TanK.HEIGHT/2-Missile.HEIGHT/2,this.good,dir,this.tc);
			
			tc.missiles.add(m);
			return m;
			
		}
		public void superFire(){
			Direction dirs[]=Direction.values();
			for(int i=0;i<8;i++){
				fire(dirs[i]);
			}
		}
        public void keyReleased(KeyEvent e) {
	     int k=e.getKeyCode();
	     switch (k) {
	    case KeyEvent.VK_F2:
	    	if(!this.live){
	    		this.setLive(true);
	    		this.setLife(100);
	    	}
	    	break;
		case KeyEvent.VK_RIGHT:
			br = false;
			break;
		case KeyEvent.VK_LEFT:
			bl = false;
			break;
		case KeyEvent.VK_UP:
			bu = false;
			break;
		case KeyEvent.VK_DOWN:
			bd = false;
			break;
		case KeyEvent.VK_CONTROL:
			fire();
			break;
		case KeyEvent.VK_A:
			superFire();

		}
		locateDirection();
	}
		void locateDirection(){
			if(bl&&!bu&&!br&&!bd) dir=Direction.L;
			else if(bl&&bu&&!br&&!bd) dir=Direction.LU;
			else if(!bl&&bu&&br&&!bd) dir=Direction.RU;
			else if(!bl&&!bu&&br&&bd) dir=Direction.RD;
			else if(bl&&!bu&&!br&&bd) dir=Direction.LD;
			else if(!bl&&bu&&!br&&!bd) dir=Direction.U;
			else if(!bl&&!bu&&br&&!bd) dir=Direction.R;
			else if(!bl&&!bu&&!br&&bd) dir=Direction.D;
			else if(!bl&&!bu&&!br&&!bd) dir=Direction.STOP;
			
		}
		private void stay(){
			x=this.oldX;
			y=this.oldY;
		}
		public Rectangle getRec(){
			return new Rectangle(x,y,WIDTH,HEIGHT);
		}
		public boolean isLive() {
			return live;
		}
		public void setLive(boolean live) {
			this.live = live;
		}
		public boolean isGood() {
			return good;
		}
		public void setGood(boolean good) {
			this.good = good;
		}
		/**
		 * 撞墙检测
		 * @param w 被撞的墙
		 * @return 装上了返回true,否则返回false
		 */
		public boolean collidesWithWall(Wall w){
			if(this.live&&this.getRec().intersects(w.getRec())){
				this.stay();
				return true;
			}
			return false;
		}
		/**
		 * 坦克之间碰撞检测
		 * @param tanks 所有的坦克
		 * @return 撞上了返回true,否着返回false
		 */
		public boolean collidesWithTanks(java.util.List<TanK>tanks){
			for(int i=0;i<tanks.size();i++){
				TanK t=tanks.get(i);
				if(this!=t){
					if(this.live&&t.isLive()&&this.getRec().intersects(t.getRec())){
						this.stay();
						t.stay();
						return true;
					}
					
				}
				return false;
			}
			
			return false;
			
		}
		public int getLife() {
			return life;
		}
		public void setLife(int life) {
			this.life = life;
		}
		/**
		 * 坦克的生命值
		 * @author sun
		 *
		 */
		private class BloodBar{
			public void draw(Graphics g){
			Color c=g.getColor();
			g.setColor(Color.red);
			g.drawRect(x, y-10, WIDTH, 10);
			int w=WIDTH*life/100;
			g.fillRect(x, y-10, w, 10);
			g.setColor(c);
				
			}
		}
		/**
		 * 坦克吃小血块,增加一条生命
		 * @param b 小血块
		 * @return  吃到血块返回true,否则返回false
		 */
		public boolean eat(Blood b){
			if (this.isGood()&&this.isLive()&&b.isLive()){
				if(this.getRec().intersects(b.getRec())){
					this.life=100;
					b.setLive(false);
					return true;
				}
			}
			return false;
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -