"Java" a small game made with an object-oriented method

1. Design requirements

2. Demonstration effect

"Java" a small game made with an object-oriented method

3. Code implementation

Part 1: Player

package GAME;
import java.util.Scanner;
public class Player {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到小型文字扮演游戏《假传奇》,我该如何称呼您?");
        String name = scanner.next();
        System.out.println("角色创建完毕,正式开起你的游戏之旅");
        System.out.println("**********************************");
        System.out.println("有这些角色供你选择:1.金桑 技能1:嘲讽 技能2:高原血统");
        System.out.println("2.老许 技能1:召唤老婆 技能2:甜蜜暴击");
        System.out.println("3.曹欧巴 技能1:丘比特之箭  技能2:芳心纵火");
        System.out.println("4.李馆长 技能1:扔哑铃 技能2:喝蛋白粉");

        Role role = new Role();
        int i = scanner.nextInt();
        int hp1=50;
        int attack1=3;
        int hp2=75;
        int attack2=10;
        int hp3=100;
        int attack3=20;
        role.Career(i);
        int hp = role.getHP();
        int attack=role.getAttackPower();
        int exp=role.getExp();
        String skill1=role.getSkill1();
        String skill2=role.getskill2();
        int skills1=role.skills(1);//技能1造成的伤害
        int skills2=role.skills(2);//技能2造成的伤害
        System.out.println("你选择的是" + role.getCareer());
       /* case 2:
            role.Career(2);
            System.out.println("你选择的是老许");
            break;
        case 3:
            role.Career(3);
            System.out.println("你选择的是曹欧巴");
            break;
        case 4:
            role.Career(4);
            System.out.println("你选择的是李馆长");
            break;*/

        for (int a = 0; ; ) {
    
    
            System.out.println("新手村村长:你现在要做什么? 1.打怪升级 2.学习技能 3.疗伤");
            switch (scanner.nextInt()) {
    
    
                case 1:
                    System.out.println("现在有这些地方供你选择 1.新手村 2.蝴蝶谷 3.梅花山庄");
                    int b = scanner.nextInt();
                    System.out.println("现在有以下敌人供你选择:1.土匪 2.强盗 3.匪首");
                    switch (scanner.nextInt()) {
    
    //问题:不受控制

                        case 1:
                            System.out.println("你遭遇了土匪");
                            while (true) {
    
    
                                System.out.println("你可以选择以下攻击方式 1.攻击 2.施法");
                                int f = scanner.nextInt();
                                if (f == 1) {
    
    
                                    hp1 = hp1 - attack;
                                    hp = hp - attack1;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp1);
                                } else if (f == 2) {
    
    
                                    System.out.println("请选择使用技能1"+role.getSkill1()+"技能2"+role.getskill2());
                                    int attacks = role.skills(scanner.nextInt());
                                    hp1 = hp1 - attacks;
                                    hp = hp - attack1;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp1);
                                }

                                if (hp1 <= 0) {
    
    
                                    System.out.println("你已打败土匪");
                                    exp=exp+50;
                                    System.out.println("你现在的经验是"+exp);
                                    hp1=50;
                                    break;
                                }

                                if (hp < 0) {
    
    
                                    System.out.println("你已被打败");
                                }

                            }

                            break;


                        case 2:
                            System.out.println("你遭遇了强盗");
                            while (true) {
    
    
                                System.out.println("你可以选择以下攻击方式 1.攻击 2.施法");
                                int f = scanner.nextInt();
                                if (f == 1) {
    
    
                                    hp2 = hp2 - attack;
                                    hp = hp - attack2;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp2);
                                } else if (f == 2) {
    
    
                                    System.out.println("请选择使用技能1"+role.getSkill1()+"技能2"+role.getskill2());
                                    int attacks = role.skills(scanner.nextInt());
                                    hp2 = hp2 - attacks;
                                    hp = hp - attack2;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp2);
                                }

