2017-06-24 17 views
0

私は、私のクラスの1つの要件としてテキストベースの植物対ゾンビを開発しようとしています。 私はJavaをかなり新しくしており、実装方法は不明です。これは私が今まで考えていたものです。植物対ゾンビのテキストベースのゲームのコンセプト

6x10のボード/マップまたはタイルで、オブジェクト[] []の2D配列を使用して、ひまわり、ウォールナット、ゾンビなどのオブジェクトをその中に格納しようとしました。 2D配列でユーザーが希望する場所に植物を配置することができます。 次に、ゾンビオブジェクトが10秒ごとに生成されます。

これはこれまで私が試したことです。

public static void main(String[] args) throws InterruptedException { 
    Random rand = new Random(); 
    Object[][] objectlist = new Object[6][10]; 

    int x = rand.nextInt(5); 
    int y = 9; 
    objectlist[x][y] = new Zombie(); 

    System.out.println(((Zombie)(objectlist[x][y])).getClass().getName()+" appeared in Row " +(x + 1)+ " Column " + (y + 1) + 
         "\nwith the following initialized values" + 
         "\nHealth = " +((Zombie)(objectlist[x][y])).getHealth() + 
         "\nDamage = " +((Zombie)(objectlist[x][y])).getDamage() + 
         "\nSpeed = " +((Zombie)(objectlist[x][y])).getSpeed()); 

    while(y > 0){ 
     thread.sleep(4000); 
     objectlist[x][y] = objectlist[x][y--]; 
     System.out.println("Zombie previously in Row " +(x + 1)+ " Column " + (y + 2) + 
          " moved to Row " +(x + 1)+ " Column " +(y + 1)); 
    } 


} 

}

私の問題は、私が生成ゾンビとその動きを追跡するかどうかは不確かだ、で、また、私はゾンビがされている間工場にプレーヤーを聞いてどのように確認していません生成され、同時に移動します。最後に、何が起きているかの画面更新を表示して表示する時間間隔を把握したいと思います。

すべてのことを考慮すると、これも最初から可能ですか?

+0

テキストモードで画面をどのように更新しますか? Java cursesライブラリが必要です。 –

答えて

0

私はこれが良いスタートだと信じていますが、修正が必要なことがいくつかあります。

すべてのエンティティを含む2Dオブジェクト配列を使用する代わりに、エンティティの種類ごとに複数のList <>を持ち、すべてのクラスにPointを含める必要があります。これにより、プラントの位置を誤って更新することなく、ゾンビの位置を更新することができます。

List<Plants> plants; 
List<Zombies> zombies; // and anything else that needs to be added to the board 

これにより、ほとんどの場合、キャストは行われません。

ゲームカウンターの場合は、現在の時間をそのまま使用することができます。ゲームが開始されるミリ秒を保存して、開始時間を現在の時間から差し引いて、ゲームの進行時間を取得します。 1000で除して秒だけを表示することもできます。

for(Zombie zombie : zombies){ 
     if((zombie.getLastUpdate() - System.currentTimeMillis()) > 1000){ 
      // checking if it's time to update position. Zombie contains a long for the last time it updated 
      zombie.movePos(0,-1); 
      // adds this to the current internal Point and update last update time 
     } 
} 

またfor(Zombie zombie : zombies) zombie.update();までのループのためにそれを凝縮し、その関数内のコードのすべてを持つことができます。

もう1つの考慮すべき点は、トップレベルのクラスを持つクラスの階層構造を実装して、他のすべてのクラスをカプセル化することです。これはおそらくインターフェイスや抽象クラスの形式で行われます。

    - Zombie interface - branching types of Zombies including normal zombie 
Entity interface - Plant interface - branching types of Plants 
       - Shots from plants as interface - branching types of shots 

は、ゾンビの動きとゾンビの生成を追跡を追跡する方法についてのあなたの質問に答えるために、あなたはスタックを持っているゲームは、ゲームが進むにつれて、さまざまなゾンビの種類のリストを開始する前に、このスタックを埋めることができますそれは最終的に終わることを可能にするすべてのゾンビをゆっくりと得る。

植物を置く場所をプレイヤーに問い合わせるには、植物を置く場所の座標を尋ねてください。実際に入力を取得するには、コンソールから入力を読み取るために別の入力スレッドが必要な可能性が非常に高いでしょう。私の提案を

、あなたが私たちを与えたサンプルでは、​​このコードは、複数のメソッド間で拡大し、多分ゲームのオブジェクトの内部で行われるべきである

public static void main(String[] args) throws InterruptedException { 
    Stack<Zombie> zombiesToSpawn = fillZombieStack(); // creates zombies in order of when they spawn 
    // If you want a zombie to spawn 10 seconds after the game starts, you would set the time to be 10*1000 milliseconds plus the current time 

    List<Plants> plants = ArrayList<>(); 
    List<Zombies> zombies = ArrayList<>(); 
    while(!zombies.isEmpty()){ 
     if(!zombiesToSpawn.empty() && 
      zombiesToSpawn.peek().getLastUpdateTime() <= System.currentTimeMillis()){ 
      // last update time gets reused here as when to spawn 
      Zombie currentZombie = zombiesToSpawn.pop(); 
      zombies.add(currentZombie) // creates zombie at specified position 

      System.out.printf("%s appeared in Row %d Column %d\n" + 
        "with the following initialized values\n" + 
        "Health = %d\n" + 
        "Damage = %d\n" + 
        "Speed = %d\n", 
       currentZombie.getName(), 
       currentZombie.getPos().x+1, 
       currentZombie.getPos().y+1, 
       currentZombie.getHealth(), 
       currentZombie.getDamage(), 
       currentZombie.getSpeed() 
      ); 
     } 

     for(Zombie zombie : zombies){ 
      if(!zombie.update()) continue; // if it updates, return true, else return false 
      System.out.printf("Zombie previously in Row %d Column %d moved to Row %d Column %d\n", 
       zombie.getPos().x+1, 
       zombie.getPos().y+2, 
       zombie.getPos().x+1, 
       zombie.getPos().y+1 
      ); 
     } 
    } 
} 

ように書き換えることができます。

希望すると便利です。

関連する問題