王者荣耀展示

实现代码:

package com.sxt;
 
import javax.swing.*;
import java.awt.*;
 
public class Background extends GameObject {
 
    public Background(GameFrame gameFrame) {
		super(gameFrame);
		// TODO Auto-generated constructor stub
	}
 
	Image bg = Toolkit.getDefaultToolkit().getImage("img/Map.jpg");
 
    public void paintSelf(Graphics g){
        g.drawImage(bg,0,0,null);
 
    }
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return null;
	}
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
 
import com.sxt.beast.Beast;
 
public class Bullet extends GameObject {
 
	// 发射子弹的游戏元素
	GameObject attacker;
	// 目标
	GameObject target;
	// 攻击力
	int ad;
 
	public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd) {
		super(attacker.getX(), attacker.getY(), gameFrame);
		this.attacker = attacker;
		this.target = target;
		setAd(ad);
		setSpd(spd);
		// TODO Auto-generated constructor stub
	}
 
	public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd, String img) {
		super(attacker.getX(), attacker.getY(), gameFrame);
		this.attacker = attacker;
		this.target = target;
		setImg(img);
		setAd(ad);
		setSpd(spd);
		// TODO Auto-generated constructor stub
	}
	public Bullet() {
		super();
		// TODO Auto-generated constructor stub
	}
 
	public void move() {
		// 子弹与目标碰撞,子弹消失,目标减血
		if (recIntersectsRec(getRec(), target.getRec())) {
			if (Beast.class.isAssignableFrom(target.getClass())) {
				target.setTarget(gameFrame.player);
				target.setHasTarget(true);
			}
 
			target.setCurrentHp(target.getCurrentHp() - getAd());
			gameFrame.removeList.add(this);
		}
		double dis = getDis(getX(), getY(), target.getX(), target.getY());
		int xSpeed = (int) (getSpd() * (target.getX() - getX()) / dis);
		int ySpeed = (int) (getSpd() * (target.getY() - getY()) / dis);
		setX(getX() + xSpeed);
		setY(getY() + ySpeed);
	}
 
	@Override
	public void paintSelf(Graphics g) {
		// TODO Auto-generated method stub
		g.drawImage(getImg(), getX() - 16, getY() - 16, null);
		if (this.getImg() == null) {
			g.setColor(Color.BLACK);
			g.fillOval(getX() - 5, getY() - 5, 10, 10);
			g.drawRect(getX() - 5, getY() - 5, 10, 10);
		}
		move();
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - 5, getY() - 5, 10, 10);
	}
 
	/**
	 * @return the ad
	 */
	public int getAd() {
		return ad;
	}
 
	/**
	 * @param ad the ad to set
	 */
	public void setAd(int ad) {
		this.ad = ad;
	}
 
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
 
import javax.swing.JButton;
 
public abstract class Champion extends GameObject {
 
	// 移动
	public boolean up, down, left, right;
	// 移动图集
	static String[] imgs = new String[8];
	// 第几张图片
	int moveCount = 1;
	//技能图片
	Image abilityOne;
	Image abilityTwo;
	Image abilityThree;
	//英雄头像
	Image classical;
	//技能冷却时间
	int coolDownTimeOne;
	int coolDownTimeTwo;
	int coolDownTimeThree;
	//三个技能是否处于冷却状态
	boolean coolDownOne = true;
	boolean coolDownTwo = true;
	boolean coolDownThree = true;
	//英雄列表
	ArrayList<Champion> championList = new ArrayList<Champion>();
	
	static {
		for (int i = 1; i < 8; i++) {
			imgs[i] = "img/" + i + ".png";
		}
	}
 
	public Champion(GameFrame gameFrame, int x, int y) {
		super(gameFrame);
		setImg("img/stand.png");
		setX(x);
		setY(y);
		setSpd(75);
		setHp(24000);
		setDis(250);
		setAttackCoolDownTime(100);
		setCurrentHp(getHp());
		championList.add(new ChampionDaji(gameFrame));
		championList.add(new ChampionHouyi(gameFrame));
	}
	
	public Champion(GameFrame gameFrame) {
		// TODO Auto-generated constructor stub
		super(gameFrame);
		setImg("img/stand.png");
		setX(700);
		setY(3800);
		setSpd(75);
		setHp(24000);
		setDis(250);
		setAttackCoolDownTime(100);
		setCurrentHp(getHp());
	}
 
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_D) {
			right = true;
		}
		if (key == KeyEvent.VK_A) {
			left = true;
		}
		if (key == KeyEvent.VK_W) {
			up = true;
		}
		if (key == KeyEvent.VK_S) {
			down = true;
		}
	}
 
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_D) {
			right = false;
		}
		if (key == KeyEvent.VK_A) {
			left = false;
		}
		if (key == KeyEvent.VK_W) {
			up = false;
		}
		if (key == KeyEvent.VK_S) {
			down = false;
		}
	}
 
	public void move() {
		if (up) {
			setY(getY() - getSpd());
		}
		if (down) {
			setY(getY() + getSpd());
		}
		if (left) {
			setX(getX() - getSpd());
		}
		if (right) {
			setX(getX() + getSpd());
		}
		if (up || down || left || right) {
			setImg(imgs[moveCount]);
			moveCount++;
			if (moveCount == 8) {
				moveCount = 1;
			}
		} else {
			setImg("img/stand.png");
		}
	}
	
	/**
	 * 添加三个技能按钮
	 * */
	public void addButton() {
		JButton button1 = new JButton();
		button1.setSize(100, 100);
		button1.setLocation(1056, 513);
		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				abilityOne();
			}
		});
		JButton button2 = new JButton();
		button2.setSize(100, 100);
		button2.setLocation(1090, 370);
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				abilityTwo();
			}
		});
		JButton button3 = new JButton();
		button3.setSize(100, 100);
		button3.setLocation(1220, 300);
		button3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				abilityThree();
			}
		});
		gameFrame.add(button1);
		gameFrame.add(button2);
		gameFrame.add(button3);
	}
 
	public abstract void abilityOne();
	public abstract void abilityTwo();
	public abstract void abilityThree();
	public abstract void abilityEffect(Graphics g);
	
	@Override
	public void paintSelf(Graphics g) {
		// TODO Auto-generated method stub
		// 生命值为0
		if (getCurrentHp() <= 0) {
			setAlive(false);
			gameFrame.removeList.add(this);
 
		} else {
			// 添加生命值
			addHp(g, 30, 80, 80, 20, Color.GREEN);
			//绘制技能图片
			g.drawImage(abilityOne, getX() + 360, getY() + 180, null);
			g.drawImage(abilityTwo, getX() + 400, getY() + 40, null);
			g.drawImage(abilityThree, getX() + 520, getY() - 30, null);
			// 绘制图片
			g.drawImage(this.getImg(), getX() - 33, getY() - 50, null);
			// 改变画笔颜色
			//g.setColor(Color.GREEN);
			// 绘制中心圆点
			//g.fillOval(getX(), getY(), 10, 10);
			// 绘制矩形边框
			//g.drawRect(getX() - 23, getY() - 50, 60, 120);
			move();
			abilityEffect(g);
		}
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - 30, getY() - 60, 60, 120);
	}
 
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
 
import javax.swing.JButton;
 
import com.sxt.beast.Beast;
 
public class ChampionHouyi extends Champion {
 
	// 技能是否处于释放状态
	boolean ifAbilityThree = false;
	boolean ifAbilityTwo = false;
	// 鼠标监视器
	MouseMonitor m;
	MouseMonitorTwo m2;
	// 三技能多边形
	Polygon p;
	// 三技能三角函数
	double sin;
	double cos;
	// 三技能命中的目标
	GameObject abilityThreeTarget;
	// 三技能是否出界
	boolean ifXOutside;
	boolean ifYOutside;
	// 二技能位置
	int X_AbilityTwo;
	int Y_AbilityTwo;
 