                                if (hp2 <= 0) {
    
    
                                    System.out.println("你已打败土匪");
                                    exp=exp+75;
                                    System.out.println("你现在的经验是"+exp);
                                    hp2=75;

                                    break;
                                }
                                if (hp < 0) {
    
    
                                    System.out.println("你已被打败");
                                }
                                break;
                            }


                        case 3:
                            System.out.println("你遭遇了匪首");
                            while (true) {
    
    
                                System.out.println("你可以选择以下攻击方式 1.攻击 2.施法");
                                int f = scanner.nextInt();
                                if (f == 1) {
    
    
                                    hp3 = hp3 - attack;
                                    hp = hp - attack3;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp3);
                                } else if (f == 2) {
    
    
                                    System.out.println("请选择使用技能1"+role.getSkill1()+"技能2"+role.getskill2());
                                    int attacks = role.skills(scanner.nextInt());
                                    hp3 = hp3 - attacks;
                                    hp = hp - attack3;
                                    System.out.println(name + "剩余的生命值是:" + hp + "敌人剩余生命值" + hp3);
                                }

                                if (hp3 <= 0) {
    
    
                                    System.out.println("你已打败土匪");
                                    hp3=100;
                                    exp=exp+100;
                                    System.out.println("你现在的经验是"+exp);
                                    break;
                                }
                                if (hp < 0) {
    
    
                                    System.out.println("你已被打败");
                                }
                            }
                            break;
                    }

                    break;
                case 2://学习技能
                    if (exp>=200&&exp<300){
    
    
                        System.out.println("你提升的技能是"+skill1);
                        skills1=skills1+5;
                        exp=exp-200;
                        System.out.println("现在技能"+skill1+"伤害是"+skills1);
                        System.out.println("目前经验值剩余"+exp);
                    }
                    else if (exp>=300){
    
    
                        System.out.println("你提升的技能是"+skill2);
                        skills2=skills2+10;
                        exp=exp-300;
                        System.out.println("现在技能"+skill2+"伤害是"+skills2);
                        System.out.println("目前经验值剩余"+exp);
                    }


                    else
                        System.out.println("你的经验不足,请继续刷怪积累经验");

                    break;
                case 3://疗伤
                    System.out.println("你目前的生命值是"+hp);
                    hp= role.getHP();
                    System.out.println("治疗之后,你的生命值是"+hp);
            }
        }
    }
}











Part 2: Role

package GAME;

public class Role {
    
    
    private String career =null ;//角色职业
    private int attackPower = 0;//角色攻击力
    private int HP = 0;//生命值
    private String skill1 = null;
    private String skill2=null;
    private int exp=0;
//        public Role(String career, int attackPower, int HP,String skill) {
    
    
//            this.career = career;
//            this.attackPower = attackPower;
//            this.HP = HP;
//            this.skill=skill;
    //   }

    public String getCareer() {
    
    
        return career;
    }

    public int getAttackPower() {
    
    
        return attackPower;
    }

    public int getHP() {
    
    
        return HP;
    }

    public String getSkill1(){
    
    
        return skill1;
    }

    public void setCareer(String career) {
    
    
        this.career = career;
    }

    public void setAttackPower(int attackPower) {
    
    
        this.attackPower = attackPower;
    }

    public void setHP(int HP) {
    
    
        this.HP = HP;
    }
    public void setSkill1(String skill){
    
    this.skill1=skill;}
    public String getskill2(){
    
    return skill2;}
    public void setskill2(String skill2){
    
    this.skill2=skill2;}
    public void setExp(int exp ){
    
    this.exp=exp;}
    public int getExp(){
    
    return exp;}

    //选择职业

    public  void Career(int i){
    
    
        //System.out.println("请选择你的职业:0金桑、1老许、2李哑铃、3曹欧巴");
        //String s;
        // s=in.nextLine();
        // Scanner scanner= new Scanner(System.in);
        //int i =scanner.nextInt();
        if(i == 1){
    
    
            this.career="金桑";
            this.attackPower=10;
            this.HP=400;
            this.skill1="嘲讽";
            this.skill2="高原血统";
        }
        if(i == 2) {
    
    
            this.career = "老许";
            this.attackPower=40;
            this.HP=100;
            this.skill1="召唤老婆";
            this.skill2="甜蜜暴击";
        }

        if(i == 3){
    
    
            this.career="曹欧巴";
            this.attackPower=30;
            this.HP=200;
            this.skill1="丘比特之箭";
            this.skill2="芳心纵火";
        }
        if (i == 4) {
    
    
            this.career="李馆长";
            this.attackPower=20;
            this.HP=300;
            this.skill1="扔哑铃";
            this.skill2="喝蛋白粉";
        }

    }
    public int skills(int i){
    
    
        if (i==1){
    
    
            return 20;
        }
        else if (i==2){
    
    
            return 30;
        }
        else {
    
    
            System.out.println("输入错误");
            return 0;
        }
    }
}

4. Self-feeling

In this project design, we first understood the entire project and conceived a framework. The characters, monsters, and NPCs were set into three categories, and the skill attributes of the characters were discussed again, and they were virtualized into character identities with personal characteristics. And the skills and corresponding attributes that the character can have. The so-called inspiration comes from life. At this time, the character is easy to create, but at this time, what is oncoming is the character’s skills. At this time, we exaggerate the use of the character’s life habits or characteristics, and then the character class is determined. Next is the NPC and the map. We have a simple design and a simple little game that is entangled with the main line. Did not use too many object-oriented knowledge points. **Summary:** This time the level of participation is not high or even a draw, the overall code implementation is handed over to the "curator Li" (work hard). The reason is that there are too many knowledge points in arrears, so slowly start to make up.

Guess you like

Origin blog.csdn.net/weixin_45662838/article/details/114893995