私はこれが良いスタートだと信じていますが、修正が必要なことがいくつかあります。
すべてのエンティティを含む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
);
}
}
}
ように書き換えることができます。
希望すると便利です。
テキストモードで画面をどのように更新しますか? Java cursesライブラリが必要です。 –