	public ChampionHouyi(GameFrame gameFrame) {
		super(gameFrame);
		abilityOne = Toolkit.getDefaultToolkit().getImage("img/HyabilityOne.jpg");
		abilityTwo = Toolkit.getDefaultToolkit().getImage("img/HyabilityTwo.jpg");
		abilityThree = Toolkit.getDefaultToolkit().getImage("img/HyabilityThree.jpg");
		classical = Toolkit.getDefaultToolkit().getImage("img/后羿-辉光之辰.jpg");
		// 三个技能冷却时间
		coolDownTimeOne = 14000;
		coolDownTimeTwo = 10000;
		coolDownTimeThree = 28000;
		// TODO Auto-generated constructor stub
	}
 
	public ChampionHouyi(GameFrame gameFrame, int i, int j) {
		// TODO Auto-generated constructor stub
		super(gameFrame, i, j);
	}
 
	public void exit(MouseAdapter ma) {
		this.gameFrame.removeMouseListener(ma);
	}
 
	public void attack() {
		if (isAttackCoolDown()) {
			ArrayList<GameObject> targets = new ArrayList<GameObject>();// 目标列表,最多三个目标
			for (GameObject redObj : this.gameFrame.redList) {
				if (this.recIntersectsCir(redObj.getRec(), getX() - 250, getY() - 250, 500)) {
					targets.add(redObj);
					if (targets.size() == 3) {
						break;
					}
				}
			}
			for (GameObject beastObj : this.gameFrame.beast.beastList) {
				if (this.recIntersectsCir(beastObj.getRec(), getX() - 250, getY() - 250, 500)) {
					targets.add(beastObj);
					if (targets.size() == 3) {
						break;
					}
				}
			}
			for (int i = 0; i < targets.size(); i++) {
				Bullet bullet;
				if (i == 0) {
					bullet = new Bullet(gameFrame, this, targets.get(i), 400, 50, "img/bullet.gif");
				} else {
					bullet = new Bullet(gameFrame, this, targets.get(i), 200, 50, "img/bullet.gif");
				}
				gameFrame.objList.add(bullet);
			}
			new AttackCD().start();
		}
	}
 
	public void abilityTwoAttack() {
		for (GameObject redObj : this.gameFrame.objList) {
			if (redObj instanceof MinionRed || Beast.class.isAssignableFrom(redObj.getClass())) {
				if (this.recIntersectsCir(redObj.getRec(), X_AbilityTwo - 60, Y_AbilityTwo - 60, 120)) {
					redObj.setCurrentHp(redObj.getCurrentHp() - 400);
				}
				if (this.recIntersectsCir(redObj.getRec(), X_AbilityTwo - 30, Y_AbilityTwo - 30, 60)) {
					redObj.setCurrentHp(redObj.getCurrentHp() - 200);
				}
			}
		}
	}
 
	public void abilityThreeMove() {
		p.translate((int) (50 * cos), -(int) (50 * sin));
		for (GameObject redObj : gameFrame.objList) {
			// 是红色方小兵 && 发生碰撞
			if ((redObj instanceof MinionRed || Beast.class.isAssignableFrom(redObj.getClass()))
					&& p.intersects(redObj.getRec())) {
				// 击中目标后,目标减血并眩晕,多边形消失
				redObj.setCurrentHp(redObj.getCurrentHp() - 400);
				abilityThreeTarget = redObj;
				new AbilityControlCD().start();
				ifAbilityThree = false;
				new AbilityThreeCD().start();
			}
		}
		if (!ifXOutside) {
			for (int x : p.xpoints) {
				if (x < 0 || x >= 5165) {
					ifXOutside = true;
					break;
				}
			}
		}
		if (!ifYOutside) {
			for (int y : p.ypoints) {
				if (y < 0 || y >= 4085) {
					ifYOutside = true;
					break;
				}
			}
		}
	}
 
	/**
	 * 一技能 制作游戏界面攻击按钮的替身 替换攻击按钮 新的攻击按钮事件: 1. 定义目标列表 2. 添加目标,最多添加三个 3.
	 * 目标列表第一个造成400伤害,其余200伤害 4. 持续5秒,结束后替换攻击按钮️
	 */
	@Override
	public void abilityOne() {
		// TODO Auto-generated method stub
		if (coolDownOne) {
			new AbilityOneDuration().start();// 强化普攻持续时间
			new AbilityOneCD().start();
		}
	}
 
	/**
	 * 二技能 先点击按钮释放技能 再点击技能范围内任意位置 之后点击的位置会出现 两个大小不同的圆 在大圆内会受到300伤害 在小圆内受到额外200伤害
	 */
	@Override
	public void abilityTwo() {
		// TODO Auto-generated method stub
		if (coolDownTwo) {
			m2 = new MouseMonitorTwo();
			gameFrame.addMouseListener(m2);
			X_AbilityTwo = 0;
			Y_AbilityTwo = 0;
		}
 
	}
 
	@Override
	public void abilityThree() {
		// TODO Auto-generated method stub
		if (coolDownThree) {
			m = new MouseMonitor();
			p = new Polygon();
			gameFrame.addMouseListener(m);
			ifAbilityThree = true;
			ifXOutside = false;
			ifYOutside = false;
		}
	}
 
	@Override
	public void abilityEffect(Graphics g) {
		// TODO Auto-generated method stub
		if (ifAbilityTwo) {
			System.out.println("222");
			g.setColor(Color.RED);
			g.fillOval(X_AbilityTwo - 60, Y_AbilityTwo - 60, 120, 120);
			g.setColor(Color.BLACK);
			g.fillOval(X_AbilityTwo - 30, Y_AbilityTwo - 30, 60, 60);
			abilityTwoAttack();
			X_AbilityTwo = 0;
			Y_AbilityTwo = 0;
			ifAbilityTwo = false;
			new AbilityTwoCD().start();
		}
		if (ifAbilityThree) {
			System.out.println("333");
			g.setColor(Color.RED);
			g.fillPolygon(p);
			abilityThreeMove();
			// 如果出界,技能释放结束
			if (ifXOutside || ifYOutside) {
				ifAbilityThree = false;
				p = new Polygon();
				new AbilityThreeCD().start();
			}
		}
	}
 
	// 攻击冷却时间
	class AttackCD extends Thread {
		public void run() {
			// 将攻击功能设置为冷却状态
			setAttackCoolDown(false);
			// 线程休眠
			try {
				Thread.sleep(getAttackCoolDownTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将攻击功能设置为攻击状态
			setAttackCoolDown(true);
			// 线程终止
			this.stop();
		}
	}
 
	// 一技能冷却时间
	class AbilityOneCD extends Thread {
		public void run() {
			// 将攻击功能设置为冷却状态
			coolDownOne = false;
			// 休眠
			try {
				// one用来储存冷却时间
				int one = coolDownTimeOne;
				while (coolDownTimeOne > 0) {
					Thread.sleep(1000);
					System.out.println("技能一冷却时间: " + coolDownTimeOne / 1000);
					coolDownTimeOne -= 1000;
				}
				coolDownTimeOne = one;
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将攻击功能解除冷却状态
			coolDownOne = true;
			// 线程终止
			this.stop();
		}
	}
 
	// 技能一持续时间
	class AbilityOneDuration extends Thread {
		public void run() {
			// 制作攻击按钮的替身
			JButton substitute = gameFrame.attackButton;
			gameFrame.remove(gameFrame.attackButton);
 
			JButton button = new JButton();
			button.setSize(130, 132);
			button.setLocation(1150, 430);
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 获取到的事件源就是按钮本身
					attack();
				}
			});
			gameFrame.add(button);
			// 休眠
			try {
				Thread.sleep(5000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			gameFrame.remove(button);
			gameFrame.add(substitute);
			// 线程终止
			this.stop();
		}
	}
 
	// 二技能冷却时间
	class AbilityTwoCD extends Thread {
		public void run() {
			// 将二技能设置为冷却状态
			coolDownTwo = false;
			// 休眠
			try {
				int two = coolDownTimeTwo;
				while (coolDownTimeTwo > 0) {
					Thread.sleep(1000);
					System.out.println("技能二冷却时间: " + coolDownTimeTwo / 1000);
					coolDownTimeTwo -= 1000;
				}
				coolDownTimeTwo = two;
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将攻击功能解除冷却状态
			coolDownTwo = true;
			// 线程终止
			this.stop();
		}
	}
 
	// 技能三冷却时间
	class AbilityThreeCD extends Thread {
		public void run() {
			// 将技能三设置为冷却状态
			coolDownThree = false;
			// 线程休眠
			try {
				// three来表示三技能冷却时间
				int three = coolDownTimeThree;
				while (three > 0) {
					Thread.sleep(1000);
					System.out.println("三技能冷却时间: " + three / 1000);
					three -= 1000;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将技能三设置为攻击状态
			coolDownThree = true;
			// 线程终止
			this.stop();
		}
	}
 
	// 技能三控制时间
	class AbilityControlCD extends Thread {
		public void run() {
			abilityThreeTarget.beControlled = true;
			// 线程休眠
			try {
				Thread.sleep(20000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			abilityThreeTarget.beControlled = false;
			this.stop();
		}
 
	}
 
	// 鼠标监视器
	private class MouseMonitorTwo extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {// 当鼠标点击时
			System.out.println("pressed");
			int mouseX = e.getX(), mouseY = e.getY(), playerX = 700, playerY = 350;
			double dis = getDis(mouseX, mouseY, playerX, playerY);
			if (dis < 250) {
				X_AbilityTwo = e.getX() - playerX + getX();
				Y_AbilityTwo = e.getY() - playerY + getY();
			}
			ifAbilityTwo = true;
			exit(this);
		}
	}
 
	// 鼠标监视器
	private class MouseMonitor extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {// 当鼠标点击时
			int mouseX = e.getX(), mouseY = e.getY(), playerX = 700, playerY = 350;
			double dis = getDis(mouseX, mouseY, playerX, playerY);
			// 三角函数
			cos = (mouseX - playerX) / dis;
			sin = -(mouseY - playerY) / dis;
			// 坐标差
			int difX = (int) (60 * sin);
			int difY = (int) (60 * cos);
			p.addPoint(getX() - difX, getY() - difY);
			p.addPoint(getX() + difX, getY() + difY);
			p.addPoint(getX() + difX + (int) (20 * cos), getY() + difY - (int) (20 * sin));
			p.addPoint(getX() - difX + (int) (20 * cos), getY() - difY - (int) (20 * sin));
			ifAbilityThree = true;
			exit(this);
		}
	}
 
}
package com.sxt;
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
import com.sxt.beast.Beast;
 
public class GameFrame extends JFrame {
 
	// 游戏模式 0 选择英雄,1 游戏开始,2 游戏胜利,3 游戏失败
	int state = 0;
	// 窗口尺寸
	private int windowWidth = 1400;
	private int windowHeight = 700;
	// 双缓冲图片
	private Image offScreenImage = null;
	// 攻击图片
	private Image attack = Toolkit.getDefaultToolkit().getImage("img/attack.jpg");
	//游戏胜利失败图片
	private Image gameWin = Toolkit.getDefaultToolkit().getImage("img/gameWin.png");
	private Image gameLose = Toolkit.getDefaultToolkit().getImage("img/gameLose.png");
	// 游戏背景
	Background background = new Background(this);
	// 游戏玩家
	Champion player;
	Champion champion = new ChampionHouyi(this,700,3800);
	// 双方小兵
	MinionBlue mb = new MinionBlue(this);
	MinionRed mr = new MinionRed(this);
	// 防御塔
	Turret turret = new Turret(this);
	//野怪
	public Beast beast = new Beast(this);
	// 攻击按钮
	JButton attackButton;
	// 游戏元素列表
	public ArrayList<GameObject> objList = new ArrayList<GameObject>();
	ArrayList<GameObject> redList = new ArrayList<GameObject>();// 红色方
	ArrayList<GameObject> blueList = new ArrayList<GameObject>();// 蓝色方
	public ArrayList<GameObject> removeList = new ArrayList<GameObject>();// 存放将要删除的元素
 
	public void launch() {
		// 设置尺寸
		setSize(windowWidth, windowHeight);
		// 窗口居中
		setLocationRelativeTo(null);
		// 关闭事件
		setDefaultCloseOperation(3);
		// 用户不能调整窗口大小
		setResizable(false);
		// 标题
		setTitle("王者荣耀");
		// 窗口可见
		setVisible(true);
		// 添加键盘监视器
		this.addKeyListener(new GameFrame.KeyMonitor());
		// 添加游戏元素
		objList.add(background);
		objList.addAll(turret.turretList);
		objList.addAll(beast.beastList);
		blueList.add(turret.turretList.get(0));
		redList.add(turret.turretList.get(4));
 
		/**
		 * 攻击按钮
		 */
		attackButton = new JButton();
		attackButton.setSize(130, 132);
		attackButton.setLocation(1150, 430);
		attackButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 按钮事件
				player.attack(redList);
			}
		});
		this.add(attackButton);
 
		while (true) {
			if (state == 1) {
				// 添加小兵
				//mb.createMinion(this, blueList);
				//mr.createMinion(this, redList);
				// 添加防御塔
				turret.addTurret();
			}
			repaint();
			try {
				Thread.sleep(17);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
 
	public void paint(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(5984, 4452);
		}
		Graphics gImage = offScreenImage.getGraphics();
		if (state == 0) {
			for(int i = 0; i < champion.championList.size(); i++) {
				//添加图片
				Image classical = champion.championList.get(i).classical;
				gImage.drawImage(classical, i*160, 20, null);
				
				//添加按钮
				JButton championButton = new JButton();
				championButton.setSize(150, 150);
				championButton.setLocation(i*150, 0);
				int a = i;
				championButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						state = 1;
						player = champion.championList.get(a);
						objList.add(player);
						player.addButton();
					}
				});
				this.add(championButton);
			}
		} else if (state == 1) {
			for (int i = 0; i < objList.size(); i++) {
				objList.get(i).paintSelf(gImage);
			}
			// 绘制攻击图片
			gImage.drawImage(attack, player.getX() + 500, player.getY() + 100, null);
			objList.removeAll(removeList);
		} else if (state == 2) {//游戏胜利
			gImage.drawImage(gameWin, 0, 5, null);
		} else if (state == 3) {//游戏失败
			gImage.drawImage(gameLose, 0, 5, null);
		}
		if (state != 1) {
			g.drawImage(offScreenImage, 0, 0, null);
		} else {
			g.drawImage(offScreenImage, -player.getX() + 700, -player.getY() + 350, null);
		}
		/**
		 * 添加按钮后不能调用键盘事件 因为程序的焦点变成了按钮 this.requestFocus() 把焦点重新改变到游戏界面上
		 */
		this.requestFocus();
	}
 
	// main方法
	public static void main(String[] args) {
		GameFrame gameFrame = new GameFrame();
		gameFrame.launch();
	}
 
	// 键盘事件
	private class KeyMonitor extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			player.keyPressed(e);
		}
 
		@Override
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			player.keyReleased(e);
		}
	}
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.ArrayList;
 
import com.sxt.beast.Beast;
 
public abstract class GameObject {
 
