目录

1.用随机数表示概率

2.用arraylist中的某一属性作为筛选标准-从大到小

3.判断字符是否为空与不空工具类

4.写游戏任务-先写抽象类,然后再写实现类,比较正规

5.JTextArea与JTextField

6.List

7.Jfram小知识

8.Jtable获取选中值


1.用随机数表示概率

    public static boolean eighty_five() {int n = random.nextInt(100);boolean m;if(n < 85){m = true;return m;}else{m = false;return m;}}public static boolean fifity() {int n = random.nextInt(100);boolean m;if(n < 50){m = true;return m;}else{m = false;return m;}}

2.用arraylist中的某一属性作为筛选标准-从大到小

 //Sort by hero speedpublic static List<Hero> SpeedByHero(List<Hero> list) {Collections.sort(list, new Comparator<Hero>() {@Overridepublic int compare(Hero o1, Hero o2) {int flag;flag = o2.getSpeed()-o1.getSpeed();return flag;}});return list;}

3.判断字符是否为空与不空工具类

//Checks if the character is empty
public class StringUtil {public static boolean isEmpty(String str) {if(str==null || "".equals(str.trim())) {return true;}else {return false;}}public static boolean isNotEmpty(String str) {if(str!=null && !"".equals(str.trim())) {return true;}else {return false;}}//判断string是否为数字public static boolean isNumeric(String str){for (int i = 0; i < str.length(); i++){if (!Character.isDigit(str.charAt(i))){return false;}}return true;}
}

4.写游戏任务-先写抽象类,然后再写实现类,比较正规

public abstract class Role {private String name;private int speed;private int power;private int life;//Maximum healthprivate int health;//Health point//武器private int type;//Weapon status: 0 for no weapon, 1 for fire, 2 for water, and 3 for ice  private int type_power;//The power of the weapon//技能private String sName1;//attack(%80 chance of success. )private String sName2;//heal(%50 chance of success. heals teammates and yourself)private String sName3;//power up(75% probability of success, when the speed is greater than 1 available, each successful use, speed half, rounded up  )//attack methodpublic abstract void attack(int skill,Role role,Villain villain);//skill methodpublic abstract String skill1(Villain villain);public abstract String skill2(Role role);public abstract String skill3(Role role);public Role(String name,  int power, int life, int speed,int type,String sName1, String sName2, String sName3) {super();this.name = name;this.power = power;this.life = life;this.speed = speed;this.type = type;this.sName1 = sName1;this.sName2 = sName2;this.sName3 = sName3;}public int getType() {return type;}public void setType(int type) {this.type = type;}public int getType_power() {return type_power;}public void setType_power(int type_power) {this.type_power = type_power;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getPower() {return power;}public void setPower(int power) {this.power = power;}public int getLife() {return life;}public void setLife(int life) {this.life = life;}public int getHealth() {return health;}public void setHealth(int health) {this.health = health;}public String getsName1() {return sName1;}public void setsName1(String sName1) {this.sName1 = sName1;}public String getsName2() {return sName2;}public void setsName2(String sName2) {this.sName2 = sName2;}public String getsName3() {return sName3;}public void setsName3(String sName3) {this.sName3 = sName3;}
}
import demo.Role;
import demo.Villain;
import utils.RandomUtils;
public class Hero extends Role {private boolean flag=false;private int total=0;//Total healing or total damagepublic Hero(String name,  int power, int life,int speed, int type,String sName1, String sName2, String sName3) {super(name,power,life,speed,type, "Attack","Heal", "Power Up");}public void attack(int skill,Role role, Villain villain) {role.setHealth(role.getHealth());if(skill == 1) {skill1(villain);}else if(skill == 2) {skill2(role);}else {skill3(role);}}//Attack method(%80)public String skill1(Villain villain) {if(RandomUtils.eighty()) {if(this.getType()!=0) {if(this.getType()==1&&villain.getType().contains("3")||this.getType()==2&&villain.getType().contains("1")||this.getType()==3&&villain.getType().contains("2")) {//Property restraintthis.setType_power(20);flag=true;}else {//Don't resistthis.setType_power(10);flag=false;}//Method implementationtotal=this.getPower()+this.getType_power();villain.setHealth(villain.getHealth()-total);//Judge monster healthif(villain.getHealth()<=0) {return "Hero "+this.getName()+" defeatd "+villain.getName();}else {if(flag) {return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing "+total+"damage,The monster still has "+villain.getHealth()+" health point";}else {return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing "+total+" damage,The monster still has "+villain.getHealth()+" health point";}}}else {//No weaponstotal=this.getPower();villain.setHealth(villain.getHealth()-total);//Judge monster healthif(villain.getHealth()<=0) {return "Hero victory";}else {return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing"+total+" damage,The monster still has "+villain.getHealth()+" health point";}}}else {return "Against failure";}}//heal(%50)public String skill2(Role role) {if(RandomUtils.fifity()) {if(role.getHealth()+this.getPower()<=role.getLife()) {total=this.getPower();role.setHealth(role.getHealth()+total);}else {total=role.getLife()-role.getHealth();role.setHealth(role.getLife());}return this.getName()+" heal "+role.getName()+",heal "+total+" health point";}else {return "treatment failure";}}//power up (75%)public String skill3(Role role) {if(RandomUtils.eighty_five()) {role.setPower(this.getPower()*2);//round up to an integerrole.setSpeed(this.getSpeed()/2+ (this.getSpeed() % 2 != 0 ? 1 : 0));return this.getName()+" power up "+role.getName()+",Twice as strong, half as fast";}else {return "Reinforcing failure";}}
}

5.JTextArea与JTextField

JTextField与JTextArea都是文本域,但是JTextField只有中文有自动换行功能,解决起来特别麻烦,我这里为了省事,直接换成了JTextArea,这个有专门的方法,可以英文等自动换行。

    private JTextArea MsgField;MsgField = new JTextArea();  MsgField.setLineWrap(true);        //Enable line wrappingMsgField.setWrapStyleWord(true);  // Activate the line break function

6.List

list.size()返回的是list大小(从1开始)list.get(index)得到对应索引的值(index从0开始)。list.remove(index);删除索引对应的元素(index从0开始)

7.Jfram小知识

dispose();销毁当前页面new MonsterFrame().setVisible(true);调用新页面并显示jfram页面间传值有几种方法,这里我简要说明一下参数直接传值:new MonsterCombatOutcomes(msg).setVisible(true);用静态变量传值public static int temporary;这两个是我经常用的给按钮或文本框赋值
都有对应的setText()方法
获得对应的值有getText()方法弹出提示
JOptionPane.showMessageDialog(null, "The name cannot be empty!");jframe居中显示,放在Jpane建立后
setLocationRelativeTo(null);

8.Jtable获取选中值

model = new DefaultTableModel(new Object[][] {},new String[] {header});table = new JTable(model);scrollPane.setViewportView(table);Object valueAt = model.getValueAt(table.getSelectedRow(), table.getSelectedColumn());//Add role data to the tableprivate void fillTable(List<Hero> list) {DefaultTableModel dtm=(DefaultTableModel) table.getModel();dtm.setRowCount(0);for (Hero hero : list) {Vector<String> v=new Vector<String>();v.add(hero.getName());dtm.addRow(v);}}

