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
ArrayList
ArrayList
public 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 < champion.championList.size(); i++) {
//添加图片
Image classical = champion.championList.get(i).classical;
gImage.drawImage(classical, i*160, 20, null);
//添加按钮
JButton championButton = new JButton();
championButton.setSize(150, 150);
championButton.setLocation(i*150, 0);
int a = i;
championButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
state = 1;
player = champion.championList.get(a);
objList.add(player);
player.addButton();
}
});
this.add(championButton);
}
} else if (state == 1) {
for (int i = 0; i < objList.size(); i++) {
objList.get(i).paintSelf(gImage);
}
// 绘制攻击图片
gImage.drawImage(attack, player.getX() + 500, player.getY() + 100, null);
objList.removeAll(removeList);
} else if (state == 2) {//游戏胜利
gImage.drawImage(gameWin, 0, 5, null);
} else if (state == 3) {//游戏失败
gImage.drawImage(gameLose, 0, 5, null);
}
if (state != 1) {
g.drawImage(offScreenImage, 0, 0, null);
} else {
g.drawImage(offScreenImage, -player.getX() + 700, -player.getY() + 350, null);
}
/**
* 添加按钮后不能调用键盘事件 因为程序的焦点变成了按钮 this.requestFocus() 把焦点重新改变到游戏界面上
*/
this.requestFocus();
}
// main方法
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame();
gameFrame.launch();
}
// 键盘事件
private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
player.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
player.keyReleased(e);
}
}
}
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) < r) || (getDis(x, y, rec.x, rec.y + rec.height) < r)
|| (getDis(x, y, rec.x + rec.width, rec.y) < r)
|| (getDis(x, y, rec.x + rec.width, rec.y + rec.height) < r)) {
return true;
}
return false;
}
// 攻击方法
public void attack(ArrayList
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 < getHp()) {
if (Beast.class.isAssignableFrom(getClass())) {
System.out.println("yes");
setTarget(gameFrame.player);
setHasTarget(true);
}
}
this.currentHp = currentHp;
}
/**
* @return the target
*/
public GameObject getTarget() {
return target;
}
/**
* @param target the target to set
*/
public void setTarget(GameObject target) {
this.target = target;
}
/**
* @return the hasTarget
*/
public boolean isHasTarget() {
return hasTarget;
}
/**
* @param hasTarget the hasTarget to set
*/
public void setHasTarget(boolean hasTarget) {
this.hasTarget = hasTarget;
}
/**
* @return the dis
*/
public int getDis() {
return dis;
}
/**
* @param dis the dis to set
*/
public void setDis(int dis) {
this.dis = dis;
}
/**
* @return the attackCoolDownTime
*/
public int getAttackCoolDownTime() {
return attackCoolDownTime;
}
/**
* @param attackCoolDownTime the attackCoolDownTime to set
*/
public void setAttackCoolDownTime(int attackCoolDownTime) {
this.attackCoolDownTime = attackCoolDownTime;
}
/**
* @return the attackCoolDown
*/
public boolean isAttackCoolDown() {
return attackCoolDown;
}
/**
* @param attackCoolDown the attackCoolDown to set
*/
public void setAttackCoolDown(boolean attackCoolDown) {
this.attackCoolDown = attackCoolDown;
}
/**
* @return the alive
*/
public boolean isAlive() {
return alive;
}
/**
* @param alive the alive to set
*/
public void setAlive(boolean alive) {
this.alive = alive;
}
}