	// 坐标
	private int x;
	private int y;
	// 图片
	private Image img;
	// 游戏界面
	public GameFrame gameFrame;
	// 速度
	private int spd;
	// 初始生命值
	private int hp;
	// 当前生命值
	private int currentHp;
	// 攻击目标
	private GameObject target;
	// 是否有目标
	private boolean hasTarget = false;
	// 攻击距离
	private int dis;
	// 攻击时间间隔
	private int attackCoolDownTime;
	// 攻击是否冷却
	private boolean attackCoolDown = true;
	// 是否存活
	private boolean alive = true;
	// 是否被控制
	boolean beControlled = false;
 
	public GameObject(GameFrame gameFrame) {
		this.gameFrame = gameFrame;
	}
 
	public GameObject(int x, int y, GameFrame gameFrame) {
		this.x = x;
		this.y = y;
		this.gameFrame = gameFrame;
	}
 
	public GameObject() {
		// TODO Auto-generated constructor stub
	}
 
	public void addHp(Graphics g, int difX, int difY, int width, int height, Color color) {
		// 绘制外部轮廓
		g.setColor(Color.black);
		g.drawRect(getX() - difX, getY() - difY, width, height);
		// 填充矩形
		g.setColor(color);
		g.fillRect(getX() - difX, getY() - difY, (int) (width * getCurrentHp() / getHp()), height);
	}
 
	public double getDis(int x1, int y1, int x2, int y2) {
		return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
	}
 
	// 矩形矩形碰撞检测
	public boolean recIntersectsRec(Rectangle r1, Rectangle r2) {
		return r1.intersects(r2);
	}
 
	public boolean recIntersectsCir(Rectangle rec, int x, int y, int r) {
		/** 矩形于圆相交: 圆心到至少一个矩形定点的距离小于r */
		if ((getDis(x, y, rec.x, rec.y) < r) || (getDis(x, y, rec.x, rec.y + rec.height) < r)
				|| (getDis(x, y, rec.x + rec.width, rec.y) < r)
				|| (getDis(x, y, rec.x + rec.width, rec.y + rec.height) < r)) {
			return true;
		}
		return false;
	}
 
	// 攻击方法
	public void attack(ArrayList<GameObject> gameObjList) {
		if (hasTarget) {
			// 目标离开范围后寻找新的目标
			if (!recIntersectsCir(target.getRec(), getX(), getY(), getDis())) {
				setHasTarget(false);
			}
			// 目标死亡,寻找新目标
			else if (!target.isAlive()) {
				setHasTarget(false);
			} else if (isAttackCoolDown() && isAlive()) {
				Bullet bullet = null;
				// 防御塔攻击
				if (Turret.class.isAssignableFrom(getClass())) {
					bullet = new Bullet(gameFrame, this, getTarget(), 500, 50);
				}
				// 小兵攻击
				else if (Minion.class.isAssignableFrom(getClass())) {
					bullet = new Bullet(gameFrame, this, getTarget(), 50, 30);
				}
				// 玩家攻击
				else if (this instanceof Champion) {
					bullet = new Bullet(gameFrame, this, getTarget(), 500, 50, "img/bullet.gif");
				}
				gameFrame.objList.add(bullet);
				// 线程开始
				new AttackCD().start();
			}
		} else {
			// 遍历列表
			for (GameObject obj : gameObjList) {
				// 判断攻击范围(圆形)与敌方(矩形)是否相交
				if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
					// 找到目标
					setTarget(obj);
					setHasTarget(true);
					// 跳出循环
					break;
				}
			}
			// 玩家是否在攻击范围内
			if (!hasTarget && gameObjList == gameFrame.blueList) {
				if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), getDis())) {
					// 找到目标
					setTarget(gameFrame.player);
					setHasTarget(true);
				}
			} else {
				for (GameObject obj : gameFrame.beast.beastList) {
					// 判断攻击范围(圆形)与敌方(矩形)是否相交
					if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
						// 找到目标
						setTarget(obj);
						setHasTarget(true);
						// 跳出循环
						break;
					}
				}
			}
		}
	}
 
	class AttackCD extends Thread {
		public void run() {
			// 将攻击功能设置为冷却状态
			setAttackCoolDown(false);
			// 线程休眠
			try {
				Thread.sleep(attackCoolDownTime);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将攻击功能设置为攻击状态
			setAttackCoolDown(true);
			// 线程终止
			this.stop();
		}
	}
 
	// 绘制元素
	public abstract void paintSelf(Graphics g);
 
	// 返回矩形
	public abstract Rectangle getRec();
 
	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}
 
	/**
	 * @param x the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}
 
	/**
	 * @return the y
	 */
	public int getY() {
		return y;
	}
 
	/**
	 * @param y the y to set
	 */
	public void setY(int y) {
		this.y = y;
	}
 
	/**
	 * @return the img
	 */
	public Image getImg() {
		return img;
	}
 
	/**
	 * @param img the img to set
	 */
	public void setImg(String img) {
		this.img = Toolkit.getDefaultToolkit().getImage(img);
	}
 
	/**
	 * @return the spd
	 */
	public int getSpd() {
		return spd;
	}
 
	/**
	 * @param spd the spd to set
	 */
	public void setSpd(int spd) {
		this.spd = spd;
	}
 
	/**
	 * @return the hp
	 */
	public int getHp() {
		return hp;
	}
 
	/**
	 * @param hp the hp to set
	 */
	public void setHp(int hp) {
		this.hp = hp;
	}
 
	/**
	 * @return the currentHp
	 */
	public int getCurrentHp() {
		return currentHp;
	}
 
	/**
	 * @param currentHp the currentHp to set
	 */
	public void setCurrentHp(int currentHp) {
		if (currentHp < getHp()) {
			if (Beast.class.isAssignableFrom(getClass())) {
				System.out.println("yes");
				setTarget(gameFrame.player);
				setHasTarget(true);
			}
		}
		this.currentHp = currentHp;
	}
 
	/**
	 * @return the target
	 */
	public GameObject getTarget() {
		return target;
	}
 
	/**
	 * @param target the target to set
	 */
	public void setTarget(GameObject target) {
		this.target = target;
	}
 
	/**
	 * @return the hasTarget
	 */
	public boolean isHasTarget() {
		return hasTarget;
	}
 
	/**
	 * @param hasTarget the hasTarget to set
	 */
	public void setHasTarget(boolean hasTarget) {
		this.hasTarget = hasTarget;
	}
 
	/**
	 * @return the dis
	 */
	public int getDis() {
		return dis;
	}
 
	/**
	 * @param dis the dis to set
	 */
	public void setDis(int dis) {
		this.dis = dis;
	}
 
	/**
	 * @return the attackCoolDownTime
	 */
	public int getAttackCoolDownTime() {
		return attackCoolDownTime;
	}
 
	/**
	 * @param attackCoolDownTime the attackCoolDownTime to set
	 */
	public void setAttackCoolDownTime(int attackCoolDownTime) {
		this.attackCoolDownTime = attackCoolDownTime;
	}
 
	/**
	 * @return the attackCoolDown
	 */
	public boolean isAttackCoolDown() {
		return attackCoolDown;
	}
 
	/**
	 * @param attackCoolDown the attackCoolDown to set
	 */
	public void setAttackCoolDown(boolean attackCoolDown) {
		this.attackCoolDown = attackCoolDown;
	}
 
	/**
	 * @return the alive
	 */
	public boolean isAlive() {
		return alive;
	}
 
	/**
	 * @param alive the alive to set
	 */
	public void setAlive(boolean alive) {
		this.alive = alive;
	}
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
 
public abstract class Minion extends GameObject {
 
	// 是否生成下一个小兵
	private boolean nextMinion = true;
	// 是否生成下一波小兵
	private boolean nextLine = true;
	// 生成小兵数量
	private int minionCount = 0;
	// 是否检测到目标
	private boolean ifFindTarget = false;
 
