用Java写一个王者荣耀游戏

目录

 sxt包

Background

Bullet

 Champion

 ChampionDaji

 GameFrame

 GameObject

Minion

MinionBlue

MinionRed

Turret

TurretBlue

TurretRed

beast包

Bear

Beast

Bird

BlueBuff

RedBuff

Wolf

Xiyi


打开Eclipse创建图片中的几个包

 sxt包

Background

package sxt;import java.awt.*;
//背景类
public  class Background extends GameObject{public Background(GameFrame gameFrame) {super(gameFrame);}Image bg = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Map.jpg");public void paintSelf(Graphics g){g.drawImage(bg,0,0,null);}@Overridepublic Rectangle getRec() {return null;}
}

Bullet

package sxt;import java.awt.*;//子弹类
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);}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);}public Bullet() {super();}public void move() {//子弹与目标碰撞,子弹消失,目标减血if (recIntersectsRec(getRec(), target.getRec())) {target.setCurrentHp(target.getCurrentHp() - getAd());gameFrame.removeList.add(this);}int dis = (int) getDis(getX(), getY(), target.getX(), target.getY());if (dis != 0) {int xSpeed =  (getSpd() * (target.getX() - getX()) / dis);int ySpeed =  (getSpd() * (target.getY() - getY()) / dis);setX(getX() + xSpeed);setY(getY() + ySpeed);}}@Overridepublic void paintSelf(Graphics g) {g.drawImage(getImg(), getX()-16, getY()-16, null);g.setColor(Color.BLACK);g.fillOval(getX()-5, getY()-5, 10, 10);g.drawRect(getX()-5, getY()-5, 10, 10);move();}@Overridepublic Rectangle getRec() {return new Rectangle(getX()-5, getY()-5, 10, 10);}public int getAd() {return ad;}public void setAd(int ad) {this.ad = ad;}
}

 Champion

package sxt;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;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;//技能冷却时间int coolDownTimeOne;int coolDownTimeTwo;int coolDownTimeThree;//三个技能是否处于冷却状态boolean coolDownOne = true;boolean coolDownTwo = true;boolean coolDownThree = true;static {for (int i = 1; i < 8; i++) {imgs[i] = "C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\move\\" + i + ".png";}}public Champion(GameFrame gameFrame) {super(gameFrame);//定义英雄的图片和坐标setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\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("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\stand.png");}}//添加三个技能按钮public void addButton() {JButton button1 = new JButton();button1.setSize(100, 100);button1.setLocation(1056, 513);button1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {abilityOne();}});JButton button2 = new JButton();button2.setSize(100, 100);button2.setLocation(1090, 370);button2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {abilityTwo();}});JButton button3 = new JButton();button3.setSize(100, 100);button3.setLocation(1220, 300);button3.addActionListener(new ActionListener() {@Overridepublic 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);@Overridepublic void paintSelf(Graphics g) {// 生命值为0if (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);}}@Overridepublic Rectangle getRec() {return new Rectangle(getX() - 30, getY() - 60, 60, 120);}}

 ChampionDaji