角色扮演swing小游戏项目总结相关推荐

  1. [源码和文档分享]基于C#实现的RPG角色扮演类小游戏

    1 需求分析 1.1 游戏概述 DragonQuest是一个角色扮演类游戏(RPG),该游戏实现的具体功能是设计两种类型的人物,分别为被玩家所控制的玩家人物(Hero)和由系统所控制的外部人物(Ene ...

  2. C#服务端的微信小游戏——多人在线角色扮演(六)

    C#服务端的微信小游戏--多人在线角色扮演(六) 地图上来了只小狗 优化代码 用内容清单来实现地图的刷新,根据开发需求的细化来优化代码结构. --茂叔 地图上来了只小狗 上一篇里,我们成功让游戏世界有 ...

  3. lg 传奇手游java_2020年手机游戏角色扮演类和传奇类 排行榜NO.1 小编强势推荐

    原标题:2020年手机游戏角色扮演类和传奇类 排行榜NO.1 小编强势推荐 随着手机系统的崛起,手机网游也成了小伙伴们热衷的环节.今天小编带大家看看有哪些值得玩的手机角色扮演类手游.当然,这里还为80 ...

  4. C#服务端的微信小游戏——多人在线角色扮演(一)

    C#服务端的微信小游戏--多人在线角色扮演(一) 前言 平台选择 前端 后端 开发语言 准备工作 开发目标 软件开发重在思路,其他都可以看文档的-- --茂叔 前言 一个人,从0开始,开发一个打怪练级 ...

  5. C#服务端的微信小游戏——多人在线角色扮演(十一)

    C#服务端的微信小游戏--多人在线角色扮演(十一) 账号 角色生成 角色名字生成 账号是现实世界对用户的描述,而角色则是游戏世界当中用户的体现. --茂叔 账号 上一篇,我们利用微信的api完成了用户 ...

  6. C#服务端的微信小游戏——多人在线角色扮演(十四)

    C#服务端的微信小游戏--多人在线角色扮演(十四) 服务器端 客户端 名字不是自己定义的,但人生却可以 --茂叔 为了实现让用户选择角色的姓名和性别,我们需要这样一个界面: 服务器端 要实现这个功能, ...

  7. 使用C#开发了一个RPG角色扮演类的小游戏^_^

    角色扮演类游戏(RPG),该游戏实现的具体功能是设计两种类型的人物,分别为被玩家所控制的玩家人物(Hero)和由系统所控制的外部人物(Enemy),游戏中的主要情节就是Hero与 Enemy之间的战斗 ...

  8. For the king:出色的冒险,失败的角色扮演

    <为了吾王>(简称FTK)是一款融合Roguelike.战棋.回合战斗与DND等多种元素的RPG 游戏,我们可以把它看为精简版"魔法门"与Roguelike的结合. 不 ...

  9. 《开源合辑-(游戏/娱乐-角色扮演)之(Java)》

    NWN Munchkin Munchkin is a toolset for editing characters for Bioware's Neverwinter Nights game. It ...

最新文章

  1. 三角量测(Triangulation)之再学习
  2. AttributeError: 'str' object has no attribute 'decode' django问题
  3. MySQL-通过MaxScale实现读写分离初探
  4. 盛辉智能机器人安全吗_人工智能真的安全吗?快看这些已经发出的警告
  5. 『Material Design 入门学习笔记』前言
  6. linux tomcat守护_linux 设置tomcat为守护进程教程
  7. 利用before伪元素创建图标
  8. 代码行数统计工具,java,go,c++,html文件都适用
  9. 树莓派+aria2+yaaw搭建下载机
  10. 第一章(1.2) 机器学习算法工程师技能树
  11. Word入门教程之插入文字批注(转)
  12. 拼音四线三格图片_一年级拼音总结,请查收
  13. MAC OS下设置bits/stdc++.h万能头文件
  14. J2EE 框架结构及核心技术基础面面观
  15. Flutter 2(1),还在等机会
  16. 安全技术(Security)
  17. 完美主义者适合当程序员?
  18. 腾讯为何急于收购“弃子”搜狗 ?
  19. 毕业设计-基于深度学习的指针式仪表读数识别系统
  20. html中table标签及属性

热门文章

  1. 手撕Bert代码(torch版)
  2. 赠书啦!《阿里巴巴Java开发手册》实体书面世!
  3. mysql的单引号[ ' ],双引号[ ]和esc下面的反勾号[ ` ]的区别
  4. labelme标注结果可视化(持续补充)
  5. 窥探现代浏览器架构(二)
  6. 《系统相关》Intel VT-x 处于禁用状态开启
  7. Vs调试报错:不能将 “const char *“ 类型的值分配到 “char *“ 类型的实体
  8. ROS rviz gazebo No transform from [left_leg] to [base_link]
  9. asp.net错误解决:Unable to Validate Data in ASP.NET website
  10. 关于期货套期保值的疑问?