主要由
Constant常量类
GameCanavas类 用来绘制
LeftBobo 左边人攻击的动作
LeftMan 左边人
RightMan
RightBobo 右边人的动作组成
先看做出的效果:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: Constant
* Author: meng
* Date: 2018/7/9 15:26
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
/**
* 常量类<br>
* 〈〉
*
* @author meng
* @create 2018/7/9
* @since 1.0.0
*/
public class Constant {
//左边的人
public static final int LEFTMAN = 1001;
//右边的人
public static final int RIGHTMAN = 1002;
//左边的技能
public static final int LEFTBOBO = 2001;
//右边的技能
public static final int RIGHTBOBO = 2002;
/**
* JFame其实坐标
*/
public static final int LOCATION_X = 300;
public static final int LOCATION_Y = 300;
//宽和高
public static final int J_WIDTH = 500;
public static final int J_HEIGHT = 200;
/**
* 人物属性
*/
public static final int LEFT_MAN_X = 5;
public static final int LEFT_MAN_Y = 5;
public static final int LEFT_MAN_WIDTH = 135;
public static final int LEFT_MAN_HEIGHT = 140;
public static final int RIGHT_MAX_X = 350;
public static final int RIGHT_MAX_Y = 5;
public static final int RIGHT_MAN_WIDTH = 135;
public static final int RIGHT_MAN_HEIGHT = 140;
/**
* 子弹的属性
*/
//右边子弹的属性
public static final int LEFT_BOBO_Y = 57;
public static final int LEFT_BOBO_WIDTH =80 ;
public static final int LEFT_BOBO_HIGHT =37;
//左边子弹的属性
public static final int RIGHT_BOBO_Y = 57;
public static final int RIGHT_BOBO_WIDTH =80 ;
public static final int RIGHT_BOBO_HIGHT =37;
/**
* 图片文件地址
*/
//左边人物图片的地址
public static final String LEFT_MAN1_PIC = "./pic/man1.png";
public static final String LEFT_MAN2_PIC = "./pic/man2.png";
public static final String LEFT_MAN3_PIC = "./pic/man3.png";
public static final String LEFT_MAN4_PIC = "./pic/man4.png";
public static final String LEFT_MAN5_PIC = "./pic/man1.png";
public static final String DEATH_PIC = "./pic/dead.png";
//右边人物图片的地址
public static final String RIGHT_MAN1_PIC = "./pic/man21.png";
public static final String RIGHT_MAN2_PIC = "./pic/man22.png";
public static final String RIGHT_MAN3_PIC = "./pic/man23.png";
public static final String RIGHT_MAN4_PIC = "./pic/man24.png";
public static final String RIGHT_MAN5_PIC = "./pic/man21.png";
/**
* 子弹图片的地址
*/
//左边技能的地址
public static final String LEFT_BOBO1 = "./pic/bobo1.png";
public static final String LEFT_BOBO2 = "./pic/bobo2.png";
//右边人的技能
public static final String RIGHT_BOBO1 = "./pic/bobo3.png";
public static final String RIGHT_BOBO2 = "./pic/bobo4.png";
/**
* 音频地址
*/
public static final String SHOUT_SOUND = "./pic/Shoot.wav";
public static final String DEATH_SOUND = "./pic/Dead.wav";
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: GameCanavas
* Author: meng
* Date: 2018/7/9 10:46
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
/**
* <br>
* 〈〉
*
* @author meng
* @create 2018/7/9
* @since 1.0.0
*/
public class GameCanavas extends Canvas implements Runnable {
//定义缓存
private Image imageCache;
//人物
// private Man leftMan;
private RightMan rightMan;
private LeftMan leftMan;
public RightMan getRightMan() {
return rightMan;
}
public LeftMan getLeftMan() {
return leftMan;
}
private RightBobo rightBobo;
private LeftBobo leftBobo;
private List<LeftBobo> listLeftBobo = new ArrayList<LeftBobo>();
private List<RightBobo> listRightBobo =new ArrayList<RightBobo>();
private boolean isLeftManHit;
private boolean isRightManHit;
private long time1;
private long time2;
private MyKeyAdapter myKeyAdapter;
public List<LeftBobo> getListLeftBobo() {
return listLeftBobo;
}
public void setListLeftBobo(List listLeftBobo) {
this.listLeftBobo = listLeftBobo;
}
public List<RightBobo> getListRightBobo() {
return listRightBobo;
}
public void setListRightBobo(List listRightBobo) {
this.listRightBobo = listRightBobo;
}
public MyKeyAdapter getMyKeyAdapter() {
return myKeyAdapter;
}
public RightBobo getRightBobo() {
return rightBobo;
}
public LeftBobo getLeftBobo() {
return leftBobo;
}
public GameCanavas() {
//添加监听
myKeyAdapter = new MyKeyAdapter();
this.addKeyListener(myKeyAdapter);
leftMan = new LeftMan(this);
// leftMan = new Man(this);
rightMan = new RightMan(this);
//
// //leftBobo = new LeftBobo();
// leftBobo = new LeftBobo(this);
// rightBobo = new RightBobo();
// listLeftBobo = new ArrayList<LeftBobo>();
// listRightBobo = new ArrayList<RightBobo>();
isLeftManHit = false;
isRightManHit = false;
this.setBackground(Color.BLACK);
}
public Image getImage() {
return imageCache;
}
public void setImage(Image image) {
this.imageCache = image;
}
// public Man getLeftMan() {
// return leftMan;
// }
//
// public void setLeftMan(Man leftMan) {
// this.leftMan = leftMan;
// }
// public Man getRightMan() {
// return rightMan;
// }
//
// public void setRightMan(Man rightMan) {
// this.rightMan = rightMan;
// }
// public Bobo getLeftBobo() {
// return leftBobo;
// }
//
// public void setLeftBobo(Bobo leftBobo) {
// this.leftBobo = leftBobo;
// }
// public Bobo getRightBobo() {
// return rightBobo;
// }
//
// public void setRightBobo(Bobo rightBobo) {
// this.rightBobo = rightBobo;
// }
@Override
public void update(Graphics g) {
if(imageCache == null){
imageCache = this.createImage(600,200);
}
//获取画笔
Graphics graphics = imageCache.getGraphics();
// Color color = graphics.getColor();
graphics.setColor(Color.BLACK);
//构建缓存区
graphics.fillRect(0,0,600,600);
paint(graphics);
g.drawImage(imageCache,0,0,null);
}
@Override
public void paint(Graphics g) {
//画上人物
leftMan.draw(g,isLeftManHit);
rightMan.draw(g,isRightManHit);
//画子弹
for(int i=0;i<listLeftBobo.size();i++){
leftBobo = (LeftBobo) listLeftBobo.get(i);
if(leftBobo != null){
leftBobo.draw(g);
}
}//end for
for(int i=0;i<listRightBobo.size();i++){
rightBobo = (RightBobo) listRightBobo.get(i);
if(rightBobo != null){
rightBobo.draw(g);
}
}
isLeftManHit = false;
isRightManHit = false;
}//
@Override
public void run() {
while (true) {
this.repaint();//重绘制
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}//end try
}//end while
}//end run
//键盘的点击事件
class MyKeyAdapter extends KeyAdapter{
public MyKeyAdapter() {
super();
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getExtendedKeyCode()){
//装子弹
case KeyEvent.VK_J:
//更改人物状态,下同
isLeftManHit = true;
// time1 = System.currentTimeMillis();
// time2 = System.currentTimeMillis();
// if(time2-time1>500){
// isLeftManHit = true;
// }
System.out.println("J is press");
break;
case KeyEvent.VK_K:
isRightManHit = true;
System.out.println("K is press");
break;
}
}//end pressKey
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: GameFram
* Author: meng
* Date: 2018/7/9 10:46
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import javax.swing.*;
import java.awt.*;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author meng
* @create 2018/7/9
* @since 1.0.0
*/
public class GameFram extends JFrame{
private GameCanavas gameCanavas;
public GameFram() throws HeadlessException {
gameCanavas = new GameCanavas();
this.add(gameCanavas);
new Thread(gameCanavas).start();
//拿到画布的监听
this.addKeyListener(gameCanavas.getMyKeyAdapter());
this.pack();
this.setLocation(Constant.LOCATION_X,Constant.LOCATION_Y);
this.setSize(Constant.J_WIDTH,Constant.J_HEIGHT);
this.setResizable(false);//禁止拉拽效果
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GameFram();
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: LeftBobo
* Author: meng
* Date: 2018/7/10 14:00
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author meng
* @create 2018/7/10
* @since 1.0.0
*/
public class LeftBobo {
private ImageIcon pic1;
private ImageIcon pic2;
private int speed;//实际是代表x,因为只在x轴方向移动
private GameCanavas canavas;
private boolean isLive;
public LeftBobo() {
// pic1 = new ImageIcon(Constant.LEFT_BOBO1);
// pic2 = new ImageIcon(Constant.LEFT_BOBO2);
// isLive = true;
// speed = 80;//初始化子弹的位置
}
public LeftBobo(Graphics g,GameCanavas canavas) {
this.canavas = canavas;
pic1 = new ImageIcon(Constant.LEFT_BOBO1);
pic2 = new ImageIcon(Constant.LEFT_BOBO2);
isLive = true;
speed = 80;//初始化子弹的位置
}
public LeftBobo(GameCanavas gameCanavas) {
canavas = gameCanavas;
pic1 = new ImageIcon(Constant.LEFT_BOBO1);
pic2 = new ImageIcon(Constant.LEFT_BOBO2);
isLive = true;
speed = 80;//初始化子弹的位置
}
public void draw(Graphics g) {
if(isLive){
//从弹夹移除
}
//移动子弹
speed += 10;
if(speed % 20 ==0){
g.drawImage(
pic1.getImage(),
speed,
Constant.LEFT_BOBO_Y,
Constant.LEFT_BOBO_WIDTH,
Constant.LEFT_BOBO_HIGHT,
null);
}else{
g.drawImage(
pic2.getImage(),
speed,
Constant.LEFT_BOBO_Y,
Constant.LEFT_BOBO_WIDTH,
Constant.LEFT_BOBO_HIGHT,
null);
}
}
//监测的位置
public Rectangle getRec(){
return new Rectangle(speed,
Constant.LEFT_BOBO_Y,
Constant.LEFT_BOBO_WIDTH,
Constant.LEFT_BOBO_HIGHT);
}
//监测是否撞击到对面的人
public boolean isHitRightMan(RightMan man){
if(this.getRec().intersects(man.getRec())&&
this.isLive == true&&
man.isLive() == true){//&&man.getislive() == ture
System.out.println("isHitRightMan ok");
//子弹消失
this.isLive = false;
//人会死
man.setLive(false);
//画骷髅
return true;
}
return false;
}
public void setLive(boolean live) {
isLive = live;
}
//监测是否撞击到对面飞过来的子弹
public void isHitRightBobo(List<RightBobo> bobo){
int num;
if(canavas.getListLeftBobo().size()>canavas.getListRightBobo().size()){
num = canavas.getListRightBobo().size();
}else{
num = canavas.getListLeftBobo().size();
}
for(int i=0;i<num;i++){
if(this.getRec().intersects(canavas.getListRightBobo().get(0).getRec())){
canavas.getListRightBobo().remove(i);
canavas.getListLeftBobo().remove(i);
}
// return true;
}
// return false;
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: LeftMan
* Author: meng
* Date: 2018/7/10 11:45
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import sun.audio.AudioPlayer;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author meng
* @create 2018/7/10
* @since 1.0.0
*/
public class LeftMan {
//存放图片信息
private ImageIcon[] pic;
private ImageIcon picDeath;
//是否活着
private boolean isLive;
//技能
//private Bobo bobo;
private LeftBobo bobo;
//攻击时的音频
private File shout;
//死亡的时候的音频
private File died;
//画布
private GameCanavas canavas;
public LeftMan(GameCanavas gameCanavas) {
canavas = gameCanavas;
pic = new ImageIcon[5];
//初始化死亡的图片
picDeath = new ImageIcon(Constant.DEATH_PIC);
//还原为初始化
pic[4] = new ImageIcon(Constant.LEFT_MAN5_PIC);
pic[0] = new ImageIcon(Constant.LEFT_MAN1_PIC);
pic[1] = new ImageIcon(Constant.LEFT_MAN2_PIC);
pic[2] = new ImageIcon(Constant.LEFT_MAN3_PIC);
pic[3] = new ImageIcon(Constant.LEFT_MAN4_PIC);
//初始化声音
shout = new File(Constant.SHOUT_SOUND);
died = new File(Constant.DEATH_SOUND);
//标记自己开始是活着的
isLive = true;
}
public void draw(Graphics graphics,boolean isHit){
if(isLive){
//画静止的人
graphics.drawImage(pic[0].getImage(),
Constant.LEFT_MAN_X,
Constant.LEFT_MAN_Y,
Constant.LEFT_MAN_WIDTH,
Constant.LEFT_MAN_HEIGHT,
null);
fire(graphics, isHit);
//调用碰撞检测
if(bobo !=null){
if(bobo.isHitRightMan(canavas.getRightMan())){
canavas.getRightMan().setLive(false);
}
if(canavas.getListRightBobo().size()>0){
bobo.isHitRightBobo(canavas.getListRightBobo());
}
}
}else{
//画死亡状态
graphics.drawImage(picDeath.getImage(),
Constant.LEFT_MAN_X,
Constant.LEFT_MAN_Y,
Constant.LEFT_MAN_WIDTH,
Constant.LEFT_MAN_HEIGHT,
null);
}
}
public void setLive(boolean live) {
isLive = live;
}
public void fire(Graphics graphics, boolean isFire){
if(isFire){
for (int i = 0; i < 4; i++) {
graphics.drawImage(
pic[i].getImage(),
Constant.LEFT_MAN_X,
Constant.LEFT_MAN_Y,
Constant.LEFT_MAN_WIDTH,
Constant.LEFT_MAN_HEIGHT,
null);
}
//添加音频
try {
AudioPlayer.player.start(shout.toURI().toURL().openStream());
} catch (IOException e) {
e.printStackTrace();
}
bobo = new LeftBobo(graphics,canavas);
canavas.getListLeftBobo().add(bobo);
}
}//end fire
public Rectangle getRec(){
return new Rectangle(5,5,135,115);
}
public boolean isLive() {
return isLive;
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: RightBobo
* Author: meng
* Date: 2018/7/10 14:00
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author meng
* @create 2018/7/10
* @since 1.0.0
*/
public class RightBobo {
private ImageIcon pic1;
private ImageIcon pic2;
private int speed;//实际是代表x,因为只在x轴方向移动
private GameCanavas canavas;
private boolean isLive;
public RightBobo() {
System.out.println("RightBobo");
pic1 = new ImageIcon(Constant.RIGHT_BOBO1);
pic2 = new ImageIcon(Constant.RIGHT_BOBO2);
isLive = true;
speed = 280;//初始化子弹的位置
}
public RightBobo(Graphics g,GameCanavas canavas) {
this.canavas = canavas;
pic1 = new ImageIcon(Constant.RIGHT_BOBO1);
pic2 = new ImageIcon(Constant.RIGHT_BOBO2);
isLive = true;
speed = 300;//初始化子弹的位置
}
public int getSpeed() {
return speed;
}
public void setLive(boolean live) {
isLive = live;
}
public void draw(Graphics g) {
if(isLive){
//从弹夹移除
//canavas.getListRightBobo().remove(0);
}
//移动子弹speed -= 10;
if(speed>0){
speed -= 10;
}
if(speed %20 ==0){
g.drawImage(
pic1.getImage(),
speed,
Constant.RIGHT_BOBO_Y,
Constant.LEFT_BOBO_WIDTH,
Constant.LEFT_BOBO_HIGHT,
null);
}else{
g.drawImage(
pic2.getImage(),
speed,
Constant.RIGHT_BOBO_Y,
Constant.RIGHT_BOBO_WIDTH,
Constant.RIGHT_BOBO_HIGHT,
null);
}
}
//监测的位置
public Rectangle getRec(){
return new Rectangle(speed,40,80,37);
}
// 监测是否撞击到对面的人
public boolean isHitLestMan(LeftMan man){
if(this.getRec().intersects(man.getRec()) &&
this.isLive == true &&
man.isLive() == true){//&&man.getislive() == ture
this.isLive = false;
//人会死
man.setLive(false);
return true;
}
return false;
}
//监测是否撞击到对面飞过来的子弹
public boolean isHitRightBobo(List<LeftBobo> bobo){
int num;
if(canavas.getListLeftBobo().size()>canavas.getListRightBobo().size()){
num = canavas.getListRightBobo().size();
}else{
num = canavas.getListLeftBobo().size();
}
for(int i=0;i<num;i++){
if(this.getRec().intersects(canavas.getListLeftBobo().get(i).getRec())){
canavas.getListRightBobo().remove(i);
canavas.getListLeftBobo().remove(i);
return true;
}
}
return false;
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: RightMan
* Author: meng
* Date: 2018/7/10 11:44
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.wust.fightingGame;
import sun.audio.AudioPlayer;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author meng
* @create 2018/7/10
* @since 1.0.0
*/
public class RightMan {
//存放图片信息
private ImageIcon[] pic;
private ImageIcon picDeath;
//是否活着
private boolean isLive;
//技能
private RightBobo bobo;
//攻击时的音频
private File shout;
//死亡的时候的音频
private File died;
//画布
private GameCanavas canavas;
public RightMan(GameCanavas gameCanavas) {
canavas = gameCanavas;
pic = new ImageIcon[5];
//初始化死亡的图片
picDeath = new ImageIcon(Constant.DEATH_PIC);
//还原为初始化
pic[4] = new ImageIcon(Constant.RIGHT_MAN5_PIC);
pic[0] = new ImageIcon(Constant.RIGHT_MAN1_PIC);
pic[1] = new ImageIcon(Constant.RIGHT_MAN2_PIC);
pic[2] = new ImageIcon(Constant.RIGHT_MAN3_PIC);
pic[3] = new ImageIcon(Constant.RIGHT_MAN4_PIC);
//初始化声音
shout = new File(Constant.SHOUT_SOUND);
died = new File(Constant.DEATH_SOUND);
//标记自己开始是活着的
isLive = true;
}
public void setLive(boolean live) {
isLive = live;
}
public boolean isLive() {
return isLive;
}
public void draw(Graphics graphics, boolean isHit){
if(isLive){
//画静止的人
graphics.drawImage(pic[0].getImage(),
Constant.RIGHT_MAX_X,
Constant.RIGHT_MAX_Y,
Constant.RIGHT_MAN_WIDTH,
Constant.RIGHT_MAN_HEIGHT,
null);
fire(graphics, isHit);
if(bobo !=null){
if(bobo.isHitLestMan(canavas.getLeftMan())){
canavas.getLeftMan().setLive(false);
}
if(bobo.isHitRightBobo(canavas.getListLeftBobo())){
}
}
}else{
//画死亡状态
graphics.drawImage(picDeath.getImage(),
Constant.RIGHT_MAX_X,
Constant.RIGHT_MAX_Y,
Constant.RIGHT_MAN_WIDTH,
Constant.RIGHT_MAN_HEIGHT,
null);
}
}
public void fire(Graphics graphics,boolean isFire){
if(isFire) {
for (int i = 0; i < 4; i++) {
graphics.drawImage(
pic[i].getImage(),
Constant.RIGHT_MAX_X,
Constant.RIGHT_MAX_Y,
Constant.RIGHT_MAN_WIDTH,
Constant.RIGHT_MAN_HEIGHT,
null);
}
//添加音频
try {
AudioPlayer.player.start(shout.toURI().toURL().openStream());
} catch (IOException e) {
e.printStackTrace();
}
bobo = new RightBobo(graphics,canavas);
canavas.getListRightBobo().add(bobo);
}
}//end fire
public Rectangle getRec(){
return new Rectangle(350,5,135,115);
}
}
项目网站代码: