简单RPG游戏创建角色

注:下述仅为控制台界面输出
完整RPG游戏(附下载)见本博客文章《角色扮演RPG游戏项目》。链接为:角色扮演RPG游戏项目

1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。

2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。

3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业 狂战士 圣骑士 刺客 猎手 祭司 巫师
人类 允许 允许 允许 允许 允许 允许
精灵 不允许 不允许 允许 允许 允许 允许
兽人 允许 不允许 不允许 允许 允许 不允许
矮人 允许 允许 不允许 不允许 允许 不允许
元素 不允许 不允许 不允许 不允许 允许 允许
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。

4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
职业/属性 力量 敏捷 体力 智力 智慧
狂战士 40 20 30 5 5
圣骑士 25 15 30 20 10
刺客 20 35 20 15 10
猎手 15 40 15 10 20
祭司 15 20 15 35 15
巫师 10 20 10 20 40
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。

5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。

package game;

import java.io.IOException;
import java.util.Scanner;

public class Create {
	public static void main(String[] args) throws IOException {
		
		while(true) {
		System.out.println("***************************");
		System.out.println("***	1:开始游戏		***");
		System.out.println("***	2:保存游戏		***");
		@SuppressWarnings("resource")
		Scanner a = new Scanner(System.in);
		int n=a.nextInt();
		if(n>0&&n<3) {
		switch(n){
		case 1://game()
			Game g=new Game();      //Game实例对象
			g.Start();
			g.race();
			property.out();
			break;
		case 2://Save()
			Save S =new Save();
			S.save();
			break;
		}
		System.out.println("是否满意此属性值?若满意则注意选择保存信息,否则选择重新开始游戏!");
		}
		System.out.println("请重新输入正确选项:");
	}
	}
}
package game;

