找不到在Java中使用“Enter to Continue”的简单方法

时间:2015-06-29 23:15:20

标签: java multithreading input enter

因此,我试图通过“按Enter继续”功能进行文本冒险。但是,我还没有找到一种简单的方法来做到这一点。

=============================== NOTES =============== =====================

1。)我试图输入ONLY键进入下一行

2。)由于我将大部分内容剪切并粘贴到.txt文件中供以后使用,因此有大量未使用的内容。我这样做是因为我知道我所处的情况最好是与线程一起使用,但由于我的教练时间有限而无法教我穿线,因此被许多案例陈述所取代。

3。)我知道我的标签已关闭。我很难将代码格式化为正确的格式。我通常对我的标签非常挑剔。

4。)不确定这是否重要,但如果确实如此,我使用Eclipse进行java编辑。

5.)我正在使用JDK和一个名为“Processing”的程序。我确信这可能很明显,但我只想提供尽可能多的信息,如果有帮助的话。

package ThatGameWithAName;
import processing.core.PApplet;
import processing.core.PImage;
import java.util.Scanner;
import ThatGameWithAName.Player;

@SuppressWarnings("serial")
public class ThatGameWithAName extends PApplet implements Runnable
{
    boolean insideBeginBox = false;
    boolean insideOptionsBox = false;
    boolean insideQuitBox = false;
    boolean insideStatsBox = false;
    boolean beginGame = false; 

    String output = "NotSet";
    String input = "";
    boolean enteringInput = false;
    int storyCounter = 1;
    boolean displayedStory = false;
    Player player;
    Scanner playerInput;
    String playerGender;
    String playerReadyInput;
    String playerRace;
    String playerReadyRaceInput;
    boolean playerReady = false;
    boolean gameStarted = false;
    boolean enterCont = false;
    public int gameState = 0;
    public int saveState = 0;

    int beginX, beginY, beginWidth, beginHeight;
    int optionsX, optionsY, optionsWidth, optionsHeight;
    int quitX, quitY, quitWidth, quitHeight;
    int inputX, inputY, inputWidth, inputHeight;
    int choicesBgX, choicesBgY, choicesBgWidth, choicesBgHeight;
    PImage beginImage, optionsImage, quitImage, statsImage;
    int speedDispX, speedDispY, speedDispWidth, speedDispHeight;

    public void keyReleased() 
    {
        if(enteringInput == true)
        {
            if(key != ENTER)
            {
                if(key == BACKSPACE)
                {
                    input = input.substring(0, input.length() -1);
                }
                else
                {
                    input += key;
                }
            }
            else
            {
                enteringInput = false;
            }
        }
    }
    public void setup() 
    {   

        size(700, 700);

        beginImage = new PImage();
        beginImage = loadImage("C:/users/anon/Desktop/RenameMeGame/ThatGameWithAName/src/data/BeginButton.png");
        beginX = 100;
        beginY = 0;
        beginWidth = 500;
        beginHeight = 100;

        optionsImage = new PImage();
        optionsImage = loadImage("C:/users/anon/Desktop/RenameMeGame/ThatGameWithAName/src/data/SettingsButton.png");
        optionsX = 100;
        optionsY = 200;
        optionsWidth = 500;
        optionsHeight = 100;

        quitImage = new PImage();
        quitImage = loadImage("C:/users/anon/Desktop/RenameMeGame/ThatGameWithAName/src/data/QuitButton.png");  
        quitX = 100;
        quitY = 400;
        quitWidth = 500;
        quitHeight = 100;

        choicesBgX = 0;
        choicesBgY = 600;
        choicesBgWidth = 700;
        choicesBgHeight = 100;

        speedDispX = 100;
        speedDispY = 200;
        speedDispWidth = 500;
        speedDispHeight = 100;

        inputX = 0;
        inputY = 700;
        inputWidth = 700;
        inputHeight = 0;
        rect(0, 700, 700, 0);
        fill(255, 255, 255);
    }
    public void runGame()
    {
        if(gameState == 1 && gameStarted == true)
        gameState = 1;
        output = "Press any key to continue...";
        player.entToCont();
        output = "yay";
    }

