Java实现王者荣耀小游戏

主要功能

键盘W,A,S,D键:控制玩家上下左右移动。按钮一:控制英雄发射一个矩形攻击红方小兵。按钮控制英雄发射魅惑技能,伤害小兵并让小兵停止移动。技能三:攻击多个敌人并让小兵停止移动。普攻:对小兵造成基础伤害。小兵每隔一段时间自动生成,在王者峡谷下路移动,遇到敌人则自动攻击。防御塔可以自动识别敌人进行攻击。野怪分为RedBuff,BlueBuff,普通野怪,死亡后都可以复活。游戏胜利的条件是红方水晶生命值为零,游戏失败条件是蓝方水晶生命值为零。游戏的设计模式为可以偷塔。
游戏流程图 Java实现王者荣耀小游戏_第1张图片

一,主窗口类

作用:初始化变量,窗口的启动方法,键盘监听事件,添加按钮事件,游戏不同状态的变化,播放背景音乐。

 
  
  1. package com.sxt;

  2. import com.sxt.beast.Beast;

  3. import java.awt.*;

  4. import java.awt.event.ActionEvent;

  5. import java.awt.event.ActionListener;

  6. import java.awt.event.KeyAdapter;

  7. import java.awt.event.KeyEvent;

  8. import java.io.File;

  9. import java.util.ArrayList;

  10. import javax.sound.sampled.AudioInputStream;

  11. import javax.sound.sampled.AudioSystem;

  12. import javax.sound.sampled.Clip;

  13. import javax.sound.sampled.FloatControl;

  14. import javax.swing.JButton;

  15. import javax.swing.JFrame;

  16. public class GameFrame extends JFrame {

  17. // 游戏开始 0, 游戏胜利1,游戏失败2

  18. int state = 0;

  19. // 窗口尺寸

  20. final int windowWidth = 1400;

  21. final int windowHeight = 700;

  22. // 双缓冲图片

  23. private Image offScreenImage = null;

  24. // 攻击图片

  25. private Image attack = Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\attack.jpg");

  26. //游戏胜利失败图片

  27. private Image gameWin = Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\gameWin.png");

  28. private Image gameLose = Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\gameLose.png");

  29. // 游戏背景

  30. Background background = new Background(this);

  31. // 游戏玩家

  32. Champion player = new ChampionDaji(this);

  33. // 双方小兵

  34. MinionBlue mb = new MinionBlue(this);

  35. MinionRed mr = new MinionRed(this);

  36. //野怪

  37. public Beast beast = new Beast(this);

  38. // 防御塔

  39. Turret turret = new Turret(this);

  40. // 游戏元素列表

  41. public ArrayList objList = new ArrayList<>();

  42. ArrayList redList = new ArrayList<>();// 红色方

  43. ArrayList blueList = new ArrayList<>();// 蓝色方

  44. public ArrayList removeList = new ArrayList<>();// 存放将要删除的元素

  45. public void launch() {

  46. // 设置尺寸

  47. setSize(windowWidth, windowHeight);

  48. // 窗口居中

  49. setLocationRelativeTo(null);

  50. // 关闭事件

  51. setDefaultCloseOperation(3);

  52. // 用户不能调整窗口大小

  53. setResizable(false);

  54. // 标题

  55. setTitle("王者荣耀");

  56. // 窗口可见

  57. setVisible(true);

  58. // 添加键盘监视器

  59. this.addKeyListener(new GameFrame.KeyMonitor());

  60. // 添加游戏元素

  61. objList.add(background);

  62. objList.add(player);

  63. objList.addAll(beast.beastList);

  64. objList.addAll(turret.turretList);

  65. playMusic();

  66. for (int i = 0; i < 4; i++) {

  67. blueList.add(turret.turretList.get(i));

  68. }

  69. for (int i = 4; i < 8; i++) {

  70. redList.add(turret.turretList.get(i));

  71. }

  72. /**

  73. * 攻击按钮

  74. */

  75. JButton button = new JButton();

  76. button.setSize(130, 132);

  77. button.setLocation(1150, 430);

  78. button.addActionListener(new ActionListener() {

  79. @Override

  80. public void actionPerformed(ActionEvent e) {

  81. // 按钮事件

  82. player.attack(redList);

  83. }

  84. });

  85. this.add(button);

  86. player.addButton();

  87. while (true) {

  88. mb.createMinion(this, blueList);

  89. mr.createMinion(this, redList);

  90. repaint();

  91. try {

  92. Thread.sleep(25);

  93. } catch (Exception e) {

  94. e.printStackTrace();

  95. }

  96. }

  97. }

  98. public void paint(Graphics g) {

  99. if (offScreenImage == null) {

  100. offScreenImage = this.createImage(5984, 4452);

  101. }

  102. Graphics gImage = offScreenImage.getGraphics();

  103. if(state==0){

  104. turret.isLive();

  105. for (int i = 0; i < objList.size(); i++) {

  106. objList.get(i).paintSelf(gImage);

  107. }

  108. // 绘制攻击图片

  109. gImage.drawImage(attack, player.getX() + 500, player.getY() + 100, null);

  110. objList.removeAll(removeList);

  111. } else if (state ==1) {//游戏胜利

  112. gImage.drawImage(gameWin, player.getX()-700, player.getY()-300 , null);

  113. } else if (state == 2) {//游戏失败

  114. gImage.drawImage(gameLose, player.getX()-700, player.getY()-300, null);

  115. }

  116. g.drawImage(offScreenImage, -player.getX() + 700, -player.getY() + 350, null);

  117. // 添加按钮后不能调用键盘事件 因为程序的焦点变成了按钮 this.requestFocus() 把焦点重新改变到游戏界面上

  118. this.requestFocus();

  119. }

  120. // main方法

  121. public static void main(String[] args) {

  122. GameFrame gameFrame = new GameFrame();

  123. gameFrame.launch();

  124. }

  125. // 键盘事件

  126. private class KeyMonitor extends KeyAdapter {

  127. @Override

  128. public void keyPressed(KeyEvent e) {

  129. int key = e.getKeyCode();

  130. player.keyPressed(e);

  131. }

  132. @Override

  133. public void keyReleased(KeyEvent e) {

  134. int key = e.getKeyCode();

  135. player.keyReleased(e);

  136. }

  137. }

  138. static Clip clip;

  139. public static void playMusic() {

  140. try

  141. {

  142. //这里面放 绝对路径,音频必须是wav格式,用音频转换软件 把mp3 转成wav格式

  143. File musicPath = new File("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\PlayMusic\\bgm.wav");

  144. if(musicPath.exists())

  145. {

  146. AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);

  147. clip = AudioSystem.getClip();

  148. clip.open(audioInput);

  149. FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);

  150. gainControl.setValue(-20.0f);//设置音量,范围为 -60.0f 到 6.0f

  151. clip.start();

  152. clip.loop(Clip.LOOP_CONTINUOUSLY);

  153. }

  154. else

  155. {

  156. }

  157. }

  158. catch(Exception ex)

  159. {

  160. ex.printStackTrace();

  161. }

  162. }

  163. }

