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

📄 tank.java

📁 坦克大战游戏
💻 JAVA
字号:
	import java.awt.Color;
	import java.awt.Graphics;
import java.awt.Rectangle;
	import java.awt.event.KeyEvent;
	import java.util.List;
import java.util.ArrayList;
	
	
	public class Tank {
		public static final int XSPEED=2;
		public static final int YSPEED=2;
		
		
		private int x,y;
		private boolean bL=false,bU=false,bR=false,bD=false;
		
		enum Direction {L,LU,U,RU,R,RD,D,LD,STOP};
		
		Direction dir=Direction.STOP;
		Direction ptdir=Direction.D;
		
		private TankWar tw=null;
		private boolean good=true;
		private boolean live=true;
		public Tank(int x,int y,boolean good,TankWar tw)
		{
			this(x,y,good);
			this.tw=tw;
		}
		public Tank(int x, int y,boolean good) {
			this.x = x;
			this.y = y;
			this.good=good;
		}
		public void draw(Graphics g) 
		{
			if(!live==true) return;
			
			Color c=g.getColor();
			if(good ==true)
				g.setColor(Color.red);
			else 
				g.setColor(Color.blue);
			g.fillOval(x, y, 30,30);
			g.setColor(c);	
			switch(ptdir) {
			case L:
				g.drawLine(x + 15, y + 15, x, y + 15);
				break;
			case LU:
				g.drawLine(x + 15, y + 15, x, y);
				break;
			case U:
				g.drawLine(x + 15, y + 15, x + 15, y);
				break;
			case RU:
				g.drawLine(x + 15, y + 15, x + 30, y);
				break;
			case R:
				g.drawLine(x + 15, y + 15, x + 30, y + 15);
				break;
			case RD:
				g.drawLine(x + 15, y + 15, x + 30, y + 30);
				break;
			case D:
				g.drawLine(x + 15, y + 15, x + 15, y + 30);
				break;
			case LD:
				g.drawLine(x + 15, y + 15, x, y + 30);
				break;
			}
	
			Move();
		}
		public void keyPressed(KeyEvent e) {
			
			int k=e.getKeyCode();
			switch(k){
		
			case KeyEvent.VK_LEFT:
				bL=true;
				break;
			case KeyEvent.VK_DOWN:
				bD=true;
				break;
			case KeyEvent.VK_RIGHT:
				bR=true;
				break;
			case KeyEvent.VK_UP:
				bU=true;
				break;
			}
			LocateDirection();
		}
		public Missile  fire()
		{
			int x = this.x + 10;
			int y = this.y + 10;
			Missile m=new Missile(x,y,ptdir,tw);
			tw.Missiles.add(m);
			return m;
		}
		
		private void Move(){
			switch (dir) {
			case L:
				x-=XSPEED;
				break;
			case LU:
				x-=XSPEED;
				y-=YSPEED;
				break;
			case U:
				y-=YSPEED;
				break;
			case RU:
				x+=XSPEED;
				y-=YSPEED;
				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(this.dir!= Direction.STOP)
				this.ptdir=this.dir;
			if(x < 0) x = 0;
			if(y < 30) y = 30;
			if(x + 30 > 800) x = 800- 30;
			if(y + 30> 600) y = 600 - 30;
		}
		
		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.U;
			else if(!bL && bU && bR && !bD) dir = Direction.RU;
			else if(!bL && !bU && bR && !bD) dir = Direction.R;
			else if(!bL && !bU && bR && bD) dir = Direction.RD;
			else if(!bL && !bU && !bR && bD) dir = Direction.D;
			else if(bL && !bU && !bR && bD) dir = Direction.LD;
			else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
		}
		public void keyReleased(KeyEvent e) {
			
			int k=e.getKeyCode();
			switch(k){
			case KeyEvent.VK_CONTROL:
				fire();
				break;
			case KeyEvent.VK_LEFT:
				bL=false;
				break;
			case KeyEvent.VK_DOWN:
				bD=false;
				break;
			case KeyEvent.VK_RIGHT:
				bR=false;
				break;
			case KeyEvent.VK_UP:
				bU=false;
				break;
			}
			LocateDirection();
		}
		
		public Rectangle getRect()
		{
			return new Rectangle(x,y,30,30);
			
		}
		public boolean isLive() {
			return live;
		}
		public void setLive(boolean live) {
			this.live = live;
		}
		
	}

⌨️ 快捷键说明

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