java设计模式之假设要实现一个RPG游戏系统框架的部分功能

问题描述

RPG游戏中具备基本角色:属性为 生命,魔法,攻击力,防御力等。
  具备基本功能:普通攻击,技能攻击,防御
RPG游戏能够从基本角色扩展出不同的角色:例如法师、战士等.
  这些角色的区别主要体现在基本功能的区别上。
  请用策略模式实现RPG游戏的基本角色,能够达到生成基本角色,并搭配不同的角色的基本功能,形成新的角色的效果。例如:基本角色+法师基本功能 = 法师。
  请提供一个角色生成工厂,根据要创建的角色类型的不同,形成不同的角色。
  一个RPG游戏角色,在游戏中具有以下基本状态:待命(没有任何操作),移动(有移动操作),战斗(进入战斗状态),濒死(hp<10%),死亡(hp=0)5种状态,状态之间存在行为转换,请使用状态模式进行描述。

设计思路

   1.首先确定我们角色应该具备的基本属性(生命,魔法,攻击力,防御力)他们所具有的属性该有的值类型
   2.每个用户使用不同的角色时,使用的技能不一样,因此当我们对不同的角色最开始不知道用户会使用哪个技能,所以通过策略模式
   3.为了方便以后在公差个里面创建具体角色,所以我写了一个抽象类,方便以后工厂创建角色.
   4.最后生成角色的uml图如下:
java设计模式之假设要实现一个RPG游戏系统框架的部分功能_第1张图片 
 
 

   1.我们会在工厂里面通过角色的键入选择来创造相应的角色.
   2.最后生成工厂的uml图如下:
java设计模式之假设要实现一个RPG游戏系统框架的部分功能_第2张图片 
 
 

  1.当我们创建好了角色以后,我们就要事项对其的使用了.
  2.首先确定我们的角色应该具备的一些状态,我在这里建立了5种状态(等待,移动,战斗,濒死,死亡)
  3.状态定义好以后我们就得实现状态之间的切换.通过状态里面的方法,当达到某一个条件以后,我们就可以从一个状态转到另外一个状态.
  4.最后生成工厂的uml图如下:
java设计模式之假设要实现一个RPG游戏系统框架的部分功能_第3张图片

具体实现

为了通过工厂去更好的构建我们不同的角色,所以需要将其所有的攻击和防御抽象为一个接口

基础攻击方式:

//基本攻击方式
public interface BaseAttack {
    String operation();
}

public class Hatchet implements BaseAttack{
    @Override
    public String operation() {
        return "斧头";
    }
}

public class Sceptre implements BaseAttack{

    @Override
    public String operation() {
        return "权杖";
    }
}

public class Sword implements BaseAttack{
    @Override
    public String operation() {
        return "剑";
    }
}

魔法攻击方式

//魔法攻击方式
public interface MagicAttack {
     String operation();
}

public class Flying_shield implements MagicAttack{
    @Override
    public String operation() {
        return "飞盾投掷";
    }
}

public class Magic implements MagicAttack{
    @Override
    public String operation() {
        return "超星星";
    }
}

public class Wan_jian implements MagicAttack{
    @Override
    public String operation() {
        return "万剑齐发";
    }
}

除开攻击还具有防御的方式

public interface Defend {
    String operation();
}

public class Cross_sword implements Defend{
    @Override
    public String operation() {
        return "横剑格挡";
    }
}

public class Magic_shield implements Defend{

    @Override
    public String operation() {
        return "魔法护盾";
    }
}

public class Shield implements Defend{
    @Override
    public String operation() {
        return "盾牌";
    }
}

角色的攻击方式和防御方解决以后开始我们的基础角色的创建

这是角色基类,具体的角色都要对里面的方法进行具体的实现以及赋值才能成为一个角色

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.MagicAttack.MagicAttack;
import RPG.State.State;


