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

📄 missile.java

📁 java 写的坦克大战,,里面涉及了java几乎所有的基础知识..
💻 JAVA
字号:
package com.wuxiaohong.tank;
import java.awt.*;
import java.util.List;



public class Missile {

	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	private static final int XSPEED = 10;
	private static final int YSPEED = 10;
	private int x,y;
	Tank.direction dir;
	TankClient tc;
	private boolean good;
	
	public Missile(int x, int y, Tank.direction dir) {
		this.x = x;
		this.y = y;
		this.dir = dir;
	}
	
	public Missile(int x, int y, boolean good,Tank.direction dir,TankClient tc) {
		this(x,y,dir);
		this.tc = tc;
		this.good = good;
	}
	
	
	private boolean live = true;
	
	
	public void draw(Graphics g)
	{
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		move();
	}

	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 += XSPEED;
			break;
		case LD:
			x -= XSPEED;
			y += YSPEED;
			break;
		case STOP:
			break;
		}
		
		if(x<0||y<0||x>TankClient.GAME_WITH||y>TankClient.GAME_HIGHT)
		{
			
			this.tc.missiles.remove(this);
		}
		
	}
	
	public Rectangle Rect()
	{
		return new Rectangle(x,y,WIDTH,HEIGHT);
	}

	
	public boolean isHit(Tank t)
	{
		if(this.Rect().intersects(t.Rect())&&t.live&&t.isGood()!=this.good)
		{
			if(t.isGood())
			{
				t.setLife(t.getLife()-30);
				if(t.getLife()<=0){
					t.setLive(false);
				} 
				
			}else t.setLive(false);
			
				Explode e = new Explode(x, y, tc);
				tc.explode.add(e);
				
				this.live =false;
				return true;
			
		}
		return false;
	}

	public boolean isLive() {
		return live;
	}
	
	public boolean isHits(List<Tank> t)
	{
		for(int i = 0;i<t.size();i++)
		{
			Tank tank = t.get(i);
			if(isHit(tank)) {
				tank.setLive(false);
				return true;
			}
			
		}
		return false;
	}
	
	public boolean hitWall(Wall w)
	{
		if(this.Rect().intersects(w.getRect()))
		{
			this.tc.missiles.remove(this);
			return true;
		}
		return false;
	}
	
	
	
}

⌨️ 快捷键说明

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