Java实现RPG游戏(mini版pokemon) | 您所在的位置:网站首页 › 角色扮演类游戏英文缩写怎么写 › Java实现RPG游戏(mini版pokemon) |
Java实现mini版RPG游戏
总体概述英雄与NPC的创建图形化界面战斗过程结算界面最后代码连接
总体概述
游戏素材来源:游戏素材 java图形界面开发:Swing教程 RPG游戏人物移动等等:RPG 对战界面:回合制对战 这是本人在大二暑期做的课程设计,要求基于java实现一个RPG游戏,在此做一个记录,代码里许多实现也是参考网上许多大佬分享的例子(链接在上面贴出),再做出自己的修改,有许多不足的地方欢迎大家指出。 英雄与NPC的创建先写一个抽象类,是所有角色的基类:包含基本的属性,名字,血量,蓝量,攻击力,防御力,等级等等;平a,技能,升级等等方法。 public abstract class Role { private String name; private double hp; private double maxHp; private double lastHp; private double mp; private double maxMp; private double lastMp; private int atk; private int def; private int lv; private int exp; private String bodypath; private String sName1; private String sName2; private String sName3; public abstract void attack(int skill,Role role); public abstract void skill1(Role role); public abstract void skill2(); public abstract void skill3(Role role); public abstract void equipment(int i); public abstract void removeEqu(int i); public abstract void levelup(); public Role(String name, double hp, double maxHp, double mp, double maxMp, int atk, int def, int lv, String bodypath, String sName1, String sName2, String sName3) { super(); this.name = name; this.hp = hp; this.maxHp = maxHp; this.mp = mp; this.maxMp = maxMp; this.atk = atk; this.def = def; this.lv = lv; this.bodypath = bodypath; this.sName1 = sName1; this.sName2 = sName2; this.sName3 = sName3; } // get,set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } ...... }继承Role类,实现主角 public class Hero extends Role { //实例化一个音效类 Au a = new Au(); public Hero() { super("皮卡丘", 100, 100, 100,100, 60, 20, 1, "src\\Image\\皮卡丘.png", "电光一闪","治疗", "十万伏特"); this.setExp(0); } public void attack(int skill, Role role) { role.setLastHp(role.getHp()); if(skill == 1) { skill1(role); }else if(skill == 2) { skill2(); }else { skill3(role); } } public void skill1(Role role) { StartFrame.showMessage("我方使用"+this.getsName1()); a.play(4); role.setLastHp(role.getHp()); role.setHp(role.getHp()-this.getAtk()+role.getDef()); } public void skill2() { this.setLastMp(this.getMp()); this.setMp(this.getMp()-(0.3-0.01*this.getLv())*this.getMaxMp()); if(this.getMp() StartFrame.showMessage("我方使用"+this.getsName2()); a.play(6); this.setLastHp(this.getHp()); this.setHp(this.getHp()+(this.getMaxHp()-this.getHp())*0.3); } } public void skill3(Role role) { this.setLastMp(this.getMp()); this.setMp(this.getMp()-(0.7-0.03*this.getLv())*this.getMaxMp()); if(this.getMp() StartFrame.showMessage("我方使用"+this.getsName3()); a.play(5); role.setLastHp(role.getHp()); role.setHp(role.getHp()-this.getAtk()*2+role.getDef()); } } // 战斗前添加装备(老师要求有装备,(◔‸◔)所以简单的实现有些粗糙) public void equipment(int i) { if(i == 0) { this.setAtk(this.getAtk()+10); }else if(i == 1) { this.setDef(this.getDef()+10); } } // 战斗后卸掉装备 public void removeEqu(int i) { if(i == 0) { this.setAtk(this.getAtk()-10); }else if(i == 1) { this.setDef(this.getDef()-10); } } // 升级 public void levelup() { if((this.getExp() >= this.getLv()*(this.getLv()+1)) && (this.getLv() this.setHp(this.getMaxHp()); this.setMp(this.getMaxMp()); } } }其他NPC也是类似的方法,就不一个个贴了,NPC不会升级什么的,所以更简单一些。 图形化界面图形化界面都是由Swing制作的,按钮,进度条等等控件都可以直接拖,还是很方便的。地图地形是本人写了一个循环一张张贴出来的,比较low,大家可以参考开头RPC连接文章中实现地图生成器来自由的实现地图。 移动效果可以参考开头RPG那位博主的文章,里面有实现玩家移动,地图生成器等等,地图生成器小弟当时比较菜没有实现,就跟着教程实现了移动的部分,主要先找到一张4*4的人物移动素材图,然后在循环里面根据键盘的输入切换这些图片达到移动的动画效果。
战斗界面可以学习回合制对战这篇文章,里面还有通过延时实现血条逐渐下降的效果,还是很炫酷的。 主角攻击 // 不同的按钮调用主角类不同的技能即可 JButton btnNewButton = new JButton(); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { flag = true; try { attack_course(1); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }); btnNewButton.setBounds(73, 255, 93, 23); btnNewButton.setText(role.getsName1()); panel.add(btnNewButton); JButton btnNewButton_1 = new JButton(); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { flag = true; try { attack_course(2); } catch (InterruptedException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } }); btnNewButton_1.setBounds(176, 255, 93, 23); btnNewButton_1.setText(role.getsName2()); panel.add(btnNewButton_1); JButton btnNewButton_2 = new JButton(); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { flag = true; try { attack_course(3); } catch (InterruptedException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } });NPC攻击 public void enemyattack(Role role,Role role2) { int i; while(true) { i = (int)(Math.random()*8); if(i>=0&&i role.skill2(); if(flag) { showMessage("敌方使用了"+role.getsName2()); refreshHp(role,role2,progressBar_1,progressBar,blueBar_1,2,1000); break; }else { //System.out.println("治疗蓝量不足 重新选择"); continue; } }else{ role.skill3(role2); if(flag) { showMessage("敌方使用了"+role.getsName3()); refreshHp(role,role2,progressBar_1,progressBar,blueBar_1,3,1000); break; }else { //System.out.println("大招蓝量不足 重新选择"); continue; } } } } 结算界面
这是一年多前写的东西了,现在看来编码习惯还有没有注释什么的看起来确实有点离谱哈哈,虽然有很多不会的地方都是借鉴大佬的,不过这次实践也是受益匪浅。为了增加游戏的趣味,我还加入了音效,以及调整游戏数值让主角和NPC战力更接近。 总而言之,还是很好的一次提升自己的机会,如果大家发现有什么不足之处,欢迎指出。 代码连接上传的有点乱,代码在src/RPG中:https://gitee.com/sanguinedab/pokemon_mini |
CopyRight 2018-2019 实验室设备网 版权所有 |