1.添加人物技能
主要代码
StaticValue.JAVA
点击查看代码
public static List<BufferedImage> leftSkillImgs= new ArrayList<>();
public static List<BufferedImage> rightSkillImgs = new ArrayList<>();//Static中
for (int i = 0; i <=7; i++) {File rightfile = new File(ImagePath+"skill/right/"+i+".png");File leftfile = new File(ImagePath+"skill/left/"+i+".png");rightSkillImgs.add(ImageIO.read(rightfile));leftSkillImgs.add(ImageIO.read(leftfile));
Person.JAVA
点击查看代码
protected List<BufferedImage> leftSkillImages;
protected List<BufferedImage> rightSkillImages;
//setImageList方法leftSkillImages=StaticValue.leftSkillImgs.subList(0,7);rightSkillImages=StaticValue.rightSkillImgs.subList(0,7);
//新增skill方法public void skill(){if (this.status!=3&&this.status!=-3){if (this.status>0){this.status=6;}else{this.status=-6;}if (this.moving>=7){this.status= this.status>0?1:-1;this.y=235;}}}
//在run方法增加
case 6:
this.y=350;
if (this.moving>7){
this.moving=0;
}
this.showImage=rightSkillImages.get((int)moving);moving+=0.5;
if (this.moving>=7){
this.status= this.status>0?1:-1;
this.y=235;
}break;
case -6:
this.y=350;
if (this.moving>7){ this.moving=0;
}
this.showImage=leftSkillImages.get((int)moving);
moving+=0.5;
if (this.moving>=7){
this.status= this.status>0?1:-1;
this.y=235;
}
break;
//修改其中攻击敌人死亡的条件
if (this.backGround !=null){
List<Enemy> allEnemy = this.backGround.getAllEnemy();
for (int i=0;i<allEnemy.size();i++){
Enemy enemy = allEnemy.get(i);
if (this.status ==4 && (this.x + 125)>enemy.getX() && (this.x+125)<enemy.getX()+250||this.status ==6 && (this.x + 125)>enemy.getX() && (this.x+125)<enemy.getX()+250){
enemy.dead();
} else if (this.status ==-4 &&(this.x+125)<(enemy.getX()+400)&&(this.x+125)>enemy.getX()+256||this.status ==-6 &&(this.x+125)<(enemy.getX()+400)&&(this.x+125)>enemy.getX()+256) {
enemy.dead();}}
}
//在move方法添加
case 6:
break;
case -6:
break;
MyFrame.JAVA
点击查看代码
//在keyPressed中添加
if (keyCode==74){
this.person.attack();
}
2.添加敌人死亡倒地效果动画
主要代码
StaticValue
点击查看代码
//在static中增加
for (int i=0 ;i<5;i++){File rightfile = new File(ImagePath+"enemy/right/1555_death_"+i+".png");File leftfile = new File(ImagePath+"enemy/left/1555_death_"+i+".png");leftEnemyImgs.add(ImageIO.read(leftfile));rightEnemyImgs.add(ImageIO.read(rightfile));
Enemy.JAVA
点击查看代码
private boolean isDeathAnimationPlaying = false;
//在setImageList
leftDeathImages=StaticValue.leftEnemyImgs.subList(20,25);
rightDeathImages=StaticValue.rightEnemyImgs.subList(20,25);
//重新写dead方法
public void dead() {if (this.status == 2) {this.status = 5;isDeathAnimationPlaying = true; // 启动一个定时器或者计数器,用于检查死亡动画是否播放完毕new Thread(() -> {try { // 假设死亡动画持续时间为3秒,这里可以根据实际情况调整Thread.sleep(900);isDeathAnimationPlaying = false;if (this.status == 5 &&!isDeathAnimationPlaying) {this.backGround.getAllEnemy().remove(this);}} catch (InterruptedException e) {e.printStackTrace();}}).start();} else if (this.status == -2) {this.status = -5;isDeathAnimationPlaying = true; // 同理,对于另一种方向的死亡动画也进行类似处理new Thread(() -> {try {Thread.sleep(900);isDeathAnimationPlaying = false;if (this.status == -5 &&!isDeathAnimationPlaying) {this.backGround.getAllEnemy().remove(this);}} catch (InterruptedException e) {e.printStackTrace();}}).start();}}
3.增加技能蓝条和使用次数
MyFrame.JAVA
点击查看代码
//在paint方法中增加
graphics.setColor(Color.BLACK);graphics.drawRect(50,80,200,20);int manaWidth= (int)(person.getMana()/100*200);graphics.setColor(Color.BLUE);graphics.fillRect(50,80,manaWidth,20);graphics.setColor(Color.BLACK);
Person.JAVA
点击查看代码
protected double Mana=100.0;
//在skill方法中添加if限制技能使用次数
public void skill(){if (this.Mana!=0){if (this.status!=3&&this.status!=-3){if (this.status>0){this.status=6;}else{this.status=-6;}if (this.moving>=7){this.status= this.status>0?1:-1;this.y=235;}this.Mana-=10;}}}
具体效果实现
1.技能效果
2.敌人死亡倒地效果
3.蓝条使用效果