    public void draw() 
    {
        background(0, 0, 255);

        //Main Menu

        if(gameState == 0)
        {
            if(beginImage != null)
            {
                begin();
            }
            if(optionsImage != null)
            {
                options();
            }
            if(quitImage != null)
            {
                quit();
            }   

            //Start button

            if(mouseX > beginX && mouseX < beginX + beginWidth && mouseY > beginY && mouseY < beginY + beginHeight)
            {
                insideBeginBox = true;
            }
            else
            {
                insideBeginBox = false;
            }

            //Options Button

            if(mouseX > optionsX && mouseX < optionsX + optionsWidth && mouseY > optionsY && mouseY < optionsY + optionsHeight)
            {
                insideOptionsBox = true;
            }
            else
            {
                insideOptionsBox = false;
            }

            //Quit button

            if(mouseX > quitX && quitX < quitX + quitWidth && mouseY > quitY && mouseY < quitY + quitHeight)
            {
                insideQuitBox = true;
            }
            else
            {
                insideQuitBox = false;
            }
        }

        //Text

        if(gameState == 1)
        {
            setUpGame();
            runGame();
            fill(0,0,0);
            rect(0, 550, 700, 150);
            fill(255,255,255);
            textSize(30);
            text(output, 100, 590);
            text(input, 0, 690);
            text(player.speed, 50, 110);
        }

        //Map Movement

        if(gameState == 2)
        {

        }
        if(gameState == 3)
        {

        }
        if(gameState == 4)
        {

        }
    }

    private void text(double speed, int i, int j) {

    }
    public void begin()
    {
        image(beginImage, beginX, beginY, beginWidth, beginHeight);
    }

    public void options()
    {
        image(optionsImage, optionsX, optionsY, optionsWidth,optionsHeight);
    }
    public void setUpGame()
    {
        player = new Player();
        player.level = 1;
        player.exp = 0;
        player.name = "";
        player.health = 100;
        gameStarted = true;
        playerInput = new Scanner(System.in);
        /*if(player.isPlayerDead == true)
        {
            gameState = 3;
        }*/
    }

    public void quit()
    {
        image(quitImage, quitX, quitY, quitWidth, quitHeight);
    }
    public void mousePressed()
    {
         if (insideBeginBox == true)
         {
             gameState = 1;
         } 
         else if (insideOptionsBox == true)
         {
            //Add difficulty later
            System.out.println("1: Change Game Difficulty");
            System.out.println("1.A: Current Difficulty is :");
            System.out.println(" ");
            //Add challenge modifiers later
            System.out.println("2: Add and/or remove game modifiers");
            System.out.println("2.A: <#> Current challenge modifiers");
        }
        else if (insideQuitBox == true)
        {   
            System.out.println("Bye!");
            System.exit(0);
        } 
    }
}

===================== CLASS FILE“播放器”===================== ==

package ThatGameWithAName;
import java.util.Random;
import java.util.Scanner;

