RPG游戏角色创建
实验内容
1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
题目分析
1.分析各个角色及其属性,一个游戏角色应该包括姓名、性别、职业、种族,同时,职业会限制某些种族选择,属性值随机并且与职业相关。
2.建立三个类,分别用来设置姓名和性别、种族和职业、属性值,并根据需求添加函数实现相关功能。
3.创建一个save类,询问用户是否满意当前角色,如果用户不满意则重新创建,若用户满意则将用户创建角色的相关信息写入文件保存。
UML类图
代码
import java.util.Scanner;
public class Role {
private String name;
private int sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public void input() {
System.out.println("请输入角色姓名:");
Scanner in = new Scanner(System.in);
this.name = in.next();//返回字符串类型
while (true) {
System.out.println("输入角色性别:(0男 1女)");
this.sex = in.nextInt();//返回int类型
if (sex == 0 || sex == 1) {
break;
} else
{
System.out.println("请在0和1之间选择!");
}
}
}
public void output(){
System.out.println("------------------------");
System.out.println("姓名\t\t\t"+this.name);
System.out.println("------------------------");
if(this.sex==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 in = new Scanner(System.in);
this.race = in.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 in = new Scanner(System.in);
this.career = in.nextInt();
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 in = new Scanner(System.in);
this.career = in.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 in = new Scanner(System.in);
this.career = in.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 in = new Scanner(System.in);
this.career = in.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 in = new Scanner(System.in);
this.career = in.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 Attribute {
private int strength; //力量
private int agility; //敏捷
private int physical; //体力
private int intelligence; //智力
private int wisdom; //智慧
private int blood; //生命值
private int magic; //魔法值
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getAgility() {
return agility;
}
public void setAgility(int agility) {
this.agility = agility;
}
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 getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getMagic() {
return magic;
}
public void setMagic(int magic) {
this.magic = magic;
}
//获取随机数
public void AutoAttribute(int str,int agi,int phy,int intell,int wis){
int sum = 0;
Random random = new Random();
do{
strength = random.nextInt(5)%10 + str;
agility = random.nextInt(5)%10 + agi;
physical = random.nextInt(5)%10 + phy;
intelligence = random.nextInt(5)%10 + intell;
wisdom = random.nextInt(5)%10 + wis;
sum = strength + agility + physical + intelligence + wisdom;
}while(sum != 100);
blood = physical * 20;
magic = (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.agility);
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.blood);
System.out.println("-------------------------------");
System.out.println("魔法值\t\t\t"+this.magic);
System.out.println("--------------------------------");
}
}
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Save {
public static void main(String[] args) {
boolean flag = true;
Role role = new Role(); //为类创建对象
RaceAndCareer rac = new RaceAndCareer();
Attribute ca = new Attribute();
do{
role.input();
int race = rac.SelectRace();
rac.SelectCareer(race);
role.output();
rac.output();
ca.initialAttribute(rac.getCareer());
ca.output();
System.out.println("是否满意角色属性?(0不满意 1满意)若不满意,则重新创建。");
Scanner in = new Scanner(System.in);
String str = in.next();
if("0".equals(str) || "1".equals(str)){
break;
}
}while(flag);
saveRoleInformation(role,rac,ca);
System.out.println("角色信息已成功保存。");
}
//保存Role类、RaceAndCareer类和Attribute类中的信息到文本文件
public static void saveRoleInformation(Role role,RaceAndCareer rac,Attribute ca){
try {
FileWriter fl = new FileWriter("D:\\eclipse-workspace\\RPG\\rolesinformation.txt");
BufferedWriter out = new BufferedWriter(fl);
out.write("姓名\t\t\t"+role.getName());
out.newLine();
if(role.getSex()==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.getAgility());
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.getBlood());
out.newLine();
out.write("魔法值\t\t\t"+ca.getMagic());
out.newLine();
out.close(); //关闭缓冲流
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
调试及运行结果
1.调试
① 输入姓名和性别
② 种族和职业
Output( )函数:
③ 属性值和保存文本
3.运行结果
上一篇: 简化的游戏角色创建