在经过一段时间的java学习后,通过多人合作,以宝可梦作为参考,完成了简易版本的制作。在制作过程中,虽然没有出现太大的问题,但在许多细节的地方没有注意到。修改代码时不够仔细,导致残留了一些没有必要的代码,虽然对代码的运行没有产生影响,但是代码整体十分凌乱。java是面向对象的语言,但是在实际制作时无法明确对象所具有的属性以及方法。
结构
map(部分代码)
public class City {
Npc npc=new Npc();
Scanner scanner=new Scanner(System.in);
//青木市地图
public int qingmu(Player player){
System.out.println("欢迎来到青木市");
System.out.println("1.商店");
System.out.println("2.回复阵");
System.out.println("3.道馆");
System.out.println("4.野外");
int choice=scanner.nextInt();
while (choice<1||choice>4){
System.out.println("抱歉,其他区域还未建设完成,请去别的地方看看吧:");
choice=scanner.nextInt();
}
switch (choice){
//商店
case 1:npc.store(player);break;
//回复阵
case 2:npc.recoveryMatrix(player);break;
//进入道馆,boss战
case 3:player.encounter("boss");break;
//返回值3,进入野外
case 4:return 3;
}
return 2;
}
}
pokemon(部分代码)
public class Pokemon {
public String pokemonName;
// 生命值
public int pokemonHp;
public int pokemonhp;
// 攻击值
public int pokemonAttack;
// 防御
public int pokemonDefend;
//状态
public boolean pokemonStatus=true;
// 技能
// 攻击技能1 ,攻击技能所对应的技能值
public String attackSkill1;
public int attackSkill1Value;
public int proficiency1;
// 攻击技能2 ,攻击技能所对应的技能值
public String attackSkill2;
public int attackSkill2Value;
public int proficiency2;
// 攻击技能3 ,攻击技能所对应的技能值
public String attackSkill3;
public int attackSkill3Value;
public int proficiency3;
// 攻击技能4,攻击技能所对应的技能值
public String specSkill;
public int specSkillValue;
public int proficiency4;
// 初始化各项属性,第一级
public Pokemon() {
// 生命值 开始初始化的值随机数(50,70)
this.pokemonHp= (int) (Math.random()*20)+50;
this.pokemonhp=this.pokemonHp;
// 攻击值 随机数(10,30)
this.pokemonAttack=(int) (Math.random()*20)+10;
// 防御值 随机数(10,30)
this.pokemonDefend=(int) (Math.random()*20)+10;
}
public void elfSkills(){ }
public void upgrade(){ }
public void upgradeattribute(){ }
public void show() {
System.out.println("==========^===========");
System.out.println(this.pokemonName);
System.out.println("生命值:" + this.pokemonHp);
System.out.println("攻击力:" + this.pokemonAttack);
System.out.println("防御力:" + this.pokemonDefend);
System.out.println("==========V===========");
}
}
Player
public class Player {
//名字
public String name;
//金钱
public int money = 100;
//精灵球
public int pokeBall = 2;
//疗伤药
public int medicine;
public Pokemon[] pokemon = new Pokemon[4];
Scanner scanner = new Scanner(System.in);
Random random = new Random();
Boss boss = new Boss();
Pokemon pokemons = new Pokemon();
//遭遇野怪
public void encounter(String monster) {
int l = 0;
if (monster.equals("boss")) {
pokemons = boss;
System.out.println("你嚣张的向道馆馆主发起了挑战。你大叫一声:呔! 你敢应战么?");
System.out.println("道馆馆主抬起眼眸,轻蔑的瞟了你一眼:GO first!");
System.out.println("你巴拉巴拉精灵袋:");
showAll();//打印精灵
System.out.println("————— @ ————");
System.out.println(" || @@@@@ ||");
System.out.println(" || @@@@@ ||");
System.out.println(" ||_______||");
System.out.println("你从袋子里选出来了一个小精灵:");
int chioce = scanner.nextInt() - 1;
show(chioce);
System.out.println("就是你了,宝可梦!||");
// show(chioce);
pokemons.show();
while (l == 0) {
l = interfaces(monster, chioce);
}
} else if (monster.equals("monster")) {
pokemons = createmonster();
System.out.println("野怪突然闪现,吓你一跳!");
System.out.println("哎嘿?前面那是什么?难道是..." + pokemons.pokemonName + "?!!");
System.out.println("这下可太好了,这种小怪小力的,用来训练最好了!@_@,去吧!宝可梦!");
int chioce = scanner.nextInt() - 1;
while (chioce < 0 || chioce > 3 || this.pokemon[chioce].pokemonName == null) {
System.out.println("【NPC】你有吗?不,你没有!");
chioce = scanner.nextInt() - 1;
}
while (!this.pokemon[chioce].pokemonStatus) {
System.out.println("【NPC】你的宝可梦真实太菜了!竟然打不过?!你还有其他的宝可梦么?有就快上!别怂!");
chioce = scanner.nextInt() - 1;
}
show(chioce);//我拥有的宝可梦
pokemons.show();//小怪宝可梦
while (l == 0) {
l = interfaces(monster, chioce);
}
}
}
public int interfaces(String monster, int chioces) {
System.out.println("+--------------+");
System.out.println("|1.战斗 2.物品|");
System.out.println("|3.捕获 4.逃跑|");
System.out.println("+--------------+");
System.out.print("请选择你要进行的操作:");
int choice = scanner.nextInt();
while (choice > 4 || choice < 1) {
System.out.println("不会吧,这么简单的操作都不会?重来重来!");
choice = scanner.nextInt();
}
switch (choice) {
//战斗
case 1:
return battle(chioces);
//物品
case 2:
openpackage(monster);
break;
//捕获
case 3:
switch (capture()) {
//捕获失败直接结束
case -1:
return -1;
//精灵球不足或放弃捕捉
case 0:
interfaces(monster, choice);
break;
//捕捉成功
case 1:
for (int i = 0; i < 4; i++) {
if (this.pokemon[i] == null) {
this.pokemon[i] = pokemons;
break;
} else if (i == 3) {
System.out.println("你这瘦弱的身板只能携带四只,你要放弃你原有的宝可梦吗?(y/n)");
String z = scanner.next();
if (z.equals("y")) {
//放弃
System.out.println("好吧,你这个喜新厌旧的家伙");
System.out.println("你要放弃哪一只?");
System.out.println("1." + pokemon[0].pokemonName);
System.out.println("2." + pokemon[1].pokemonName);
System.out.println("3." + pokemon[2].pokemonName);
System.out.println("4." + pokemon[3].pokemonName);
int v = scanner.nextInt() - 1;
pokemon[v] = pokemons;
} else {
//不放弃
System.out.println("看来你还是喜欢原来的,好吧那就这样吧");
}
}
}
return 2;
}
break;
//逃跑
case 4:
System.out.println("你个怂货,竟然跑了!");
return -1;
}
return 0;
}
//
public int battle(int chioces) {
int attack = 0;
if (pokemons.pokemonStatus && pokemon[chioces].pokemonStatus) {
System.out.println("+——宝可梦技能——+");
System.out.println("+-------------------------------+");
System.out.println("1." + this.pokemon[chioces].attackSkill1 + " 2." + this.pokemon[chioces].attackSkill2);
System.out.println("3." + this.pokemon[chioces].attackSkill3 + " 4." + this.pokemon[chioces].specSkill);
System.out.println("+-------------------------------+");
System.out.print("【Select】选择你要进行的操作:");
int choice1 = scanner.nextInt();
// int z, y;
switch (choice1) {
case 1:
System.out.println("你使用了" + this.pokemon[chioces].attackSkill1);
attack = this.pokemon[chioces].attackSkill1Value + this.pokemon[chioces].pokemonAttack - pokemons.pokemonDefend;
if (attack > 0) {
pokemons.pokemonhp -= attack;
}
this.pokemon[chioces].attackSkill1Value++;
break;
case 2:
System.out.println("你使用了" + this.pokemon[chioces].attackSkill2);
attack = this.pokemon[chioces].attackSkill2Value + this.pokemon[chioces].pokemonAttack - pokemons.pokemonDefend;
if (attack > 0) {
pokemons.pokemonhp -= attack;
}
this.pokemon[chioces].attackSkill2Value++;
break;
case 3:
System.out.println("你使用了" + this.pokemon[chioces].attackSkill3);
attack = this.pokemon[chioces].attackSkill3Value + this.pokemon[chioces].pokemonAttack - pokemons.pokemonDefend;
if (attack > 0) {
pokemons.pokemonhp -= attack;
}
this.pokemon[chioces].attackSkill3Value++;
break;
case 4:
System.out.println("你使用了" + this.pokemon[chioces].specSkill);
attack = this.pokemon[chioces].specSkillValue + this.pokemon[chioces].pokemonAttack - pokemons.pokemonDefend;
if (attack > 0) {
pokemons.pokemonhp -= attack;
}
this.pokemon[chioces].attackSkill1Value++;
break;
}
//敌对生物随机技能
int monsterSkill = 0;
switch (random.nextInt(4)) {
case 0:
monsterSkill = pokemons.attackSkill1Value;
break;
case 1:
monsterSkill = pokemons.attackSkill2Value;
break;
case 2:
monsterSkill = pokemons.attackSkill3Value;
break;
case 3:
monsterSkill = pokemons.specSkillValue;
break;
}
int a = monsterSkill + pokemons.pokemonAttack - this.pokemon[chioces].pokemonDefend;
if (a > 0) {
this.pokemon[chioces].pokemonhp -= a;
}
System.out.println("【Tips】您目前的生命值还有:" + this.pokemon[chioces].pokemonhp);
if (this.pokemon[chioces].pokemonhp <= 0) {
this.pokemon[chioces].pokemonStatus = false;
} else if (pokemons.pokemonhp <= 0) {
pokemons.pokemonStatus = false;
}
}
if (pokemons.pokemonStatus == false) {
pokemons.pokemonStatus = true;
pokemons.pokemonhp = pokemons.pokemonHp;
System.out.println("你打到了对手");
this.money += 100;
return 1;
} else if (pokemon[chioces].pokemonStatus == false) {
System.out.println("【NPC】虽然你还有可以出战的宝可梦,但你已经输了,真可惜,我的勇士!你还需要历练吖!!!");
return -1;
}
return 0;
}
//打开物品
public void openpackage(String monster) {
//非战斗状态无法使用
System.out.println("1.精灵球.......x" + this.pokeBall);
System.out.println("2.疗伤药.......x" + this.medicine);
//战斗状态无法使用
System.out.println("3.返回");
System.out.print("【Tips】请选择你要进行的操作:");
int choice = scanner.nextInt();
while (choice > 3 || choice < 1) {
System.out.println("【Tips】输入错误");
choice = scanner.nextInt();
}
switch (choice) {
//精灵球
case 1:
System.out.println("【Tips】当前页面无法操作");
break;
//疗伤药
case 2:
if (this.medicine > 0) {
System.out.println("【NPC】选择你要治疗的宝可梦");
showAll();//打印精灵
int a = scanner.nextInt() - 1;
if (this.pokemon[a] != null) {
System.out.println("【NPC】你治疗了你的宝可梦");
this.medicine--;
this.pokemon[a].pokemonhp += 20;
} else {
System.out.println("【NPC】你没有这只宝可梦,就想使用它?!你怕是在做梦。");
}
} else {
System.out.println("【NPC】你的药品不足吖!");
}
break;
//返回
case 3:
encounter(monster);
}
}
//捕获
public int capture() {
if (pokeBall > 0) {
this.pokeBall--;
for (int i = 0; i < 5; i++) {
// int a = 1;
if ((int) Math.random() * 5 == 1) {
System.out.println("【NPC】运气不错,被你捉到了@");
return 1;
} else {
System.out.println("【NPC】你这手气有点黑啊,小辣鸡");
return 0;
}
}
System.out.println("【NPC】我可以确认你是非洲来的,遇到好捕捉的精灵你都捉不到...我的勇士,你不行吖");
return -1;
} else {
System.out.println("【NPC】不是吧不是吧,这就用完啦?随身带着几十个精灵球这不是常识吗?");
return 0;
}
}
//随机生成野怪
public Pokemon createmonster() {
int a = random.nextInt(9);
switch (a) {
case 0:
Hitokage hitokage = new Hitokage();
pokemons = hitokage;
break;
case 1:
Zenigame zenigame = new Zenigame();
pokemons = zenigame;
break;
case 2:
Fushigidane fushigidane = new Fushigidane();
pokemons = fushigidane;
break;
case 3:
Butterfree butterfree = new Butterfree();
pokemons = butterfree;
break;
case 4:
Dagutorio dagutorio = new Dagutorio();
pokemons = dagutorio;
break;
case 5:
Fudin fudin = new Fudin();
pokemons = fudin;
break;
case 6:
Isitsubute isitsubute = new Isitsubute();
pokemons = isitsubute;
break;
case 7:
Pikachuu pikachuu = new Pikachuu();
pokemons = pikachuu;
break;
case 8:
Rokon rokon = new Rokon();
pokemons = rokon;
break;
}
return pokemons;
}
public void show(int chioce) {
System.out.println("@@@@@@@@@^@^@@@@@@@@@@");
System.out.println("+===" + this.pokemon[chioce].pokemonName + "===+");
System.out.println("@|生命值:" + this.pokemon[chioce].pokemonHp);
System.out.println("@|\t技能\t技能值");
System.out.println("@|\t" + this.pokemon[chioce].attackSkill1 + "\t" + this.pokemon[chioce].attackSkill1Value);
System.out.println("@|\t" + this.pokemon[chioce].attackSkill2 + "\t" + this.pokemon[chioce].attackSkill2Value);
System.out.println("@|\t" + this.pokemon[chioce].attackSkill3 + "\t" + this.pokemon[chioce].attackSkill3Value);
System.out.println("@|\t" + this.pokemon[chioce].specSkill + "\t" + this.pokemon[chioce].specSkillValue);
System.out.println("@|防御力:" + this.pokemon[chioce].pokemonDefend);
System.out.println("@@@@@@@@@^@^@@@@@@@@@@");
}
/**
* 展示所有的宝可梦
*/
public void showAll() {
for (int i = 0; i < pokemon.length; i++) {
if (pokemon[i] != null) {
System.out.println("###_________^__________###");
System.out.println(this.pokemon[i].pokemonName);
System.out.print("|生命值:" + this.pokemon[i].pokemonhp);
System.out.println("|防御力:" + this.pokemon[i].pokemonDefend);
System.out.println("|技 能\t" + this.pokemon[i].attackSkill1 + "\t\t" + this.pokemon[i].attackSkill2 + "\t\t" + this.pokemon[i].attackSkill3 + "\t\t" + this.pokemon[i].specSkill);
System.out.println("|技能值\t" + this.pokemon[i].attackSkill1Value + "\t\t" + this.pokemon[i].attackSkill2Value + "\t\t" + this.pokemon[i].attackSkill3Value + "\t\t" + this.pokemon[i].specSkillValue);
System.out.println("###---------v---------###");
}
}
}
}