Java实现RPG游戏

一.实验目的

  1. 掌握面向对象程序设计的方法。
    明确类与对象的概念,掌握面向对象设计七大原则;
  2. 掌握常见的设计模式以及类图的描述。

二、UML类图
在这里插入图片描述
三、实验要求
1.功能描述几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户 自己来创建自己喜欢的角色。
本次上机要求编写一个简化的创建游戏角色的程序。
在这里插入图片描述
2.游戏角色应有的属性本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。

3.职业限制很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
在这里插入图片描述

4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
在这里插入图片描述

四、程序实现
姓名和性别


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

    public String getName() {
        return name;
    }

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

    public int getGender() {
        return gender;
    }

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

    public int Setting() {
        System.out.println("请输入角色姓名:");
        Scanner sc = new Scanner(System.in);
        this.name = sc.next();
        while (true) {
            System.out.println("输入角色性别:(0.男    1.女)");
            this.gender = sc.nextInt();
            if (gender == 0 || gender == 1) {
                break;
            } else {
                System.out.println("请选择0或1来确认性别");
            }
        }
        return gender;
    }

    public void output(){
        System.out.println("------------------------");
        System.out.println("姓名\t\t\t"+this.name);
        System.out.println("------------------------");
        if(this.gender==0){
            System.out.println("性别\t\t\t"+"男性");
        }else{
            System.out.println("性别\t\t\t"+"女性");
        }
    }
}

种族和职业

import java.util.Scanner;

public class raceAndCareer {
    private int race;
    private int career;
    private String[] races = {"人类","精灵","兽人","矮人","元素"};
    private String[] careers = {"狂战士","圣骑士","刺客","猎手","祭司","巫师"};

    public int getRace() {
        return race;
    }

    public void setRace(int race) {
        this.race = race;
    }

    public int getCareer() {
        return career;
    }

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

    public int SelectRace(){
        while(true){
            System.out.println("请选择角色的种族(0人类,1精灵,2兽人,3矮人,4元素):");
            Scanner sc = new Scanner(System.in);
            this.race = sc.nextInt();
            if(race >= 0 && race <= 4){
                //输入正确跳出循环
                break;
            }else{
                System.out.println("请输入0-4之间的数字选择种族");
            }
        }
        return race;
    }
    public int SelectCareer(int race){
        switch(race){
            case 0:
                while(true){
                    System.out.println("请选择职业(0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师):");
                    Scanner sc = new Scanner(System.in);
                    if(career >= 0 && career <=5){
                        //输入正确跳出循环
                        break;
                    }else{
                        System.out.println("请输入0-5之间的数字选择职业。");
                    }
                }
                break;
            case 1:
                while(true){
                    System.out.println("请选择职业(2刺客,3猎手,4祭司,5巫师):");
                    Scanner sc = new Scanner(System.in);
                    this.career = sc.nextInt();
                    if(career >= 2 && career <= 5){
                        break;
                    }else{
                        System.out.println("请输入2-5之间的职业选择职业");
                    }
                }
                break;
            case 2:
                while(true){
                    System.out.println("请选择职业(0狂战士,3猎手,4祭司):");
                    Scanner sc = new Scanner(System.in);
                    this.career = sc.nextInt();
                    if(career == 0 || career == 3 || career == 4){
                        break;
                    }else{
                        System.out.println("请输入0、3、4选择职业");
                    }
                }
                break;
            case 3:
                while(true){
                    System.out.println("请选择职业(0狂战士,1圣骑士,4祭司):");
                    Scanner sc = new Scanner(System.in);
                    this.career = sc.nextInt();
                    if(career == 0 || career == 1 || career == 4){
                        break;
                    }else{
                        System.out.println("请输入数字0、1或4选择职业");
                    }
                }
                break;
            case 4:
                while(true){
                    System.out.println("请选择职业(4祭司,5巫师):");
                    Scanner sc = new Scanner(System.in);
                    this.career = sc.nextInt();
                    if(career == 4 || career == 5){
                        break;
                    }else{
                        System.out.println("请输入4或5来选择职业!");
                    }
                }
                break;
            default:
                break;
        }
        return career;
    }

    public void output(){
        System.out.println("-----------------------");
        System.out.println("种族\t\t\t"+races[this.race]);
        System.out.println("------------------------");
        System.out.println("职业\t\t\t"+careers[this.career]);
    }
}

职业和属性

import java.util.Random;

public class CareerAttribute {
    private int strength;
    private int flexibility;
    private int physical;
    private int intelligence;
    private int wisdom;
    private int HP;  //生命值
    private int MP;  //魔法值

    public int getStrength() { return strength;}

    public void setStrength(int strength) {this.strength = strength;}

    public int getFlexibility() { return flexibility;}

    public void setFlexibility(int flexibility) { this.flexibility = flexibility; }

    public int getPhysical() { return physical; }

    public void setPhysical(int physical) { this.physical = physical; }

    public int getIntelligence() { return intelligence; }

    public void setIntelligence(int intelligence) { this.intelligence = intelligence; }

    public int getWisdom() { return wisdom; }

