一.准备工作
首先创建一个新的Java项目命名为“王者荣耀”,并在src下创建两个包分别命名为“com.sxt"、”com.stx.beast",在相应的包中创建所需的类。
创建一个名为“img”的文件夹来储存所需的图片素材。
二.代码呈现
package com.sxt;
import javax.swing.*;
import java.awt.*;
public class Background extends GameObject {
public Background(GameFrame gameFrame) {
super(gameFrame);
// TODO Auto-generated constructor stub
}
Image bg = Toolkit.getDefaultToolkit().getImage("img/Map.jpg");
public void paintSelf(Graphics g){
g.drawImage(bg,0,0,null);
}
@Override
public Rectangle getRec() {
// TODO Auto-generated method stub
return null;
}
}
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import com.sxt.beast.Beast;
public class Bullet extends GameObject {
// 发射子弹的游戏元素
GameObject attacker;
// 目标
GameObject target;
// 攻击力
int ad;
public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd) {
super(attacker.getX(), attacker.getY(), gameFrame);
this.attacker = attacker;
this.target = target;
setAd(ad);
setSpd(spd);
// TODO Auto-generated constructor stub
}
public Bullet(GameFrame gameFrame, GameObject attacker, GameObject target, int ad, int spd, String img) {
super(attacker.getX(), attacker.getY(), gameFrame);
this.attacker = attacker;
this.target = target;
setImg(img);
setAd(ad);
setSpd(spd);
// TODO Auto-generated constructor stub
}
public Bullet() {
super();
// TODO Auto-generated constructor stub
}
public void move() {
// 子弹与目标碰撞,子弹消失,目标减血
if (recIntersectsRec(getRec(), target.getRec())) {
if (Beast.class.isAssignableFrom(target.getClass())) {
target.setTarget(gameFrame.player);
target.setHasTarget(true);
}
target.setCurrentHp(target.getCurrentHp() - getAd());
gameFrame.removeList.add(this);
}
double dis = getDis(getX(), getY(), target.getX(), target.getY());
int xSpeed = (int) (getSpd() * (target.getX() - getX()) / dis);
int ySpeed = (int) (getSpd() * (target.getY() - getY()) / dis);
setX(getX() + xSpeed);
setY(getY() + ySpeed);
}
@Override
public void paintSelf(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(getImg(), getX() - 16, getY() - 16, null);
if (this.getImg() == null) {
g.setColor(Color.BLACK);
g.fillOval(getX() - 5, getY() - 5, 10, 10);
g.drawRect(getX() - 5, getY() - 5, 10, 10);
}
move();
}
@Override
public Rectangle getRec() {
// TODO Auto-generated method stub
return new Rectangle(getX() - 5, getY() - 5, 10, 10);
}
/**
* @return the ad
package com.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;
import com.sxt.beast.Beast;
public class ChampionDaji extends Champion {
// 技能是否处于释放状态
boolean ifAbilityOne = false;
boolean ifAbilityTwo = false;
// 鼠标监视器
MouseMonitor m;
// 一技能多边形
Polygon p;
// 一技能三角函数
double sin;
double cos;
// 一技能已经攻击过的目标
ArrayList
// 一技能移动次数
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("img/abilityOne.jpg");
abilityTwo = Toolkit.getDefaultToolkit().getImage("img/abilityTwo.jpg");
abilityThree = Toolkit.getDefaultToolkit().getImage("img/abilityThree.jpg");
classical = Toolkit.getDefaultToolkit().getImage("img/Classical.jpg");
// 三个技能冷却时间
coolDownTimeOne = 6000;
coolDownTimeTwo = 8000;
coolDownTimeThree = 8000;
}
public void exit() {
this.gameFrame.removeMouseListener(m);
}
public void abilityOneMove() {
p.translate((int) (50 * cos), -(int) (50 * sin));
for (GameObject redObj : gameFrame.objList) {
// 是红色方小兵 && 发生碰撞 && 没在attacked列表里
if (redObj instanceof MinionRed && p.intersects(redObj.getRec()) && attacked.indexOf(redObj) == -1) {
// 小兵扣血,添加到attacked里
redObj.setCurrentHp(redObj.getCurrentHp() - 400);
attacked.add(redObj);
}else if (Beast.class.isAssignableFrom(redObj.getClass()) && p.intersects(redObj.getRec()) && attacked.indexOf(redObj) == -1) {
redObj.setCurrentHp(redObj.getCurrentHp() - 400);
attacked.add(redObj);
}
}
}
@Override
public void abilityOne() {
// TODO Auto-generated method stub
if (coolD
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import com.sxt.beast.Beast;
public class ChampionHouyi extends Champion {
// 技能是否处于释放状态
boolean ifAbilityThree = false;
boolean ifAbilityTwo = false;
// 鼠标监视器
MouseMonitor m;
MouseMonitorTwo m2;
// 三技能多边形
Polygon p;
// 三技能三角函数
double sin;
double cos;
// 三技能命中的目标
GameObject abilityThreeTarget;
// 三技能是否出界
boolean ifXOutside;
boolean ifYOutside;
// 二技能位置
int X_AbilityTwo;
int Y_AbilityTwo;
public ChampionHouyi(GameFrame gameFrame) {
super(gameFrame);
abilityOne = Toolkit.getDefaultToolkit().getImage("img/HyabilityOne.jpg");
abilityTwo = Toolkit.getDefaultToolkit().getImage("img/HyabilityTwo.jpg");
abilityThree = Toolkit.getDefaultToolkit().getImage("img/HyabilityThree.jpg");
classical = Toolkit.getDefaultToolkit().getImage("img/后羿-辉光之辰.jpg");
// 三个技能冷却时间
coolDownTimeOne = 14000;
coolDownTimeTwo = 10000;
coolDownTimeThree = 28000;
// TODO Auto-generated constructor stub
}
public ChampionHouyi(GameFrame gameFrame, int i, int j) {
// TODO Auto-generated constructor stub
super(gameFrame, i, j);
}
public void exit(MouseAdapter ma) {
this.gameFrame.removeMouseListener(ma);
}
public void attack() {
if (isAttackCoolDown()) {
ArrayList
for (GameObject redObj : this.gameFrame.redList) {
if (this.recIntersectsCir(redObj.getRec(), getX() - 250, getY() - 250, 500)) {
targets.add(redObj);
if (targets.size() == 3) {
break;
}
}
}
for (GameObject beastObj : this.gameFrame.beast.beastList) {
if (this.recIntersectsCir(beastObj.getRec(), getX() - 250, getY() - 250, 500)) {
package com.sxt;
import java.util.ArrayList;
public class MinionBlue extends Minion {
public MinionBlue(GameFrame gameFrame) {
super(gameFrame);
setImg("img/blue.jpg");
setX(1325);
setY(3750);
// TODO Auto-generated constructor stub
}
@Override
public void move(ArrayList
// TODO Auto-generated method stub
if (isIfFindTarget()) {
// 离开检测范围
if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
setIfFindTarget(false);
} else {
if (!isHasTarget()) {
moveToTarget();
}
attack(objList);
}
} else {
findTarget(objList);
// 原路线移动
if (getX() < 4425) {
setSpd(25);
if (!hitMinion(getX() + getSpd(), getY(), gameFrame.blueList)) {
setX(getX() + getSpd());
}
} else if (getX() < 5100 && getX() >= 4425) {
setSpd(20);
if (!hitMinion(getX() + getSpd(), getY(), gameFrame.blueList)) {
setX(getX() + getSpd());
}
if (!hitMinion(getX(), getY() - getSpd(), gameFrame.blueList)) {
setY(getY() - getSpd());
}
} else if (getX() >= 4900) {
setSpd(18);
if (!hitMinion(getX(), getY() - getSpd(), gameFrame.blueList)) {
setY(getY() - getSpd());
}
}
}
}
}
package com.sxt;
import java.util.ArrayList;
public class MinionRed extends Minion {
public MinionRed(GameFrame gameFrame) {
super(gameFrame);
setImg("img/red.jpg");
setX(5050);
setY(1125);
// TODO Auto-generated constructor stub
}
@Override
public void move(ArrayList
// TODO Auto-generated method stub
if (isIfFindTarget()) {
// 离开检测范围
if (!recIntersectsCir(getTarget().getRec(), getX(), getY(), 200)) {
setIfFindTarget(false);
} else {
if (!isHasTarget()) {
moveToTarget();
}
attack(objList);
}
} else {
findTarget(objList);
// 原路线移动
if (getY() < 3125) {
setSpd(18);
if (!hitMinion(getX(), getY() + getSpd(), gameFrame.redList)) {
setY(getY() + getSpd());
}
} else if (getY() < 3750 && getY() >= 3125) {
setSpd(20);
if (!hitMinion(getX(), getY() + getSpd(), gameFrame.redList)) {
setY(getY() + getSpd());
}
if (!hitMinion(getX() - getSpd(), getY(), gameFrame.redList)) {
setX(getX() - getSpd());
}
} else if (getY() >= 3750) {
setSpd(25);
if (!hitMinion(getX() - getSpd(), getY(), gameFrame.redList)) {
setX(getX() - getSpd());
}
}
}
}
}
三.结果呈现