//角色基类
public abstract class Role {
    public State curState;
    private String name;//角色名字
    private int hp;//角色血量
    private int mp;//角色蓝量
    private int fighting;//角色攻击力
    private int defence;//角色防御力
    BaseAttack baseAttack;//角色基本攻击方式
    MagicAttack magicAttack;//角色基本防御方式
    Defend defend;//角色防御攻击方式
    private int de;

    public int getDe() {
        return de;
    }
    public abstract void Start();

    public MagicAttack getMagicAttack() {
        return magicAttack;
    }

    public void setMagicAttack(MagicAttack magicAttack) {
        this.magicAttack = magicAttack;
    }

    public Defend getDefend() {
        return defend;
    }

    public void setDefend(Defend defend) {
        this.defend = defend;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public int getMp() {
        return mp;
    }

    public void setMp(int mp) {
        this.mp = mp;
    }

    public int getFighting() {
        return fighting;
    }

    public void setFighting(int fighting) {
        this.fighting = fighting;
    }

    public int getDefence() {
        return defence;
    }

    public void setDefence(int defence) {
        this.defence = defence;
    }
    public void starAttack(){ curState.starAttack(); }
    public void move(String a){ curState.move(a); }
    public void runAway(){
        curState.runAway();
    }
    public void resurrection(){ curState.resurrection(); }
    public void await(){ curState.await(); }

    public abstract BaseAttack getBaseAttack();

    public void setBaseAttack(BaseAttack baseAttack) {
        this.baseAttack = baseAttack;
    }
}

法师:

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.MagicAttack.MagicAttack;
import RPG.State.WaitState;

public class Mage extends Role{
    public String name="法师";//角色名字
    public int hp=100;//角色血量
    public int mp=200;//角色蓝条
    public int fighting=50;//角色攻击力
    public int defence=20;//角色防御力
    BaseAttack baseAttack;//角色基本攻击方式
    MagicAttack magicAttack;//角色基本防御方式
    Defend defend;//角色防御攻击方式
    private int de = 100;

