(Java小游戏)魔塔v1.0
作者:互联网
实现一个控制台操作的4399小游戏——魔塔
游戏情景如下:
- 勇士类
- 怪物类
- 怪物数组类
- 地图类
- 游戏类测试类
- 测试类
代码如下
勇士类Hero.java
package com.nll.oop1;
//勇士
public class Hero {
//属性
private String name;
private int level;
private int blood;
private int attack;
private int defense;
private int gold;
private int exp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getGold() {
return gold;
}
public void setGold(int gold) {
this.gold = gold;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
@Override
public String toString() {
return "勇士,等级"+level+",血量"+blood+",攻击"+attack+",防御"+defense+",金币"+gold+",经验"+exp;
}
public Hero() {
//一开始的数据是定死的
this.name="勇士";
this.level=1;
this.blood=1000;
this.attack=10;
this.defense=10;
this.gold=10;
this.exp=0;}
}
怪物类 Monster.java
package com.nll.oop1;
//怪物
public class Monster {
//属性
private String name;
private int blood;
private int attack;
private int defense;
private int gold;
private int exp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getGold() {
return gold;
}
public void setGold(int gold) {
this.gold = gold;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public Monster(String name, int blood, int attack, int defense, int gold, int exp) {
super();
this.name = name;
this.blood = blood;
this.attack = attack;
this.defense = defense;
this.gold = gold;
this.exp = exp;
}
}
怪兽数组类Monsters.java
package com.nll.oop1;
//定义很多怪物
public class Monsters {
//创建一个该怪物:绿色史莱姆 血量80 攻击力:15 防御力 2 金币 1 经验
static Monster m1=new Monster("绿色史莱姆",80,15,2,1,1);
static Monster m2=new Monster("红色史莱姆",100,18,0,2,2);
static Monster m3=new Monster("骷髅兵",80,50,0,4,4);
static Monster m4=new Monster("智障骷髅兵",120,60,20,8,8);
//将所有的怪物都放到了一个数组中
public static Monster[] ms=new Monster[] {m1,m2,m3,m4};
}
地图类Map.java
package com.nll.oop1;
public class Map {
//有几张地图,每一张地图有多少数据 10关 每关为10*10大小的地图
public static String[][] maps=new String[10][100];
public Map() {
maps[0]=new String[]{
"x","x","x","x","x","x","x","x","x","x",
"x","a","d","x","x","n","x","x"," ","x",
"x","x","1","x","x","4","a","2","a","x",
"x","x"," ","1"," ","d","x","x","d","x",
"x","x"," ","x","x","a","x","x","a","x",
"x","x"," ","x","x"," ","x","x","a","x",
"x","x"," ","x","x"," ","x","x","a","x",
"x","x"," ","x","2","3","a","a","d","x",
"x","x","h","x","a","x","x","x","x","x",
"x","x","x","x","x","x","x","x","x","x",
};
}
}
游戏类Game.java
package com.nll.oop1;
import java.util.Scanner;
public class Game {
// 勇士只有一个
private static Hero hero = new Hero();
//有很多怪物
//private static Monster ms;//这里可以不定义,因为是静态的
private static Map map = new Map();
private int mindex = 0;// 默认是第一层
//定义勇士的位置
private int hindex = 82;// 数组的索引下标
//移动
public void move(int num) {
String str = map.maps[mindex][hindex + num];
if ("x".equals(str)) {
// 意味着,勇士移动之后,是一面墙不能移动
createMap(mindex);// 重新显示地图
return;
} else if (" ".equals(str)) {
// 可以移动
} else if ("a".equals(str)) {
// 吃到红宝石 攻击+3
hero.setAttack(hero.getAttack() + 3);
} else if ("d".equals(str)) {
// 吃到蓝宝石 防御+3
hero.setAttack(hero.getDefense() + 3);
} else if ("r".equals(str)) {
// 吃到红血瓶 生命+200
hero.setAttack(hero.getBlood() + 200);
} else if ("b".equals(str)) {
// 吃到蓝血瓶 生命+500
hero.setAttack(hero.getBlood() + 500);
} else {
// 除了墙 空格 宝石 血瓶之外 剩下的不就是怪物了嘛
// 首先 str要转为int类型的值,因为要索引下标
int m = Integer.parseInt(str) - 1;// 因为索引下标从0开始,而我们的怪物,是从1开始
Monster monster = new Monster(Monsters.ms[m].getName(), Monsters.ms[m].getBlood(),
Monsters.ms[m].getAttack(), Monsters.ms[m].getDefense(), Monsters.ms[m].getGold(),
Monsters.ms[m].getExp());
if (fight(hero, Monsters.ms[m]) == false) {
System.out.println("游戏结束");
System.exit(0);
}
}
map.maps[mindex][hindex] = " ";
hindex = hindex + num;
map.maps[mindex][hindex] = "h";
// 重新显示地图
System.out.println(hero.toString());
createMap(mindex);
}
//打架 勇士和怪物打架,而且我们要知道,打赢了还是打输了
public boolean fight(Hero h, Monster m) {
// 勇士攻击怪物一次造成的伤害
int h2m = h.getAttack() - m.getDefense();
// 怪物攻击勇士一次造成的伤害
int m2h = m.getAttack() - h.getDefense();
// 万一 勇士的防御力:100 怪物的攻击力 20 -80;
if (m2h <= 0) {
m2h = 0;
}
if (h2m <= 0) {
System.out.println("勇士砍不动怪物,被怪物乱刀砍死");
return false;
}
// 如果能破防,那么勇士攻击怪物一次,怪物攻击勇士一次,依次循环
// 直到勇士和怪物,有一方血量小于等于0
do {
// 开始打架,勇士先打怪物
int mblood = m.getBlood() - h2m;
m.setBlood(mblood);
if (mblood <= 0) {
System.out.println("恭喜胜利,您获得" + m.getGold() + "金币," + m.getExp() + "经验");
h.setGold(m.getGold() + h.getGold());
h.setExp(m.getExp() + h.getExp());
// 输出勇士现在的状态
System.out.println(h.toString());
return true;
}
} while (h.getBlood() > 0 && m.getBlood() > 0);
return false;
}
//创建地图
public void createMap(int mindex) {
String[] maps = map.maps[mindex];
for (int i = 0; i < maps.length; i++) {
System.out.print(maps[i] + " ");
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
}
public void startGame() {
// 首先创建地图
createMap(mindex);
System.out.println("游戏开始,请按wsad进行移动");
Scanner sc = new Scanner(System.in);
while (true) {
// 勇士要一直移动
String choise = sc.nextLine();
if ("w".equalsIgnoreCase(choise)) {
move(-10);
} else if ("s".equalsIgnoreCase(choise)) {
move(+10);
System.out.println("游戏开始,请按wsad进行移动");
} else if ("a".equalsIgnoreCase(choise)) {
move(-1);
} else {
move(1);
}
// 注意我们这里取巧了,四周都是墙,意味着勇士是不能穿墙的,因此不需要判断边界
}
}
}
测试类Test.java
package com.nll.oop1;
public class Test {
public static void main(String[] args) {
Game g=new Game();
g.startGame();
//测试打架
// Hero h=new Hero();
// Monster m=Monsters.m1;
// g.fight(h, m);
}
}
代码贴的些许潦草.ha
标签:return,Monster,int,魔塔,private,v1.0,小游戏,void,public 来源: https://blog.csdn.net/qq_43320246/article/details/104575944