学习JAVA做的第一个控制台小游戏
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
因为要忙毕设和毕业论文,精力无法集中,导致游戏无法达到自己的预期效果
一、游戏介绍
我们这款游戏是一款文字控制台小游戏,背景是中国武侠风,玩家可以在游戏开始时,了解本游戏的背景故事,阅读完后可以输入玩家的角色名,选择自己想要加入的门派,在各自门派中可以学习该门派的独有技能,每个大地图都有一个门派,医管和修炼房,而其中的一个地图有怪物的小地图,玩家可以通过打怪获得经验和金币进行升级,最终打败最终BOSS来实现游戏通关。
二、功能实现
1.玩家类属性
代码如下(示例):
public String name;//名字
public String[] skill = new String[5];//技能
public String profession = "散人";//职业门派
public int money;//金钱
public int experience;//经验值
public int gradexcpiton;//当前等级经验最大值
public int grade;//等级
//四大基本属性
public int HP;//血量
public int allHP;//血量上限
public int MP;//蓝量
public int allMP;//蓝量上限
public int PA;//物理攻击
public int MA;//魔法攻击
2.角色门派初始属性
//职业选择
public void pfChoose(int i) {
if (i == 1) {
this.profession = "绝情谷";
// System.out.println("您的门派为:" + this.profession);
} else if (i == 2) {
this.profession = "藏剑山庄";
// System.out.println("您的门派为:" + this.profession);
} else if (i == 3) {
this.profession = "神龙岛";
// System.out.println("您的门派为:" + this.profession);
} else if (i == 4) {
this.profession = "红花会";
// System.out.println("您的门派为:" + this.profession);
} else if (i == 5) {
this.profession = "唐门";
// System.out.println("您的门派为:" + this.profession);
} else {
System.out.println("该门派不存在,请重新输入");
System.out.println(0);
}
}
// 初始属性
public void cssx() {
if (this.profession == "绝情谷") {
this.HP = 100;
this.MP = 50;
this.PA = 20;
this.MA = 50;
}
if (this.profession == "藏剑山庄") {
this.HP = 100;
this.MP = 50;
this.PA = 55;
this.MA = 40;
}
if (this.profession == "神龙岛") {
this.HP = 100;
this.MP = 50;
this.PA = 40;
this.MA = 40;
}
if (this.profession == "红花会") {
this.HP = 100;
this.MP = 50;
this.PA = 45;
this.MA = 35;
}
if (this.profession == "唐门") {
this.HP = 100;
this.MP = 50;
this.PA = 15;
this.MA = 40;
}
this.allHP = this.HP;
this.allMP = this.MP;
}
3.角色升级
代码如下(示例):
public void gradeIn() {
this.grade++;//等级增加
this.experience = this.experience - this.gradexcpiton;//升级后余下经验
this.gradexcpiton = this.grade * 10;//经验上限增加
System.out.println("恭喜你升级了!");
gradesdAdd();//升级后属性成长
}
4.角色升级属性成长
代码如下(示例):
//人物升级
public void gradeIn() {
this.grade++;//等级增加
this.experience = this.experience - this.gradexcpiton;//升级后余下经验
this.gradexcpiton = this.grade * 10;//经验上限增加
System.out.println("恭喜你升级了!");
gradesdAdd();//升级后属性成长
}
public void gradesdAdd() {
if (this.profession == "绝情谷") {
this.allHP = this.allHP + 10;
this.allMP = this.allMP + 10;
this.PA = this.PA + 2;
this.MA = this.MA + 4;
}
if (this.profession == "藏剑山庄") {
this.allHP = this.allHP + 20;
this.allMP = this.allMP + 3;
this.PA = this.PA + 4;
this.MA = this.MA + 0;
}
if (this.profession == "神龙岛") {
this.allHP = this.allHP + 10;
this.allMP = this.allMP + 10;
this.PA = this.PA + 3;
this.MA = this.MA + 3;
// System.out.println("您的1技能为:"+this.skill[0]);
}
if (this.profession == "红花会") {
this.HP = this.HP + 8;
this.MP = this.MP + 10;
this.PA = this.PA + 2;
this.MA = this.MA + 4;
}
if (this.profession == "唐门") {
this.HP = this.HP + 8;
this.MP = this.MP + 12;
this.PA = this.PA + 2;
this.MA = this.MA + 3;
}
this.HP = this.allHP;
this.MP = this.allMP;
}
5.修炼馆
代码如下(示例):
//练功房修炼
Scanner scanner = new Scanner(System.in);
public void xiulian(Player player) {
System.out.println("你到了练功房,修炼一次消耗500金币,增加50经验值 是否进行修炼(true 1/false 0)");
int s = scanner.nextInt();
if (s == 1) {
if (s == 1) {
if (player.money >= 500) {
player.money=player.money - 500;
player.experience = player.experience + 50;
if (player.experience >= player.gradexcpiton) {
player.gradeIn();
}
if (player.experience >= player.gradexcpiton) {
player.gradeIn();
}
} else {
System.out.println("您的金钱不足,不足以支付修炼费用");
}
}
}
}
6.医馆
代码如下(示例):
//医馆
public void doctors(Player player){
System.out.println("你遇到了医师 是否需要服务 (true 1/false 0)");
int s = scanner.nextInt();
if (s == 1) {
System.out.println("1、500金币治疗满血 2、200金币回复满蓝");
System.out.println("请输入需要的操作");
int x = scanner.nextInt();
if (x == 1) {
if (player.money >= 500) {
player.HP = player.allHP;
player.money = player.money - 500;
} else {
System.out.println("您的金钱不足,不足以支付治疗费用");
}
} else {
if (player.money >= 200) {
player.MP = player.allMP;
player.money = player.money - 200;
} else {
System.out.println("您的金钱不足,不足以支付回蓝费用");
}
}
}
}
7.门派
代码如下(示例):
这里角色无论有没有金钱学习技能都会学会,因为我在调用显示技能的时候就把技能赋值进去了。我这里搞了一个对象来存应该就能解决这个问题了
public void professionly(Player player){
Skill skill=new Skill();
System.out.println("你遇到了你的师傅 是否学习技能(true 1/false 0)");
int s = scanner.nextInt();
if (s == 1) {
System.out.println("技能列表");
System.out.println("0、"+skill.secondSkill(player));
// System.out.println(skill.skill[1]);
int x = scanner.nextInt();
if(player.money>=3000) {
if (x == 0) {
player.money=player.money-3000;
String a=skill.secondSkill(player);
player.skill[1] = a;
System.out.println("你习得技能:" + player.skill[1]);
}
}
else
System.out.println("您的金钱不足以学习该技能");
}
}
8.游戏演示
总结
通过两周的学习,期间了解了JAVA的特性,封装、继承、多态,在本次项目中老师要求我们尽量的使用面向对象的思想来做,但是做的时候却还是使用面向过程的思想进行的编码,虽然进行了封装,但是封装的过于功能多元化,继承方面就是为了继承某个属性而去继承,至于多态更是没有用到。总之,学习JAVA还是得先有面向对象的思想,思维不转变,JAVA的运用将会很难,学习效率也会不高。