package sxt;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;public class ChampionDaji extends Champion {// 技能是否处于释放状态boolean ifAbilityOne = false;boolean ifAbilityTwo = false;// 鼠标监视器MouseMonitor m;// 一技能多边形Polygon p;// 一技能三角函数double sin;double cos;// 一技能已经攻击过的目标ArrayList<GameObject> attacked;// 一技能移动次数int step = 0;// 技能二目标GameObject abilityTwoTarget;// 技能二子弹Bullet abilityTwoBullet;// 三技能的五个子弹,释放三技能后重新定义Bullet[] bulletList = { new Bullet(), new Bullet(), new Bullet(), new Bullet(), new Bullet() };public ChampionDaji(GameFrame gameFrame) {super(gameFrame);abilityOne = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityOne.jpg");abilityTwo = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityTwo.jpg");abilityThree = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityThree.jpg");// 三个技能冷却时间coolDownTimeOne = 3000;coolDownTimeTwo = 5000;coolDownTimeThree = 8000;}public void exit() {this.gameFrame.removeMouseListener(m);}public void abilityOneMove() {p.translate((int) (50 * cos), -(int) (50 * sin));for (GameObject redObj : gameFrame.redList) {// 是红色方小兵 && 发生碰撞 && 没在attacked列表里if (redObj instanceof MinionRed && p.intersects(redObj.getRec()) && attacked.indexOf(redObj) == -1) {// 小兵扣血,添加到attacked里redObj.setCurrentHp(redObj.getCurrentHp() - 400);attacked.add(redObj);}}}@Overridepublic void abilityOne() {if (coolDownOne) {m = new MouseMonitor();p = new Polygon();gameFrame.addMouseListener(m);attacked = new ArrayList<GameObject>();}}public void abilityTwo() {if (coolDownTwo) {boolean find = false;for (GameObject redObj : gameFrame.objList) {// 是红色小兵 && 距离小于250 && 存活if (redObj instanceof MinionRed && recIntersectsCir(redObj.getRec(), getX(), getY(), 250)&& redObj.isAlive()) {// 添加子弹abilityTwoBullet = new Bullet(gameFrame, this, redObj, 250, 60, "C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityTwoBullet.png");gameFrame.objList.add(abilityTwoBullet);// 给目标赋值abilityTwoTarget = redObj;// 释放二技能ifAbilityTwo = true;find = true;break;}}if (find) {new AbilityTwoCD().start();find = false;}}}/*** 点击技能三释放技能 先将技能范围内目标储存到targetList里 提前定义五个子弹 技能释放时初始化五个子弹 子弹目标从targetList里随机选择* 如果期间目标死亡,制作一个目标替身,生命值设置为true 子弹与目标或替身碰撞后消失*/@Overridepublic void abilityThree() {if (coolDownThree) {// 创建列表来储存目标ArrayList<GameObject> targetList = new ArrayList<GameObject>();// 遍历redList,找到符合条件的目标,储存到列表里for (int i = 0; i < gameFrame.objList.size(); i++) {GameObject target = gameFrame.objList.get(i);// 是红色小兵 && 在技能范围里 && 存活if (target instanceof MinionRed && recIntersectsCir(target.getRec(), getX(), getY(), 250)&& target.isAlive()) {targetList.add(target);}}// 找到目标if (targetList.size() != 0) {// 创建五个子弹,随机攻击列表里的目标Random random = new Random();int count = 0; // 统计三技能发射子弹数量while (count < 5) {int r = random.nextInt(targetList.size());if (!targetList.get(r).isAlive()) {GameObject substitute = targetList.get(r);substitute.setAlive(true);bulletList[count] = new Bullet(gameFrame, this, substitute, 150, 60,"C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityTwoBullet.png");} else {bulletList[count] = new Bullet(gameFrame, this, targetList.get(r), 150, 60,"C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\Daji\\abilityTwoBullet.png");}count++;}new AbilityThreeBulletCD().start();// 三技能进入冷却new AbilityThreeCD().start();}}}@Overridepublic void abilityEffect(Graphics g) {if (ifAbilityOne) {g.setColor(Color.RED);g.fillPolygon(p);abilityOneMove();step++;if (step == 10) {step = 0;ifAbilityOne = false;}}if (ifAbilityTwo) {System.out.println(abilityTwoTarget.beControlled);if (abilityTwoBullet.getRec().intersects(abilityTwoTarget.getRec())) {new AbilityControllCD().start();ifAbilityTwo = false;}}}// 技能一冷却时间class AbilityOneCD extends Thread {public void run() {// 将技能一设置为冷却状态coolDownOne = false;// 线程休眠try {// one来表示一技能冷却时间int one = coolDownTimeOne;while (one > 0) {Thread.sleep(1000);System.out.println("一技能冷却时间: " + one / 1000);one -= 1000;}} catch (Exception e) {e.printStackTrace();}// 将技能一设置为攻击状态coolDownOne = true;// 线程终止this.interrupt();}}// 技能二冷却时间class AbilityTwoCD extends Thread {public void run() {// 将技能二设置为冷却状态coolDownTwo = false;// 线程休眠try {// one来表示二技能冷却时间int two = coolDownTimeTwo;while (two > 0) {Thread.sleep(1000);System.out.println("二技能冷却时间: " + two / 1000);two -= 1000;}} catch (Exception e) {e.printStackTrace();}// 将技能二设置为攻击状态coolDownTwo = true;// 线程终止this.interrupt();}}// 技能二控制时间class AbilityControllCD extends Thread {public void run() {abilityTwoTarget.beControlled = true;// 线程休眠try {Thread.sleep(20000);} catch (Exception e) {e.printStackTrace();}abilityTwoTarget.beControlled = false;this.interrupt();}}//技能三冷却状态class AbilityThreeCD extends Thread {public void run() {// 将攻击功能设置为冷却状态coolDownThree = false;// 休眠try {int three = coolDownTimeThree;while (coolDownTimeThree > 0) {Thread.sleep(1000);System.out.println("技能三冷却时间: " + coolDownTimeThree / 1000);coolDownTimeThree -= 1000;}coolDownTimeThree = three;} catch (Exception e) {e.printStackTrace();}// 将攻击功能解除冷却状态coolDownThree = true;// 线程终止this.interrupt();}}class AbilityThreeBulletCD extends Thread {public void run() {// 休眠try {System.out.println("Thread start");gameFrame.objList.add(bulletList[0]);Thread.sleep(100);gameFrame.objList.add(bulletList[1]);Thread.sleep(100);gameFrame.objList.add(bulletList[2]);Thread.sleep(100);gameFrame.objList.add(bulletList[3]);Thread.sleep(100);gameFrame.objList.add(bulletList[4]);} catch (Exception e) {e.printStackTrace();}// 线程终止this.interrupt();}}// 鼠标监视器private class MouseMonitor extends MouseAdapter {@Overridepublic 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));exit();new AbilityOneCD().start();ifAbilityOne = true;}}
}

 GameFrame

