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

📄 missile.java

📁 简单的单机游戏
💻 JAVA
字号:
package game;

import java.awt.*;
import java.util.List;

/**
 *根据主战坦克的方向和位置 
 *打出子弹
 */
public class Missile {
	int x,y;
	People.Direction dir;
	public static final int XSPEED=10;
	public static final int YSPEED=10;
	
	private boolean alive=true;
	private Client client=null;
	private boolean good;//为子弹加入好坏
	
	public static final int WIDTH=10;
	public static final int HEIGHT=10;
	
	public Missile(int x, int y,People.Direction dir) {
		this.x = x;
		this.y = y;
		this.dir=dir;
	}
	
	public Missile(int x, int y,boolean good,People.Direction dir,Client client) {
		this(x,y,dir);
		this.good=good;
		this.client=client;
	}
	
	public void draw(Graphics g){
		if(!alive){
			client.missiles.remove(this);
		}
		Color c=g.getColor();
		if(good)		g.setColor(Color.RED);		//“好”子弹是红色
		else			g.setColor(Color.BLACK);	//“坏”子弹是黑色
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		move();
	}
	
	void move(){
		switch(dir){
		case L:
			x-=XSPEED;
			break;
		
		case U:
			y-=YSPEED;
			break;
		
		case R:
			x+=XSPEED;
			break;
		
		case D:
			y+=XSPEED;
			break;
		
		case STOP:
			break;
		
		}
		//判断移动后子弹是否出界
		if(x<0||y<0||x>client.BG_WIDTH||y>client.BG_HEIGHT){
			alive=false;
			
		}
	}

	public boolean isAlive() {
		return alive;
	}

	public Rectangle getRect(){				//获得子弹的位置
		return new Rectangle(x,y,WIDTH,HEIGHT);
	}
	
	public boolean hitWay(Way way){			//判断子弹是否撞墙
		if(this.alive&&this.getRect().intersects(way.getRect())){
			this.alive=false;
			return true;
		}
		return false;
	}
	
	public boolean hitPeople(People t){
		if(this.alive&&this.getRect().intersects(t.getRect())&&t.isAlive()&&this.good!=t.isGood()){
			//子弹还活着						//判断是否两矩形相交
				//好子弹不打好坦克
		
			if(t.isGood()){
				t.setLife(t.getLife()-20);
				if(t.getLife()<=0){
					t.setAlive(false);	//击中后坦克消失			
				}
			}
			else{
				t.setAlive(false);
			}
			
			this.alive=false;	//击中后子弹消失
			Explode e=new Explode(x,y,client);
			client.explodes.add(e);
			return true;
		}
		return false;
	}
	
	public boolean hitPeoples(List<People> tanks){
		for(int i=0;i<tanks.size();i++){
			if(hitPeople(tanks.get(i))){
				return true;
			}
		}
		return false;
			
	}
}

⌨️ 快捷键说明

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