import java.util.Random;
/*角色属性信息**/
public class property { 
	static String name;
	static String Sex;
	static String race;
	static String occupation;
	static int power;         //力量
	static int agility;       //敏捷
	static int strength;      //体力
	static int intelligence;  //智力
	static int wisdom;        //智慧
	static int health;        //生命值
	static int magicValue;    //魔法值
	public static void kuangzhan() { //狂战属性
		Random random = new Random();
		property.wisdom=random.nextInt(15)+3;
		property.intelligence=random.nextInt(15)+3;
		property.strength=random.nextInt(35)+10;
		property.agility=random.nextInt(20)+3;
		property.power=100-wisdom-intelligence-strength-agility;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void shengqishi() {//圣骑士属性
		Random random = new Random();
		property.wisdom=random.nextInt(15)+3;
		property.intelligence=random.nextInt(35)+10;
		property.strength=random.nextInt(15)+3;
		property.power=random.nextInt(20)+3;
		property.agility=100-wisdom-intelligence-strength-power;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void cike() {//刺客属性
		Random random = new Random();
		property.wisdom=random.nextInt(15)+3;
		property.intelligence=random.nextInt(15)+3;
		property.strength=random.nextInt(20)+3;
		property.agility=random.nextInt(35)+10;
		property.power=100-wisdom-intelligence-strength-agility;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void lianshou() {//猎手属性
		Random random = new Random();
		property.wisdom=random.nextInt(35)+10;
		property.intelligence=random.nextInt(15)+3;
		property.strength=random.nextInt(15)+3;
		property.power=random.nextInt(20)+3;
		property.agility=100-wisdom-intelligence-strength-power;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void jisi() {//祭司属性
		Random random = new Random();
		property.agility=random.nextInt(35)+10;
		property.intelligence=random.nextInt(15)+3;
		property.strength=random.nextInt(15)+3;
		property.power=random.nextInt(20)+3;
		property.wisdom=100-agility-intelligence-strength-power;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void wushi() {//巫师属性
		Random random = new Random();
		property.agility=random.nextInt(15)+3;
		property.intelligence=random.nextInt(30)+10;
		property.strength=random.nextInt(15)+3;
		property.power=random.nextInt(20)+3;
		property.wisdom=100-agility-intelligence-strength-power;
		property.health=strength*20;
		property.magicValue=(wisdom+intelligence)*10;
	}
	public static void out() {//输出函数
		System.out.println("======================================");
		System.out.println("      角色:                                                               "+name);
		System.out.println("======================================");
		System.out.println("      性别:                                                               "+Sex);
		System.out.println("======================================");
		System.out.println("      种族:                                                               "+race);
		System.out.println("======================================");
		System.out.println("      职业:                                                               "+occupation);
		System.out.println("======================================");
		System.out.println("      力量:                                                               "+power);
		System.out.println("======================================");
		System.out.println("      敏捷:                                                               "+agility);
		System.out.println("======================================");
		System.out.println("      体力:                                                               "+strength);
		System.out.println("======================================");
		System.out.println("      智力:                                                               "+intelligence);
		System.out.println("======================================");
		System.out.println("      智慧:                                                              "+wisdom);
		System.out.println("======================================");
		System.out.println("      生命值:                                                          "+health);
		System.out.println("======================================");
		System.out.println("      魔法值:                                                          "+magicValue);
		System.out.println("======================================");
	}
}
package game;

import java.util.Scanner;

public class Game {
	public void Start(){//开始游戏
		System.out.println("请输入要创建的人物姓名:");
		@SuppressWarnings("resource")
		Scanner a=new Scanner(System.in);
		String name=a.next();
		property.name=name;//给静态变量name赋值
		System.out.println("请选择要创建人物的性别(1、女;2、男):");
		@SuppressWarnings("resource")
		Scanner b=new Scanner(System.in);
		int num=b.nextInt();
		if(num>0&&num<3) {
			switch(num){
			case 1:property.Sex="女";break;
			case 2:property.Sex="男";break;
			}
			}
		else
			System.out.println("请按指令输入:");
		}
	//occupation o=new occupation();
	//o.race();
	 public void race(){
			System.out.println("请选择人物种族(1、人类2、精灵3、兽人4、矮人5、元素):");
			@SuppressWarnings("resource")
			Scanner d=new Scanner(System.in);
			int num1=d.nextInt();
			if(num1>0&&num1<6) {
			switch(num1) {
			case 1: 
				property.race= "人类";
				System.out.println("请选择职业:(1、狂战士2、圣骑士3、刺客4、猎手5、祭司6、巫师)");
				@SuppressWarnings("resource") Scanner e=new Scanner(System.in);
				int num2=e.nextInt();
				while(true){
				if(num2>0&&num2<7) {
					switch(num2){//职业
					case 1:
						property.occupation="狂战士";
						property.kuangzhan();
						break;
					case 2: 
						property.occupation="圣骑士";
						property.shengqishi();
						break;
					case 3:
						property.occupation= "刺客";
						property.cike();
						break;
					case 4: 
						property.occupation= "猎手";
						property.lianshou();
						break;
					case 5: 
						property.occupation= "祭司";
						property.jisi();
						break;
					case 6: 
						property.occupation= "巫师";
						property.wushi();
						break;
					}
					break;
				}
				else 
					System.out.println("请输入正确范围!");
				}	
			case 2: 
				property.race= "精灵";
				System.out.println("选择职业:(1、刺客2、猎手3、祭司4、巫师)");
				@SuppressWarnings("resource") Scanner f=new Scanner(System.in);
				int num3=f.nextInt();
				while(true){
				if(num3>0&&num3<5) {
					switch(num3){//职业
					case 1:
						property.occupation= "刺客";
						property.cike();
						break;
					case 2: 
						property.occupation= "猎手";
						property.lianshou();
						break;
					case 3: 
						property.occupation= "祭司";
						property.jisi();
						break;
					case 4: 
						property.occupation= "巫师";
						property.wushi();
						break;
					}
				}
				else {
						System.out.println("请输入正确范围!");
					}
				return;
				}
			case 3:
				property.race= "兽人";
				System.out.println("选择职业:(1、狂战士2、猎手3、祭司)");
				@SuppressWarnings("resource") Scanner g=new Scanner(System.in);
				int num4=g.nextInt();
				while(true){
				if(num4>0&&num4<4) {
					switch(num4){//职业
					case 1:
						property.occupation= "狂战士";
						property.kuangzhan();
						break;
					case 2: 
						property.occupation= "猎手";
						property.lianshou();
						break;
					case 3:
						property.occupation= "祭司";
						property.jisi();
						break;
					}
				}
				else {
						System.out.println("请输入正确范围!");
					}
				return;
				}
				case 4:
					property.race= "矮人";
					System.out.println("选择职业:(1、狂战士2、圣骑士3、祭司)");
					@SuppressWarnings("resource") Scanner h=new Scanner(System.in);
					int num5=h.nextInt();
					while(true){
					if(num5>0&&num5<4) {
						switch(num5){//职业
						case 1:
							property.occupation= "狂战士";
							property.kuangzhan();
							break;
						case 2: 
							property.occupation= "圣骑士";
							property.shengqishi();
							break;
						case 3:
							property.occupation= "祭司";
							property.jisi();
							break;
						}
					}
					else {
							System.out.println("请输入正确范围!");
						}
					break;
					}
				case 5:
					property.race= "元素";
					System.out.println("选择职业:(1、祭司2、巫师)");
					@SuppressWarnings("resource") Scanner i=new Scanner(System.in);
					int num6=i.nextInt();
					while(true){
					if(num6>0&&num6<3) {
						switch(num6){//职业
						case 1:
							property.occupation= "祭司";
							property.jisi();
							break;
						case 2:
							property.occupation= "巫师";
							property.wushi();
							break;
						}
					}
					else {
							System.out.println("请输入正确范围!");
						}
					return;
					}
			}
			}
			
			}
			}
package game;

import java.io.*;
import java.nio.file.*;
import java.util.*;

public class Save {
	public void save() throws IOException {
		//定义一个目录路径的Path对象
		Path directoryPath = Paths.get("D:/test/sample");
		//根据Path对象创建多级目录
		Files.createDirectories(directoryPath);
		System.out.println("目录创建成功!");
		
		//定义一个文件路径的Path对象
		Path filePath =Paths.get("D:/test/sample/test.txt");
		//根据Path对象创建一个文件
		Files.createFile(filePath);
		//创建一个List集合,并向集合中添加内容
		List<String>list =new ArrayList<String>();
		list.add("======================================");
		list.add("      角色:                  "+property.name);
		list.add("======================================");
		list.add("      性别:                  "+property.Sex);
		list.add("======================================");
		list.add("      种族:                  "+property.race);
		list.add("======================================");
		list.add("      职业:                  "+property.occupation);
		list.add("======================================");
		list.add("      力量:                 "+property.power);
		list.add("======================================");
		list.add("      敏捷:                "+property.agility);
		list.add("======================================");
		list.add("      体力:               "+property.strength);
		list.add("======================================");
		list.add("      智力:              "+property.intelligence);
		list.add("======================================");
		list.add("      智慧:               "+property.wisdom);
		list.add("======================================");
		list.add("      生命值:           "+property.health);
		list.add("======================================");
		list.add("      魔法值:           "+property.magicValue);
		list.add("======================================");
		//将集合中的内容追加写入到指定的文件中
		Files.write(filePath, list, StandardOpenOption.APPEND);
		List<String>lines =Files.readAllLines(filePath);
		
		System.out.println("文件的大小为:"+Files.size(filePath));
		System.out.println("文件中的内容为:"+lines);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值