    public void setWisdom(int wisdom) { this.wisdom = wisdom; }

    public int getHP() { return HP; }

    public void setHP(int HP) { this.HP = HP; }

    public int getMP() { return MP; }

    public void setMP(int MP) { this.MP = MP; }

    public void AutoAttribute(int str,int flex,int phy,int intell,int wis){
        int sum = 0;
        Random random = new Random();
        do{
            strength = random.nextInt(5)%10 + str;
            flexibility = random.nextInt(5)%10 + flex;
            physical = random.nextInt(5)%10 + phy;
            intelligence = random.nextInt(5)%10 + intell;
            wisdom = random.nextInt(5)%10 + wis;
            sum = strength + flexibility + physical + intelligence + wisdom;
        }while(sum != 100);
        HP = physical * 20;
        MP = (wisdom + intelligence) *10;
    }

    public void initialAttribute(int career){
        if(career == 0){
            AutoAttribute(40,20,30,5,5);
        }
        if(career == 1){
            AutoAttribute(25,15,30,20,10);
        }
        if(career == 2){
            AutoAttribute(20,35,20,15,10);
        }
        if(career == 3){
            AutoAttribute(15,40,15,10,20);
        }
        if(career == 4){
            AutoAttribute(15,20,15,35,15);
        }
        if(career == 5){
            AutoAttribute(10,20,10,20,40);
        }
    }

    public void output(){
        System.out.println("----------------------------");
        System.out.println("力量\t\t\t"+this.strength);
        System.out.println("----------------------------");
        System.out.println("敏捷\t\t\t"+this.flexibility);
        System.out.println("----------------------------");
        System.out.println("体力\t\t\t"+this.physical);
        System.out.println("-----------------------------");
        System.out.println("智力\t\t\t"+this.intelligence);
        System.out.println("------------------------------");
        System.out.println("智慧\t\t\t"+this.wisdom);
        System.out.println("------------------------------");
        System.out.println("生命值\t\t\t"+this.HP);
        System.out.println("-------------------------------");
        System.out.println("魔法值\t\t\t"+this.MP);
        System.out.println("--------------------------------");
    }
}

RolePlaying

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class RolePlaying {
    public static void main(String[] args) {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        Role role = new Role();
        raceAndCareer rac = new raceAndCareer();
        CareerAttribute ca = new CareerAttribute();
        do{
            role.Setting();
            int race = rac.SelectRace();
            rac.SelectCareer(race);
            role.output();
            rac.output();
            ca.initialAttribute(rac.getCareer());
            ca.output();
            System.out.println("是否满意角色属性?(0/1)若不满意,则重新创建。");
            String str = sc.next();
            if("0".equals(str) || "1".equals(str)){
                break;
            }
        }while(flag);
        saveRoleInformation(role,rac,ca);
        System.out.println("角色信息已成功保存。");
    }
    public static void saveRoleInformation(Role role,raceAndCareer rac,CareerAttribute ca){
        try {
            FileWriter desFile = new FileWriter("E:\\程序设计方法学\\RPG游戏\\roles_information.txt",true);
            BufferedWriter out = new BufferedWriter(desFile);
            out.write("姓名\t\t\t"+role.getName());
            out.newLine();
            if(role.getGender()==0){
                out.write("性别\t\t\t"+"男性");
            }else{
                out.write("性别\t\t\t"+"女性");
            }
            out.newLine();
            switch(rac.getRace()){
                case 0:
                    out.write("种族\t\t\t"+"人类");
                    break;
                case 1:
                    out.write("种族\t\t\t"+"精灵");
                    break;
                case 2:
                    out.write("种族\t\t\t"+"兽人");
                case 3:
                    out.write("种族\t\t\t"+"矮人");
                    break;
                case 4:
                    out.write("种族\t\t\t"+"元素");
                    break;
                default:
                    break;
            }
            out.newLine();
            switch(rac.getCareer()){
                case 0:
                    out.write("职业\t\t\t"+"狂战士");
                    break;
                case 1:
                    out.write("职业\t\t\t"+"圣骑士");
                    break;
                case 2:
                    out.write("职业\t\t\t"+"刺客");
                    break;
                case 3:
                    out.write("职业\t\t\t"+"猎手");
                    break;
                case 4:
                    out.write("职业\t\t\t"+"祭司");
                    break;
                case 5:
                    out.write("职业\t\t\t"+"巫师");
                    break;
                default:
                    break;
            }
            out.newLine();
            out.write("力量\t\t\t"+ca.getStrength());
            out.newLine();
            out.write("敏捷\t\t\t"+ca.getFlexibility());
            out.newLine();
            out.write("体力\t\t\t"+ca.getPhysical());
            out.newLine();
            out.write("智力\t\t\t"+ca.getIntelligence());
            out.newLine();
            out.write("智慧\t\t\t"+ca.getWisdom());
            out.newLine();
            out.write("生命值\t\t\t"+ca.getHP());
            out.newLine();
            out.write("魔法值\t\t\t"+ca.getMP());
            out.newLine();
            out.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

五、运行结果
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


版权声明:本文为rrrrghi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。