2016-11-22 2 views
0

Hallo, ich habe mein Spiel wie ein Text-Abenteuer gemacht, das nach zufälligen Ereignissen sucht und so passiert, aber es ist nicht tickend. Ich ticke in der Laufmethode und versuche 1 Tick pro Sekunde zu bekommen. Ich denke, es ist wegen der Schlafmethode. Denken Sie daran, ich bin ein Anfänger und das ist eine meiner ersten Java-Spiele dank :)Warum tickt mein Java-Textspiel nicht?

import java.lang.Runnable; 
import java.util.Random; 
import java.util.Scanner; 

public class Main implements Runnable { 

Thread thread; 
public boolean running = false; 

Scanner scan = new Scanner(System.in); 
Random rand = new Random(); 

public boolean playing = false; 
public String currentInput; 
public String allOptions[] = new String[] { "gather wood", "build a house" }; 
public String unlockedOptions[] = new String[allOptions.length]; 
public String commonRandomEvents[] = new String[] { "settler moves in", "settler dies", "settler born" }; 
public String rareRandomEvents[] = new String[] { "plague" }; 

public int housesBuilt = 0; 
public int wood = 10; 
public int woodGathered; 
public int houseCost = 10; 

public Main() { 
    unlockOption(0); 
    playing = true; 
    play(); 
} 

public static void main(String[] args) { 
    new Main().start(); 
} 

public void play() { 
    while (playing) { 
     printOptions(); 
     sleep(250); 
     getInput(); 
     System.out.println(); 
    } 
} 

public void getInput() { 
    boolean dontKnow = false; 
    System.out.println(); 
    printText("wyd?: "); 
    currentInput = scan.nextLine(); 
    currentInput = currentInput.toLowerCase(); 
    for (int i = 0; i < unlockedOptions.length; i++) { 
     if (currentInput.equals(unlockedOptions[i])) { 
      interpretOptionInput(i); 
      break; 
     } else if (currentInput.equals(allOptions[i])) { 
      { 
       printText("you haven't unlocked that yet..."); 
       break; 
      } 
     } else { 
      dontKnow = true; 
     } 
    } 
    if (dontKnow) { 
     System.out.println("you dont know how to " + currentInput); 
    } 
    currentInput = null; 
} 

public void printResources() { 

} 

public void interpretOptionInput(int arrayId) { 
    if (arrayId == 0) { 
     // gather wood 
     for (int i = 0; i < woodGathered; i++) 
      wood++; 
     printText("you gather " + woodGathered + " wood in the nearby forest."); 
     if (arrayId == 1) { 
      // build house 
      if (wood < houseCost) { 
       printText(
         "you don't have enough wood. there is a nearby forest. maybe we can get some wood from there?"); 
      } else { 
       wood -= houseCost; 
       housesBuilt++; 
       printText("you build a nice wood house with " + houseCost 
         + " wood. someone will move in, right? You have " + housesBuilt + " houses. You have " 
         + wood + " wood."); 
       houseCost += 10; 
      } 
     } 
    } 
} 

public void unlockOption(int optionId) { 
    if (allOptions[optionId] != unlockedOptions[optionId]) { 
     unlockedOptions[optionId] = allOptions[optionId]; 
    } else { 
     printText("option already unlocked. "); 
    } 
} 

public void generateRandomEvent() { 
    int commonRandomEventInt = rand.nextInt(2500); 
    int rareRandomEventInt = rand.nextInt(10000); 

    if (commonRandomEventInt == 1) { 
     int randomEventCommonInt = rand.nextInt(commonRandomEvents.length); 
     interpretRandomEvent("common", commonRandomEvents.length); 
    } 

    if (rareRandomEventInt == 1) { 
     int randomEventRareInt = rand.nextInt(rareRandomEvents.length); 
     interpretRandomEvent("rare", rareRandomEvents.length); 
    } 

} 

public void runCommonEvent(int arrayId) { 
    if (arrayId == 0) { 

    } 

    if (arrayId == 1) { 

    } 

    if (arrayId == 2) { 

    } 

    if (arrayId == 3) { 

    } 

} 

public void runRareEvent(int arrayId) { 
    if (arrayId == 0) { 

    } 

    if (arrayId == 1) { 

    } 

} 

public void interpretRandomEvent(String rarity, int arrayId) { 
    if (rarity.equals("common")) { 
     for (int i = 0; i < commonRandomEvents.length; i++) { 
      if (arrayId == i) { 
       runCommonEvent(i); 
      } 
     } 
    } 

    if (rarity.equals("rare")) { 
     for (int i = 0; i < rareRandomEvents.length; i++) { 
      if (arrayId == i) { 
       runRareEvent(i); 
      } 
     } 
    } 

} 

public void printOptions() { 
    printText("you can: "); 
    sleep(350); 
    for (int i = 0; i < unlockedOptions.length - 1; i++) { 
     if (allOptions[i] == unlockedOptions[i]) { 
      printText(unlockedOptions[i] + ", "); 
      sleep(350); 
     } 
    } 
    for (int i = 0; i <= unlockedOptions.length; i++) { 
     if (i == unlockedOptions.length) { 
      printText("or " + unlockedOptions[i - 1] + "."); 
     } 
    } 
} 

public void tick() { 
    System.out.println("tick"); 
} 

@Override 
public void run() { 

    int maxTps = 60; 
    double timePerTick = 1000000000/maxTps; 
    double deltaTicks = 0; 
    long now; 
    long lastTime = System.nanoTime(); 
    long timer = 0; 
    int ticks = 0; 

    while (running) { 
     now = System.nanoTime(); 
     deltaTicks += (now - lastTime)/timePerTick; 
     timer += now - lastTime; 
     lastTime = now; 

     if (deltaTicks >= 1) { 

      tick(); 
      ticks++; 
      deltaTicks--; 

     } 

     if (timer >= 1000000000) { 

      ticks = 0; 
      timer = 0; 

     } 

    } 

    stop(); 

} 

public void sleep(int milliseconds) { 
    try { 
     Thread.sleep(milliseconds); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

public synchronized void start() { 

    running = true; 
    thread = new Thread(this); 
    thread.start(); 

} 

public synchronized void stop() { 
    if (!running) 
     return; 
    running = false; 
    try { 
     thread.join(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

public void printText(String text) { 

    char[] textArray = text.toCharArray(); 

    for (int i = 0; i < textArray.length; i++) { 
     System.out.print(textArray[i]); 
     try { 
      if (textArray[i - 1] == '.') { 
       sleep(300); 
      } else if (textArray[i - 1] == ',') { 
       sleep(175); 
      } else { 
       sleep(20); 
      } 
     } catch (Exception e) { 

     } 
    } 
} 

}

+5

Bitte entfernen Sie alle irrelevanten Code. Poste a [mcve] - alles was du brauchst sind die Methoden 'run()' und 'tick()'. –

Antwort

1

Sie sind nicht tick ing, weil man nie den Faden beginnen.

Obwohl Sie aufrufen:

new Main().start(); 

Sie rufen play() der Konstruktor von Main():

public Main() { 
    unlockOption(0); 
    playing = true; 
    play(); 
} 

und play() enthält eine Schleife:

public void play() { 
    while (playing) { 
     printOptions(); 
     sleep(250); 
     getInput(); 
     System.out.println(); 
    } 
} 

und so der Faden nicht Beginnen Sie, bis die Schleife bricht.

Wenn Sie möchten, dass diese tick Nachrichten angezeigt werden, während die Schleife in play() noch läuft, müssten Sie das in einem separaten Thread ausführen.

+0

Also was ich getan habe, war ich gerade hinzugefügt start(); zum Start Main-Konstruktor. Wäre das gut? – raze