JAVA实训新增功能

1.新增人物技能

主要代码

StaticValue.JAVA

public static List leftSkillImgs= new ArrayList<>();
public static List 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==76){
     this.person.skill();
}

代码解释:在StaticValue类中添加两个LIst集合存放新的图片,通过for循环将图片存入集合。在Person类中在setImages方法中存入技能图片。新增skill方法来更新run方法,实现case 6和case -6。更新敌人的死亡条件,将case 6和case-6写入if条件。在MyFrame类的keyPressed方法中写入如果按L可以实现person.skill方法。

2.添加敌人死亡倒地效果动画

主要代码

StaticValue.JAVA

//在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();
        }
    }

代码解释:在StaticValue类中通过for循环将敌人倒地的图片存入EnemyImgs的left和right集合中。在Enemy类中重写dead方法,设置定时器用于检测播放动画是否结束,通过if实现左右两边的死亡动画效果。

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;
            }
        }
    }

代码解释:在MyFrame类的paint方法中通过graphics绘画出蓝条的位置大小,定义manaWidth来实现蓝条的减少。在Person类中定义Mana初始化蓝条为100,在skill方法中在最外层套if限制方法的使用,定义每次使用蓝条减10。

4.增加人物血条和扣血效果

在血条清零后会自动关闭程序

主要代码

Person.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);

MyFrame.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;
            }
        }
    }

代码解析:在MyFrame类的paint方法中通过graphics绘画出血条的位置大小,定义healthWidth来实现血条的减少。通过if判定血条清零则关闭游戏。在Person类中定义health初始为100,增加get方法返回血条值,在run方法中增加if来判定碰撞后扣血效果。

具体效果实现

1.技能效果

2.敌人死亡倒地效果

3.蓝条使用效果

4.血条的效果

来源链接:https://www.cnblogs.com/505-jw/p/18647745

,3.增加技能蓝条和使用次数

来源链接:https://www.cnblogs.com/505-jw/p/18647745

© 版权声明
THE END
支持一下吧
点赞13 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码

    暂无评论内容