当前位置:   article > 正文

Java 简易版王者荣耀_王者荣耀java下载

王者荣耀java下载

所有包和类 

GameFrame类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.io.File;
  8. import java.util.ArrayList;
  9. import javax.sound.sampled.AudioInputStream;
  10. import javax.sound.sampled.AudioSystem;
  11. import javax.sound.sampled.Clip;
  12. import javax.sound.sampled.FloatControl;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. public class GameFrame extends JFrame {
  16. // 游戏开始 0, 游戏胜利1,游戏失败2
  17. int state = 0;
  18. // 窗口尺寸
  19. final int windowWidth = 1400;
  20. final int windowHeight = 700;
  21. // 双缓冲图片
  22. private Image offScreenImage = null;
  23. // 攻击图片
  24. private Image attack = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/attack.jpg");
  25. //游戏胜利失败图片
  26. private Image gameWin = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/gameWin.png");
  27. private Image gameLose = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/gameLose.png");
  28. // 游戏背景
  29. Background background = new Background(this);
  30. // 游戏玩家
  31. Champion player = new ChampionDaji(this);
  32. // 双方小兵
  33. MinionBlue mb = new MinionBlue(this);
  34. MinionRed mr = new MinionRed(this);
  35. //野怪
  36. public Beast beast = new Beast(this);
  37. // 防御塔
  38. Turret turret = new Turret(this);
  39. // 游戏元素列表
  40. public ArrayList<GameObject> objList = new ArrayList<>();
  41. ArrayList<GameObject> redList = new ArrayList<>();// 红色方
  42. ArrayList<GameObject> blueList = new ArrayList<>();// 蓝色方
  43. public ArrayList<GameObject> removeList = new ArrayList<>();// 存放将要删除的元素
  44. public void launch() {
  45. // 设置尺寸
  46. setSize(windowWidth, windowHeight);
  47. // 窗口居中
  48. setLocationRelativeTo(null);
  49. // 关闭事件
  50. setDefaultCloseOperation(3);
  51. // 用户不能调整窗口大小
  52. setResizable(false);
  53. // 标题
  54. setTitle("王者荣耀");
  55. // 窗口可见
  56. setVisible(true);
  57. // 添加键盘监视器
  58. this.addKeyListener(new GameFrame.KeyMonitor());
  59. // 添加游戏元素
  60. objList.add(background);
  61. objList.add(player);
  62. objList.addAll(beast.beastList);
  63. objList.addAll(turret.turretList);
  64. for (int i = 0; i < 4; i++) {
  65. blueList.add(turret.turretList.get(i));
  66. }
  67. for (int i = 4; i < 8; i++) {
  68. redList.add(turret.turretList.get(i));
  69. }
  70. /**
  71. * 攻击按钮
  72. */
  73. JButton button = new JButton();
  74. button.setSize(130, 132);
  75. button.setLocation(1150, 430);
  76. button.addActionListener(new ActionListener() {
  77. @Override
  78. public void actionPerformed(ActionEvent e) {
  79. // 按钮事件
  80. player.attack(redList);
  81. }
  82. });
  83. this.add(button);
  84. player.addButton();
  85. while (true) {
  86. mb.createMinion(this, blueList);
  87. mr.createMinion(this, redList);
  88. repaint();
  89. try {
  90. Thread.sleep(25);
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. public void paint(Graphics g) {
  97. if (offScreenImage == null) {
  98. offScreenImage = this.createImage(5984, 4452);
  99. }
  100. Graphics gImage = offScreenImage.getGraphics();
  101. if(state==0){
  102. turret.isLive();
  103. for (int i = 0; i < objList.size(); i++) {
  104. objList.get(i).paintSelf(gImage);
  105. }
  106. // 绘制攻击图片
  107. gImage.drawImage(attack, player.getX() + 500, player.getY() + 100, null);
  108. objList.removeAll(removeList);
  109. } else if (state ==1) {//游戏胜利
  110. gImage.drawImage(gameWin, player.getX()-700, player.getY()-300 , null);
  111. } else if (state == 2) {//游戏失败
  112. gImage.drawImage(gameLose, player.getX()-700, player.getY()-300, null);
  113. }
  114. g.drawImage(offScreenImage, -player.getX() + 700, -player.getY() + 350, null);
  115. // 添加按钮后不能调用键盘事件 因为程序的焦点变成了按钮 this.requestFocus() 把焦点重新改变到游戏界面上
  116. this.requestFocus();
  117. }
  118. // main方法
  119. public static void main(String[] args) {
  120. GameFrame gameFrame = new GameFrame();
  121. gameFrame.launch();
  122. }
  123. // 键盘事件
  124. private class KeyMonitor extends KeyAdapter {
  125. @Override
  126. public void keyPressed(KeyEvent e) {
  127. int key = e.getKeyCode();
  128. player.keyPressed(e);
  129. }
  130. @Override
  131. public void keyReleased(KeyEvent e) {
  132. int key = e.getKeyCode();
  133. player.keyReleased(e);
  134. }
  135. }
  136. static Clip clip;
  137. }

GameObject类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. //游戏元素的父类
  5. public abstract class GameObject {
  6. // 坐标
  7. private int x;
  8. private int y;
  9. // 图片
  10. private Image img;
  11. // 游戏界面
  12. public GameFrame gameFrame;
  13. // 速度
  14. private int spd;
  15. // 初始生命值
  16. private int hp;
  17. // 当前生命值
  18. private int currentHp;
  19. // 攻击目标
  20. private GameObject target;
  21. // 是否有目标
  22. private boolean hasTarget = false;
  23. // 攻击距离
  24. private int dis;
  25. // 攻击时间间隔
  26. private int attackCoolDownTime;
  27. // 攻击是否冷却
  28. private boolean attackCoolDown = true;
  29. // 是否存活
  30. private boolean alive = true;
  31. //是否被控制
  32. boolean beControlled = false;
  33. public GameObject(GameFrame gameFrame) {
  34. this.gameFrame = gameFrame;
  35. }
  36. public GameObject(int x, int y, GameFrame gameFrame) {
  37. this.x = x;
  38. this.y = y;
  39. this.gameFrame = gameFrame;
  40. }
  41. public GameObject() {
  42. }
  43. public void addHp(Graphics g, int difX, int difY, int width, int height, Color color) {
  44. // 绘制外部轮廓
  45. g.setColor(Color.black);
  46. g.drawRect(getX() - difX, getY() - difY, width, height);
  47. // 填充矩形
  48. g.setColor(color);
  49. g.fillRect(getX() - difX, getY() - difY, (int) (width * getCurrentHp() / getHp()), height);
  50. }
  51. public double getDis(int x1, int y1, int x2, int y2) {
  52. return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
  53. }
  54. // 矩形矩形碰撞检测
  55. public boolean recIntersectsRec(Rectangle r1, Rectangle r2) {
  56. return r1.intersects(r2);
  57. }
  58. public boolean recIntersectsCir(Rectangle rec, int x, int y, int r) {
  59. // 矩形于圆相交: 圆心到至少一个矩形定点的距离小于r
  60. if ((getDis(x, y, rec.x, rec.y) < r) || (getDis(x, y, rec.x, rec.y + rec.height) < r)
  61. || (getDis(x, y, rec.x + rec.width, rec.y) < r)
  62. || (getDis(x, y, rec.x + rec.width, rec.y + rec.height) < r)) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. // 攻击方法
  68. public void attack(ArrayList<GameObject> gameObjList) {
  69. if (hasTarget) {
  70. // 目标离开范围后寻找新的目标
  71. if (!recIntersectsCir(target.getRec(), getX(), getY(), getDis())) {
  72. setHasTarget(false);
  73. }
  74. // 目标死亡,寻找新目标
  75. else if (!target.isAlive()) {
  76. setHasTarget(false);
  77. } else if (isAttackCoolDown() && isAlive()) {
  78. Bullet bullet = null;
  79. // 防御塔攻击
  80. if (Turret.class.isAssignableFrom(getClass())) {
  81. bullet = new Bullet(gameFrame, this, getTarget(), 500, 50);
  82. }
  83. // 小兵攻击
  84. else if (Minion.class.isAssignableFrom(getClass())) {
  85. bullet = new Bullet(gameFrame, this, getTarget(), 50, 30);
  86. }
  87. // 玩家攻击
  88. else if (this instanceof Champion) {
  89. bullet = new Bullet(gameFrame, this, getTarget(), 500, 50);
  90. }
  91. gameFrame.objList.add(bullet);
  92. // 线程开始
  93. new AttackCD().start();
  94. }
  95. } else {
  96. // 遍历列表
  97. for (GameObject obj : gameObjList) {
  98. // 判断攻击范围(圆形)与敌方(矩形)是否相交
  99. if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
  100. // 找到目标
  101. setTarget(obj);
  102. setHasTarget(true);
  103. // 跳出循环
  104. break;
  105. }
  106. }
  107. // 玩家是否在攻击范围内
  108. if (!hasTarget && gameObjList == gameFrame.blueList) {
  109. if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), getDis())) {
  110. // 找到目标
  111. setTarget(gameFrame.player);
  112. setHasTarget(true);
  113. }
  114. }
  115. else {
  116. //野怪是否在攻击范围内
  117. for (GameObject obj : gameFrame.beast.beastList) {
  118. // 判断攻击范围(圆形)与敌方(矩形)是否相交
  119. if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
  120. // 找到目标
  121. setTarget(obj);
  122. setHasTarget(true);
  123. // 跳出循环
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. //public abstract void addTurret();
  131. class AttackCD extends Thread {
  132. public void run() {
  133. // 将攻击功能设置为冷却状态
  134. setAttackCoolDown(false);
  135. // 线程休眠
  136. try {
  137. Thread.sleep(attackCoolDownTime);
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. // 将攻击功能设置为攻击状态
  142. setAttackCoolDown(true);
  143. // 线程终止
  144. this.interrupt();
  145. }
  146. }
  147. // 绘制元素
  148. public abstract void paintSelf(Graphics g);
  149. // 返回矩形
  150. public abstract Rectangle getRec();
  151. public int getX() {
  152. return x;
  153. }
  154. public void setX(int x) {
  155. this.x = x;
  156. }
  157. public int getY() {
  158. return y;
  159. }
  160. public void setY(int y) {
  161. this.y = y;
  162. }
  163. public Image getImg() {
  164. return img;
  165. }
  166. public void setImg(String img) {
  167. this.img = Toolkit.getDefaultToolkit().getImage(img);
  168. }
  169. public int getSpd() {
  170. return spd;
  171. }
  172. public void setSpd(int spd) {
  173. this.spd = spd;
  174. }
  175. public int getHp() {
  176. return hp;
  177. }
  178. public void setHp(int hp) {
  179. this.hp = hp;
  180. }
  181. public int getCurrentHp() {
  182. return currentHp;
  183. }
  184. public void setCurrentHp(int currentHp) {
  185. this.currentHp = currentHp;
  186. }
  187. public GameObject getTarget() {
  188. return target;
  189. }
  190. public void setTarget(GameObject target) {
  191. this.target = target;
  192. }
  193. public boolean isHasTarget() {
  194. return hasTarget;
  195. }
  196. public void setHasTarget(boolean hasTarget) {
  197. this.hasTarget = hasTarget;
  198. }
  199. public int getDis() {
  200. return dis;
  201. }
  202. public void setDis(int dis) {
  203. this.dis = dis;
  204. }
  205. public int getAttackCoolDownTime() {
  206. return attackCoolDownTime;
  207. }
  208. public void setAttackCoolDownTime(int attackCoolDownTime) {
  209. this.attackCoolDownTime = attackCoolDownTime;
  210. }
  211. public boolean isAttackCoolDown() {
  212. return attackCoolDown;
  213. }
  214. public void setAttackCoolDown(boolean attackCoolDown) {
  215. this.attackCoolDown = attackCoolDown;
  216. }
  217. public boolean isAlive() {
  218. return alive;
  219. }
  220. public void setAlive(boolean alive) {
  221. this.alive = alive;
  222. }
  223. }

Turret类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. //防御塔类
  5. public class Turret extends GameObject{
  6. ArrayList<Turret> turretList = new ArrayList<>();
  7. public Turret turretBlueOne;
  8. public Turret turretBlueTwo;
  9. public Turret turretBlueThree;
  10. public Turret turretBlueBase;
  11. public Turret turretRedOne;
  12. public Turret turretRedTwo;
  13. public Turret turretRedThree;
  14. public Turret turretRedBase;
  15. public Turret(GameFrame gameFrame) {
  16. super(gameFrame);
  17. setImg("img/王者荣耀图片/turret.png");
  18. // 初始化八个防御塔
  19. turretList.add(turretBlueOne = new TurretBlue(1860, 3790, gameFrame));
  20. turretList.add(turretBlueTwo = new TurretBlue(2650, 3820, gameFrame));
  21. turretList.add(turretBlueThree = new TurretBlue(3995, 3830, gameFrame));
  22. turretList.add(turretBlueBase = new TurretBlue(1130, 3650, gameFrame));
  23. turretList.add(turretRedOne = new TurretRed(5100, 3030, gameFrame));
  24. turretList.add(turretRedTwo = new TurretRed(5120, 2100, gameFrame));
  25. turretList.add(turretRedThree = new TurretRed(5060, 1570, gameFrame));
  26. turretList.add(turretRedBase = new TurretRed(4850, 1100, gameFrame));
  27. }
  28. public Turret(int x, int y, GameFrame gameFrame) {
  29. super(x, y, gameFrame);
  30. setImg("img/王者荣耀图片/turret.png");
  31. setHp(6000);
  32. setCurrentHp(getHp());
  33. setAttackCoolDownTime(1000);
  34. setDis(300);
  35. }
  36. public void isLive(){
  37. if(gameFrame.turret.turretBlueBase.getCurrentHp()==0){
  38. gameFrame.state=2;
  39. }
  40. if(gameFrame.turret.turretRedBase.getCurrentHp()==0){
  41. gameFrame.state=1;
  42. }
  43. }
  44. @Override
  45. public void paintSelf(Graphics g) {
  46. // 生命值为0
  47. if (getCurrentHp() <= 0) {
  48. setAlive(false);
  49. gameFrame.removeList.add(this);
  50. if (this instanceof TurretBlue) {
  51. gameFrame.blueList.remove(this);
  52. } else {
  53. gameFrame.redList.remove(this);
  54. }
  55. } else {
  56. // 添加生命值
  57. if (this instanceof TurretBlue) {
  58. this.addHp(g, 50, 130, 100, 20, Color.GREEN);
  59. attack(gameFrame.redList);
  60. } else {
  61. this.addHp(g, 50, 130, 100, 20, Color.RED);
  62. attack(gameFrame.blueList);
  63. }
  64. g.drawImage(getImg(), getX() - 50, getY() - 100, null);
  65. g.fillOval(getX(), getY(), 10, 10);
  66. g.drawRect(getX() - 50, getY() - 100, 100, 180);
  67. g.drawOval(getX() - 300, getY() - 300, 600, 600);
  68. }
  69. }
  70. @Override
  71. public Rectangle getRec() {
  72. return new Rectangle(getX() - 50, getY() - 100, 100, 180);
  73. }
  74. }

TurretBlue类

  1. package newKingOfHonor;
  2. //蓝方防御塔
  3. public class TurretBlue extends Turret{
  4. public TurretBlue(GameFrame gameFrame){
  5. super(gameFrame);
  6. }
  7. public TurretBlue(int x,int y,GameFrame gameFrame){
  8. super(x,y,gameFrame);
  9. }
  10. }

TurretRed类

  1. package newKingOfHonor;
  2. //蓝方防御塔
  3. public class TurretRed extends Turret{
  4. public TurretRed(GameFrame gameFrame){
  5. super(gameFrame);
  6. }
  7. public TurretRed(int x,int y,GameFrame gameFrame){
  8. super(x,y,gameFrame);
  9. }
  10. }

Champion类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import javax.swing.JButton;
  7. //游戏英雄类
  8. public abstract class Champion extends GameObject {
  9. // 移动
  10. public boolean up, down, left, right;
  11. // 移动图集
  12. static String[] imgs = new String[8];
  13. // 第几张图片
  14. int moveCount = 1;
  15. //技能图片
  16. Image abilityOne;
  17. Image abilityTwo;
  18. Image abilityThree;
  19. //技能冷却时间
  20. int coolDownTimeOne;
  21. int coolDownTimeTwo;
  22. int coolDownTimeThree;
  23. //三个技能是否处于冷却状态
  24. boolean coolDownOne = true;
  25. boolean coolDownTwo = true;
  26. boolean coolDownThree = true;
  27. static {
  28. for (int i = 1; i < 8; i++) {
  29. imgs[i] = "img/王者荣耀图片/move/" + i +".png";
  30. }
  31. }
  32. public Champion(GameFrame gameFrame) {
  33. super(gameFrame);
  34. //定义英雄的图片和坐标
  35. setImg("img/王者荣耀图片/stand.png");
  36. setX(700);
  37. setY(3800);
  38. setSpd(75);
  39. setHp(24000);
  40. setDis(250);
  41. setAttackCoolDownTime(100);
  42. setCurrentHp(getHp());
  43. }
  44. public void keyPressed(KeyEvent e) {
  45. int key = e.getKeyCode();
  46. if (key == KeyEvent.VK_D) {
  47. right = true;}
  48. if (key == KeyEvent.VK_A) {
  49. left = true;}
  50. if (key == KeyEvent.VK_W) {
  51. up = true;}
  52. if (key == KeyEvent.VK_S) {
  53. down = true;}
  54. }
  55. public void keyReleased(KeyEvent e) {
  56. int key = e.getKeyCode();
  57. if (key == KeyEvent.VK_D) {
  58. right = false;}
  59. if (key == KeyEvent.VK_A) {
  60. left = false;}
  61. if (key == KeyEvent.VK_W) {
  62. up = false;}
  63. if (key == KeyEvent.VK_S) {
  64. down = false;}
  65. }
  66. public void move() {
  67. if (up) {
  68. setY(getY() - getSpd());
  69. }
  70. if (down) {
  71. setY(getY() + getSpd());
  72. }
  73. if (left) {
  74. setX(getX() - getSpd());
  75. }
  76. if (right) {
  77. setX(getX() + getSpd());
  78. }
  79. if (up || down || left || right) {
  80. setImg(imgs[moveCount]);
  81. moveCount++;
  82. if (moveCount == 8) {
  83. moveCount = 1;
  84. }
  85. } else {
  86. setImg("img/王者荣耀图片/stand.png");
  87. }
  88. }
  89. //添加三个技能按钮
  90. public void addButton() {
  91. JButton button1 = new JButton();
  92. button1.setSize(100, 100);
  93. button1.setLocation(1056, 513);
  94. button1.addActionListener(new ActionListener() {
  95. @Override
  96. public void actionPerformed(ActionEvent e) {
  97. abilityOne();
  98. }
  99. });
  100. JButton button2 = new JButton();
  101. button2.setSize(100, 100);
  102. button2.setLocation(1090, 370);
  103. button2.addActionListener(new ActionListener() {
  104. @Override
  105. public void actionPerformed(ActionEvent e) {
  106. abilityTwo();
  107. }
  108. });
  109. JButton button3 = new JButton();
  110. button3.setSize(100, 100);
  111. button3.setLocation(1220, 300);
  112. button3.addActionListener(new ActionListener() {
  113. @Override
  114. public void actionPerformed(ActionEvent e) {
  115. abilityThree();
  116. }
  117. });
  118. gameFrame.add(button1);
  119. gameFrame.add(button2);
  120. gameFrame.add(button3);
  121. }
  122. public abstract void abilityOne();
  123. public abstract void abilityTwo();
  124. public abstract void abilityThree();
  125. public abstract void abilityEffect(Graphics g);
  126. @Override
  127. public void paintSelf(Graphics g) {
  128. // 生命值为0
  129. if (getCurrentHp() <= 0) {
  130. setAlive(false);
  131. gameFrame.removeList.add(this);
  132. } else {
  133. // 添加生命值
  134. addHp(g, 30, 80, 80, 20, Color.GREEN);
  135. //绘制技能图片
  136. g.drawImage(abilityOne, getX() + 360, getY() + 180, null);
  137. g.drawImage(abilityTwo, getX() + 400, getY() + 40, null);
  138. g.drawImage(abilityThree, getX() + 520, getY() - 30, null);
  139. // 绘制图片
  140. g.drawImage(this.getImg(), getX() - 33, getY() - 50, null);
  141. // 改变画笔颜色
  142. g.setColor(Color.GREEN);
  143. // 绘制中心圆点
  144. g.fillOval(getX(), getY(), 10, 10);
  145. // 绘制矩形边框
  146. g.drawRect(getX() - 23, getY() - 50, 60, 120);
  147. move();
  148. abilityEffect(g);
  149. }
  150. }
  151. @Override
  152. public Rectangle getRec() {
  153. return new Rectangle(getX() - 30, getY() - 60, 60, 120);
  154. }
  155. }

ChampionDaji类

  1. package newKingOfHonor;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Polygon;
  5. import java.awt.Toolkit;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. public class ChampionDaji extends Champion {
  11. // 技能是否处于释放状态
  12. boolean ifAbilityOne = false;
  13. boolean ifAbilityTwo = false;
  14. // 鼠标监视器
  15. MouseMonitor m;
  16. // 一技能多边形
  17. Polygon p;
  18. // 一技能三角函数
  19. double sin;
  20. double cos;
  21. // 一技能已经攻击过的目标
  22. ArrayList<GameObject> attacked;
  23. // 一技能移动次数
  24. int step = 0;
  25. // 技能二目标
  26. GameObject abilityTwoTarget;
  27. // 技能二子弹
  28. Bullet abilityTwoBullet;
  29. // 三技能的五个子弹,释放三技能后重新定义
  30. Bullet[] bulletList = { new Bullet(), new Bullet(), new Bullet(), new Bullet(), new Bullet() };
  31. public ChampionDaji(GameFrame gameFrame) {
  32. super(gameFrame);
  33. abilityOne = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityOne.jpg");
  34. abilityTwo = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityTwo.jpg");
  35. abilityThree = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityThree.jpg");
  36. // 三个技能冷却时间
  37. coolDownTimeOne = 3000;
  38. coolDownTimeTwo = 5000;
  39. coolDownTimeThree = 8000;
  40. }
  41. public void exit() {
  42. this.gameFrame.removeMouseListener(m);
  43. }
  44. public void abilityOneMove() {
  45. p.translate((int) (50 * cos), -(int) (50 * sin));
  46. for (GameObject redObj : gameFrame.redList) {
  47. // 是红色方小兵 && 发生碰撞 && 没在attacked列表里
  48. if (redObj instanceof MinionRed && p.intersects(redObj.getRec()) && attacked.indexOf(redObj) == -1) {
  49. // 小兵扣血,添加到attacked里
  50. redObj.setCurrentHp(redObj.getCurrentHp() - 400);
  51. attacked.add(redObj);
  52. }
  53. }
  54. }
  55. @Override
  56. public void abilityOne() {
  57. if (coolDownOne) {
  58. m = new MouseMonitor();
  59. p = new Polygon();
  60. gameFrame.addMouseListener(m);
  61. attacked = new ArrayList<GameObject>();
  62. }
  63. }
  64. public void abilityTwo() {
  65. if (coolDownTwo) {
  66. boolean find = false;
  67. for (GameObject redObj : gameFrame.objList) {
  68. // 是红色小兵 && 距离小于250 && 存活
  69. if (redObj instanceof MinionRed && recIntersectsCir(redObj.getRec(), getX(), getY(), 250)
  70. && redObj.isAlive()) {
  71. // 添加子弹
  72. abilityTwoBullet = new Bullet(gameFrame, this, redObj, 250, 60, "img/王者荣耀图片/Daji/abilityTwoBullet.png");
  73. gameFrame.objList.add(abilityTwoBullet);
  74. // 给目标赋值
  75. abilityTwoTarget = redObj;
  76. // 释放二技能
  77. ifAbilityTwo = true;
  78. find = true;
  79. break;
  80. }
  81. }
  82. if (find) {
  83. new AbilityTwoCD().start();
  84. find = false;
  85. }
  86. }
  87. }
  88. /**
  89. * 点击技能三释放技能 先将技能范围内目标储存到targetList里 提前定义五个子弹 技能释放时初始化五个子弹 子弹目标从targetList里随机选择
  90. * 如果期间目标死亡,制作一个目标替身,生命值设置为true 子弹与目标或替身碰撞后消失
  91. */
  92. @Override
  93. public void abilityThree() {
  94. if (coolDownThree) {
  95. // 创建列表来储存目标
  96. ArrayList<GameObject> targetList = new ArrayList<GameObject>();
  97. // 遍历redList,找到符合条件的目标,储存到列表里
  98. for (int i = 0; i < gameFrame.objList.size(); i++) {
  99. GameObject target = gameFrame.objList.get(i);
  100. // 是红色小兵 && 在技能范围里 && 存活
  101. if (target instanceof MinionRed && recIntersectsCir(target.getRec(), getX(), getY(), 250)
  102. && target.isAlive()) {
  103. targetList.add(target);
  104. }
  105. }
  106. // 找到目标
  107. if (targetList.size() != 0) {
  108. // 创建五个子弹,随机攻击列表里的目标
  109. Random random = new Random();
  110. int count = 0; // 统计三技能发射子弹数量
  111. while (count < 5) {
  112. int r = random.nextInt(targetList.size());
  113. if (!targetList.get(r).isAlive()) {
  114. GameObject substitute = targetList.get(r);
  115. substitute.setAlive(true);
  116. bulletList[count] = new Bullet(gameFrame, this, substitute, 150, 60,
  117. "img/王者荣耀图片/Daji/abilityTwoBullet.png");
  118. } else {
  119. bulletList[count] = new Bullet(gameFrame, this, targetList.get(r), 150, 60,
  120. "img/王者荣耀图片/Daji/abilityTwoBullet.png");
  121. }
  122. count++;
  123. }
  124. new AbilityThreeBulletCD().start();
  125. // 三技能进入冷却
  126. new AbilityThreeCD().start();
  127. }
  128. }
  129. }
  130. @Override
  131. public void abilityEffect(Graphics g) {
  132. if (ifAbilityOne) {
  133. g.setColor(Color.RED);
  134. g.fillPolygon(p);
  135. abilityOneMove();
  136. step++;
  137. if (step == 10) {
  138. step = 0;
  139. ifAbilityOne = false;
  140. }
  141. }
  142. if (ifAbilityTwo) {
  143. System.out.println(abilityTwoTarget.beControlled);
  144. if (abilityTwoBullet.getRec().intersects(abilityTwoTarget.getRec())) {
  145. new AbilityControllCD().start();
  146. ifAbilityTwo = false;
  147. }
  148. }
  149. }
  150. // 技能一冷却时间
  151. class AbilityOneCD extends Thread {
  152. public void run() {
  153. // 将技能一设置为冷却状态
  154. coolDownOne = false;
  155. // 线程休眠
  156. try {
  157. // one来表示一技能冷却时间
  158. int one = coolDownTimeOne;
  159. while (one > 0) {
  160. Thread.sleep(1000);
  161. System.out.println("一技能冷却时间: " + one / 1000);
  162. one -= 1000;
  163. }
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. }
  167. // 将技能一设置为攻击状态
  168. coolDownOne = true;
  169. // 线程终止
  170. this.interrupt();
  171. }
  172. }
  173. // 技能二冷却时间
  174. class AbilityTwoCD extends Thread {
  175. public void run() {
  176. // 将技能二设置为冷却状态
  177. coolDownTwo = false;
  178. // 线程休眠
  179. try {
  180. // one来表示二技能冷却时间
  181. int two = coolDownTimeTwo;
  182. while (two > 0) {
  183. Thread.sleep(1000);
  184. System.out.println("二技能冷却时间: " + two / 1000);
  185. two -= 1000;
  186. }
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. }
  190. // 将技能二设置为攻击状态
  191. coolDownTwo = true;
  192. // 线程终止
  193. this.interrupt();
  194. }
  195. }
  196. // 技能二控制时间
  197. class AbilityControllCD extends Thread {
  198. public void run() {
  199. abilityTwoTarget.beControlled = true;
  200. // 线程休眠
  201. try {
  202. Thread.sleep(20000);
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. }
  206. abilityTwoTarget.beControlled = false;
  207. this.interrupt();
  208. }
  209. }
  210. //技能三冷却状态
  211. class AbilityThreeCD extends Thread {
  212. public void run() {
  213. // 将攻击功能设置为冷却状态
  214. coolDownThree = false;
  215. // 休眠
  216. try {
  217. int three = coolDownTimeThree;
  218. while (coolDownTimeThree > 0) {
  219. Thread.sleep(1000);
  220. System.out.println("技能三冷却时间: " + coolDownTimeThree / 1000);
  221. coolDownTimeThree -= 1000;
  222. }
  223. coolDownTimeThree = three;
  224. } catch (Exception e) {
  225. e.printStackTrace();
  226. }
  227. // 将攻击功能解除冷却状态
  228. coolDownThree = true;
  229. // 线程终止
  230. this.interrupt();
  231. }
  232. }
  233. class AbilityThreeBulletCD extends Thread {
  234. public void run() {
  235. // 休眠
  236. try {
  237. System.out.println("Thread start");
  238. gameFrame.objList.add(bulletList[0]);
  239. Thread.sleep(100);
  240. gameFrame.objList.add(bulletList[1]);
  241. Thread.sleep(100);
  242. gameFrame.objList.add(bulletList[2]);
  243. Thread.sleep(100);
  244. gameFrame.objList.add(bulletList[3]);
  245. Thread.sleep(100);
  246. gameFrame.objList.add(bulletList[4]);
  247. } catch (Exception e) {
  248. e.printStackTrace();
  249. }
  250. // 线程终止
  251. this.interrupt();
  252. }
  253. }
  254. // 鼠标监视器
  255. private class MouseMonitor extends MouseAdapter {
  256. @Override
  257. public void mousePressed(MouseEvent e) {// 当鼠标点击时
  258. int mouseX = e.getX(), mouseY = e.getY(), playerX = 700, playerY = 350;
  259. double dis = getDis(mouseX, mouseY, playerX, playerY);
  260. // 三角函数
  261. cos = (mouseX - playerX) / dis;
  262. sin = -(mouseY - playerY) / dis;
  263. // 坐标差
  264. int difX = (int) (60 * sin);
  265. int difY = (int) (60 * cos);
  266. p.addPoint(getX() - difX, getY() - difY);
  267. p.addPoint(getX() + difX, getY() + difY);
  268. p.addPoint(getX() + difX + (int) (20 * cos), getY() + difY - (int) (20 * sin));
  269. p.addPoint(getX() - difX + (int) (20 * cos), getY() - difY - (int) (20 * sin));
  270. exit();
  271. new AbilityOneCD().start();
  272. ifAbilityOne = true;
  273. }
  274. }
  275. }

Minion类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. public abstract class Minion extends GameObject{
  5. // 是否生成下一个小兵
  6. private boolean nextMinion = true;
  7. // 是否生成下一波小兵
  8. private boolean nextLine = true;
  9. // 生成小兵数量
  10. private int minionCount = 0;
  11. // 是否检测到目标
  12. private boolean ifFindTarget = false;
  13. public Minion(GameFrame gameFrame) {
  14. super(gameFrame);
  15. setHp(800);
  16. setCurrentHp(getHp());
  17. setDis(100);
  18. setAttackCoolDownTime(2000);
  19. }
  20. /**
  21. * (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)
  22. */
  23. public abstract void move(ArrayList<GameObject> objList);
  24. public void findTarget(ArrayList<GameObject> objList) {
  25. for (GameObject obj : objList) {
  26. if (recIntersectsCir(obj.getRec(), getX(), getY(), 200)) {
  27. setTarget(obj);
  28. setIfFindTarget(true);
  29. }
  30. }
  31. if (objList == gameFrame.blueList) {
  32. if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), 200)) {
  33. setTarget(gameFrame.player);
  34. setIfFindTarget(true);
  35. }
  36. }
  37. }
  38. public void moveToTarget() {
  39. int dis = (int) getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
  40. if(dis!=0) {
  41. int xSpeed = (getSpd() * (getTarget().getX() - getX()) / dis);
  42. int ySpeed = (getSpd() * (getTarget().getY() - getY()) / dis);
  43. setX(getX() + xSpeed);
  44. setY(getY() + ySpeed);
  45. }
  46. }
  47. public void createMinion(GameFrame gameFrame, ArrayList<GameObject> minionList) {
  48. if (nextLine) {
  49. if (nextMinion) {
  50. // 蓝色方小兵
  51. if (minionList == this.gameFrame.blueList) {
  52. MinionBlue mb = new MinionBlue(gameFrame);
  53. gameFrame.objList.add(mb);
  54. minionList.add(mb);
  55. }
  56. // 红色方小兵
  57. else {
  58. MinionRed mr = new MinionRed(gameFrame);
  59. gameFrame.objList.add(mr);
  60. minionList.add(mr);
  61. }
  62. minionCount++;
  63. new NextMinion().start();
  64. }
  65. if (minionCount == 3) {
  66. minionCount = 0;
  67. new NextLine().start();
  68. }
  69. }
  70. }
  71. // 每个小兵生成时间
  72. class NextMinion extends Thread {
  73. public void run() {
  74. nextMinion = false;
  75. // 休眠1.5s
  76. try {
  77. Thread.sleep(1500);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. nextMinion = true;
  82. // 线程终止
  83. this.interrupt();
  84. }
  85. }
  86. // 每波小兵生成时间
  87. class NextLine extends Thread {
  88. public void run() {
  89. nextLine = false;
  90. // 休眠15s
  91. try {
  92. Thread.sleep(15000);
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. nextLine = true;
  97. // 线程终止
  98. this.interrupt();
  99. }
  100. }
  101. @Override
  102. public void paintSelf(Graphics g) {
  103. // 生命值为0
  104. if (getCurrentHp() <= 0) {
  105. setAlive(false);
  106. gameFrame.removeList.add(this);
  107. if (this instanceof MinionBlue) {
  108. gameFrame.blueList.remove(this);
  109. } else {
  110. gameFrame.redList.remove(this);
  111. }
  112. } else {
  113. // 添加生命值
  114. if (this instanceof MinionBlue) {
  115. this.addHp(g, 17, 28, 45, 10, Color.GREEN);
  116. } else {
  117. this.addHp(g, 17, 28, 45, 10, Color.RED);
  118. }
  119. g.drawImage(getImg(), getX() - 16, getY() - 16, null);
  120. g.setColor(Color.RED);
  121. g.fillOval(getX(), getY(), 10, 10);
  122. g.drawRect(getX() - 16, getY() - 16, 45, 45);
  123. g.drawOval(getX() - 200, getY() - 200, 400, 400);
  124. // 小兵移动
  125. if (!beControlled) {
  126. if (this instanceof MinionBlue) {
  127. move(gameFrame.redList);
  128. } else {
  129. move(gameFrame.blueList);
  130. }
  131. }
  132. }
  133. }
  134. @Override
  135. public Rectangle getRec() {
  136. return new Rectangle(getX() - 16, getY() - 16, 45, 45);
  137. }
  138. public boolean isIfFindTarget() {
  139. return ifFindTarget;
  140. }
  141. public void setIfFindTarget(boolean ifFindTarget) {
  142. this.ifFindTarget = ifFindTarget;
  143. }
  144. }

MinionBlue类

  1. package newKingOfHonor;
  2. import java.util.ArrayList;
  3. public class MinionBlue extends Minion{
  4. public MinionBlue(GameFrame gameFrame) {
  5. super(gameFrame);
  6. setImg("img/王者荣耀图片/minion/blue.jpg");
  7. setX(1325);
  8. setY(3750);
  9. }
  10. @Override
  11. public void move(ArrayList<GameObject> objList) {
  12. if(isIfFindTarget()) {
  13. //离开检测范围
  14. if(!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
  15. setIfFindTarget(false);
  16. }else {
  17. if(!isHasTarget()) {
  18. moveToTarget();
  19. }
  20. attack(objList);
  21. }
  22. }else {
  23. findTarget(objList);
  24. //原路线移动
  25. if(getX() < 4425) {
  26. setSpd(5);
  27. setX(getX() + getSpd());
  28. }else if(getX() < 5100 && getX() >= 4425) {
  29. setSpd(20);
  30. setX(getX() + getSpd());
  31. setY(getY() - getSpd());
  32. }else if(getX() >= 4900) {
  33. setSpd(18);
  34. setY(getY() - getSpd());
  35. }
  36. }
  37. }
  38. }

MinionRed类

  1. package newKingOfHonor;
  2. import java.util.ArrayList;
  3. public class MinionRed extends Minion {
  4. public MinionRed(GameFrame gameFrame) {
  5. super(gameFrame);
  6. setImg("img/王者荣耀图片/minion/red.jpg");
  7. setX(5050);
  8. setY(1125);
  9. }
  10. @Override
  11. public void move(ArrayList<GameObject> objList) {
  12. if (isIfFindTarget()) {
  13. // 离开检测范围
  14. if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
  15. setIfFindTarget(false);
  16. } else {
  17. if (!isHasTarget()) {
  18. moveToTarget();
  19. }
  20. attack(objList);
  21. }
  22. } else {
  23. findTarget(objList);
  24. // 原路线移动
  25. if (getY() < 3125) {
  26. setSpd(28);
  27. setY(getY() + getSpd());
  28. } else if (getY() < 3750 && getY() >= 3125) {
  29. setSpd(20);
  30. setY(getY() + getSpd());
  31. setX(getX() - getSpd());
  32. } else if (getY() >= 3750) {
  33. setSpd(25);
  34. setX(getX() - getSpd());
  35. }
  36. }
  37. }
  38. }

Bullet类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. //子弹类
  4. public class Bullet extends GameObject {
  5. //发射子弹的游戏元素
  6. GameObject attacker;
  7. //目标
  8. GameObject target;
  9. //攻击力
  10. int ad;
  11. public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd) {
  12. super(attacker.getX(), attacker.getY(), gameFrame);
  13. this.attacker = attacker;
  14. this.target = target;
  15. setAd(ad);
  16. setSpd(spd);
  17. }
  18. public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd, String img) {
  19. super(attacker.getX(), attacker.getY(), gameFrame);
  20. this.attacker = attacker;
  21. this.target = target;
  22. setImg(img);
  23. setAd(ad);
  24. setSpd(spd);
  25. }
  26. public Bullet() {
  27. super();
  28. }
  29. public void move() {
  30. //子弹与目标碰撞,子弹消失,目标减血
  31. if (recIntersectsRec(getRec(), target.getRec())) {
  32. target.setCurrentHp(target.getCurrentHp() - getAd());
  33. gameFrame.removeList.add(this);
  34. }
  35. int dis = (int) getDis(getX(), getY(), target.getX(), target.getY());
  36. if (dis != 0) {
  37. int xSpeed = (getSpd() * (target.getX() - getX()) / dis);
  38. int ySpeed = (getSpd() * (target.getY() - getY()) / dis);
  39. setX(getX() + xSpeed);
  40. setY(getY() + ySpeed);
  41. }
  42. }
  43. @Override
  44. public void paintSelf(Graphics g) {
  45. g.drawImage(getImg(), getX()-16, getY()-16, null);
  46. g.setColor(Color.BLACK);
  47. g.fillOval(getX()-5, getY()-5, 10, 10);
  48. g.drawRect(getX()-5, getY()-5, 10, 10);
  49. move();
  50. }
  51. @Override
  52. public Rectangle getRec() {
  53. return new Rectangle(getX()-5, getY()-5, 10, 10);
  54. }
  55. public int getAd() {
  56. return ad;
  57. }
  58. public void setAd(int ad) {
  59. this.ad = ad;
  60. }
  61. }

Background类

  1. package newKingOfHonor;
  2. import java.awt.*;
  3. //背景类
  4. public class Background extends GameObject{
  5. public Background(GameFrame gameFrame) {
  6. super(gameFrame);
  7. }
  8. Image bg = Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Map.jpg");
  9. public void paintSelf(Graphics g){
  10. g.drawImage(bg,0,0,null);
  11. }
  12. @Override
  13. public Rectangle getRec() {
  14. return null;
  15. }
  16. }

Beast类

  1. package newKingOfHonor;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Rectangle;
  5. import java.util.ArrayList;
  6. public class Beast extends GameObject {
  7. public ArrayList<Beast> beastList = new ArrayList<>();
  8. int width;
  9. int height;
  10. // 复活的元素
  11. Beast beast = null;
  12. public Beast(GameFrame gameFrame) {
  13. super(gameFrame);
  14. beastList.add(new RedBuff(3045, 3170, gameFrame));
  15. beastList.add(new Bear(2800, 2855, gameFrame));
  16. beastList.add(new Bird(3570, 3380, gameFrame));
  17. beastList.add(new Lizard(4585, 2365, gameFrame));
  18. beastList.add(new BlueBuff(4025, 2295, gameFrame));
  19. beastList.add(new Wolf(4235, 1945, gameFrame));
  20. }
  21. public Beast(int x, int y, GameFrame gameFrame) {
  22. super(x, y, gameFrame);
  23. setHp(1000);
  24. setCurrentHp(getHp());
  25. beast = this;
  26. }
  27. @Override
  28. public void paintSelf(Graphics g) {
  29. if (getCurrentHp() <= 0) {
  30. System.out.println("beast die");
  31. setAlive(false);
  32. gameFrame.removeList.add(this);
  33. gameFrame.beast.beastList.remove(this);
  34. new ReviveCD().start();
  35. } else {
  36. // 添加生命值
  37. addHp(g, width / 2, 80, width, 20, Color.GREEN);
  38. g.drawImage(getImg(), getX() - width / 2, getY() - height / 2, null);
  39. g.setColor(Color.RED);
  40. g.fillOval(getX(), getY(), 10, 10);
  41. g.drawOval(getX() - getDis(), getY() - getDis(), 2 * getDis(), 2 * getDis());
  42. }
  43. }
  44. @Override
  45. public Rectangle getRec() {
  46. return new Rectangle(getX() - width / 2, getY() - height / 2, width, height);
  47. }
  48. //野怪复活
  49. class ReviveCD extends Thread {
  50. public void run() {
  51. // 线程休眠
  52. try {
  53. Thread.sleep(5000);
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. Beast reviveBeast;
  58. if (beast instanceof RedBuff) {
  59. reviveBeast = new RedBuff(3045, 3170, gameFrame);
  60. } else if (beast instanceof Bear) {
  61. reviveBeast = new Bear(2800, 2855, gameFrame);
  62. } else if (beast instanceof Bird) {
  63. reviveBeast = new Bird(3570, 3380, gameFrame);
  64. } else if (beast instanceof Lizard) {
  65. reviveBeast = new Lizard(4585, 2365, gameFrame);
  66. } else if (beast instanceof BlueBuff) {
  67. reviveBeast = new BlueBuff(4025, 2295, gameFrame);
  68. } else {
  69. reviveBeast = new Wolf(4235, 1945, gameFrame);
  70. }
  71. gameFrame.objList.add(reviveBeast);
  72. gameFrame.beast.beastList.add(reviveBeast);
  73. }
  74. }
  75. }

Bear类

  1. package newKingOfHonor;
  2. public class Bear extends Beast {
  3. public Bear(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("img/王者荣耀图片/beast/bear.jpg");
  6. width = 85;
  7. height = 112;
  8. setDis(65);
  9. }
  10. }

Bird类

  1. package newKingOfHonor;
  2. public class Bird extends Beast {
  3. public Bird(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("img/王者荣耀图片/beast/红隼.jpg");
  6. width = 122;
  7. height = 98;
  8. setDis(125);
  9. }
  10. }

BlueBuff类

  1. package newKingOfHonor;
  2. public class BlueBuff extends Beast {
  3. public BlueBuff(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("img/王者荣耀图片/beast/blueBuff.jpg");
  6. width = 142;
  7. height = 176;
  8. setDis(70);
  9. }
  10. }

RedBuff类

  1. package newKingOfHonor;
  2. public class RedBuff extends Beast {
  3. public RedBuff(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("img/王者荣耀图片/beast/redBuff.jpg");
  6. width = 103;
  7. height = 150;
  8. setDis(70);
  9. }
  10. }

Wolf类

  1. package 王者荣耀;
  2. public class Wolf extends Beast {
  3. public Wolf(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("C:\\Users\\czl21\\Desktop\\java\\王者荣耀图片\\beast\\wolf.jpg");
  6. width = 145;
  7. height = 140;
  8. setDis(65);
  9. }
  10. }

Lizard类

  1. package newKingOfHonor;
  2. public class Lizard extends Beast {
  3. public Lizard(int x, int y, GameFrame gameFrame) {
  4. super(x, y, gameFrame);
  5. setImg("img/王者荣耀图片/beast/蜥蜴.jpg");
  6. width = 111;
  7. height = 65;
  8. setDis(125);
  9. }
  10. }

 运行结果

 

主要的代码来源

 00_王者荣耀_宣传片_哔哩哔哩_bilibili

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/98142
推荐阅读
相关标签
  

闽ICP备14008679号