【问题标题】:Implementing a simple text adventure game in java (working with interfaces)用java实现一个简单的文本冒险游戏(使用接口)
【发布时间】:2016-05-04 21:15:52
【问题描述】:

我正在阅读ECS/Composition over inheritance 并决定修改我目前正在开发的一款文字冒险游戏,以更好地理解这个概念。

我决定在我的游戏中使用特定武器进行尝试。

现在,在我的游戏中,逻辑是每个武器都被视为一个 IGameObject。考虑到这一切,我想出了以下几点:

武器界面:

public interface IWeapon {
    int getWeaponDamage();
}

武器类:

public class Weapon implements IWeapon {

    private int damage;

    public Weapon(int damage)
    {
        this.damage = damage;
    }

    @Override
    public int getWeaponDamage() {

        return this.damage;
    }

IGameObject 接口:

public interface IGameObject {

    String getGameObjectID();
    String getGameObjectName();
    String getGameObjectDescription();
}

游戏对象类:

public class GameObject implements IGameObject {

    private String ID;
    private String name;
    private String Desc;

    public GameObject(String ID, String name, String Desc)
    {
        this.ID = ID;
        this.name = name;
        this.Desc = Desc;
    }

使用所有这些的抽象类:

public abstract class GameGun implements IGameObject, IWeapon {

    IGameObject gameObj;
    IWeapon weaponObj;

    //Left out getters to keep it simple and easy to read.
    public GameGun(IGameObject game, IWeapon weapon)
    {
        this.gameObj = game;
        this.weaponObj = weapon;
    }
    public void fire()
    {
        System.out.println("Shot Fired");
    }

    public void reload()
    {
        //implementation for reload method
    }

把它们放在一起:

主要方法:

    GameObject sleep = new GameObject("SLP-1","Sleep Weapon","A Custom Made Gun");
    Weapon sleepDamage = new Weapon(10);

    GameGun sleepWeapon = new SleepWeapon(sleep,sleepDamage);

问题:

  1. 我只是想知道我的实现是否正确?
  2. 如果不是,我将如何更正它?

【问题讨论】:

    标签: java oop game-engine composition


    【解决方案1】:

    我一直在纠正很多东西并让它发挥作用。你现在可以玩了 :D 请了解要点以便获得所有工作代码。

    控制台

    Revolver Gun:Gun reloaded
    Revolver Gun:Gun shots => 10 damage(s)
    Basic Sword:Sword cuts =>  5 damage(s)
    Basic Sword:Sword cuts =>  5 damage(s)
    Basic Sword:Sword cuts =>  5 damage(s)
    Revolver Gun:Gun shots => 10 damage(s)
    

    代码

    package com.rizze.test.labs.sof.gamegun;
    
    import org.junit.Test;
    
    public class GameGunTest {
    
    
        @Test
        public void betterCode2() {
    
            Game sleep = new Game("SLP-1","Sleep Weapon","A Custom Made Gun");
            Gun gun = new Gun();
            Sword sword = new Sword();
    
            gun.hit();
            sword.hit();
            sword.hit();
            sword.hit();
            gun.hit();
        }
    
    
    
        public class Gun implements IWeapon {
    
            private int damage;
            private String name;
    
            public Gun(int damage)
            {
                this.damage = damage;
                this.reload();
            }
    
            public Gun()
            {
                this.damage = 10;
                this.name= "Revolver Gun";
                this.reload();
            }
    
            @Override
            public int getDamages() {
    
                return this.damage;
            }
    
            /**
             * 
             * @return damages
             */
            @Override
            public int hit() {
                System.out.println( this.name + ":Gun shots => "+damage +" damage(s)");
                return this.damage;
            }
    
            @Override
            public void reload() {
                System.out.println(this.name+":Gun reloaded");          
            }
    
        }
    
    
        public class Sword implements IWeapon {
    
            private String name;
            private int damage;
    
            public Sword(String name){  
                this.damage = 5;
                this.name=name;
    
            }
    
            public Sword(){ 
                this.damage = 5;
                this.name="Basic Sword";
    
            }
    
            @Override
            public int getDamages() {
    
                return this.damage;
            }
    
            /**
             * 
             * @return damages
             */
            @Override
            public int hit() {
                System.out.println( name+ ":Sword cuts =>  "+damage +" damage(s)");
                return damage;
            }
    
            @Override
            public void reload() {
                System.out.println( name+ ":Sword - skipped reload" );          
            }
    
        }
    
    
    
    
    
        public class Game implements IGame {
    
            private String id;
            private String name;
            private String desc;
    
            public Game(String ID, String name, String Desc)
            {
                this.id = ID;
                this.name = name;
                this.desc = Desc;
            }
    
            @Override
            public String getId() {
                return this.id;
            }
    
            @Override
            public String getName() {
                return this.getName();
            }
    
            @Override
            public String getDesc() {
                return this.desc;
            }
        }
    
    
    
    }
    //IGAME
    
    public interface IGame {
    
        String getId();
        String getName();
        String getDesc();
    }
    
    
     //IWEAPON 
    
    package com.rizze.test.labs.sof.gamegun;
    
        public interface IWeapon {
            int getDamages();
            int hit();
            void reload();
        }
    

    要点 这是完整的 GIST:https://gist.github.com/jeorfevre/92d093853bfd3fc5c666b915b309abf1

    【讨论】:

    • 我不明白你为什么需要一个游戏枪对象?所以在 betterCode() 示例中我没有使用它!看例子!享受:D
    • 非常感谢!很高兴看到我走在正确的轨道上。我有一个 GameGun 抽象类,因为并非所有武器都是枪。我可能也想要剑。 GameGun 将处理枪的基本动作。虽然武器只是返回并保持伤害。
    • 90% 的代码都在工作。我不会做一个 GameGun 类,因为你会一次又一次地重复样板代码来传递函数的返回。您是否一直在考虑使用 scala 编写游戏。代码简洁干净。值得花点钱来看看!如果解决方案对您有好处,也可以考虑投票:)。
    • 鉴于并非所有武器都将成为枪,拥有 IGameGun 然后直接创建枪而不是通过抽象类创建枪会更好吗?
    • 使用 IWeapon,并将其用于所有武器 不要创建很多界面,因为您会失去很多界面的含义/对 java 的兴趣!把事情简单化。更新了剑与枪的回应(也是要点)
    猜你喜欢
    • 2016-08-25
    • 2016-12-07
    • 2012-05-13
    • 2013-07-08
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多