    public int getDe() {
        return de;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setHp(int hp) {
        this.hp = hp;
    }

    @Override
    public void setMp(int mp) {
        this.mp = mp;
    }

    @Override
    public void setFighting(int fighting) {
        this.fighting = fighting;
    }

    @Override
    public void setDefence(int defence) {
        this.defence = defence;
    }

    @Override
    public void setBaseAttack(BaseAttack baseAttack) {
        this.baseAttack = baseAttack;
    }

    @Override
    public void setMagicAttack(MagicAttack magicAttack) {
        this.magicAttack = magicAttack;
    }

    @Override
    public void setDefend(Defend defend) {
        this.defend = defend;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getHp() {
        return hp;
    }

    @Override
    public int getMp() {
        return mp;
    }

    @Override
    public int getFighting() {
        return fighting;
    }

    @Override
    public int getDefence() {
        return defence;
    }

    @Override
    public BaseAttack getBaseAttack() {
        return baseAttack;
    }

    @Override
    public MagicAttack getMagicAttack() {
        return magicAttack;
    }

    @Override
    public Defend getDefend() {
        return defend;
    }

    public Mage(BaseAttack baseAttack, MagicAttack magicAttack, Defend defend) {
        this.baseAttack = baseAttack;
        this.magicAttack = magicAttack;
        this.defend = defend;
        this.curState = new WaitState(this);
    }

    @Override
    public void Start() {

    }
}

坦克:

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.MagicAttack.MagicAttack;
import RPG.State.WaitState;
public class Tank extends Role{
    String name="坦克";//角色名字
    int hp=250;//角色血量
    int mp=70;//角色蓝条
    int fighting=20;//角色攻击力
    int defence=70;//角色防御力
    BaseAttack baseAttack;//角色基本攻击方式
    MagicAttack magicAttack;//角色基本防御方式
    Defend defend;//角色防御攻击方式
    private int de = 250;
    public int getDe() {
        return de;
    }
    @Override
    public String getName() {
        return name;
    }
    @Override
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void setHp(int hp) {
        this.hp = hp;
    }
    @Override
    public void setMp(int mp) {
        this.mp = mp;
    }
    @Override
    public void setFighting(int fighting) {
        this.fighting = fighting;
    }
    @Override
    public void setDefence(int defence) {
        this.defence = defence;
    }
    @Override
    public void setBaseAttack(BaseAttack baseAttack) {
        this.baseAttack = baseAttack;
    }
    @Override
    public void setMagicAttack(MagicAttack magicAttack) {
        this.magicAttack = magicAttack;
    }
    @Override
    public void setDefend(Defend defend) {
        this.defend = defend;
    }
    @Override
    public int getHp() {
        return hp;
    }
    @Override
    public int getMp() {
        return mp;
    }
    @Override
    public int getFighting() {
        return fighting;
    }
    @Override
    public int getDefence() {
        return defence;
    }
    @Override
    public BaseAttack getBaseAttack() {
        return baseAttack;
    }
    @Override
    public MagicAttack getMagicAttack() {
        return magicAttack;
    }
    @Override
    public Defend getDefend() {
        return defend;
    }
    public Tank(BaseAttack baseAttack, MagicAttack magicAttack, Defend defend){
        this.baseAttack = baseAttack;
        this.magicAttack = magicAttack;
        this.defend = defend;
        this.curState = new WaitState(this);
    }
    @Override
    public void Start() {
    }
}

战士:

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.MagicAttack.MagicAttack;
import RPG.State.State;
import RPG.State.WaitState;

public class Warrior extends Role{
    String name="战士";//角色名字
    int hp=150;//角色血量
    int mp=100;//角色蓝条
    int fighting=70;//角色攻击力
    int defence=40;//角色防御力
    BaseAttack baseAttack;//角色基本攻击方式
    MagicAttack magicAttack;//角色基本防御方式
    private int de = 150;

    public int getDe() {
        return de;
    }

    public State getCurState() {
        return curState;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getHp() {
        return hp;
    }

    @Override
    public int getMp() {
        return mp;
    }

    @Override
    public int getFighting() {
        return fighting;
    }

    @Override
    public int getDefence() {
        return defence;
    }

    @Override
    public BaseAttack getBaseAttack() {
        return baseAttack;
    }

    @Override
    public MagicAttack getMagicAttack() {
        return magicAttack;
    }

    @Override
    public Defend getDefend() {
        return defend;
    }

    Defend defend;//角色防御攻击方式

    public Warrior(BaseAttack baseAttack, MagicAttack magicAttack, Defend defend) {
        this.baseAttack = baseAttack;
        this.magicAttack = magicAttack;
        this.defend = defend;
        this.curState = new WaitState(this);
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setHp(int hp) {
        this.hp = hp;
    }

    @Override
    public void setMp(int mp) {
        this.mp = mp;
    }

    @Override
    public void setFighting(int fighting) {
        this.fighting = fighting;
    }

    @Override
    public void setDefence(int defence) {
        this.defence = defence;
    }

    public void setBaseAttack(BaseAttack baseAttack) {
        this.baseAttack = baseAttack;
    }

    @Override
    public void setMagicAttack(MagicAttack magicAttack) {
        this.magicAttack = magicAttack;
    }

    @Override
    public void setDefend(Defend defend) {
        this.defend = defend;
    }

    @Override
    public void Start() {
    }
}

具体的角色的创建需要在工厂里面才能实现,通过简单工厂对每个角色赋予它技能
通过有参构造器判断传入的名字来对角色赋予技能.

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.Hero.Role;
import RPG.MagicAttack.MagicAttack;


public interface Factory {
    Role createHero(String name, BaseAttack baseAttack, Defend defend, MagicAttack magicAttack);
}

工厂具体的实现

import RPG.BaseAttack.BaseAttack;
import RPG.Defend.Defend;
import RPG.Hero.Mage;
import RPG.Hero.Role;
import RPG.Hero.Tank;
import RPG.Hero.Warrior;
import RPG.MagicAttack.MagicAttack;

public class Hero_factory implements Factory{
    Role role;
    @Override
    public Role createHero(String name, BaseAttack baseAttack, Defend defend, MagicAttack magicAttack) {
        if (name.equals("法师")){
            role = new Mage(baseAttack,magicAttack,defend);
        }
        else if (name.equals("战士")){
            role = new Warrior(baseAttack,magicAttack,defend);
        }
        else if (name.equals("坦克")){
            role = new Tank(baseAttack,magicAttack,defend);
        }
        return role;
    }
}

为了角色能够活动起来,通过状态模式改变他们的状态
这是简单的几个状态转化是会调用的方法

public abstract class State {
    Role role;

    public State(Role role) {
        this.role = role;
    }

    public abstract void starAttack();
    public abstract void move(String a);
    public abstract void runAway();
    public abstract void resurrection();
    public abstract void await();
}

等状态:

import RPG.Hero.Role;

public class WaitState extends State{
    public WaitState(Role role) {
        super(role);
    }

    @Override
    public void starAttack() {
        //todo 什么都不做
    }

    @Override
    public void move(String a) {
        System.out.println("准备移动");
        System.out.println("由[等待状态]转化为[移动状态]");
        role.curState= new MoveState(role);
    }

    @Override
    public void runAway() {
        //todo 什么都不做
    }

    @Override
    public void resurrection() {
        //todo 什么都不做
    }

    @Override
    public void await() {
        System.out.println("继续等待");
    }

}

移动态:

import RPG.Hero.Role;

public class MoveState extends State{
    public MoveState(Role role) {
        super(role);
    }

    @Override
    public void starAttack() {
        //todo 都不做
    }

    @Override
    public void move(String a) {
        if (a.equals("w")){
            System.out.println("向上移动");
            System.out.println("寻找小怪");
        }
        if (a.equals("a")){
            System.out.println("向左移动");
            System.out.println("寻找小怪");
        }
        if (a.equals("s")){
            System.out.println("向下移动");
            System.out.println("找到小怪");
            System.out.println("准备进入战斗");
            System.out.println("由[移动状态]转化为[战斗状态]");
            role.curState = new FightState(role);
        }if (a.equals("d")){
            System.out.println("向右移动");
            System.out.println("寻找小怪");
        }
    }

    @Override
    public void runAway() {

    }

    @Override
    public void resurrection() {

    }

    @Override
    public void await() {

    }
}

战斗态:

import RPG.Hero.Role;

import java.util.Random;

public class FightState extends State{
    public FightState(Role role) {
        super(role);
    }

    @Override
    public void starAttack() {
        System.out.println("开始进行攻击");
        int hp = role.getHp();
        Random random = new Random();
        int i = random.nextInt(20);
        System.out.println("小怪打了你"+i+"的血量");
        hp =hp-i;
        role.setHp(hp);
        if (role.getHp()<=0){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("由[战斗状态]转化为[死亡状态]");
            role.curState = new DethState(role);
        }
        else if (hp<role.getDe()*0.1){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("由[战斗状态]转化为[濒死状态]");
            role.curState = new DyingState(role);
        }
    }

    @Override
    public void move(String a) {
        //todo 什么都不做
    }

    @Override
    public void runAway() {
        System.out.println("打不过,选择逃跑");
        System.out.println("由[战斗状态]转化为[等待状态]");
        role.curState = new WaitState(role);
    }

    @Override
    public void resurrection() {
        //todo 什么都不做
    }

    @Override
    public void await() {

    }
}

濒死态:

import RPG.Hero.Role;

public class DyingState extends State{
    public DyingState(Role role) {
        super(role);
    }


    @Override
    public void starAttack() {
        System.out.println("我要放手一搏");
        int hp = role.getHp();
        hp -= 1;
        role.setHp(hp);
        if (role.getHp()<=0){
            System.out.println("由[濒死状态]转化为[死亡状态]");
            role.curState = new DethState(role);
        }
        else if (role.getHp()<role.getDe()*0.1){
            role.curState = new DyingState(role);
        }
    }

    @Override
    public void move(String a) {

    }

    @Override
    public void runAway() {
        System.out.println("血量不足");
        System.out.println("由[濒死状态]转化为[等待状态]");
        role.curState = new WaitState(role);
    }

    @Override
    public void resurrection() {
        //todo 什么都不做
    }

    @Override
    public void await() {

    }
}

死亡态:

import RPG.Hero.Role;

public class DethState extends State{

    public DethState(Role role) {
        super(role);
    }

    @Override
    public void starAttack() {
        System.out.println("已经死亡");
        System.out.println("只能结束,0退出");
        return ;
    }

    @Override
    public void move(String a) {
        //todo 不做任何事
    }

    @Override
    public void runAway() {
        //todo 不做任何事
    }

    @Override
    public void resurrection() {
        System.out.println("复活,进入待命状态");
        role.setHp(role.getDe());
        role.curState = new WaitState(role);
    }

    @Override
    public void await() {

    }
}

具体的测试时格式控制:

import RPG.BaseAttack.Hatchet;
import RPG.BaseAttack.Sceptre;
import RPG.BaseAttack.Sword;
import RPG.Defend.Cross_sword;
import RPG.Defend.Magic_shield;
import RPG.Defend.Shield;
import RPG.Factory.Hero_factory;
import RPG.Hero.Role;
import RPG.MagicAttack.Flying_shield;
import RPG.MagicAttack.Magic;
import RPG.MagicAttack.Wan_jian;
import RPG.State.WaitState;

import java.util.Scanner;

public class App {
    void person(){
        Hero_factory factory = new Hero_factory();
        int x;
        Role role = null;
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入选择的角色:[0]法师,[1]坦克,[2]战士");
            x=scanner.nextInt();
            if (x==0||x==1||x==2){
                break;
            }
        }
        switch (x){
            case 0 :
                role = factory.createHero("法师", new Sceptre(), new Magic_shield(), new Magic());
                break;
            case 1 :
                role = factory.createHero("坦克", new Hatchet(), new Shield(), new Flying_shield());
                break;
            case 2 :
                role = factory.createHero("战士", new Sword(), new Cross_sword(), new Wan_jian());
                break;
        }
        System.out.println("您选择的角色的属性:"+'\n'+"角色名:"+'\t'+role.getName()+'\n'+
                "角色蓝条:"+'\t'+role.getMp()+'\n'+"角色攻击力:"+'\t'+role.getFighting()+'\n'+
                "角色防御力:"+'\t'+role.getDefence()+'\n'+
                "角色基本攻击:"+'\t'+ role.getBaseAttack().operation()+
                '\n'+ "角色魔法攻击:"+'\t'+role.getMagicAttack().operation()+
                '\n'+"角色防御方式:"+'\t'+role.getDefend().operation()
                );
        System.out.println();
        System.out.println("创建成功,请开始游戏:[0]准备移动寻找小怪,[1]继续等待");
        int y ,n;
        String a;
        y=scanner.nextInt();
        switch (y){
            case 0:
                while (true){
                    System.out.println("请输入操作:w,a,s,d,输入0退出程序");
                    a=scanner.next();
                    role.move(a);
                    if (a.equals("s")){
                        role.starAttack();
                        while (true){
                            n = role.getHp();
                            if (n<=0){
                                role.setHp(0);
                                System.out.println("血量为0");
                                role.resurrection();
                                break;
                            }
                            System.out.println("剩余血量还有"+n+",选择是否攻击,y/n");
                            a= scanner.next();
                            if (a.equals("y")){
                                role.starAttack();
                                if (n<=0){
                                    break;
                                }
                            }else if (a.equals("n")){
                                role.runAway();
                                break;
                            }
                        }
                    }
                    if (a.equals("0")){
                        break;
                    }
                }
            case 1 :
                role.curState = new WaitState(role);
                role.await();
        }
    }

}

客户端的测试:

public class Client {
    public static void main(String[] args) {
        App app = new App();
        app.person();
    }
}

 

  初次发表文章,且技术还不好,还请大佬指正错误

你可能感兴趣的:(java,游戏,开发语言)