public class Player 
{
public String name;
public int level;
public int exp;
public int health = 100;
public double speed = 10;
public double damage = 10;
public double range = 10;
public double luck = 10;
public boolean isMale = true;
public boolean playerTurn = true;
public String playerFight;
Scanner playerFightput;
public int eHealth = 50;
public boolean playerWin = false;
Scanner playerFightInput;
public boolean isPlayerDead = false;

public int slowedTimer = 0;
public boolean slowedStat = false;
public int weakenTimer = 0;
public boolean weakenStat = false;
public int nearsightTimer = 0;
public boolean nearsightStat = false;
public int cursedTimer = 0;
public boolean cursedStat = false;

public static void entToCont()
    {
        Scanner enterPressed = new Scanner(System.in);
        enterPressed.nextLine();
    }

public static void main(String [] args)
{
    entToCont();
}
public void slowStatus()
{
    slowedStat = true;
    slowedTimer = 0;
    double slowed = .8;
    System.out.println(name + " was slowed by 20%!");
    speed *= slowed;

}
public void dmgdownStatus()
{
    System.out.println(name + " became weakened by 20%!");
    weakenStat = true;
    weakenTimer = 0;
    double weakened = .8;
    damage *= weakened; 
}

public void nearsightStatus()
{
    if(isMale == true)
    {
        System.out.println(name + " became nearsighted, reducing his range by 20%!");
        nearsightStat = true;
        nearsightTimer = 0;
        double nearsight = .8;
        range *= nearsight; 
    }
    else if(isMale == false)
    {
        System.out.println(name + " became nearsighted, reducing her range by 20%!");
        nearsightStat = true;
        nearsightTimer = 0;
        double nearsight = .8;
        range *= nearsight; 
    }
}
public void curseStatus()
{
    System.out.println(name + " become cursed, lowering luck by 20%!");
    cursedStat = true;
    cursedTimer = 0;
    double cursed = .8;
    luck *= cursed;
}   
public void failSafe()
{
    if (speed <= 1)
    {
        speed = 1;
    }
    if (damage <= 1)
    {
        damage = 1;
    }
    if (range <= 1)
    {
        range = 1;
    }
    if (luck <= 1)
    {
        luck = 1;
    }
}
public void enemyFight()
{   
    playerFightInput = new Scanner(System.in);
    while(playerWin == false)
    {
        Random enemyHit = new Random(); 
        int number = enemyHit.nextInt(21);

        System.out.println("Enemy encountered!");

        if (number == 0)
        {
            System.out.println("Enemy missed!");
            playerFightInput.nextLine();
            playerTurn = true;
        }
        else
        {
            System.out.println("Enemy deals " + number + " damage!" + health + "/100");
            health -= number;   
            playerFightInput.nextLine();
            playerTurn = true;
        }   
        if (playerTurn = true)
        {
            Random playerHit = new Random();    
            int pNumber = playerHit.nextInt(21);

            if (pNumber == 0)
            {
                System.out.println(name + " missed!");
                playerFightInput.nextLine();
                playerTurn = false;
            }
            else if (eHealth <= 0)
            {
                System.out.print("You win!");
                playerFightInput.nextLine();
                playerWin = true;
            }
            else if (health <= 0)
            {
                System.out.println("You died...");
                playerFightInput.nextLine();
                isPlayerDead = true;
            }
            else
            {
                eHealth -= pNumber;
                System.out.println("Player deals " + pNumber + " damage!" + eHealth + "/50");
                playerFightInput.nextLine();
                playerTurn = false;
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:-1)

这很简单。在按下Enter键之前,Scanner nextLine方法不会返回。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

/// <summary>
/// Instantiates an object. Must pass PropertyType.AssemblyQualifiedName for factory to operate
/// returns instantiated object
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
public static object Create(string typeAssemblyQualifiedName)
{
  // resolve the type
  Type targetType = ResolveType(typeAssemblyQualifiedName);
  if (targetType == null)
    throw new ArgumentException("Unable to resolve object type: " + typeAssemblyQualifiedName);

  return Create(targetType);
}

/// <summary>
/// create by type of T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Create<T>()
{
  Type targetType = typeof(T);
  return (T)Create(targetType);
}

/// <summary>
/// general object creation
/// </summary>
/// <param name="targetType"></param>
/// <returns></returns>
public static object Create(Type targetType)
{
  //string test first - it has no parameterless constructor
  if (Type.GetTypeCode(targetType) == TypeCode.String)
    return string.Empty;

  // get the default constructor and instantiate
  Type[] types = new Type[0];
  ConstructorInfo info = targetType.GetConstructor(types);
  object targetObject = null;

  if (info == null) //must not have found the constructor
    if (targetType.BaseType.UnderlyingSystemType.FullName.Contains("Enum"))
      targetObject = Activator.CreateInstance(targetType);
    else
      throw new ArgumentException("Unable to instantiate type: " + targetType.AssemblyQualifiedName + " - Constructor not found");
  else
    targetObject = info.Invoke(null);

  if (targetObject == null)
    throw new ArgumentException("Unable to instantiate type: " + targetType.AssemblyQualifiedName + " - Unknown Error");
  return targetObject;
}

/// <summary>
/// Loads the assembly of an object. Must pass PropertyType.AssemblyQualifiedName for factory to operate
/// Returns the object type.
/// </summary>
/// <param name="typeString"></param>
/// <returns></returns>
public static Type ResolveType(string typeAssemblyQualifiedName)
{
  int commaIndex = typeAssemblyQualifiedName.IndexOf(",");
  string className = typeAssemblyQualifiedName.Substring(0, commaIndex).Trim();
  string assemblyName = typeAssemblyQualifiedName.Substring(commaIndex + 1).Trim();

  if (className.Contains("[]"))
    className.Remove(className.IndexOf("[]"), 2);

  // Get the assembly containing the handler
  Assembly assembly = null;
  try
  {
    assembly = Assembly.Load(assemblyName);
  }
  catch
  {
    try
    {
      assembly = Assembly.LoadWithPartialName(assemblyName);//yes yes this is obsolete but it is only a backup call
    }
    catch
    {
      throw new ArgumentException("Can't load assembly " + assemblyName);
    }
  }

  // Get the handler
  return assembly.GetType(className, false, false);
}