JAVA练习:文字格斗游戏

题目:设计一个程序,定义一个游戏类,在主函数中可以调用游戏类并给两个玩家的属性赋值和运行游戏。

1.实现思路(候补)

2.代码实现截图:

3.游戏类代码:

public class gdGame {
    private String name;
    private int hp;
    private String xb;
    private String face;
    public gdGame(String name,int hp,String xb){
        this.name=name;
        this.hp=hp;
        this.xb=xb;
    }

    public String getName() {
        return name;
    }

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

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setXb(String xb) {
        this.xb = xb;
    }
String[] xb_n={"风流倜傥","气宇轩昂","相貌平平","一塌糊涂","面目狰狞"};
    String[] xb_m={"美奂绝伦","沉鱼落雁","相貌平平","相貌简陋","惨不忍睹"};
    public String setFace(gdGame g) {
        if(g.xb=="男"){
            Random r=new Random();
            int xb=r.nextInt(5);
            this.xb=xb_n[xb];
            return this.xb;
        }else if(g.xb=="女"){
            Random r=new Random();
            int xb=r.nextInt(5);
            this.xb=xb_m[xb];
            return this.xb;
        }
        return "";
    }

    public void show(){
        System.out.println("名称:"+this.name);
        System.out.println("血量:"+this.hp);
        System.out.println("性别"+this.xb);
        System.out.println("性格:"+setFace(this));
    }
    String[] ttk={
            "%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去,",
            "%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s,",
            "%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿,",
            "%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s,",
            "%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s,",
            "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s,"
    };
    String[] bttk={
            "结果%s退了半步,毫发无损",
            "结果给%s造成一处瘀伤",
            "结果一击命中,%s痛得弯下腰",
            "结果%s痛苦地闷哼了一声,显然受了点内伤",
            "结果%s摇摇晃晃,一跤摔倒在地",
            "结果%s脸色一下变得惨白,连退了好几步",
            "结果『轰』的一声,%s口中鲜血狂喷而出",
            "结果%s一声惨叫,像滩软泥般塌了下去"
    };
    public void akt(gdGame b){
        Random r=new Random();
        int hurt=r.nextInt(20);
        b.hp-=hurt;
        Random w=new Random();
        int ww=w.nextInt(6);
        Random m=new Random();
        int mm=w.nextInt(6);
        String ttkk=ttk[ww];
        String bttkk=bttk[mm];
        System.out.printf(ttkk,this.name,b.name);
        System.out.printf(bttkk,b.name);
        System.out.println("剩余血量为:"+b.hp);
        if(b.hp<=0){
            System.out.println(this.name+"把"+b.name+"打死了");
            System.exit(0);
        }
    }
}

4.测试代码:

public class gdGameTest {
    public static void main(String[] args) {
        gdGame p1 = new gdGame("吕布", 100,"男");
        gdGame p2 = new gdGame("貂蝉", 100,"女");
        p1.show();
        p2.show();

        while(true){
        p1.akt(p2);
        p2.akt(p1);
        }

    }
}

将悲伤的事对半分吧,将快乐的事拼凑成双吧