主要功能
键盘W,A,S,D键:控制玩家上下左右移动。按钮一:控制英雄发射一个矩形攻击红方小兵。按钮控制英雄发射魅惑技能,伤害小兵并让小兵停止移动。技能三:攻击多个敌人并让小兵停止移动。普攻:对小兵造成基础伤害。小兵每隔一段时间自动生成,在王者峡谷下路移动,遇到敌人则自动攻击。防御塔可以自动识别敌人进行攻击。野怪分为RedBuff,BlueBuff,普通野怪,死亡后都可以复活。游戏胜利的条件是红方水晶生命值为零,游戏失败条件是蓝方水晶生命值为零。游戏的设计模式为可以偷塔。
游戏流程图
一,主窗口类
作用:初始化变量,窗口的启动方法,键盘监听事件,添加按钮事件,游戏不同状态的变化,播放背景音乐。
package com.sxt;
import com.sxt.beast.Beast;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GameFrame extends JFrame {
// 游戏开始 0, 游戏胜利1,游戏失败2
int state = 0;
// 窗口尺寸
final int windowWidth = 1400;
final int windowHeight = 700;
// 双缓冲图片
private Image offScreenImage = null;
// 攻击图片
private Image attack = Toolkit.getDefaultToolkit().getImage("C:Users23839IdeaProjectsasdsrcimgsattack.jpg");
//游戏胜利失败图片
private Image gameWin = Toolkit.getDefaultToolkit().getImage("C:Users23839IdeaProjectsasdsrcimgsgameWin.png");
private Image gameLose = Toolkit.getDefaultToolkit().getImage("C:Users23839IdeaProjectsasdsrcimgsgameLose.png");
// 游戏背景
Background background = new Background(this);
// 游戏玩家
Champion player = new ChampionDaji(this);
// 双方小兵
MinionBlue mb = new MinionBlue(this);
MinionRed mr = new MinionRed(this);
//野怪
public Beast beast = new Beast(this);
// 防御塔
Turret turret = new Turret(this);
// 游戏元素列表
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.add(player);
objList.addAll(beast.beastList);
objList.addAll(turret.turretList);
playMusic();
for (int i = 0; i
二,游戏元素父类
作用:初始化基本变量,定义addHp()方法,两点之间的距离方法,矩形与矩形的碰撞检测方法,矩形与圆形的碰撞检测方法,定义防御塔,英雄,小兵的攻击方法,用线程实现攻击的冷却时间。
package com.sxt;
import java.awt.*;
import java.util.ArrayList;
//游戏元素的父类
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() {
}
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);
}
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;
}
}
}
}
}
//public abstract void addTurret();
class AttackCD extends Thread {
public void run() {
// 将攻击功能设置为冷却状态
setAttackCoolDown(false);
// 线程休眠
try {
Thread.sleep(attackCoolDownTime);
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能设置为攻击状态
setAttackCoolDown(true);
// 线程终止
this.interrupt();
}
}
// 绘制元素
public abstract void paintSelf(Graphics g);
// 返回矩形
public abstract Rectangle getRec();
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImg() {
return img;
}
public void setImg(String img) {
this.img = Toolkit.getDefaultToolkit().getImage(img);
}
public int getSpd() {
return spd;
}
public void setSpd(int spd) {
this.spd = spd;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getCurrentHp() {
return currentHp;
}
public void setCurrentHp(int currentHp) {
this.currentHp = currentHp;
}
public GameObject getTarget() {
return target;
}
public void setTarget(GameObject target) {
this.target = target;
}
public boolean isHasTarget() {
return hasTarget;
}
public void setHasTarget(boolean hasTarget) {
this.hasTarget = hasTarget;
}
public int getDis() {
return dis;
}
public void setDis(int dis) {
this.dis = dis;
}
public int getAttackCoolDownTime() {
return attackCoolDownTime;
}
public void setAttackCoolDownTime(int attackCoolDownTime) {
this.attackCoolDownTime = attackCoolDownTime;
}
public boolean isAttackCoolDown() {
return attackCoolDown;
}
public void setAttackCoolDown(boolean attackCoolDown) {
this.attackCoolDown = attackCoolDown;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
}
三,防御塔类
作用:初始化防御塔并添加到窗口中,游戏失败与胜利方法,在绘制自身方法中通过判断血量删除元素或者绘制防御塔。
package com.sxt;
import java.awt.*;
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("C:Users23839IdeaProjectsasdsrcimgsturret.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));
}
public Turret(int x, int y, GameFrame gameFrame) {
super(x, y, gameFrame);
setImg("C:Users23839IdeaProjectsasdsrcimgsturret.png");
setHp(6000);
setCurrentHp(getHp());
setAttackCoolDownTime(1000);
setDis(300);
}
public void isLive(){
if(gameFrame.turret.turretBlueBase.getCurrentHp()==0){
gameFrame.state=2;
}
if(gameFrame.turret.turretRedBase.getCurrentHp()==0){
gameFrame.state=1;
}
}
@Override
public void paintSelf(Graphics g) {
// 生命值为0
if (getCurrentHp()
1.蓝色防御塔
作用:有参构造。
package com.sxt;
//蓝方防御塔
public class TurretBlue extends Turret{
public TurretBlue(GameFrame gameFrame){
super(gameFrame);
}
public TurretBlue(int x,int y,GameFrame gameFrame){
super(x,y,gameFrame);
}
}
2.红方防御塔
作用:有参构造。
package com.sxt;
//蓝方防御塔
public class TurretRed extends Turret{
//有参构造
public TurretRed(GameFrame gameFrame){
super(gameFrame);
}
public TurretRed(int x,int y,GameFrame gameFrame){
super(x,y,gameFrame);
}
}
四,英雄类
作用:实现键盘监听,添加按钮,鼠标监听。
package com.sxt;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
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;
//技能冷却时间
int coolDownTimeOne;
int coolDownTimeTwo;
int coolDownTimeThree;
//三个技能是否处于冷却状态
boolean coolDownOne = true;
boolean coolDownTwo = true;
boolean coolDownThree = true;
static {
for (int i = 1; i
1.英雄妲己
作用:定义三个技能的具体实现方法,用线程实现技能的冷却时间,控制时间,鼠标监视器实现释放技能一,定义技能效果。
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;
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("C:Users23839IdeaProjectsasdsrcimgsDajiabilityOne.jpg");
abilityTwo = Toolkit.getDefaultToolkit().getImage("C:Users23839IdeaProjectsasdsrcimgsDajiabilityTwo.jpg");
abilityThree = Toolkit.getDefaultToolkit().getImage("C:Users23839IdeaProjectsasdsrcimgsDajiabilityThree.jpg");
// 三个技能冷却时间
coolDownTimeOne = 3000;
coolDownTimeTwo = 5000;
coolDownTimeThree = 8000;
}
public void exit() {
this.gameFrame.removeMouseListener(m);
}
public void abilityOneMove() {
p.translate((int) (50 * cos), -(int) (50 * sin));
for (GameObject redObj : gameFrame.redList) {
// 是红色方小兵 && 发生碰撞 && 没在attacked列表里
if (redObj instanceof MinionRed && p.intersects(redObj.getRec()) && attacked.indexOf(redObj) == -1) {
// 小兵扣血,添加到attacked里
redObj.setCurrentHp(redObj.getCurrentHp() - 400);
attacked.add(redObj);
}
}
}
@Override
public void abilityOne() {
if (coolDownOne) {
m = new MouseMonitor();
p = new Polygon();
gameFrame.addMouseListener(m);
attacked = new ArrayList();
}
}
public void abilityTwo() {
if (coolDownTwo) {
boolean find = false;
for (GameObject redObj : gameFrame.objList) {
// 是红色小兵 && 距离小于250 && 存活
if (redObj instanceof MinionRed && recIntersectsCir(redObj.getRec(), getX(), getY(), 250)
&& redObj.isAlive()) {
// 添加子弹
abilityTwoBullet = new Bullet(gameFrame, this, redObj, 250, 60, "C:Users23839IdeaProjectsasdsrcimgsDajiabilityTwoBullet.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() {
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.interrupt();
}
}
// 技能二冷却时间
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.interrupt();
}
}
// 技能二控制时间
class AbilityControllCD extends Thread {
public void run() {
abilityTwoTarget.beControlled = true;
// 线程休眠
try {
Thread.sleep(20000);
} catch (Exception e) {
e.printStackTrace();
}
abilityTwoTarget.beControlled = false;
this.interrupt();
}
}
//技能三冷却服务器托管网状态
class AbilityThreeCD extends Thread {
public void run() {
// 将攻击功能设置为冷却状态
coolDownThree = false;
// 休眠
try {
int three = coolDownTimeThree;
while (coolDownTimeThree > 0) {
Thread.sleep(1000);
System.out.println("技能三冷却时间: " + coolDownTimeThree / 1000);
coolDownTimeThree -= 1000;
}
coolDownTimeThree = three;
} catch (Exception e) {
e.printStackTrace();
}
// 将攻击功能解除冷却状态
coolDownThree = true;
// 线程终止
this.interrupt();
}
}
class AbilityThreeBulletCD extends Thread {
public void run() {
// 休眠
try {
System.out.println("Thread start");
gameFrame.objList.add(bulletList[0]);
Thread.sleep(100);
gameFrame.objList.add(bulletList[1]);
Thread.sleep(100);
gameFrame.objList.add(bulletList[2]);
Thread.sleep(100);
gameFrame.objList.add(bulletList[3]);
Thread.sleep(100);
gameFrame.objList.add(bulletList[4]);
} catch (Exception e) {
e.printStackTrace();
}
// 线程终止
this.interrupt();
}
}
// 鼠标监视器
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.*;
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);
}
/**
* (1325, 3750) (4425, 3750) (5050, 3125) (5050, 1125)
*/
public abstract void move(ArrayList objList);
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() {
int dis = (int) getDis(getX(), getY(), getTarget().getX(), getTarget().getY());
if(dis!=0) {
int xSpeed = (getSpd() * (getTarget().getX() - getX()) / dis);
int ySpeed = (getSpd() * (getTarget().getY() - getY()) / dis);
setX(getX() + xSpeed);
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.interrupt();
}
}
// 每波小兵生成时间
class NextLine extends Thread {
public void run() {
nextLine = false;
// 休眠15s
try {
Thread.sleep(15000);
} catch (Exception e) {
e.printStackTrace();
}
nextLine = true;
// 线程终止
this.interrupt();
}
}
@Override
public void paintSelf(Graphics g) {
// 生命值为0
if (getCurrentHp()
1.红方小兵
作用:具体的红方小兵移动方法。
package com.sxt;
import java.util.ArrayList;
public class MinionRed extends Minion {
public MinionRed(GameFrame gameFrame) {
super(gameFrame);
setImg("C:Users23839IdeaProjectsasdsrcimgsminionred.jpg");
setX(5050);
setY(1125);
}
@Override
public void move(ArrayList objList) {
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);
setY(getY() + getSpd());
setX(getX() - getSpd());
} else if (getY() >= 3750) {
setSpd(25);
setX(getX() - getSpd());
}
}
}
}
2.蓝方小兵
作用:具体的蓝方小兵移动方法。
package com.sxt;
import java.util.ArrayList;
public class MinionBlue extends Minion{
public MinionBlue(GameFrame gameFrame) {
super(gameFrame);
setImg("C:Users23839IdeaProjectsasdsrcimgsminionblue.jpg");
setX(1325);
setY(3750);
}
@Override
public void move(ArrayList objList) {
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);
setX(getX() + getSpd());
setY(getY() - getSpd());
}else if(getX() >= 4900) {
setSpd(18);
setY(getY() - getSpd());
}
}
}
}
六,子弹类
作用:子弹移动方法。
package com.sxt;
import java.awt.*;
//子弹类
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);
}
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);
}
public Bullet() {
super();
}
public void move() {
//子弹与目标碰撞,子弹消失,目标减血
if (recIntersectsRec(getRec(), target.getRec())) {
target.setCurrentHp(target.getCurrentHp() - getAd());
gameFrame.removeList.add(this);
}
int dis = (int) getDis(getX(), getY(), target.getX(), target.getY());
if (dis != 0) {
int xSpeed = (getSpd() * (target.getX() - getX()) / dis);
int ySpeed = (getSpd() * (target.getY() - getY()) / dis);
setX(getX() + xSpeed);
setY(getY() + ySpeed);
}
}
@Override
public void paintSelf(Graphics g) {
g.drawImage(getImg(), getX()-16, getY()-16, 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() {
return new Rectangle(getX()-5, getY()-5, 10, 10);
}
public int getAd() {
return ad;
}
public void setAd(int ad) {
this.ad = ad;
}
}
七,背景类
作用:初始化背景。
package com.sxt;
import java.awt.*;
//背景类
public class Background extends GameObject{
public Background(GameFrame gameFrame) {
super(gameFrame);
}
Image bg = Toolkit.getDefaultToolkit().getImag服务器托管网e("C:Users23839IdeaProjectsasdsrcimgsMap.jpg");
public void paintSelf(Graphics g){
g.drawImage(bg,0,0,null);
}
@Override
public Rectangle getRec() {
return null;
}
}
八,野怪类
作用:在绘制自身方法中通过判断血量删除元素或者绘制自身,用线程实现自动复活功能。
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;
// 复活的元素
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());
beast = this;
}
@Override
public void paintSelf(Graphics g) {
if (getCurrentHp()
1.熊类
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeastbear.jpg");
width = 85;
height = 112;
setDis(65);
}
}
2.鸟类
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeast红隼.jpg");
width = 122;
height = 98;
setDis(125);
}
}
3.蓝Buff
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeastblueBuff.jpg");
width = 142;
height = 176;
setDis(70);
}
}
4.红Buff
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeastredBuff.jpg");
width = 103;
height = 150;
setDis(70);
}
}
5.狼类
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeastwolf.jpg");
width = 145;
height = 140;
setDis(65);
}
}
6.蜥蜴类
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("C:Users23839IdeaProjectsHonorOfKingssrcimgsbeast蜥蜴.jpg");
width = 111;
height = 65;
setDis(125);
}
}
作用:这几个野怪类初始化野怪具体图片与坐标
图片资源
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
一丶概述 RocketMQ 消息发送的原理流程可以分为以下几个步骤: 1. 创建生产者 在发送消息前,客户端首先需要创建一个消息生产者(Producer)实例,并设置必要的配置参数,如NameServer地址、生产组名称、消息发送失败的重试次数等。 2. 启动…