	public Minion(GameFrame gameFrame) {
		super(gameFrame);
		setHp(800);
		setCurrentHp(getHp());
		setDis(100);
		setAttackCoolDownTime(2000);
		// TODO Auto-generated constructor stub
	}
 
	/**
	 * (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)
	 */
 
	public abstract void move(ArrayList<GameObject> objList);
 
	/**
	 * @param x:       下一步的横坐标
	 * @param y:       下一步的纵坐标
	 * @param objList: 小兵列表
	 * @return 下一步位置与其他小兵是否碰撞
	 */
	public boolean hitMinion(int x, int y, ArrayList<GameObject> objList) {
		// 新的区域生成矩形
		Rectangle r = new Rectangle(x - 16, y - 16, 45, 45);
		for (GameObject obj : objList) {
			// 相同类 && 不是自身
			if (obj.getClass() == this.getClass() && obj != this) {
				if (r.intersects(obj.getRec())) {
					return true;
				}
			}
		}
		return false;
	}
 
	public void findTarget(ArrayList<GameObject> objList) {
		for (GameObject obj : objList) {
			if (recIntersectsCir(obj.getRec(), getX(), getY(), 200)) {
				setTarget(obj);
				setIfFindTarget(true);
			}
		}
		if (objList == gameFrame.blueList) {
			if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), 200)) {
				setTarget(gameFrame.player);
				setIfFindTarget(true);
			}
		}
	}
 
	public void moveToTarget() {
		double dis = getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
		int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
		int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
		if (!hitMinion(getX() + xSpeed, getY(), gameFrame.objList)) {
			setX(getX() + xSpeed);
		}
		if (!hitMinion(getX(), getY() + ySpeed, gameFrame.objList)) {
			setY(getY() + ySpeed);
		}
	}
 
	public void createMinion(GameFrame gameFrame, ArrayList<GameObject> minionList) {
		if (nextLine) {
			if (nextMinion) {
				// 蓝色方小兵
				if (minionList == this.gameFrame.blueList) {
					MinionBlue mb = new MinionBlue(gameFrame);
					gameFrame.objList.add(mb);
					minionList.add(mb);
				}
				// 红色方小兵
				else {
					MinionRed mr = new MinionRed(gameFrame);
					gameFrame.objList.add(mr);
					minionList.add(mr);
				}
				minionCount++;
				new NextMinion().start();
			}
			if (minionCount == 3) {
				minionCount = 0;
				new NextLine().start();
			}
		}
	}
 
	// 每个小兵生成时间
	class NextMinion extends Thread {
		public void run() {
			nextMinion = false;
			// 休眠1.5s
			try {
				Thread.sleep(1500);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nextMinion = true;
			// 线程终止
			this.stop();
		}
	}
 
	// 每波小兵生成时间
	class NextLine extends Thread {
		public void run() {
			nextLine = false;
			// 休眠15s
			try {
				Thread.sleep(15000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nextLine = true;
			// 线程终止
			this.stop();
		}
	}
 
	@Override
	public void paintSelf(Graphics g) {
		// TODO Auto-generated method stub
		// 生命值为0
		if (getCurrentHp() <= 0) {
			setAlive(false);
			gameFrame.removeList.add(this);
			if (this instanceof MinionBlue) {
				gameFrame.blueList.remove(this);
			} else {
				gameFrame.redList.remove(this);
			}
		} else {
			// 添加生命值
			if (this instanceof MinionBlue) {
				this.addHp(g, 17, 28, 45, 10, Color.GREEN);
			} else {
				this.addHp(g, 17, 28, 45, 10, Color.RED);
			}
			g.drawImage(getImg(), getX() - 16, getY() - 16, null);
			//绘制检测范围圆形
			//g.setColor(Color.RED);
			//g.fillOval(getX(), getY(), 10, 10);
			//g.drawRect(getX() - 16, getY() - 16, 45, 45);
			// 小兵移动
			if (!beControlled) {
				if (this instanceof MinionBlue) {
					move(gameFrame.redList);
				} else {
					move(gameFrame.blueList);
				}
			}
		}
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - 16, getY() - 16, 45, 45);
	}
 
	/**
	 * @return the ifFindTarget
	 */
	public boolean isIfFindTarget() {
		return ifFindTarget;
	}
 
	/**
	 * @param ifFindTarget the ifFindTarget to set
	 */
	public void setIfFindTarget(boolean ifFindTarget) {
		this.ifFindTarget = ifFindTarget;
	}
 
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
 
public abstract class Minion extends GameObject {
 
	// 是否生成下一个小兵
	private boolean nextMinion = true;
	// 是否生成下一波小兵
	private boolean nextLine = true;
	// 生成小兵数量
	private int minionCount = 0;
	// 是否检测到目标
	private boolean ifFindTarget = false;
 
	public Minion(GameFrame gameFrame) {
		super(gameFrame);
		setHp(800);
		setCurrentHp(getHp());
		setDis(100);
		setAttackCoolDownTime(2000);
		// TODO Auto-generated constructor stub
	}
 
	/**
	 * (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)
	 */
 
	public abstract void move(ArrayList<GameObject> objList);
 
	/**
	 * @param x:       下一步的横坐标
	 * @param y:       下一步的纵坐标
	 * @param objList: 小兵列表
	 * @return 下一步位置与其他小兵是否碰撞
	 */
	public boolean hitMinion(int x, int y, ArrayList<GameObject> objList) {
		// 新的区域生成矩形
		Rectangle r = new Rectangle(x - 16, y - 16, 45, 45);
		for (GameObject obj : objList) {
			// 相同类 && 不是自身
			if (obj.getClass() == this.getClass() && obj != this) {
				if (r.intersects(obj.getRec())) {
					return true;
				}
			}
		}
		return false;
	}
 
	public void findTarget(ArrayList<GameObject> objList) {
		for (GameObject obj : objList) {
			if (recIntersectsCir(obj.getRec(), getX(), getY(), 200)) {
				setTarget(obj);
				setIfFindTarget(true);
			}
		}
		if (objList == gameFrame.blueList) {
			if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), 200)) {
				setTarget(gameFrame.player);
				setIfFindTarget(true);
			}
		}
	}
 
	public void moveToTarget() {
		double dis = getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
		int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
		int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
		if (!hitMinion(getX() + xSpeed, getY(), gameFrame.objList)) {
			setX(getX() + xSpeed);
		}
		if (!hitMinion(getX(), getY() + ySpeed, gameFrame.objList)) {
			setY(getY() + ySpeed);
		}
	}
 
	public void createMinion(GameFrame gameFrame, ArrayList<GameObject> minionList) {
		if (nextLine) {
			if (nextMinion) {
				// 蓝色方小兵
				if (minionList == this.gameFrame.blueList) {
					MinionBlue mb = new MinionBlue(gameFrame);
					gameFrame.objList.add(mb);
					minionList.add(mb);
				}
				// 红色方小兵
				else {
					MinionRed mr = new MinionRed(gameFrame);
					gameFrame.objList.add(mr);
					minionList.add(mr);
				}
				minionCount++;
				new NextMinion().start();
			}
			if (minionCount == 3) {
				minionCount = 0;
				new NextLine().start();
			}
		}
	}
 
	// 每个小兵生成时间
	class NextMinion extends Thread {
		public void run() {
			nextMinion = false;
			// 休眠1.5s
			try {
				Thread.sleep(1500);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nextMinion = true;
			// 线程终止
			this.stop();
		}
	}
 
	// 每波小兵生成时间
	class NextLine extends Thread {
		public void run() {
			nextLine = false;
			// 休眠15s
			try {
				Thread.sleep(15000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nextLine = true;
			// 线程终止
			this.stop();
		}
	}
 
	@Override
	public void paintSelf(Graphics g) {
		// TODO Auto-generated method stub
		// 生命值为0
		if (getCurrentHp() <= 0) {
			setAlive(false);
			gameFrame.removeList.add(this);
			if (this instanceof MinionBlue) {
				gameFrame.blueList.remove(this);
			} else {
				gameFrame.redList.remove(this);
			}
		} else {
			// 添加生命值
			if (this instanceof MinionBlue) {
				this.addHp(g, 17, 28, 45, 10, Color.GREEN);
			} else {
				this.addHp(g, 17, 28, 45, 10, Color.RED);
			}
			g.drawImage(getImg(), getX() - 16, getY() - 16, null);
			//绘制检测范围圆形
			//g.setColor(Color.RED);
			//g.fillOval(getX(), getY(), 10, 10);
			//g.drawRect(getX() - 16, getY() - 16, 45, 45);
			// 小兵移动
			if (!beControlled) {
				if (this instanceof MinionBlue) {
					move(gameFrame.redList);
				} else {
					move(gameFrame.blueList);
				}
			}
		}
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - 16, getY() - 16, 45, 45);
	}
 
	/**
	 * @return the ifFindTarget
	 */
	public boolean isIfFindTarget() {
		return ifFindTarget;
	}
 
	/**
	 * @param ifFindTarget the ifFindTarget to set
	 */
	public void setIfFindTarget(boolean ifFindTarget) {
		this.ifFindTarget = ifFindTarget;
	}
 
}
package com.sxt;
 
import java.util.ArrayList;
 
public class MinionBlue extends Minion {
 
	public MinionBlue(GameFrame gameFrame) {
		super(gameFrame);
		setImg("img/blue.jpg");
		setX(1325);
		setY(3750);
		// TODO Auto-generated constructor stub
	}
 
	@Override
	public void move(ArrayList<GameObject> objList) {
		// TODO Auto-generated method stub
		if (isIfFindTarget()) {
			// 离开检测范围
			if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
				setIfFindTarget(false);
			} else {
				if (!isHasTarget()) {
					moveToTarget();
				}
				attack(objList);
			}
		} else {
			findTarget(objList);
			// 原路线移动
			if (getX() < 4425) {
				setSpd(25);
				if (!hitMinion(getX() + getSpd(), getY(), gameFrame.blueList)) {
					setX(getX() + getSpd());
 
				}
			} else if (getX() < 5100 && getX() >= 4425) {
				setSpd(20);
				if (!hitMinion(getX() + getSpd(), getY(), gameFrame.blueList)) {
					setX(getX() + getSpd());
				}
				if (!hitMinion(getX(), getY() - getSpd(), gameFrame.blueList)) {
					setY(getY() - getSpd());
				}
			} else if (getX() >= 4900) {
				setSpd(18);
				if (!hitMinion(getX(), getY() - getSpd(), gameFrame.blueList)) {
					setY(getY() - getSpd());
				}
			}
		}
	}
}
package com.sxt;
 
