一.准备工作
首先创建一个新的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
*/
public int getAd() {
return ad;
}
/**
* @param ad the ad to set
*/
public void setAd(int ad) {
this.ad = ad;
}
}
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
public abstract class Champion extends GameObject {
// 移动
public boolean up, down, left, right;
// 移动图集
static String[] imgs = new String[8];
// 第几张图片
int moveCount = 1;
//技能图片
Image abilityOne;
Image abilityTwo;
Image abilityThree;
//英雄头像
Image classical;
//技能冷却时间
int coolDownTimeOne;
int coolDownTimeTwo;
int coolDownTimeThree;
//三个技能是否处于冷却状态
boolean coolDownOne = true;
boolean coolDownTwo = true;
boolean coolDownThree = true;
//英雄列表
ArrayList<Champion> championList = new ArrayList<Champion>();
static {
for (int i = 1; i < 8; i++) {
imgs[i] = "img/" + i + ".png";
}
}
public Champion(GameFrame gameFrame, int x, int y) {
super(gameFrame);
setImg("img/stand.png");
setX(x);
setY(y);
setSpd(75);
setHp(24000);
setDis(250);
setAttackCoolDownTime(100);
setCurrentHp(getHp());
championList.add(new ChampionDaji(gameFrame));
championList.add(new ChampionHouyi(gameFrame));
}
public Champion(GameFrame gameFrame) {
// TODO Auto-generated constructor stub
super(gameFrame);
setImg("img/stand.png");
setX(700);
setY(3800);
setSpd(75);
setHp(24000);
setDis(250);
setAttackCoolDownTime(100);
setCurrentHp(getHp());
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_D) {
right = true;
}
if (key == KeyEvent.VK_A) {
left = true;
}
if (key == KeyEvent.VK_W) {
up = true;
}
if (key == KeyEvent.VK_S) {
down = true;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_D) {
right = false;
}
if (key == KeyEvent.VK_A) {
left = false;
}
if (key == KeyEvent.VK_W) {
up = false;
}
if (key == KeyEvent.VK_S) {
down = false;
}
}
public void move() {
if (up) {
setY(getY() - getSpd());
}
if (down) {
setY(getY() + getSpd());
}
if (left) {
setX(getX() - getSpd());
}
if (right) {
setX(getX() + getSpd());
}
if (up || down || left || right) {
setImg(imgs[moveCount]);
moveCount++;
if (moveCount == 8) {
moveCount = 1;
}
} else {
setImg("img/stand.png");
}
}
/**
* 添加三个技能按钮
* */
public void addButton() {
JButton button1 = new JButton();
button1.setSize(100, 100);
button1.setLocation(1056, 513);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
abilityOne();
}
});
JButton button2 = new JButton();
button2.setSize(100, 100);
button2.setLocation(1090, 370);
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
abilityTwo();
}
});
JButton button3 = new JButton();
button3.setSize(100, 100);
button3.setLocation(1220, 300);
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
abilityThree();
}
});
gameFrame.add(button1);
gameFrame.add(button2);
gameFrame.add(button3);
}
public abstract void abilityOne();
public abstract void abilityTwo();
public abstract void abilityThree();
public abstract void abilityEffect(Graphics g);
@Override
public void paintSelf(Graphics g) {
// TODO Auto-generated method stub
// 生命值为0
if (getCurrentHp() <= 0) {
setAlive(false);
gameFrame.removeList.add(this);
} else {
// 添加生命值
addHp(g, 30, 80, 80, 20, Color.GREEN);
//绘制技能图片
g.drawImage(abilityOne, getX() + 360, getY() + 180, null);
g.drawImage(abilityTwo, getX() + 400, getY() + 40, null);
g.drawImage(abilityThree, getX() + 520, getY() - 30, null);
// 绘制图片
g.drawImage(this.getImg(), getX() - 33, getY() - 50, null);
// 改变画笔颜色
//g.setColor(Color.GREEN);
// 绘制中心圆点
//g.fillOval(getX(), getY(), 10, 10);
// 绘制矩形边框
//g.drawRect(getX() - 23, getY() - 50, 60, 120);
move();
abilityEffect(g);
}
}
@Override
public Rectangle getRec() {
// TODO Auto-generated method stub
return new Rectangle(getX() - 30, getY() - 60, 60, 120);
}
}
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Toolkit;
import java.awt.event.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<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("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 (coolDownOne) {
m = new MouseMonitor();
p = new Polygon();
gameFrame.addMouseListener(m);
attacked = new ArrayList<GameObject>();
}
}
@Override
public void abilityTwo() {
// TODO Auto-generated method stub
if (coolDownTwo) {
boolean find = false;
for (GameObject redObj : gameFrame.objList) {
// 是红色小兵 && 距离小于250 && 存活
if ((redObj instanceof MinionRed || Beast.class.isAssignableFrom(redObj.getClass())) && recIntersectsCir(redObj.getRec(), getX(), getY(), 250)
&& redObj.isAlive()) {
// 添加子弹
abilityTwoBullet = new Bullet(gameFrame, this, redObj, 250, 60, "img/abilityTwoBullet.png");
gameFrame.objList.add(abilityTwoBullet);
// 给目标赋值
abilityTwoTarget = redObj;
// 释放二技能
ifAbilityTwo = true;
find = true;
break;
}
}
if (find) {
new AbilityTwoCD().start();
find = false;
}
}
}
/**
* 点击技能三释放技能 先将技能范围内目标储存到targetList里 提前定义五个子弹 技能释放时初始化五个子弹 子弹目标从targetList里随机选择
* 如果期间目标死亡,制作一个目标替身,生命值设置为true 子弹与目标或替身碰撞后消失
*/
@Override
public void abilityThree() {
// TODO Auto-generated method stub
// 三技能没在冷却状态
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 || Beast.class.isAssignableFrom(target.getClass())) && recIntersectsCir(target.getRec(), getX(), getY(), 250)
&& target.isAlive()) {
targetList.add(target);
}
}
//找到目标
if(targetList.size() != 0) {