一.准备工作
首先创建一个新的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 championList = new ArrayList();
static {
for (int i = 1; i
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 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();
}
}
@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 targetList = new ArrayList();
// 遍历redList
for (int i = 0; i 0) {
Thread.sleep(1000);
System.out.println("一技能冷却时间: " + one / 1000);
one -= 1000;
}
} catch (Exception e) {
e.printStackTrace();
}
// 将技能一设置为攻击状态
coolDownOne = true;
// 线程终止
this.stop();
}
}
// 技能二冷却时间
class AbilityTwoCD extends Thread {
public void run() {
// 将技能二设置为冷却状态
coolDownTwo = false;
// 线程休眠
try {
// one来表示二技能冷却时间
int two = coolDownTimeTwo;
while (two > 0) {
Thread.sleep(1000);
System.out.println("二技能冷却时间: " + two / 1000);
two -= 1000;
}
} catch (Exception e) {
e.printStackTrace();
}
// 将技能二设置为攻击状态
coolDownTwo = true;
// 线程终止
this.stop();
}
}
// 技能二控制时间
class AbilityControllCD extends Thread {
public void run() {
abilityTwoTarget.beControlled = true;
// 线程休眠
try {
Thread.sleep(20000);
} catch (Exception e) {
e.printStackTrace();
}
abilityTwoTarget.beControlled = false;
this.stop();
}
}
// 技能三冷却时间
class AbilityThreeCD extends Thread {
public void run() {
// 将技能三设置为冷却状态
coolDownThree = false;
// 线程休眠
try {
// three来表示三技能冷却时间
int three = coolDownTimeThree;
while (three > 0) {
Thread.sleep(1000);
System.out.println("三技能冷却时间: " + three / 1000);
three -= 1000;
}
} catch (Exception e) {
e.printStackTrace();
}
// 将技能三设置为攻击状态
coolDownThree = true;
// 线程终止
this.stop();
}
}
//技能三子弹时间间隔
class AbilityThreeBulletCD extends Thread{
public void run() {
try {
gameFrame.objList.add(bulletList[0]);
Thread.sleep(200);
gameFrame.objList.add(bulletList[1]);
Thread.sleep(200);
gameFrame.objList.add(bulletList[2]);
Thread.sleep(200);
gameFrame.objList.add(bulletList[3]);
Thread.sleep(200);
gameFrame.objList.add(bulletList[4]);
}catch(Exception e) {
e.printStackTrace();
}
this.stop();
}
}
// 鼠标监视器
private class MouseMonitor extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {// 当鼠标点击时
int mouseX = e.getX(), mouseY = e.getY(), playerX = 700, playerY = 350;
double dis = getDis(mouseX, mouseY, playerX, playerY);
// 三角函数
cos = (mouseX - playerX) / dis;
sin = -(mouseY - playerY) / dis;
// 坐标差
int difX = (int) (60 * sin);
int difY = (int) (60 * cos);
p.addPoint(getX() - difX, getY() - difY);
p.addPoint(getX() + difX, getY() + difY);
p.addPoint(getX() + difX + (int) (20 * cos), getY() + difY - (int) (20 * sin));
p.addPoint(getX() - difX + (int) (20 * cos), getY() - difY - (int) (20 * sin));
exit();
new AbilityOneCD().start();
ifAbilityOne = true;
}
}
}
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 targets = new 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)) {
targets.add(beastObj);
if (targets.size() == 3) {
break;
}
}
}
for (int i = 0; i = 5165) {
ifXOutside = true;
break;
}
}
}
if (!ifYOutside) {
for (int y : p.ypoints) {
if (y = 4085) {
ifYOutside = true;
break;
}
}
}
}
/**
* 一技能 制作游戏界面攻击按钮的替身 替换攻击按钮 新的攻击按钮事件: 1. 定义目标列表 2. 添加目标,最多添加三个 3.
* 目标列表第一个造成400伤害,其余200伤害 4. 持续5秒,结束后替换攻击按钮️
*/
@Override
public void abilityOne() {
// TODO Auto-generated method stub
if (coolDownOne) {
new AbilityOneDuration().start();// 强化普攻持续时间
new AbilityOneCD().start();
}
}
/**
* 二技能 先点击按钮释放技能 再点击技能范围内任意位置 之后点击的位置会出现 两个大小不同的圆 在大圆内会受到300伤害 在小圆内受到额外200伤害服务器托管网
*/
@Override
public void abilityTwo() {
// TODO Auto-generated method stub
if (coolDownTwo) {
m2 = new MouseMonitorTwo();
gameFrame.addMouseListener(m2);
X_AbilityTwo = 0;
Y_AbilityTwo = 0;
}
}
@Override
public void abilityThree() {
// TODO Auto-generated method stub
if (coolDownThree) {
m = new MouseMonitor();
p = new Polygon();
gameFrame.addMouseListener(m);
ifAbilityThree = true;
ifXOutside = false;
ifYOutside = false;
}
}
@Override
public void abilityEffect(Graphics g) {
// TODO Auto-generated method stub
if (ifAbilityTwo) {
System.out.println("222");
g.setColor(Color.RED);
g.fillOval(X_AbilityTwo - 60, Y_AbilityTwo - 60, 120, 120);
g.setColor(Color.BLACK);
g.fillOval(X_AbilityTwo - 30, Y_AbilityTwo - 30, 60, 60);
abilityTwoAttack();
X_AbilityTwo = 0;
Y_AbilityTwo = 0;
ifAbilityTwo = false;
new AbilityTwoCD().start();
}
if (ifAbilityThree) {
System.out.println("333");
g.setColor(Color.RED);
g.fillPolygon(p);
abilityThreeMove();
// 如果出界,技能释放结束
if (ifXOutside || ifYOutside) {
ifAbilityThree = false;
p = new Polygon();
new AbilityThreeCD().start();
}
}
}
// 攻击冷却时间
class AttackCD extends Thread {
public void run() {
// 将攻击功能设置为冷却状态
setAttackCoolDown(false);
// 线程休眠
try {
Thread.sleep(getAttackCoolDownTime());
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能设置为攻击状态
setAttackCoolDown(true);
// 线程终止
this.stop();
}
}
// 一技能冷却时间
class AbilityOneCD extends Thread {
public void run() {
// 将攻击功能设置为冷却状态
coolDownOne = false;
// 休眠
try {
// one用来储存冷却时间
int one = coolDownTimeOne;
while (coolDownTimeOne > 0) {
Thread.sleep(1000);
System.out.println("技能一冷却时间: " + coolDownTimeOne / 1000);
coolDownTimeOne -= 1000;
}
coolDownTimeOne = one;
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能解除冷却状态
coolDownOne = true;
// 线程终止
this.stop();
}
}
// 技能一持续时间
class AbilityOneDuration extends Thread {
public void run() {
// 制作攻击按钮的替身
JButton substitute = gameFrame.attackButton;
gameFrame.remove(gameFrame.attackButton);
JButton button = new JButton();
button.setSize(130, 132);
button.setLocation(1150, 430);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取到的事件源就是按钮本身
attack();
}
});
gameFrame.add(button);
// 休眠
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
gameFrame.remove(button);
gameFrame.add(substitute);
// 线程终止
this.stop();
}
}
// 二技能冷却时间
class AbilityTwoCD extends Thread {
public void run() {
// 将二技能设置为冷却状态
coolDownTwo = false;
// 休眠
try {
int two = coolDownTimeTwo;
while (coolDownTimeTwo > 0) {
Thread.sleep(1000);
System.out.println("技能二冷却时间: " + coolDownTimeTwo / 1000);
coolDownTimeTwo -= 1000;
}
coolDownTimeTwo = two;
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能解除冷却状态
coolDownTwo = true;
// 线程终止
this.stop();
}
}
// 技能三冷却时间
class AbilityThreeCD extends Thread {
public void run() {
// 将技能三设置为冷却状态
coolDownThree = false;
// 线程休眠
try {
// three来表示三技能冷却时间
int three = coolDownTimeThree;
while (three > 0) {
Thread.sleep(1000);
System.out.println("三技能冷却时间: " + three / 1000);
three -= 1000;
}
} catch (Exception e) {
e.printStackTrace();
}
// 将技能三设置为攻击状态
coolDownThree = true;
// 线程终止
this.stop();
}
}
// 技能三控制时间
class AbilityControlCD extends Thread {
public void run() {
abilityThreeTarget.beControlled = true;
// 线程休眠
try {
Thread.sleep服务器托管网(20000);
} catch (Exception e) {
e.printStackTrace();
}
abilityThreeTarget.beControlled = false;
this.stop();
}
}
// 鼠标监视器
private class MouseMonitorTwo extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {// 当鼠标点击时
System.out.println("pressed");
int mouseX = e.getX(), mouseY = e.getY(), playerX = 700, playerY = 350;
double dis = getDis(mouseX, mouseY, playerX, playerY);
if (dis
package com.sxt;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import com.sxt.beast.Beast;
public class GameFrame extends JFrame {
// 游戏模式 0 选择英雄,1 游戏开始,2 游戏胜利,3 游戏失败
int state = 0;
// 窗口尺寸
private int windowWidth = 1400;
private int windowHeight = 700;
// 双缓冲图片
private Image offScreenImage = null;
// 攻击图片
private Image attack = Toolkit.getDefaultToolkit().getImage("img/attack.jpg");
//游戏胜利失败图片
private Image gameWin = Toolkit.getDefaultToolkit().getImage("img/gameWin.png");
private Image gameLose = Toolkit.getDefaultToolkit().getImage("img/gameLose.png");
// 游戏背景
Background background = new Background(this);
// 游戏玩家
Champion player;
Champion champion = new ChampionHouyi(this,700,3800);
// 双方小兵
MinionBlue mb = new MinionBlue(this);
MinionRed mr = new MinionRed(this);
// 防御塔
Turret turret = new Turret(this);
//野怪
public Beast beast = new Beast(this);
// 攻击按钮
JButton attackButton;
// 游戏元素列表
public ArrayList objList = new ArrayList();
ArrayList redList = new ArrayList();// 红色方
ArrayList blueList = new ArrayList();// 蓝色方
public ArrayList removeList = new ArrayList();// 存放将要删除的元素
public void launch() {
// 设置尺寸
setSize(windowWidth, windowHeight);
// 窗口居中
setLocationRelativeTo(null);
// 关闭事件
setDefaultCloseOperation(3);
// 用户不能调整窗口大小
setResizable(false);
// 标题
setTitle("王者荣耀");
// 窗口可见
setVisible(true);
// 添加键盘监视器
this.addKeyListener(new GameFrame.KeyMonitor());
// 添加游戏元素
objList.add(background);
objList.addAll(turret.turretList);
objList.addAll(beast.beastList);
blueList.add(turret.turretList.get(0));
redList.add(turret.turretList.get(4));
/**
* 攻击按钮
*/
attackButton = new JButton();
attackButton.setSize(130, 132);
attackButton.setLocation(1150, 430);
attackButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 按钮事件
player.attack(redList);
}
});
this.add(attackButton);
while (true) {
if (state == 1) {
// 添加小兵
//mb.createMinion(this, blueList);
//mr.createMinion(this, redList);
// 添加防御塔
turret.addTurret();
}
repaint();
try {
Thread.sleep(17);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(5984, 4452);
}
Graphics gImage = offScreenImage.getGraphics();
if (state == 0) {
for(int i = 0; i
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.ArrayList;
import com.sxt.beast.Beast;
public abstract class GameObject {
// 坐标
private int x;
private int y;
// 图片
private Image img;
// 游戏界面
public GameFrame gameFrame;
// 速度
private int spd;
// 初始生命值
private int hp;
// 当前生命值
private int currentHp;
// 攻击目标
private GameObject target;
// 是否有目标
private boolean hasTarget = false;
// 攻击距离
private int dis;
// 攻击时间间隔
private int attackCoolDownTime;
// 攻击是否冷却
private boolean attackCoolDown = true;
// 是否存活
private boolean alive = true;
// 是否被控制
boolean beControlled = false;
public GameObject(GameFrame gameFrame) {
this.gameFrame = gameFrame;
}
public GameObject(int x, int y, GameFrame gameFrame) {
this.x = x;
this.y = y;
this.gameFrame = gameFrame;
}
public GameObject() {
// TODO Auto-generated constructor stub
}
public void addHp(Graphics g, int difX, int difY, int width, int height, Color color) {
// 绘制外部轮廓
g.setColor(Color.black);
g.drawRect(getX() - difX, getY() - difY, width, height);
// 填充矩形
g.setColor(color);
g.fillRect(getX() - difX, getY() - difY, (int) (width * getCurrentHp() / getHp()), height);
}
public double getDis(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
// 矩形矩形碰撞检测
public boolean recIntersectsRec(Rectangle r1, Rectangle r2) {
return r1.intersects(r2);
}
public boolean recIntersectsCir(Rectangle rec, int x, int y, int r) {
/** 矩形于圆相交: 圆心到至少一个矩形定点的距离小于r */
if ((getDis(x, y, rec.x, rec.y) gameObjList) {
if (hasTarget) {
// 目标离开范围后寻找新的目标
if (!recIntersectsCir(target.getRec(), getX(), getY(), getDis())) {
setHasTarget(false);
}
// 目标死亡,寻找新目标
else if (!target.isAlive()) {
setHasTarget(false);
} else if (isAttackCoolDown() && isAlive()) {
Bullet bullet = null;
// 防御塔攻击
if (Turret.class.isAssignableFrom(getClass())) {
bullet = new Bullet(gameFrame, this, getTarget(), 500, 50);
}
// 小兵攻击
else if (Minion.class.isAssignableFrom(getClass())) {
bullet = new Bullet(gameFrame, this, getTarget(), 50, 30);
}
// 玩家攻击
else if (this instanceof Champion) {
bullet = new Bullet(gameFrame, this, getTarget(), 500, 50, "img/bullet.gif");
}
gameFrame.objList.add(bullet);
// 线程开始
new AttackCD().start();
}
} else {
// 遍历列表
for (GameObject obj : gameObjList) {
// 判断攻击范围(圆形)与敌方(矩形)是否相交
if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
// 找到目标
setTarget(obj);
setHasTarget(true);
// 跳出循环
break;
}
}
// 玩家是否在攻击范围内
if (!hasTarget && gameObjList == gameFrame.blueList) {
if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), getDis())) {
// 找到目标
setTarget(gameFrame.player);
setHasTarget(true);
}
} else {
for (GameObject obj : gameFrame.beast.beastList) {
// 判断攻击范围(圆形)与敌方(矩形)是否相交
if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
// 找到目标
setTarget(obj);
setHasTarget(true);
// 跳出循环
break;
}
}
}
}
}
class AttackCD extends Thread {
public void run() {
// 将攻击功能设置为冷却状态
setAttackCoolDown(false);
// 线程休眠
try {
Thread.sleep(attackCoolDownTime);
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能设置为攻击状态
setAttackCoolDown(true);
// 线程终止
this.stop();
}
}
// 绘制元素
public abstract void paintSelf(Graphics g);
// 返回矩形
public abstract Rectangle getRec();
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
/**
* @return the img
*/
public Image getImg() {
return img;
}
/**
* @param img the img to set
*/
public void setImg(String img) {
this.img = Toolkit.getDefaultToolkit().getImage(img);
}
/**
* @return the spd
*/
public int getSpd() {
return spd;
}
/**
* @param spd the spd to set
*/
public void setSpd(int spd) {
this.spd = spd;
}
/**
* @return the hp
*/
public int getHp() {
return hp;
}
/**
* @param hp the hp to set
*/
public void setHp(int hp) {
this.hp = hp;
}
/**
* @return the currentHp
*/
public int getCurrentHp() {
return currentHp;
}
/**
* @param currentHp the currentHp to set
*/
public void setCurrentHp(int currentHp) {
if (currentHp
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public abstract class Minion extends GameObject {
// 是否生成下一个小兵
private boolean nextMinion = true;
// 是否生成下一波小兵
private boolean nextLine = true;
// 生成小兵数量
private int minionCount = 0;
// 是否检测到目标
private boolean ifFindTarget = false;
public Minion(GameFrame gameFrame) {
super(gameFrame);
setHp(800);
setCurrentHp(getHp());
setDis(100);
setAttackCoolDownTime(2000);
// TODO Auto-generated constructor stub
}
/**
* (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)
*/
public abstract void move(ArrayList objList);
/**
* @param x: 下一步的横坐标
* @param y: 下一步的纵坐标
* @param objList: 小兵列表
* @return 下一步位置与其他小兵是否碰撞
*/
public boolean hitMinion(int x, int y, ArrayList objList) {
// 新的区域生成矩形
Rectangle r = new Rectangle(x - 16, y - 16, 45, 45);
for (GameObject obj : objList) {
// 相同类 && 不是自身
if (obj.getClass() == this.getClass() && obj != this) {
if (r.intersects(obj.getRec())) {
return true;
}
}
}
return false;
}
public void findTarget(ArrayList objList) {
for (GameObject obj : objList) {
if (recIntersectsCir(obj.getRec(), getX(), getY(), 200)) {
setTarget(obj);
setIfFindTarget(true);
}
}
if (objList == gameFrame.blueList) {
if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), 200)) {
setTarget(gameFrame.player);
setIfFindTarget(true);
}
}
}
public void moveToTarget() {
double dis = getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
if (!hitMinion(getX() + xSpeed, getY(), gameFrame.objList)) {
setX(getX() + xSpeed);
}
if (!hitMinion(getX(), getY() + ySpeed, gameFrame.objList)) {
setY(getY() + ySpeed);
}
}
public void createMinion(GameFrame gameFrame, ArrayList minionList) {
if (nextLine) {
if (nextMinion) {
// 蓝色方小兵
if (minionList == this.gameFrame.blueList) {
MinionBlue mb = new MinionBlue(gameFrame);
gameFrame.objList.add(mb);
minionList.add(mb);
}
// 红色方小兵
else {
MinionRed mr = new MinionRed(gameFrame);
gameFrame.objList.add(mr);
minionList.add(mr);
}
minionCount++;
new NextMinion().start();
}
if (minionCount == 3) {
minionCount = 0;
new NextLine().start();
}
}
}
// 每个小兵生成时间
class NextMinion extends Thread {
public void run() {
nextMinion = false;
// 休眠1.5s
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
nextMinion = true;
// 线程终止
this.stop();
}
}
// 每波小兵生成时间
class NextLine extends Thread {
public void run() {
nextLine = false;
// 休眠15s
try {
Thread.sleep(15000);
} catch (Exception e) {
e.printStackTrace();
}
nextLine = true;
// 线程终止
this.stop();
}
}
@Override
public void paintSelf(Graphics g) {
// TODO Auto-generated method stub
// 生命值为0
if (getCurrentHp()
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 objList) {
// 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(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 objList) {
// 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(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());
}
}
}
}
}
package com.sxt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Turret extends GameObject {
ArrayList turretList = new ArrayList();
public Turret turretBlueOne;
public Turret turretBlueTwo;
public Turret turretBlueThree;
public Turret turretBlueBase;
public Turret turretRedOne;
public Turret turretRedTwo;
public Turret turretRedThree;
public Turret turretRedBase;
public Turret(GameFrame gameFrame) {
super(gameFrame);
setImg("img/turret.png");
// 初始化八个防御塔
turretList.add(turretBlueOne = new TurretBlue(1860, 3790, gameFrame));
turretList.add(turretBlueTwo = new TurretBlue(2650, 3820, gameFrame));
turretList.add(turretBlueThree = new TurretBlue(3995, 3830, gameFrame));
turretList.add(turretBlueBase = new TurretBlue(1130, 3650, gameFrame));
turretList.add(turretRedOne = new TurretRed(5100, 3030, gameFrame));
turretList.add(turretRedTwo = new TurretRed(5120, 2100, gameFrame));
turretList.add(turretRedThree = new TurretRed(5060, 1570, gameFrame));
turretList.add(turretRedBase = new TurretRed(4850, 1100, gameFrame));
// TODO Auto-generated constructor stub
}
public Turret(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/turret.png");
setHp(6000);
setCurrentHp(getHp());
setAttackCoolDownTime(1000);
setDis(300);
}
public void addTurret() {
/**
* 添加到objList里会被绘制出来,添加到redList和blueList才会被攻击 前一个防御塔爆炸后再把后一个添加到列表里
*/
if (!gameFrame.turret.turretBlueOne.isAlive() && gameFrame.turret.turretBlueTwo.isAlive()
&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueTwo) == -1) {
gameFrame.blueList.add(gameFrame.turret.turretBlueTwo);
}
if (!gameFrame.turret.turretBlueTwo.isAlive() && gameFrame.turret.turretBlueThree.isAlive()
&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueThree) == -1) {
gameFrame.blueList.add(gameFrame.turret.turretBlueThree);
}
if (!gameFrame.turret.turretBlueThree.isAlive() && gameFrame.turret.turretBlueBase.isAlive()
&& gameFrame.blueList.indexOf(gameFrame.turret.turretBlueBase) == -1) {
gameFrame.blueList.add(gameFrame.turret.turretBlueBase);
}
if (!gameFrame.turret.turretBlueBase.isAlive()) {
//游戏失败
gameFrame.state = 3;
}
if (!gameFrame.turret.turretRedOne.isAlive() && gameFrame.turret.turretRedTwo.isAlive()
&& gameFrame.redList.indexOf(gameFrame.turret.turretRedTwo) == -1) {
gameFrame.redList.add(gameFrame.turret.turretRedTwo);
}
if (!gameFrame.turret.turretRedTwo.isAlive() && gameFrame.turret.turretRedThree.isAlive()
&& gameFrame.redList.indexOf(gameFrame.turret.turretRedThree) == -1) {
gameFrame.redList.add(gameFrame.turret.turretRedThree);
}
if (!gameFrame.turret.turretRedThree.isAlive() && gameFrame.turret.turretRedBase.isAlive()
&& gameFrame.redList.indexOf(gameFrame.turret.turretRedBase) == -1) {
gameFrame.redList.add(gameFrame.turret.turretRedBase);
}
if (!gameFrame.turret.turretRedBase.isAlive()) {
//游戏胜利
gameFrame.state = 2;
}
}
@Override
public void paintSelf(Graphics g) {
// 生命值为0
if (getCurrentHp()
package com.sxt;
public class TurretBlue extends Turret {
public TurretBlue(GameFrame gameFrame) {
super(gameFrame);
// TODO Auto-generated constructor stub
}
public TurretBlue(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
}
}
package com.sxt.beast;
import com.sxt.GameFrame;
public class Bear extends Beast {
public Bear(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/bear.jpg");
width = 85;
height = 112;
setDis(65);
// TODO Auto-generated constructor stub
}
}
package com.sxt.beast;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import com.sxt.*;
public class Beast extends GameObject {
public ArrayList beastList = new ArrayList();
int width;
int height;
// 初始坐标
int initialX;
int initialY;
// 有没有仇恨值
public boolean isAggressive = false;
// 复活的元素
Beast beast = null;
public Beast(GameFrame gameFrame) {
super(gameFrame);
beastList.add(new RedBuff(3045, 3170, gameFrame));
beastList.add(new Bear(2800, 2855, gameFrame));
beastList.add(new Bird(3570, 3380, gameFrame));
beastList.add(new Xiyi(4585, 2365, gameFrame));
beastList.add(new BlueBuff(4025, 2295, gameFrame));
beastList.add(new Wolf(4235, 1945, gameFrame));
}
public Beast(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setHp(1000);
setCurrentHp(getHp());
setSpd(10);
setAttackCoolDownTime(2000);
initialX = getX();
initialY = getY();
beast = this;
}
public void moveToTarget() {
double dis = getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
if (dis > 500) {
isAggressive = false;
setHasTarget(false);
} else {
int xSpeed = (int) (getSpd() * (getTarget().getX() - getX()) / dis);
int ySpeed = (int) (getSpd() * (getTarget().getY() - getY()) / dis);
setX(getX() + xSpeed);
setY(getY() + ySpeed);
}
}
public void moveToInitialLocation() {
double dis = getDis(getX(), getY(), initialX, initialY);
if (dis
package com.sxt.beast;
import com.sxt.GameFrame;
public class Bird extends Beast {
public Bird(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/红隼.jpg");
width = 122;
height = 98;
setDis(125);
// TODO Auto-generated constructor stub
}
}
package com.sxt.beast;
import com.sxt.GameFrame;
public class BlueBuff extends Beast {
public BlueBuff(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/blueBuff.jpg");
width = 142;
height = 176;
setDis(70);
// TODO Auto-generated constructor stub
}
}
package com.sxt.beast;
import com.sxt.GameFrame;
public class RedBuff extends Beast {
public RedBuff(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/redBuff.jpg");
width = 103;
height = 150;
setDis(70);
// TODO Auto-generated constructor stub
}
}
package com.sxt.beast;
import com.sxt.GameFrame;
public class Wolf extends Beast {
public Wolf(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/wolf.jpg");
width = 145;
height = 140;
setDis(65);
// TODO Auto-generated constructor stub
}
}
package com.sxt.beast;
import com.sxt.GameFrame;
public class Xiyi extends Beast {
public Xiyi(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("img/蜥蜴.jpg");
width = 111;
height = 65;
setDis(125);
// TODO Auto-generated constructor stub
}
}
三.结果呈现
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
Python 提供了强大的输入输出和文件处理工具,通过 io、pickle 和 json 等模块,开发者可以轻松处理文件、序列化和反序列化数据,并在不同格式之间进行转换。在本文中,我们将深入介绍这些模块的用法和实际示例。 1. io 模块:强大的输入输出工具 …