package sxt;import beast.Beast;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.JButton;
import javax.swing.JFrame;public class GameFrame extends JFrame {// 游戏开始 0, 游戏胜利1,游戏失败2int state = 0;// 窗口尺寸final int windowWidth = 1400;final  int windowHeight = 700;// 双缓冲图片private Image offScreenImage = null;// 攻击图片private Image attack = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\attack.jpg");//游戏胜利失败图片private Image gameWin = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\gameWin.png");private Image gameLose = Toolkit.getDefaultToolkit().getImage("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\gameLose.png");// 游戏背景Background background = new Background(this);// 游戏玩家Champion player = new ChampionDaji(this);// 双方小兵MinionBlue mb = new MinionBlue(this);MinionRed mr = new MinionRed(this);//野怪public Beast beast = new Beast(this);// 防御塔Turret turret = new Turret(this);// 游戏元素列表public ArrayList<GameObject> objList = new ArrayList<>();ArrayList<GameObject> redList = new ArrayList<>();// 红色方ArrayList<GameObject> blueList = new ArrayList<>();// 蓝色方public ArrayList<GameObject> removeList = new ArrayList<>();// 存放将要删除的元素public void launch() {// 设置尺寸setSize(windowWidth, windowHeight);// 窗口居中setLocationRelativeTo(null);// 关闭事件setDefaultCloseOperation(3);// 用户不能调整窗口大小setResizable(false);// 标题setTitle("王者荣耀");// 窗口可见setVisible(true);// 添加键盘监视器this.addKeyListener(new GameFrame.KeyMonitor());// 添加游戏元素objList.add(background);objList.add(player);objList.addAll(beast.beastList);objList.addAll(turret.turretList);playMusic();for (int i = 0; i < 4; i++) {blueList.add(turret.turretList.get(i));}for (int i = 4; i < 8; i++) {redList.add(turret.turretList.get(i));}/*** 攻击按钮*/JButton button = new JButton();button.setSize(130, 132);button.setLocation(1150, 430);button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 按钮事件player.attack(redList);}});this.add(button);player.addButton();while (true) {mb.createMinion(this, blueList);mr.createMinion(this, redList);repaint();try {Thread.sleep(25);} 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){turret.isLive();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 ==1) {//游戏胜利gImage.drawImage(gameWin, player.getX()-700, player.getY()-300 , null);} else if (state == 2) {//游戏失败gImage.drawImage(gameLose, player.getX()-700, player.getY()-300, null);}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 {@Overridepublic void keyPressed(KeyEvent e) {int key = e.getKeyCode();player.keyPressed(e);}@Overridepublic void keyReleased(KeyEvent e) {int key = e.getKeyCode();player.keyReleased(e);}}static Clip clip;public static void playMusic() {try{//这里面放 绝对路径,音频必须是wav格式,用音频转换软件 把mp3 转成wav格式File musicPath = new File("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\PlayMusic\\bgm.wav");if(musicPath.exists()){AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);clip = AudioSystem.getClip();clip.open(audioInput);FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);gainControl.setValue(-20.0f);//设置音量,范围为 -60.0f 到 6.0fclip.start();clip.loop(Clip.LOOP_CONTINUOUSLY);}else{}}catch(Exception ex){ex.printStackTrace();}}}

 GameObject

package sxt;import java.awt.*;
import java.util.ArrayList;//游戏元素的父类
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() {}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) {// 矩形于圆相交: 圆心到至少一个矩形定点的距离小于rif ((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);}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;}}}}}//public abstract void addTurret();class AttackCD extends Thread {public void run() {// 将攻击功能设置为冷却状态setAttackCoolDown(false);// 线程休眠try {Thread.sleep(attackCoolDownTime);} catch (Exception e) {e.printStackTrace();}// 将攻击功能设置为攻击状态setAttackCoolDown(true);// 线程终止this.interrupt();}}// 绘制元素public abstract void paintSelf(Graphics g);// 返回矩形public abstract Rectangle getRec();public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public Image getImg() {return img;}public void setImg(String img) {this.img = Toolkit.getDefaultToolkit().getImage(img);}public int getSpd() {return spd;}public void setSpd(int spd) {this.spd = spd;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public int getCurrentHp() {return currentHp;}public void setCurrentHp(int currentHp) {this.currentHp = currentHp;}public GameObject getTarget() {return target;}public void setTarget(GameObject target) {this.target = target;}public boolean isHasTarget() {return hasTarget;}public void setHasTarget(boolean hasTarget) {this.hasTarget = hasTarget;}public int getDis() {return dis;}public void setDis(int dis) {this.dis = dis;}public int getAttackCoolDownTime() {return attackCoolDownTime;}public void setAttackCoolDownTime(int attackCoolDownTime) {this.attackCoolDownTime = attackCoolDownTime;}public boolean isAttackCoolDown() {return attackCoolDown;}public void setAttackCoolDown(boolean attackCoolDown) {this.attackCoolDown = attackCoolDown;}public boolean isAlive() {return alive;}public void setAlive(boolean alive) {this.alive = alive;}
}

Minion

package sxt;import java.awt.*;
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);}/*** (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)*/public abstract void move(ArrayList<GameObject> objList);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() {int dis = (int) getDis(getX(), getY(), getTarget().getX(), getTarget().getY());if(dis!=0) {int xSpeed =  (getSpd() * (getTarget().getX() - getX()) / dis);int ySpeed = (getSpd() * (getTarget().getY() - getY()) / dis);setX(getX() + xSpeed);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.5stry {Thread.sleep(1500);} catch (Exception e) {e.printStackTrace();}nextMinion = true;// 线程终止this.interrupt();}}// 每波小兵生成时间class NextLine extends Thread {public void run() {nextLine = false;// 休眠15stry {Thread.sleep(15000);} catch (Exception e) {e.printStackTrace();}nextLine = true;// 线程终止this.interrupt();}}@Overridepublic void paintSelf(Graphics g) {// 生命值为0if (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);g.drawOval(getX() - 200, getY() - 200, 400, 400);// 小兵移动if (!beControlled) {if (this instanceof MinionBlue) {move(gameFrame.redList);} else {move(gameFrame.blueList);}}}}@Overridepublic Rectangle getRec() {return new Rectangle(getX() - 16, getY() - 16, 45, 45);}public boolean isIfFindTarget() {return ifFindTarget;}public void setIfFindTarget(boolean ifFindTarget) {this.ifFindTarget = ifFindTarget;}}

MinionBlue

package sxt;import java.util.ArrayList;public  class MinionBlue extends Minion{public MinionBlue(GameFrame gameFrame) {super(gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\minion\\blue.jpg");setX(1325);setY(3750);}@Overridepublic void move(ArrayList<GameObject> objList) {if(isIfFindTarget()) {//离开检测范围if(!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {setIfFindTarget(false);}else {if(!isHasTarget()) {moveToTarget();}attack(objList);}}else {findTarget(objList);//原路线移动if(getX() < 4425) {setSpd(5);setX(getX() + getSpd());}else if(getX() < 5100 && getX() >= 4425) {setSpd(20);setX(getX() + getSpd());setY(getY() - getSpd());}else if(getX() >= 4900) {setSpd(18);setY(getY() - getSpd());}}}
}

MinionRed

package sxt;import java.util.ArrayList;public  class MinionRed extends Minion {public MinionRed(GameFrame gameFrame) {super(gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\minion\\red.jpg");setX(5050);setY(1125);}@Overridepublic void move(ArrayList<GameObject> objList) {if (isIfFindTarget()) {// 离开检测范围if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {setIfFindTarget(false);} else {if (!isHasTarget()) {moveToTarget();}attack(objList);}} else {findTarget(objList);// 原路线移动if (getY() < 3125) {setSpd(28);setY(getY() + getSpd());} else if (getY() < 3750 && getY() >= 3125) {setSpd(20);setY(getY() + getSpd());setX(getX() - getSpd());} else if (getY() >= 3750) {setSpd(25);setX(getX() - getSpd());}}}}

Turret

package sxt;import java.awt.*;
import java.util.ArrayList;
//防御塔类
public  class Turret extends GameObject{ArrayList<Turret> turretList = new ArrayList<>();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("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\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));}public Turret(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\turret.png");setHp(6000);setCurrentHp(getHp());setAttackCoolDownTime(1000);setDis(300);}public void isLive(){if(gameFrame.turret.turretBlueBase.getCurrentHp()==0){gameFrame.state=2;}if(gameFrame.turret.turretRedBase.getCurrentHp()==0){gameFrame.state=1;}}@Overridepublic void paintSelf(Graphics g) {// 生命值为0if (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.fillOval(getX(), getY(), 10, 10);g.drawRect(getX() - 50, getY() - 100, 100, 180);g.drawOval(getX() - 300, getY() - 300, 600, 600);}}@Overridepublic Rectangle getRec() {return new Rectangle(getX() - 50, getY() - 100, 100, 180);}
}

TurretBlue

package sxt;
//蓝方防御塔
public class TurretBlue extends Turret{public TurretBlue(GameFrame gameFrame){super(gameFrame);}public TurretBlue(int x,int y,GameFrame gameFrame){super(x,y,gameFrame);}
}

TurretRed

package sxt;
//蓝方防御塔
public class TurretRed extends Turret{//有参构造public TurretRed(GameFrame gameFrame){super(gameFrame);}public TurretRed(int x,int y,GameFrame gameFrame){super(x,y,gameFrame);}
}

beast包

Bear

package beast;
import sxt.GameFrame;
public class Bear extends Beast {public Bear(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\bear.jpg");width = 85;height = 112;setDis(65);}}

Beast

package beast;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;import sxt.*;public class Beast extends GameObject {public ArrayList<Beast> beastList = new ArrayList<>();int width;int height;// 复活的元素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());beast = this;}@Overridepublic void paintSelf(Graphics g) {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());}}@Overridepublic Rectangle getRec() {return new Rectangle(getX() - width / 2, getY() - height / 2, width, height);}//野怪复活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);}}}

Bird

package beast;
import sxt.GameFrame;
public class Bird extends Beast {public Bird(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\红隼.jpg");width = 122;height = 98;setDis(125);}}

BlueBuff

package beast;
import sxt.GameFrame;
public class BlueBuff extends Beast {public BlueBuff(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\blueBuff.jpg");width = 142;height = 176;setDis(70);}}

RedBuff

package beast;
import sxt.GameFrame;
public class RedBuff extends Beast {public RedBuff(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\redBuff.jpg");width = 103;height = 150;setDis(70);}
}

Wolf

package beast;
import sxt.GameFrame;
public class Wolf extends Beast {public Wolf(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\wolf.jpg");width = 145;height = 140;setDis(65);}}

Xiyi

package beast;
import sxt.GameFrame;
public class  extends Beast {public Xiyi(int x, int y, GameFrame gameFrame) {super(x, y, gameFrame);setImg("C:\\Users\\24465\\Desktop\\王者荣耀图片(1)\\王者荣耀图片\\beast\\蜥蜴.jpg");width = 111;height = 65;setDis(125);}
}

这是所用到的图片

链接:https://pan.baidu.com/s/10KopCcdw0TESSujoMeshUw?pwd=bupl 
提取码:bupl

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DAPP开发【06】nodejs安装与npm路径更换

windows系统在执行用户命令时顺序 windows系统在执行用户命令时&#xff0c;若用户未给出文件的绝对路径&#xff0c; 则 &#xff08;1&#xff09;首先在当前目录下寻找相应的可执行文件、批处理文件等&#xff1b; &#xff08;2&#xff09;若找不到&#xff0c;再依次在系…

[linux进程控制]进程替换

文章目录 1.进程替换的概念和原理2.如何完成进程替换2.1exec系列函数加载器的底层系统调用接口基于execve的封装--库函数 2.2 int execl(const char *path, const char *arg, ...);1.在当前进程进行替换2.在子进程进行替换 2.3 int execv(const char *path, char *const argv[]…

分治—快速选择算法

文章目录 &#x1f347;215.数组中的第K个最大元素&#x1f348;1. 题目&#x1f349;2. 算法原理&#x1f34a;3. 代码实现 &#x1f34b;LCR 159. 库存管理 III&#x1f34c;1. 题目&#x1f34d;2. 算法原理&#x1f96d;代码实现 &#x1f347;215.数组中的第K个最大元素 …

损失函数代价函数

代价函数&#xff08;Cost Function&#xff09;和损失函数&#xff08;Loss Function&#xff09;是深度学习中两个相关但不完全相同的概念&#xff0c;它们用于衡量模型的性能&#xff0c;并在训练过程中进行参数优化。尽管经常被混用&#xff0c;但在一些文献中也有区别对待…

强推六款满分AI写作工具,需要自取

&#x1f517; 运行环境&#xff1a;ChatGPT &#x1f6a9; 撰写作者&#xff1a;左手の明天 &#x1f947; 精选专栏&#xff1a;《python》 &#x1f525; 推荐专栏&#xff1a;《算法研究》 &#x1f510;#### 防伪水印——左手の明天 ####&#x1f510; &#x1f497; 大家…

31-WEB漏洞-文件操作之文件包含漏洞全解

31-WEB漏洞-文件操作之文件包含漏洞全解 一、本地包含1.1、无限制包含漏洞文件1.2、有限制包含漏洞文件1.2.1、绕过方法1.2.1.1、%00截断1.2.1.2、长度截断 二、远程包含2.1、无限制包含漏洞文件2.2、有限制包含漏洞文件 三、各种协议流提交流3.1、各协议的利用条件和方法3.1.1…

克服.360勒索病毒:.360勒索病毒的解密和预防

导言: 在数字化的今天&#xff0c;数据安全问题变得愈发棘手。.360勒索病毒是当前网络空间的一场潜在灾难&#xff0c;对于这个威胁&#xff0c;了解应对之道和采取切实的预防措施至关重要。如果您正在经历勒索病毒的困境&#xff0c;欢迎联系我们的vx技术服务号&#xff08;s…

洛谷 P5711 闰年判断 C++代码

目录 前言 思路点拨 AC代码 结尾 前言 今天我们来做洛谷上的一道题目。 网址&#xff1a;【深基3.例3】闰年判断 - 洛谷 题目&#xff1a; 思路点拨 首先题目让我们输入一个年份&#xff0c;因此我们需要定义一个变量year&#xff0c;来存储输入的年份&#xff1a; in…

回归分析:预测和建模

回归分析:预测和建模 写在开头1. 回归分析的基本概念2. 回归分析的方法2.1 简单线性回归2.1.1 数学知识2.1.2 应用举例2.2 多元线性回归2.2.1 数学公式和应用2.2.1 应用场景举例2.3 多项式回归2.3.1 数学公式和应用2.3.2 应用场景举例2.4 逻辑回归2.4.1 数学公式和应用2.4.2 应…

matlab科学计算

欢迎关注博主 Mindtechnist 或加入【智能科技社区】一起学习和分享Linux、C、C、Python、Matlab&#xff0c;机器人运动控制、多机器人协作&#xff0c;智能优化算法&#xff0c;滤波估计、多传感器信息融合&#xff0c;机器学习&#xff0c;人工智能等相关领域的知识和技术。关…

如何进行卷积特征可视化

大家好啊&#xff0c;我是董董灿。 之前写过很多关于卷积算法的文章&#xff1a;5分钟理解什么是卷积的特征提取。总的来说&#xff0c;卷积算法的本质是一个特征提取器。 那么既然卷积神经网络在图像分类、图像检测、图像分割以及其他领域有这么好的表现&#xff0c;卷积到底…

【动手学深度学习】(六)权重衰退

文章目录 一、理论知识二、代码实现2.1从零开始实现2.2简洁实现 【相关总结】 主要解决过拟合 一、理论知识 1、使用均方范数作为硬性限制&#xff08;不常用&#xff09; 通过限制参数值的选择范围来控制模型容量 通常不限制偏移b 小的意味着更强的正则项 使用均方范数作为柔…

多项式拟合求解

目录 简介 基本原理 例1 例2 例3 参考资料 简介 多项式拟合可以用最小二乘求解&#xff0c;不管是一元高阶函数&#xff0c;还是多元多项式函数&#xff0c;还是二者的混合&#xff0c;都可以通过统一的方法求解。当然除了最小二乘法&#xff0c;还是其他方法可以求解&…

极兔速递查询,极兔速递单号查询,根据更新量筛选出来需要的单号

批量查询极兔速递单号的物流信息&#xff0c;并根据物流更新量将需要的单号筛选出来。 所需工具&#xff1a; 一个【快递批量查询高手】软件 极兔速递单号若干 操作步骤&#xff1a; 步骤1&#xff1a;运行【快递批量查询高手】软件&#xff0c;第一次使用的伙伴记得先注册&…

express搭建后台node接口

在前端的学习中我们使用express来开发接口结合mysql&#xff0c;然后使用可视化的数据库工具来操作数据&#xff0c; web框架是express 文档是jsdoc swagger 数据库模型是sequelize 部署使用PM2来上服务器&#xff0c; 打包你也可以结合webpack配置target node状态 当然你也可以…

糟了,数据库崩了,又好像没崩

前言 2023 年某一天周末&#xff0c;新手程序员小明因为领导安排的一个活来到公司加班&#xff0c;小明三下五除二&#xff0c;按照领导要求写了一个跑批的数据落库任务在测试环境执行 &#xff0c;突然间公司停电了&#xff0c;小明大惊&#xff0c;“糟了&#xff0c;MySQL …

cc-product-waterfall仿天猫、淘宝购物车店铺商品列表组件

cc-product-waterfall仿天猫、淘宝购物车店铺商品列表组件 引言 在电商应用中&#xff0c;购物车体验的优化对于提升用户满意度和转化率至关重要。在本文中&#xff0c;我们将深入探讨如何使用cc-product-waterfall组件&#xff0c;结合uni-number-box和xg-widget&#xff0c;…

软著项目推荐 深度学习手势识别算法实现 - opencv python

文章目录 1 前言2 项目背景3 任务描述4 环境搭配5 项目实现5.1 准备数据5.2 构建网络5.3 开始训练5.4 模型评估 6 识别效果7 最后 1 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习手势识别算法实现 - opencv python 该项目较为新颖…

计算机网络 | 物理层下 传输媒介、信道复用技术,带宽接入技术

文章目录 3. 物理层下面的传输媒介3.1 导引型传输媒介3.2 非导引型传输媒介 4 信道复用技术5 数字传输系统5.1 旧的数字系统5.2 新数字系统 6 带宽接入技术6.1 DSL&#xff08;数字用户线&#xff09;6.2HFC 光纤同轴混合网6.2.1 接入 6.3 FTTx技术 3. 物理层下面的传输媒介 传…

FL Studio 21.2.1.3859中文破解激活版2024免费下载安装图文教程

FL Studio 21.2.1.3859中文破解激活版是我见过更新迭代最快的宿主软件&#xff0c;没有之一。FL Studio12、FL Studio20、FL Studio21等等。有时甚至我刚刚下载好了最新版本&#xff0c;熟悉了新版本一些好用的操作&#xff0c;Fl Studio就又推出了更新的版本&#xff0c;而且F…