import java.util.ArrayList;
 
public class MinionRed extends Minion {
 
	public MinionRed(GameFrame gameFrame) {
		super(gameFrame);
		setImg("img/red.jpg");
		setX(5050);
		setY(1125);
		// TODO Auto-generated constructor stub
	}
 
	@Override
	public void move(ArrayList<GameObject> objList) {
		// TODO Auto-generated method stub
		if (isIfFindTarget()) {
			// 离开检测范围
			if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
				setIfFindTarget(false);
			} else {
				if (!isHasTarget()) {
					moveToTarget();
				}
				attack(objList);
			}
		} else {
			findTarget(objList);
			// 原路线移动
			if (getY() < 3125) {
				setSpd(18);
				if (!hitMinion(getX(), getY() + getSpd(), gameFrame.redList)) {
					setY(getY() + getSpd());
				}
			} else if (getY() < 3750 && getY() >= 3125) {
				setSpd(20);
				if (!hitMinion(getX(), getY() + getSpd(), gameFrame.redList)) {
					setY(getY() + getSpd());
				}
				if (!hitMinion(getX() - getSpd(), getY(), gameFrame.redList)) {
					setX(getX() - getSpd());
				}
			} else if (getY() >= 3750) {
				setSpd(25);
				if (!hitMinion(getX() - getSpd(), getY(), gameFrame.redList)) {
					setX(getX() - getSpd());
				}
			}
		}
	}
 
}
package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
 
public class Turret extends GameObject {
 
	ArrayList<Turret> turretList = new ArrayList<Turret>();
	public Turret turretBlueOne;
	public Turret turretBlueTwo;
	public Turret turretBlueThree;
	public Turret turretBlueBase;
	public Turret turretRedOne;
	public Turret turretRedTwo;
	public Turret turretRedThree;
	public Turret turretRedBase;
 
	public Turret(GameFrame gameFrame) {
		super(gameFrame);
		setImg("img/turret.png");
 
		// 初始化八个防御塔
		turretList.add(turretBlueOne = new TurretBlue(1860, 3790, gameFrame));
		turretList.add(turretBlueTwo = new TurretBlue(2650, 3820, gameFrame));
		turretList.add(turretBlueThree = new TurretBlue(3995, 3830, gameFrame));
		turretList.add(turretBlueBase = new TurretBlue(1130, 3650, gameFrame));
		turretList.add(turretRedOne = new TurretRed(5100, 3030, gameFrame));
		turretList.add(turretRedTwo = new TurretRed(5120, 2100, gameFrame));
		turretList.add(turretRedThree = new TurretRed(5060, 1570, gameFrame));
		turretList.add(turretRedBase = new TurretRed(4850, 1100, gameFrame));
 
		// TODO Auto-generated constructor stub
	}
 
	public Turret(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/turret.png");
		setHp(6000);
		setCurrentHp(getHp());
		setAttackCoolDownTime(1000);
		setDis(300);
	}
	
	public void addTurret() {
		/**
		 * 添加到objList里会被绘制出来,添加到redList和blueList才会被攻击 前一个防御塔爆炸后再把后一个添加到列表里
		 */
		if (!gameFrame.turret.turretBlueOne.isAlive() && gameFrame.turret.turretBlueTwo.isAlive()
				&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueTwo) == -1) {
			gameFrame.blueList.add(gameFrame.turret.turretBlueTwo);
		}
		if (!gameFrame.turret.turretBlueTwo.isAlive() && gameFrame.turret.turretBlueThree.isAlive()
				&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueThree) == -1) {
			gameFrame.blueList.add(gameFrame.turret.turretBlueThree);
		}
		if (!gameFrame.turret.turretBlueThree.isAlive() && gameFrame.turret.turretBlueBase.isAlive()
				&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueBase) == -1) {
			gameFrame.blueList.add(gameFrame.turret.turretBlueBase);
		}
		if (!gameFrame.turret.turretBlueBase.isAlive()) {
			//游戏失败
			gameFrame.state = 3;
		}
 
		if (!gameFrame.turret.turretRedOne.isAlive() && gameFrame.turret.turretRedTwo.isAlive()
				&& gameFrame.redList.indexOf(gameFrame.turret.turretRedTwo) == -1) {
			gameFrame.redList.add(gameFrame.turret.turretRedTwo);
		}
		if (!gameFrame.turret.turretRedTwo.isAlive() && gameFrame.turret.turretRedThree.isAlive()
				&& gameFrame.redList.indexOf(gameFrame.turret.turretRedThree) == -1) {
			gameFrame.redList.add(gameFrame.turret.turretRedThree);
		}
		if (!gameFrame.turret.turretRedThree.isAlive() && gameFrame.turret.turretRedBase.isAlive()
				&& gameFrame.redList.indexOf(gameFrame.turret.turretRedBase) == -1) {
			gameFrame.redList.add(gameFrame.turret.turretRedBase);
		}
		if (!gameFrame.turret.turretRedBase.isAlive()) {
			//游戏胜利
			gameFrame.state = 2;
		}
	}
	
 
	@Override
	public void paintSelf(Graphics g) {
		// 生命值为0
		if (getCurrentHp() <= 0) {
			setAlive(false);
			gameFrame.removeList.add(this);
			if (this instanceof TurretBlue) {
				gameFrame.blueList.remove(this);
			} else {
				gameFrame.redList.remove(this);
			}
		} else {
			// 添加生命值
			if (this instanceof TurretBlue) {
				this.addHp(g, 50, 130, 100, 20, Color.GREEN);
				attack(gameFrame.redList);
			} else {
				this.addHp(g, 50, 130, 100, 20, Color.RED);
				attack(gameFrame.blueList);
			}
			g.drawImage(getImg(), getX() - 50, getY() - 100, null);
			g.drawOval(getX() - 300, getY() - 300, 600, 600);
		}
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - 50, getY() - 100, 100, 180);
	}
 
}
package com.sxt;
 