二,游戏元素父类

作用:初始化基本变量,定义addHp()方法,两点之间的距离方法,矩形与矩形的碰撞检测方法,矩形与圆形的碰撞检测方法,定义防御塔,英雄,小兵的攻击方法,用线程实现攻击的冷却时间。

 
  
  1. package com.sxt;

  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 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. }

三,防御塔类

作用:初始化防御塔并添加到窗口中,游戏失败与胜利方法,在绘制自身方法中通过判断血量删除元素或者绘制防御塔。

 
  
  1. package com.sxt;

  2. import java.awt.*;

  3. import java.util.ArrayList;

  4. //防御塔类

  5. public class Turret extends GameObject{

  6. ArrayList 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("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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. }

1.蓝色防御塔

作用:有参构造。

 
  
  1. package com.sxt;

  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. }

2.红方防御塔

作用:有参构造。

 
  
  1. package com.sxt;

  2. //蓝方防御塔

  3. public class TurretRed extends Turret{

  4. //有参构造

  5. public TurretRed(GameFrame gameFrame){

  6. super(gameFrame);

  7. }

  8. public TurretRed(int x,int y,GameFrame gameFrame){

  9. super(x,y,gameFrame);

  10. }

  11. }

四,英雄类

作用:实现键盘监听,添加按钮,鼠标监听。

 
  
  1. package com.sxt;

  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] = "C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\move\\" + i + ".png";

  30. }

  31. }

  32. public Champion(GameFrame gameFrame) {

  33. super(gameFrame);

  34. //定义英雄的图片和坐标

  35. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\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("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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. }

1.英雄妲己

作用:定义三个技能的具体实现方法,用线程实现技能的冷却时间,控制时间,鼠标监视器实现释放技能一,定义技能效果。

 
  
  1. package com.sxt;

  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 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("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\Daji\\abilityOne.jpg");

  34. abilityTwo = Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\Daji\\abilityTwo.jpg");

  35. abilityThree = Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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();

  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, "C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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 targetList = new ArrayList();

  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. "C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\Daji\\abilityTwoBullet.png");

  118. } else {

  119. bulletList[count] = new Bullet(gameFrame, this, targetList.get(r), 150, 60,

  120. "C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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. }

五,小兵类

作用:初始化小兵的基本参数,实现找到目标方法,向目标移动方法。每波小兵生成的时间,每个小兵生成的时间,在绘制自身方法中通过判断血量是否为零实现删除元素功能和绘制小兵功能。

 
  
  1. package com.sxt;

  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 objList);

  24. public void findTarget(ArrayList 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 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. }

1.红方小兵

作用:具体的红方小兵移动方法。 

 
  
  1. package com.sxt;

  2. import java.util.ArrayList;

  3. public class MinionRed extends Minion {

  4. public MinionRed(GameFrame gameFrame) {

  5. super(gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\minion\\red.jpg");

  7. setX(5050);

  8. setY(1125);

  9. }

  10. @Override

  11. public void move(ArrayList 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. }

2.蓝方小兵

作用:具体的蓝方小兵移动方法。

 
  
  1. package com.sxt;

  2. import java.util.ArrayList;

  3. public class MinionBlue extends Minion{

  4. public MinionBlue(GameFrame gameFrame) {

  5. super(gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\minion\\blue.jpg");

  7. setX(1325);

  8. setY(3750);

  9. }

  10. @Override

  11. public void move(ArrayList 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. }

六,子弹类

作用:子弹移动方法。

 
  
  1. package com.sxt;

  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. }

七,背景类

作用:初始化背景。

 
  
  1. package com.sxt;

  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("C:\\Users\\23839\\IdeaProjects\\asd\\src\\imgs\\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. }

八,野怪类

作用:在绘制自身方法中通过判断血量删除元素或者绘制自身,用线程实现自动复活功能。

 
  
  1. package com.sxt.beast;

  2. import java.awt.Color;

  3. import java.awt.Graphics;

  4. import java.awt.Rectangle;

  5. import java.util.ArrayList;

  6. import com.sxt.*;

  7. public class Beast extends GameObject {

  8. public ArrayList beastList = new ArrayList<>();

  9. int width;

  10. int height;

  11. // 复活的元素

  12. Beast beast = null;

  13. public Beast(GameFrame gameFrame) {

  14. super(gameFrame);

  15. beastList.add(new RedBuff(3045, 3170, gameFrame));

  16. beastList.add(new Bear(2800, 2855, gameFrame));

  17. beastList.add(new Bird(3570, 3380, gameFrame));

  18. beastList.add(new Xiyi(4585, 2365, gameFrame));

  19. beastList.add(new BlueBuff(4025, 2295, gameFrame));

  20. beastList.add(new Wolf(4235, 1945, gameFrame));

  21. }

  22. public Beast(int x, int y, GameFrame gameFrame) {

  23. super(x, y, gameFrame);

  24. setHp(1000);

  25. setCurrentHp(getHp());

  26. beast = this;

  27. }

  28. @Override

  29. public void paintSelf(Graphics g) {

  30. if (getCurrentHp() <= 0) {

  31. System.out.println("beast die");

  32. setAlive(false);

  33. gameFrame.removeList.add(this);

  34. gameFrame.beast.beastList.remove(this);

  35. new ReviveCD().start();

  36. } else {

  37. // 添加生命值

  38. addHp(g, width / 2, 80, width, 20, Color.GREEN);

  39. g.drawImage(getImg(), getX() - width / 2, getY() - height / 2, null);

  40. g.setColor(Color.RED);

  41. g.fillOval(getX(), getY(), 10, 10);

  42. g.drawOval(getX() - getDis(), getY() - getDis(), 2 * getDis(), 2 * getDis());

  43. }

  44. }

  45. @Override

  46. public Rectangle getRec() {

  47. return new Rectangle(getX() - width / 2, getY() - height / 2, width, height);

  48. }

  49. //野怪复活

  50. class ReviveCD extends Thread {

  51. public void run() {

  52. // 线程休眠

  53. try {

  54. Thread.sleep(5000);

  55. } catch (Exception e) {

  56. e.printStackTrace();

  57. }

  58. Beast reviveBeast;

  59. if (beast instanceof RedBuff) {

  60. reviveBeast = new RedBuff(3045, 3170, gameFrame);

  61. } else if (beast instanceof Bear) {

  62. reviveBeast = new Bear(2800, 2855, gameFrame);

  63. } else if (beast instanceof Bird) {

  64. reviveBeast = new Bird(3570, 3380, gameFrame);

  65. } else if (beast instanceof Xiyi) {

  66. reviveBeast = new Xiyi(4585, 2365, gameFrame);

  67. } else if (beast instanceof BlueBuff) {

  68. reviveBeast = new BlueBuff(4025, 2295, gameFrame);

  69. } else {

  70. reviveBeast = new Wolf(4235, 1945, gameFrame);

  71. }

  72. gameFrame.objList.add(reviveBeast);

  73. gameFrame.beast.beastList.add(reviveBeast);

  74. }

  75. }

  76. }

1.熊类
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class Bear extends Beast {

  4. public Bear(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\bear.jpg");

  7. width = 85;

  8. height = 112;

  9. setDis(65);

  10. }

  11. }

2.鸟类
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class Bird extends Beast {

  4. public Bird(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\红隼.jpg");

  7. width = 122;

  8. height = 98;

  9. setDis(125);

  10. }

  11. }

3.蓝Buff
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class BlueBuff extends Beast {

  4. public BlueBuff(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\blueBuff.jpg");

  7. width = 142;

  8. height = 176;

  9. setDis(70);

  10. }

  11. }

4.红Buff
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class RedBuff extends Beast {

  4. public RedBuff(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\redBuff.jpg");

  7. width = 103;

  8. height = 150;

  9. setDis(70);

  10. }

  11. }

5.狼类
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class Wolf extends Beast {

  4. public Wolf(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\wolf.jpg");

  7. width = 145;

  8. height = 140;

  9. setDis(65);

  10. }

  11. }

6.蜥蜴类
 
  
  1. package com.sxt.beast;

  2. import com.sxt.GameFrame;

  3. public class Xiyi extends Beast {

  4. public Xiyi(int x, int y, GameFrame gameFrame) {

  5. super(x, y, gameFrame);

  6. setImg("C:\\Users\\23839\\IdeaProjects\\HonorOfKings\\src\\imgs\\beast\\蜥蜴.jpg");

  7. width = 111;

  8. height = 65;

  9. setDis(125);

  10. }

  11. }

 作用:这几个野怪类初始化野怪具体图片与坐标。

图片资源Java实现王者荣耀小游戏_第2张图片Java实现王者荣耀小游戏_第3张图片Java实现王者荣耀小游戏_第4张图片

 

Java实现王者荣耀小游戏_第5张图片

 Java实现王者荣耀小游戏_第6张图片Java实现王者荣耀小游戏_第7张图片Java实现王者荣耀小游戏_第8张图片

Java实现王者荣耀小游戏_第9张图片

游戏效果图Java实现王者荣耀小游戏_第10张图片

 Java实现王者荣耀小游戏_第11张图片Java实现王者荣耀小游戏_第12张图片Java实现王者荣耀小游戏_第13张图片Java实现王者荣耀小游戏_第14张图片

你可能感兴趣的:(java)