一个简化的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.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
二、类图设计
在这里插入图片描述
三、程序实现
MyFrame.java

import javax.swing.*;

public class MyFrame{	
	public static JFrame frame=new JFrame();
	static MyPanel panel=new MyPanel();
	public static void create() {
		frame.setTitle("Dungeons");//窗口标题
		frame.setSize(600, 600);//窗口大小
		frame.setLocationRelativeTo(null);//窗口居中			
		panel.init(frame);;//显示面板
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口则退出虚拟机
		frame.setVisible(true);
	}	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(MyFrame::create);
	}
}

MyPanel.java
`
import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class MyPanel {
static String[] hero_race= {
“请选择种族”,“人类”,“精灵”,“兽人”,“矮人”,“元素”
};
static String[] hero_job= {
“请选择职业”,“狂战士”,“圣骑士”,“刺客”,“猎手”,“祭司”,“巫师”
};

public void init(JFrame frame){	
	Value s=new Value();
	//名字
	JTextField name=new JTextField(30);
	name.setText(null);
	JButton bt=new JButton("检查");
	bt.addActionListener(e->{
		String content=name.getText();			
		if(content.length()>50) {
			JOptionPane.showMessageDialog(null, "名字过长,只能输入50个字符以内", "出错啦", JOptionPane.ERROR_MESSAGE);
		}else {
			if(content!=null&&!content.trim().equals("")) {
				s.setHero_name(content);
				JOptionPane.showMessageDialog(null, "输入完成");
		}else{
			JOptionPane.showMessageDialog(null, "名字不为空","出错啦", JOptionPane.ERROR_MESSAGE);
		}
	}
		
	});
	//性别
	ButtonGroup sex=new ButtonGroup();
	JRadioButton man=new JRadioButton("男");
	JRadioButton woman=new JRadioButton("女");
	sex.add(man);
	sex.add(woman);		
	ActionListener listener=new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(man.isSelected()) {
				s.setHero_sex("男");
			}
			if(woman.isSelected()) {
				s.setHero_sex("女");
			}
		}		
	};	
	man.addActionListener(listener);
	woman.addActionListener(listener);
	
	//种族和职业
	JComboBox<String>comboBox1=new JComboBox<String>(hero_race);
	JComboBox<String>comboBox2 = new JComboBox<String>(hero_job);
	JTextField race=new JTextField(20);
	JTextField job=new JTextField(20);
	comboBox1.addItemListener(e->{
		String item1=(String) comboBox1.getSelectedItem();
		s.setRace(item1);
		if("请选择种族".equals(item1)) {
			race.setText("");
		}else {
			race.setText("您选择的种族是:"+item1);
		}
		if("人类".equals(item1)) {	
			comboBox2.removeAllItems();
			for(int i=0;i<hero_job.length;i++) {
			comboBox2.addItem(hero_job[i]);
			}
		}
		else if("精灵".equals(item1)) {		
			comboBox2.removeAllItems();
			for(int i=0;i<hero_job.length;i++) {
			comboBox2.addItem(hero_job[i]);
			}
			comboBox2.removeItem("狂战士");
			comboBox2.removeItem("圣骑士");
		
		}else if("兽人".equals(item1)) {		
			comboBox2.removeAllItems();
			for(int i=0;i<hero_job.length;i++) {
			comboBox2.addItem(hero_job[i]);
			}
			comboBox2.removeItem("圣骑士");
			comboBox2.removeItem("刺客");
			comboBox2.removeItem("巫师");
		}else if("矮人".equals(item1)) {		
			comboBox2.removeAllItems();
			for(int i=0;i<hero_job.length;i++) {
			comboBox2.addItem(hero_job[i]);
			}	
			comboBox2.removeItem("刺客");
			comboBox2.removeItem("猎手");
			comboBox2.removeItem("巫师");
		}else if("元素".equals(item1)) {		
			comboBox2.removeAllItems();
			for(int i=0;i<hero_job.length;i++) {
			comboBox2.addItem(hero_job[i]);
			}				
			comboBox2.removeItem("狂战士");
			comboBox2.removeItem("圣骑士");				
			comboBox2.removeItem("刺客");
			comboBox2.removeItem("猎手");
		}	
	});
	comboBox2.addItemListener(e->{
		String item2=(String) comboBox2.getSelectedItem();
		s.setJob(item2);
		if("请选择职业".equals(item2)) {
			job.setText("");
		}else {
			job.setText("您选择的职业是:"+item2);
		}		
		
	});	
	//显示
	JTextArea showArea=new JTextArea(12,34);
	JScrollPane scrollpane=new JScrollPane(showArea);
	showArea.setEditable(false);
	JButton selected=new JButton("确定");
	selected.addActionListener(e->{
		String j=(String)comboBox2.getSelectedItem();
		s.Init(j);	
	if(s.getHero_name()==null||sex.isSelected(null)||"请选择种族".equals(s.getRace())||"请选择职业".equals(s.getJob())){
			JOptionPane.showMessageDialog(null, "名字未检查或选择不为空");
	}else {		
		showArea.setText("");
		showArea.append("名字:"+s.getHero_name()+"\n");
		showArea.append("性别:"+s.getHero_sex()+"\n");
		showArea.append("种族:"+s.getRace()+"\n");
		showArea.append("职业:"+s.getJob()+"\n");
		showArea.append("力量:"+s.getPower()+"\n");
		showArea.append("敏捷:"+s.getAgility()+"\n");
		showArea.append("体力:"+s.getStrength()+"\n");
		showArea.append("智力:"+s.getIntelligence()+"\n");
		showArea.append("智慧:"+s.getWisdom()+"\n");
		showArea.append("生命值:"+s.getLife()+"\n");
		showArea.append("魔法值:"+s.getMagic()+"\n");
	}
	});
	//保存
	JButton save=new JButton("保存");
	save.addActionListener(e->{
		int res = JOptionPane.showConfirmDialog(null, "是否继续操作", "是否继续", JOptionPane.YES_NO_OPTION);
		if (res == JOptionPane.YES_OPTION) {
			s.save(); // 点击“是”后执行保存
			System.exit(0);
		} else {
		 // 点击“否”返回
			return;
		}
	});
	
	MyFrame.frame.setLayout(new FlowLayout());//窗口整体采用FlowLayout形式
	//添加组件,按照5行一列的形式
	JPanel jp=new JPanel(new GridLayout(5,1));
	JPanel panel1=new JPanel();
	JLabel label1=new JLabel("名字");
	panel1.add(label1);
	panel1.add(name);
	panel1.add(bt);
	jp.add(panel1);
	
	JPanel panel2=new JPanel();
	JLabel label2=new JLabel("性别");
	panel2.add(label2);
	panel2.add(man);
	panel2.add(woman);
	jp.add(panel2);
	
	JPanel panel3=new JPanel();
	JLabel label3=new JLabel("种族");
	panel3.add(label3);
	panel3.add(comboBox1);
	panel3.add(race);
	jp.add(panel3);
	
	JPanel panel4=new JPanel();
	JLabel label4=new JLabel("职业");
	panel4.add(label4);
	panel4.add(comboBox2);
	panel4.add(job);
	jp.add(panel4);	
	
	JPanel panel5=new JPanel();
	panel5.add(selected);
	panel5.add(save);
	jp.add(panel5);
	
	frame.add(jp,BorderLayout.PAGE_START);	
	frame.add(scrollpane,BorderLayout.PAGE_END);	
}

}
Value。java

import java.io.*;
import java.util.Random;
import javax.swing.JOptionPane;
public class Value {
	private String hero_name=null;
	private String hero_sex=null;
	private String race="请选择种族";
	private String job="请选择职业";
	private int power;
	private int agility;
	private int strength;
	private int intelligence;
	private int wisdom;
	private int life;
	private int magic;
	public void Init(String str) {
		Random ra=new Random();
		int sum=0;
		while(sum!=100) { 
		switch(str) {
		case "狂战士":
					this.power=ra.nextInt(45)+36;
					this.agility=ra.nextInt(25)+16;
					this.strength=ra.nextInt(35)+26;
					this.intelligence=ra.nextInt(10)+1;
					this.wisdom=ra.nextInt(10)+1;
					break;
		
		case "圣骑士":this.power=ra.nextInt(30)+21;
					this.agility=ra.nextInt(20)+11;
					this.strength=ra.nextInt(35)+26;
					this.intelligence=ra.nextInt(25)+16;
					this.wisdom=ra.nextInt(15)+6;break;

		case "刺客":this.power=ra.nextInt(25)+16;
					this.agility=ra.nextInt(40)+31;
					this.strength=ra.nextInt(25)+16;
					this.intelligence=ra.nextInt(20)+11;
					this.wisdom=ra.nextInt(15)+6;break;

		case "猎手":this.power=ra.nextInt(20)+11;
					this.agility=ra.nextInt(45)+36;
					this.strength=ra.nextInt(20)+11;
					this.intelligence=ra.nextInt(15)+6;
					this.wisdom=ra.nextInt(25)+16;break;
		case "祭司":this.power=ra.nextInt(20)+11;
					this.agility=ra.nextInt(25)+16;
					this.strength=ra.nextInt(20)+11;
					this.intelligence=ra.nextInt(40)+31;
					this.wisdom=ra.nextInt(20)+11;break;
		case "巫师":this.power=ra.nextInt(15)+6;
					this.agility=ra.nextInt(25)+16;
					this.strength=ra.nextInt(15)+6;
					this.intelligence=ra.nextInt(25)+16;
					this.wisdom=ra.nextInt(45)+36;break;
		default:return;
	}
		sum=this.power+this.agility+this.strength+this.intelligence+this.wisdom;
		this.life=this.strength*20;
		this.magic=(this.intelligence+this.wisdom)*10;
	}	
}
//实现保存文档
	public void save() {
		try {
			FileWriter f=new FileWriter("role.txt");
			f.write(this.hero_name+"\n");
			f.write(this.hero_sex+"\n");
			f.write(this.race+"\n");
			f.write(this.job+"\n");
			f.write(this.power+"\n");
			f.write(this.agility+"\n");
			f.write(this.strength+"\n");
			f.write(this.intelligence+"\n");
			f.write(this.wisdom+"\n");
			f.write(this.life+"\n");
			f.write(this.magic+"\n");
			f.close();
			JOptionPane.showMessageDialog(null, "保存成功");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}			
	}
	public String getRace() {
		return race;
	}
	public void setRace(String race) {
		this.race = race;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getHero_name() {
		return hero_name;
	}
	public void setHero_name(String hero_name) {
		this.hero_name = hero_name;
	}
	public String getHero_sex() {
		return hero_sex;
	}
	public void setHero_sex(String hero_sex) {
		this.hero_sex = hero_sex;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	public int getAgility() {
		return agility;
	}
	public void setAgility(int agility) {
		this.agility = agility;
	}
	public int getStrength() {
		return strength;
	}
	public void setStrength(int strength) {
		this.strength = strength;
	}
	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 getLife() {
		return life;
	}
	public void setLife(int life) {
		this.life = life;
	}
	public int getMagic() {
		return magic;
	}
	public void setMagic(int magic) {
		this.magic = magic;
	}
}	

四、经验总结
这次的上机实验原本是做控制台的输入和输出,但在我预习完java的GUI图形化窗口后,决定尝试做一个图形界面。
在我看来,java的图形界面主要是组件加布局,以及组件之间各种的响应,经过这次尝试,对于做界面有了很多的认识,感觉学会了这个算是java入门了。虽然没有考虑太多布局问题,但在实现组件和响应方面,我有了深刻的认识,每一个组件必须要有一个响应,不管什么样的响应,都要关联到别的东西,否则组件就没什么用处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值