文字游戏。如何使用包?

Text game. How to use a bag?

我想知道如何在我制作的游戏中使用包。这个袋子应该装有 2 件物品,可以在 "map" 周围的不同房间找到,当找到这两种物品时,游戏可以通过找到老板房间来完成。这个袋子应该是它自己的 java class。我如何让玩家激活这些项目?

rum4 和 rum6 是任何想知道的人的房间。

(我不想让任何人帮我完成这个游戏,我只是想要一些帮助)

//对不起瑞典文。

//这显然不是全部代码,但其余部分不是必需的。

import java.util.Scanner;

public class Spel
{
public static void main(String[] args) {
    Rum start = new Rum("Du är i en mörk och fuktig källare."," En källare. ");
    Rum rum1 = new Rum("Du är mitt i en snöstorm!", "En snöstorm. ");
    Rum rum2 = new Rum("Du hittade ett svärd!", "Ett hus. ");
    Rum rum3 = new Rum("Du gick in i en fälla, slå över 3 för att fly norrut.", "En skog. ");
    Rum rum4 = new Rum("Jaha... här fanns det ingenting.", "En äng. ");

    start.north = rum1;
    start.east = rum2;
    start.south = rum3;

    rum1.south = start;
    rum1.east = rum4;

    rum2.west = start;
    rum2.north = rum4;

    rum3.fälla = new trap();
    rum3.north = start;

    rum4.west = rum1;
    rum4.south = rum2;

    Rum current = start;
    while(true) {
        System.out.println(current);
        System.out.println("Vart vill du gå? (n,s,v,o)");
        char c = new Scanner(System.in).next().charAt(0);
        switch(c) {
            case 'n':
                current = current.north;
                break;
            case 's':
                current = current.south;
                break;
            case 'v':
                current = current.west;
                break;
            case 'o':
                current = current.east;
                break;
        }
        if (current.fälla != null){
            current.fälla.rulla();
        }
    //  if (monster){
    //      System.out.println("Du kan nu döda monsteret");
        if(current == null) {
            System.out.println("Du ramlar av världen och dör †††");
            System.out.println("Försök igen");
            current = start;
        }
    }

您可以创建一些 Player class 来存储所有玩家数据,然后将 Bag 放入其中以保存项目,例如:

HashMap<String, Item> itemsByName;

并且在向其中添加新项目时始终检查尺寸 2 是否未超过尺寸。

我会创建一个 class 包含两个项目的包

public class Bag {
   Item item1;
   Item item2;
}

public class Item {
   <any item properties here>
}

在那种情况下,我相信您需要 class 来实现 Item,以及特定效果的项目类型。

public abstract class Item {
    public abstract void useItemOn(Entity target);
}

public class Entity {
     private int maxHitPoints;
     private int hitPoints;
     private boolean isDead;
     ...
     public void inflictDamage(int damage) {
         hitPoints -= damage;
         if(hitPoints < 0) {
             this.isDead = true;
         }
     }

     public void heal(int healPoints) {
         hitPoints += healPoints;
         if(hitPoints > maxHitPoints) {
             this.hitPoints = maxHitPoints;
         }
     }
     ...
}

public class Player extends Entity {
    ...
}

public class Enemy extends Entity {
    ...
}

public Bomb extends Item {
    @Override
    public void useItemOn(Entity target) {
        target.inflictDamage(20);
    }
}

public Potion extends Item {
    @Override
    public void useItemOn(Entity target) {
        target.heal(20);
    }
}

然后

public class Bag {
    private List<Item> items;

    public Bag() {
         this(new ArrayList<Item>());
    }

    public Bag(List<Item> items) {
         this.items = items;
    }

    public List<Item> getItems() {
         return items;
    }
}

您当然可以使用更智能的方式来存储 Item,以免将它们混淆,因为您仍然需要弄清楚某些东西是药水还是炸弹。

但无论如何,使用它很简单,你需要从列表中取出一个项目,在实体上使用它,如果它是消耗品则从列表中删除它。

有人能在找到物品之前找到boss房间吗?如果不是:您如何预防?

使用我的解决方案时你必须做的事情:

  1. 在游戏开始时创建包
  2. 只要找到其中一项:调用 setFirstItemFound 或 setSecondItemFound 方法。
  3. 我不知道你想如何启用老板房间,但你可以使用方法"isBothItemsFound()"
  4. 检查我是否应该可以找到

请注意,此解决方案仅适用于仅需要两项的情况。如果以后需要 10 个项目,我会建议您使用 hashTbale、列表或任何相等的 "stacking" 类

public class Bag {    
    private static boolean firstItemFound = false;
    private static boolean secondItemFound = false;

    public Bag() {
        super();
        firstItemFound = false;
        secondItemFound = false;
    }
    /* Sets the status to of the first Item to found */
    public static void setFirstItemFound() {
        firstItemFound = true;
    }

    /*  Checks if the first item has been found */
    public static boolean isFirstItemFound() {
        return firstItemFound;
    }

    /* Sets the status to of the second Item to found */
    public static boolean isFSecondItemFound() {
        return secondItemFound;
    }

    /* Checks if the second item has been found */
    public static void setSecondItemFound() {
        secondItemFound = true;
    }

    /* Checks if both items has been found */
    public static boolean isBothItemsFound() {
        boolean bothItemsFound = false;
        if (firstItemFound && secondItemFound){
            bothItemsFound = true;
        }
        return bothItemsFound;
    }

    /* Sets the status to of both Items to not found */
    public static void clearBag() {
        firstItemFound = false;
        secondItemFound = false;
    }

}