游戏要求:
进入游戏后有3名英雄供玩家选择,玩家也可以手动选择或随机选择一个英雄。
技能分为:物理伤害技能和法术伤害技能。
每个英雄有三个技能、一把武器。
怪物有三个技能、一个能量盾(吸收物理或法术伤害)。打败怪兽可以获得经验、经验可以用来提英雄升技能等级+血量+攻击力。
游戏中有五关、每关都是一个不同的怪物。每关都是随机生成一个怪物、但是随着关数增加,怪物的"等级"随着增加。
英雄和妖怪最高等级为15级。
实现效果大概如下:
第一步:根据要求来分析需要建立的类:英雄类、怪兽类
英雄类中包括的属性:技能、武器、血量、等级、经验、伤害值;
而其中技能需要三个,需要随机选择,所以这里技能也定义成类比较好
技能类包括的属性有:名字、伤害值、伤害类型(法术或者物理)、技能说明
首先要定义一个技能类的对象数组
其中里面的技能类型用0/1代替 0代表法术1代表物理
(也可以用枚举类型)
因为要随机所以加入一个随机选择技能的方法
下面就是skill()技能类中的所有代码:
这里我定义的变量访问修饰符都是private,是为了防止别人随便修改属性,也算是一种常见的保护措施
import java.util.Random;
public class Skill {
public enum type2{physical,spells}
private String name;
private int type;//0:物理 1:法术
private int damage;
private String desc;
private static Skill skill[]={new Skill("九阴白骨爪",0,300,"建议女性使用"),
new Skill("一阳指",1,270,"建议男性使用"),
new Skill("降龙十八掌",0,400,"建议男性使用"),
new Skill("吸星大法",1,600,"建议神仙使用"),
new Skill("辟邪剑法",0,450,"建议女性使用"),
new Skill("移花接木",1,420,"建议女性使用")
};
private Skill(String name, int type, int damage, String desc) {
this.name = name;
this.type = type;
this.damage = damage;
this.desc = desc;
}
public static Skill radomskill(){
return skill[new Random().nextInt(skill.length)];
}
public void info(){
System.out.println("名字:"+name+",类型:"+(type==0?"物理":"法术")+",伤害值:"+damage+",技能说明:"+desc);
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
}
武器也可以三选一,与技能类操作相同,这里我就直接给出代码了:
package game;
import java.util.Random;
public class Weapon {
private String name;
private int damage;
private static Weapon weapon[]= {new Weapon("屠龙刀", 400),
new Weapon("倚天剑", 600),
new Weapon("冲锋枪", 800)
};
public Weapon(String name,int damage){
this.name=name;
this.damage=damage;
}
public static Weapon weaponrandom(){
return weapon[new Random().nextInt(weapon.length)] ;
}
public void info(){
System.out.println("武器名:"+name+",伤害值:"+damage);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
}
这下就可以开始写英雄类的属性和方法了
这段是为了防止给出的技能重复。
package game;
import java.util.Random;
import java.util.Scanner;
public class hero {
public static Scanner sc=new Scanner(System.in);
private String name;
private int exp;
private int level;
private int blood;
private int damage;
private Weapon weapon;
private Skill skill[]=new Skill[3];//[null,null,null]
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public hero(){
this.name=sc.next();
this.exp=0;
this.level=1;
this.blood=1000;
this.damage=200;
weapon=Weapon.weaponrandom();
int x=0;
lq: while(x<3){
Skill s=Skill.radomskill();
for(Skill skills:skill){
if(skills!=null&&skills.getName().equals(s.getName())){
continue lq;
}
}skill[x++]=s;
}
}
public void info(){
System.out.println("等级:"+level+",经验:"+exp+",血量:"+blood+",攻击力:"+damage);
weapon.info();
for(Skill s:skill){
s.info();
}
System.out.println("*************************");
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
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 getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public Skill[] getSkill() {
return skill;
}
public void setSkill(Skill[] skill) {
this.skill = skill;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
}
怪兽类同理
定义一个game类来搭建大体的框架:
public class Game {
private static hero h;
public static Scanner sc=new Scanner(System.in);
public static void start(){
System.out.println("游戏开始:");
h=pickhero();
System.out.println("你最终选择为:");
h.info();
for(int a=1;a<6;a++){
System.out.println("第"+a+"关:");
boolean b=fight(new Monster(a));
if(b==false){
System.out.println("GAME OVER----");return ;
}
}System.out.println("恭喜通关");
}
最后到了互相攻击的时候:英雄攻击怪兽一次,怪兽攻击英雄一次
现在需要的就是fight()这个方法没有实现。
public static boolean fight(Monster m){
while (true) {
Hurt h1= h.attrack();
int b1= m.reciveDamage(h1);
System.out.println("英雄发起"+(h1.getType()==0?"物理":"法术")+"伤害"+",伤害为:"+h1.getDamage()+",怪兽血量为:"+b1);
if(b1==0){
h.setExp(h.getExp()+ m.getExp());
h.setBlood(h.getBlood());
return true;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Hurt h2= m.attrack();
int b2= h.reciveDamage(h2);
System.out.println("怪兽发起"+(h2.getType()==0?"物理":"法术")+"伤害"+",伤害为:"+h2.getDamage()+",英雄血量为:"+b2);
if(b2==0){
return false;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
这个里面又需要两个方法攻击和掉血(因为涉及到了护盾所以攻击的伤害用类来定义)
public class Hurt {
private int damage;
private int type;
public Hurt(int damage, int type) {
this.damage = damage;
this.type = type;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
在英雄类中加入这段代码来代表随机技能攻击和平a攻击:
public Hurt attrack(){
int r= new Random().nextInt(4);
switch (r) {
case 0:
case 1:
case 2:
return new Hurt(skill[r].getDamage(),skill[r].getType());
case 3:
return new Hurt(this.damage+ weapon.getDamage(),0);
}
return null;
}
public int reciveDamage(Hurt H){
blood=getBlood()-H.getDamage();
return (blood>0?blood:0);
}
又因为打怪可以增加经验,提高属性值所以要更改setexp()方法中的内容来实现这个功能
public void setExp(int exp) {
this.exp = exp;
int level=this.exp/explevel() ;
if(level>0){
System.out.println("恭喜升级"+(getLevel()+1));
setLevel(getLevel()+1);
setBlood(1000+300*this.level);
setDamage(getDamage()+100);}
}
public int explevel(){
int cout=0;
for(int x=1;x<=getLevel();x++){
cout+=260*x;}
return cout;
}
所以英雄类中所有的代码如下:
public class hero {
public static Scanner sc=new Scanner(System.in);
private String name;
private int exp;
private int level;
private int blood;
private int damage;
private Weapon weapon;
private Skill skill[]=new Skill[3];//[null,null,null]
public Hurt attrack(){
int r= new Random().nextInt(4);
switch (r) {
case 0:
case 1:
case 2:
return new Hurt(skill[r].getDamage(),skill[r].getType());
case 3:
return new Hurt(this.damage+ weapon.getDamage(),0);
}
return null;
}
public int reciveDamage(Hurt H){
blood=getBlood()-H.getDamage();
return (blood>0?blood:0);
}
public static Scanner getSc() {
return sc;
}
public static void setSc(Scanner sc) {
hero.sc = sc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public hero(){
this.name=sc.next();
this.exp=0;
this.level=1;
this.blood=1000;
this.damage=200;
weapon=Weapon.weaponrandom();
int x=0;
lq: while(x<3){
Skill s=Skill.radomskill();
for(Skill skills:skill){
if(skills!=null&&skills.getName().equals(s.getName())){
continue lq;
}
}skill[x++]=s;
}
}
public void info(){
System.out.println("等级:"+level+",经验:"+exp+",血量:"+blood+",攻击力:"+damage);
weapon.info();
for(Skill s:skill){
s.info();
}
System.out.println("*************************");
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
int level=this.exp/explevel() ;
if(level>0){
System.out.println("恭喜升级"+(getLevel()+1));
setLevel(getLevel()+1);
setBlood(1000+300*this.level);
setDamage(getDamage()+100);}
}
public int explevel(){
int cout=0;
for(int x=1;x<=getLevel();x++){
cout+=260*x;}
return cout;
}
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 getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public Skill[] getSkill() {
return skill;
}
public void setSkill(Skill[] skill) {
this.skill = skill;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
}g怪兽类如下:
public class Monster {
private int exp;
private int level;
private int blood;
private int damage;
private Shiled shiled;
private Skill skill[]=new Skill[3];//[null,null,null]
public Hurt attrack(){
int r= new Random().nextInt(4);
switch (r) {
case 0:
case 1:
case 2:
return new Hurt(skill[r].getDamage(),skill[r].getType());
case 3:
return new Hurt(this.damage,0);
}
return null;
}
public int reciveDamage(Hurt H){
if(shiled.getDamage()>0&&H.getType()==shiled.getType()){
if(shiled.getDamage()>H.getDamage()){
shiled.setDamage(shiled.getDamage()-H.getDamage());
H.setDamage(0);
}else{
H.setDamage(H.getDamage()- shiled.getDamage());
shiled.setDamage(0);
}
}
blood-=H.getDamage();
return (blood>0?blood:0);
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
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 getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public Shiled getShiled() {
return shiled;
}
public void setShiled(Shiled shiled) {
this.shiled = shiled;
}
public Skill[] getSkill() {
return skill;
}
public void setSkill(Skill[] skill) {
this.skill = skill;
}
public Monster(int y){
this.exp=300*y;
this.level=1*y;
this.blood=1000*y;
this.damage=100*y;
shiled=Shiled.shiledradom();
int x=0;
lq: while(x<3){
Skill s=Skill.radomskill();
for(Skill skills:skill){
if(skills!=null&&skills.getName().equals(s.getName())){
continue lq;
}
}skill[x++]=s;
}
}
public void info(){
System.out.println("等级:"+level+",经验:"+exp+",血量:"+blood+",攻击力:"+damage);
shiled.info();
for(int x=0;x<skill.length;x++){
skill[x].info();
}
System.out.println("*************");
}
}
把这些类放到一个包里就可以定义一个测试类gameapp来运行了
public class GameApp {
public static void main(String[] args) {
new Game().start();
}
}
这充分说明搭建框架的重要性,把框架搭建好再往里面添加相应的功能就简单多了