文字格鬥遊戲

WuHu小猪猪發表於2024-07-30

文字格鬥遊戲

import java.util.Random;

public class Role {
    private String name;
    private int blood;

    public Role() {
    }

    public Role(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }
    //定義一個方法用於攻擊別人
    //誰攻擊誰?
    //Role r1 = new Role();
    //Role r2 = new Role();
    //r1.攻擊(r2);
    //方法的呼叫者.攻擊(引數);
    public void attack(Role role){
        //計算造成的傷害
        Random r = new Random();
        int hurt = r.nextInt(20)+1;
        //剩餘血量    (使用就要用get(),有賦值這個操作就用set() 。)
        int remainblood = role.getBlood() - hurt;
        //對剩餘血量做驗證,為負數,就修改為0
        remainblood = remainblood < 0?0:remainblood;
        //修改捱揍人的血量
        role.setBlood(remainblood);
        //this代表方法的呼叫者
        System.out.println(this.getName() + "舉起拳頭打了" + role.getName() + "一下" + "造成了" + hurt +
                "點傷害" + role.getName() + "還剩下" + remainblood +"點血。");
    }
}
public class TextGame {
    public static void main(String[] args) {
        Role r1 = new Role("喬峰",100);
        Role r2 = new Role("鳩摩智",100);
        //開始格鬥
        while(true){
            r1.attack(r2);
            if(r2.getBlood() == 0){
                System.out.println(r1.getName()+"OK了"+r2.getName());
                break;
            }
            r2.attack(r1);
            if(r1.getBlood() == 0){
                System.out.println(r2.getName()+"OK了"+r1.getName());
                break;
            }
        }
    }
}

%S 與 souf

%s的作用是佔位

souf分為兩部分引數:

  1. 要輸出的內容%s
  2. 要填充的資料
System.out.printf("你好%s","張三");

ctrl + shift + / 快速

import java.util.Random;

public class FightingGameOptimization {
    private String name;
    private int blood;
    private char gender;
    private String face;

    String[] boyfaces={"面目猙獰","玉樹臨風","相貌平平"};
    String[] grilfaces={"慘不忍睹","沉魚落雁","亭亭玉立"};

    //攻擊描述
    String[] attack_desc={"%s使用了[電光一閃]向%s的胸口打去。","%s使用[十萬伏特]釋放了無數的雷電朝向%s"};
    //傷害描述
    String[] injured_desc={"%s成功使用了[快躲開],毫髮無損。","%s似乎受了很嚴重的傷害,但他撐住了!!!發動了[站起來]。"};
    public FightingGameOptimization() {
    }

    public FightingGameOptimization(String name, int blood,char gender) {
        this.name = name;
        this.blood = blood;
        this.gender = gender;
        //長相是我們輸入性別後,隨機給的,不需要我們給資訊。
        setFace(gender);
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public String getFace() {
        return face;
    }

    public void setFace(char gender) {
        Random r =new Random();
        if (gender == '男'){
            int index=r.nextInt(boyfaces.length);
            this.face=boyfaces[index];
        } else if (gender == '女') {
            int index=r.nextInt(grilfaces.length);
            this.face=grilfaces[index];
        }else {
            System.out.println("此人面目猙獰!");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }
    //定義一個方法用於攻擊別人
    //r1.攻擊(r2);
    //方法的呼叫者.攻擊(引數);
    public void attack(FightingGameOptimization role){

        Random r = new Random();
        int index = r.nextInt(attack_desc.length);
        String kongFu = attack_desc[index];

        //輸出一個攻擊效果
        System.out.printf(kongFu,this.getName(),role.getName());
        System.out.println();
        //計算造成的傷害
        int hurt = r.nextInt(20)+1;

        //剩餘血量    (使用就要用get(),有賦值這個操作就用set() 。)
        int remainblood = role.getBlood() - hurt;

        //對剩餘血量做驗證,為負數,就修改為0
        remainblood = remainblood < 0?0:remainblood;
        //修改捱揍人的血量
        role.setBlood(remainblood);
        //受傷的描述
        /*\
        如果血量大於90,0索引的描述。
        以此類推
         */
        if (remainblood > 50){
            System.out.printf(injured_desc[0],role.getName());
        }else{
            System.out.printf(injured_desc[1],role.getName());
        }
        System.out.println();

    }
    public void showRoleInfo(){
        System.out.println("姓名為:"+getName());
        System.out.println("血量為:"+getBlood());
        System.out.println("性別為:"+getGender());
        System.out.println("長相為:"+getFace());
    }
}
public class TextGameTwo {
    public static void main(String[] args) {
        FightingGameOptimization r1=new FightingGameOptimization("喬峰",100,'男');
        FightingGameOptimization r2=new FightingGameOptimization("鳩摩智",100,'男');

        r1.showRoleInfo();
        r2.showRoleInfo();
        while(true){
            r1.attack(r2);
            if(r2.getBlood() == 0){
                System.out.println(r1.getName()+"OK了"+r2.getName());
                break;
            }
            r2.attack(r2);
            if(r1.getBlood() == 0){
                System.out.println(r2.getName()+"OK了"+r1.getName());
                break;
            }
        }
    }
}

相關文章