public class TurretBlue extends Turret {
 
	public TurretBlue(GameFrame gameFrame) {
		super(gameFrame);
		// TODO Auto-generated constructor stub
	}
	
	public TurretBlue(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
	}
 
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class Bear extends Beast {
 
	public Bear(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/bear.jpg");
		width = 85;
		height = 112;
		setDis(65);
		// TODO Auto-generated constructor stub
	}
 
}
package com.sxt.beast;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
 
import com.sxt.*;
 
public class Beast extends GameObject {
 
	public ArrayList<Beast> beastList = new ArrayList<Beast>();
	int width;
	int height;
 
	// 初始坐标
	int initialX;
	int initialY;
 
	// 有没有仇恨值
	public boolean isAggressive = false;
 
	// 复活的元素
	Beast beast = null;
 
	public Beast(GameFrame gameFrame) {
		super(gameFrame);
		beastList.add(new RedBuff(3045, 3170, gameFrame));
		beastList.add(new Bear(2800, 2855, gameFrame));
		beastList.add(new Bird(3570, 3380, gameFrame));
		beastList.add(new Xiyi(4585, 2365, gameFrame));
		beastList.add(new BlueBuff(4025, 2295, gameFrame));
		beastList.add(new Wolf(4235, 1945, gameFrame));
	}
 
	public Beast(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setHp(1000);
		setCurrentHp(getHp());
		setSpd(10);
		setAttackCoolDownTime(2000);
		initialX = getX();
		initialY = getY();
		beast = this;
	}
 
	public void moveToTarget() {
		double dis = getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
		if (dis > 500) {
			isAggressive = false;
			setHasTarget(false);
		} else {
			int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
			int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
			setX(getX() + xSpeed);
			setY(getY() + ySpeed);
		}
	}
 
	public void moveToInitialLocation() {
		double dis = getDis(getX(), getY(), initialX, initialY);
		if (dis < getSpd()) {
			setX(initialX);
			setY(initialY);
			isAggressive = true;
		} else {
			int xSpeed = (int) (getSpd() * (initialX - getX()) / dis);
			int ySpeed = (int) (getSpd() * (initialY - getY()) / dis);
			setX(getX() + xSpeed);
			setY(getY() + ySpeed);
		}
	}
 
	/**
	判断有没有目标及野怪的仇恨值(又没有攻击性)
		true,判断是否到攻击范围
			false,向目标移动,若中途离开出生点距离大于300或与目标距离大于400,则不再有攻击性。
			true,发射子弹,线程开始
		false,没有目标,回到出生地,自动回血,再次有攻击性
	*/
	public void move() {
		if (isHasTarget() && isAggressive) {
			if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), getDis())) {
				moveToTarget();
			} else if (isAttackCoolDown() && isAlive()) {
				Bullet bullet = new Bullet(gameFrame, this, getTarget(), 500, 50, "img/bullet.gif");
				gameFrame.objList.add(bullet);
				new AttackCD().start();
			}
		} else {
			moveToInitialLocation();
			if (getCurrentHp() < getHp()) {
				setCurrentHp(getCurrentHp() + 100);
			}
		}
 
	}
 
	@Override
	public void paintSelf(Graphics g) {
		// TODO Auto-generated method stub// 生命值为0
		if (getCurrentHp() <= 0) {
			System.out.println("beast die");
			setAlive(false);
			gameFrame.removeList.add(this);
			gameFrame.beast.beastList.remove(this);
			new ReviveCD().start();
		} else {
			// 添加生命值
			addHp(g, width / 2, 80, width, 20, Color.GREEN);
			g.drawImage(getImg(), getX() - width / 2, getY() - height / 2, null);
			g.setColor(Color.RED);
			g.fillOval(getX(), getY(), 10, 10);
			g.drawOval(getX() - getDis(), getY() - getDis(), 2 * getDis(), 2 * getDis());
			move();
		}
	}
 
	@Override
	public Rectangle getRec() {
		// TODO Auto-generated method stub
		return new Rectangle(getX() - width / 2, getY() - height / 2, width, height);
	}
 
	class AttackCD extends Thread {
		public void run() {
			// 将攻击功能设置为冷却状态
			setAttackCoolDown(false);
			// 线程休眠
			try {
				Thread.sleep(getAttackCoolDownTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 将攻击功能设置为攻击状态
			setAttackCoolDown(true);
			// 线程终止
			this.stop();
		}
	}
 
	class ReviveCD extends Thread {
		public void run() {
			// 线程休眠
			try {
				Thread.sleep(5000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Beast reviveBeast;
			if (beast instanceof RedBuff) {
				reviveBeast = new RedBuff(3045, 3170, gameFrame);
			} else if (beast instanceof Bear) {
				reviveBeast = new Bear(2800, 2855, gameFrame);
			} else if (beast instanceof Bird) {
				reviveBeast = new Bird(3570, 3380, gameFrame);
			} else if (beast instanceof Xiyi) {
				reviveBeast = new Xiyi(4585, 2365, gameFrame);
			} else if (beast instanceof BlueBuff) {
				reviveBeast = new BlueBuff(4025, 2295, gameFrame);
			} else {
				reviveBeast = new Wolf(4235, 1945, gameFrame);
			}
			gameFrame.objList.add(reviveBeast);
			gameFrame.beast.beastList.add(reviveBeast);
		}
	}
 
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class Bird extends Beast {
 
	public Bird(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/红隼.jpg");
		width = 122;
		height = 98;
		setDis(125);
		// TODO Auto-generated constructor stub
	}
 
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class BlueBuff extends Beast {
 
	public BlueBuff(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/blueBuff.jpg");
		width = 142;
		height = 176;
		setDis(70);
		// TODO Auto-generated constructor stub
	}
 
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class RedBuff extends Beast {
 
	public RedBuff(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/redBuff.jpg");
		width = 103;
		height = 150;
		setDis(70);
		// TODO Auto-generated constructor stub
	}
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class Wolf extends Beast {
 
	public Wolf(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/wolf.jpg");
		width = 145;
		height = 140;
 
		setDis(65);
		// TODO Auto-generated constructor stub
	}
 
}
package com.sxt.beast;
 
import com.sxt.GameFrame;
 
public class Xiyi extends Beast {
 
	public Xiyi(int x, int y, GameFrame gameFrame) {
		super(x, y, gameFrame);
		setImg("img/蜥蜴.jpg");
		width = 111;
		height = 65;
		setDis(125);
		// TODO Auto-generated constructor stub
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/238904.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Leetcode 90 子集 II

题意理解&#xff1a; 求一个集合的子集&#xff1a;该集合有重复元素。 集合[1,2,2] []、[1]、[2]、[2]、[12]、[12]、[22]、[122] 子集&#xff1a;[]、[1]、[2]、[12]、[22]、[122]——由于元素有重复需要对子集进行去重操作。 所以此题的难点在于&#xff1a;子集去重 解题…

temu国内三大仓库在哪里

在拼多多跨境电商平台Temu的运营过程中&#xff0c;仓储物流是至关重要的一环。根据现有资料&#xff0c;Temu在国内设有三个主要仓库&#xff0c;分别位于广州、义乌和香港。这些仓库的位置选择是基于地理优势和市场需求的考虑。 先给大家推荐一款拼多多/temu运营工具——多多…

压缩软件电脑版哪个好?

压缩软件是我们存储文件、清理电脑、向他人发送文件经常用到的工具&#xff0c;下面就从头到尾操作一遍各个软件压缩步骤&#xff0c;根据需求选择好啦。可以放心的是&#xff0c;这四款软件都经过了安全测试&#xff0c;能够保证文件的安全性&#xff0c;并且能够兼容多种操作…

3_流量预测综述阅读_Cellular traffic prediction with machine learning: A survey

为了方便学习英语书写&#xff0c;总结的一些话用英语书写 ♥目录♥ 0、文献来源and摘要1、introduction2、prediction problems and datasets2.1 prediction problems2.2 dataset&#xff08;1&#xff09;Telecom Italia 意大利电信 2015&#xff08;2&#xff09;City Cell…

OOD : DMAD Diversity-Measurable Anomaly Detection

Diversity-Measurable Anomaly Detection 基于重建的异常检测模型通过抑制异常的泛化能力来迭代学习。然而&#xff0c;由于这种抑制&#xff0c;不同的正常模式的重建效果也会变得不理想。为了解决这个问题&#xff0c;本文提出了一种称为多样性可测量异常检测&#xff08;DMA…

计算机网络编程

网络编程 Java 是第一大编程语言和开发平台。它有助于企业降低成本、缩短开发周期、推动创新以及改善应用服务。如今全球有数百万开发人员运行着超过 51 亿个 Java 虚拟机&#xff0c; Java 仍是企业和开发人员的首选开发平台。 课程内容的介绍 1. 计算机网络基础 2. So…

射频功率放大器的参数有哪些

射频功率放大器是射频通信系统中重要的组件&#xff0c;用于将输入的射频信号放大到需要的功率水平。在设计和选择射频功率放大器时&#xff0c;需要考虑多种参数。下面西安安泰将详细介绍射频功率放大器的常见参数。 1、P1dB功率压缩点 当放大器的输入功率比较低时&#xff0c…

2024年AI云计算专题研究报告:智算带来的变化

今天分享的人工智能系列深度研究报告&#xff1a;《2024年AI云计算专题研究报告&#xff1a;智算带来的变化》。 &#xff08;报告出品方&#xff1a;华泰证券&#xff09; 报告共计&#xff1a;32页 Al 云计算 2024:关注智算带来的新变化 通过对海内外主要云厂商及其产业链…

Godot Engine:跨平台游戏开发的新境界 | 开源日报 No.92

godotengine/godot Stars: 62.6k License: MIT Godot Engine 是一个功能强大的跨平台游戏引擎&#xff0c;可用于创建 2D 和 3D 游戏。它提供了一套全面的常见工具&#xff0c;让用户可以专注于制作游戏而不必重复造轮子。该引擎支持将游戏一键导出到多个平台上&#xff0c;包…

crmeb后台自定义菜单并生成代码

crmeb v5 版本&#xff0c; 前后端分端 后台菜单的生成 进入后台界面之后&#xff0c;我们可以看到界面如下 找到 维护->开发配置->权限维护->添加规则按扭 我们要在设置的 菜单之下&#xff0c;添加一个 基础配置的 子菜单 提交之后&#xff0c;刷新页面就会在列…

在开发微信小程序的时候,报错navigateBack:fail cannot navigate back at firstpage

这个错误的意思是&#xff1a;在这个页面已经是第一个页面了&#xff0c;没办法再返回了 报错原因 这个错误原因其实也简单&#xff0c;就是在跳转的时候使用了wx.redirectTo()&#xff0c;使用wx.redirectTo()相当于重定向&#xff0c;不算是从上一个页面跳转过来的&#xf…

消费升级:无人零售的崛起与优势

消费升级&#xff1a;无人零售的崛起与优势 随着人们生活水平的提高&#xff0c;消费内容正在从生存型消费转向以精神体验和享乐为主的发展型消费。社会居民的消费结构不断变迁&#xff0c;明显呈现消费升级趋势。个性化和多元化消费势头正在崛起&#xff0c;特别是无人零售的自…

nextcloud如何将一个文件共享给所有人

nextcloud能够将文件/文件夹共享给某个用户或者用户组或者生成链接分享&#xff0c;但是无法直接将某个文件共享给nextcloud内部所有用户&#xff0c;并且nextcloud只有分组的概念&#xff0c;没有分组上下级的概念。 我们可以一个用户一个用户的共享&#xff0c;或者创建一个…

使用rancher rke快速安装k8s集群

概述 Rancher Kubernetes Engine&#xff08;RKE&#xff09;是一个用于部署、管理和运行Kubernetes集群的开源工具。旨在简化Kubernetes集群的部署和操作。 RKE具有以下特点和功能&#xff1a; 简化的部署过程 RKE提供了一个简单的命令行界面&#xff0c;使您可以轻松地部署…

WhatsApp全球获客怎么做?

一、导语 随着全球数字化趋势的加速&#xff0c;WhatsApp作为一种即时通讯工具&#xff0c;已经成为了连接全球用户的桥梁。 对于企业和营销人员来说&#xff0c;利用WhatsApp拓展全球业务是一种非常有效的策略&#xff0c;本文将为您揭示WhatsApp全球获客的秘密&#xff0c;…

【pytest】单元测试文件的写法

前言 可怜的宾馆&#xff0c;可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》 \;\\\;\\\; 目录 前言test_1或s_test格式非测试文件pytest.fixture()装饰器pytestselenium test_1或s_test格式 要么 test_前缀 在前&#xff0c;要么 _test后缀 在后&#xff01; …

从头到尾的数据之旅

目录 引言 链表介绍 单向链表的接口实现 结构 创建节点 头插 尾插 头删 尾删 打印 节点查找 节点前插入 节点删除 内存释放 总结 引言 在前面的学习中&#xff0c;我们深入了解了顺序表&#xff0c;通过其增删查改的操作&#xff0c;我们发现了顺序表在某些情况…

MistralAI发布全球首个MoE大模型-Mixtral 8x7B,创新超越GPT-4

引言 MistralAI&#xff0c;一家法国的初创企业&#xff0c;近期在AI界引发了轰动&#xff0c;刚刚发布了全球首个基于MoE&#xff08;Mixture of Experts&#xff0c;混合专家&#xff09;技术的大型语言模型——Mistral-8x7B-MoE。这一里程碑事件标志着AI技术的一个重要突破…

【文心一言】使用飞桨 AI Studio 快速搭建,看图识猜成语应用

目录 一、背景二、实践三、创建应用3.1、零代码开发3.2、应用名称3.2、模型训练3.3、开始训练 四、应用部署4.1、发布项目4.2、搜索应用4.3、应用部署4.4、获取令牌4.4、导入依赖4.5、配置CORS4.6、使用测试API4.7、运行4.8、测试API接口4.9、前端API接口 五、启动前端5.1、模块…

百度文库下载要用券?Kotlin爬虫几步解决

百度作为国内知名的网站&#xff0c;尤其是文库里面有各种丰富的内容&#xff0c;对我们学习生活都有很大的帮助&#xff0c;就因为其内容丰富&#xff0c;如果看见好用有意思的文章还用复制粘贴等方式就显得有点落后了&#xff0c;今天我将用我所学的爬虫知识给你们好好上一课…