我目前的打字游戏功能(下面提供的代码)的目标是从10(硬),20(中)或30(简易)倒数,具体取决于游戏开始后用户的选择难度。一旦倒计时达到零或用户的生命耗尽,游戏结束(停止倒计时)。游戏正常启动,显示用户要输入的第一个单词,并从10开始倒数。我当前的问题是我无法弄清楚如何停止计时器倒数超过0。我尝试使用t .Change(Timeout.Infinite,Timeout.Infinite)并检查timeLeft == 0时不优先。我真的觉得我对这个想法(或想法不足)的想法太深了,在正确的方向上提供一些帮助或推动将不胜感激!
下面提供了我的代码:
class Program
{
// List of words to be typed
public static string[] words = { "some", "side", "add", "another", "would", "book" ,"water", "came" ,"your" ,"big","both", "from", "learn", "been", "me" ,"school" ,"land", "took", "place",
"try", "line", "tell", "earth", "can", "do","children", "without", "my", "must", "take", "follow", "year", "is", "being", "different", "miss", "could", "on", "he", "open", "night", "were",
"line","said", "around", "an", "plant", "know", "set","been", "life","young","of", "face", "we", "hand", "while", "is", "white", "call", "live","may", "study","after" ,"down", "him", "now", "different",
"could", "over", "work","all", "mean","begin","go","fall", "really", "oil", "before","into","one","name","has","a", "well", "got","something","turn" };
// Keeps track of words array
public static int numWords = 88;
public static int correctWords = 0;
// Initialize our random number generator
public static Random rand = new Random();
// Handles user input
public static string userInput = "";
public static string endGameChoice = "";
// Handles gamestate variables
public static bool gameActive = false;
public static int numLives = 0;
public static int timeLeft;
public static System.Threading.Timer t;
// Entry Point
static void Main(string[] args)
{
// Start the game
Start();
}
// Handles gameplay
static void Play()
{
// Assigns the current word to any random word in
// the words array
string currentWord = words[rand.Next(0, 88)];
// Print the current word separated by lines
Console.WriteLine("********");
Console.WriteLine(currentWord);
Console.WriteLine("********");
// While the answser is incorrect/empty
while (!userInput.Equals("exit"))
{
// Reads user input
userInput = Console.ReadLine();
if (userInput.Equals(""))
{
Play();
}
// Checks if userInput is equal to current word
if (!(userInput.Equals(currentWord)))
{
// If incorrect, display loss of life
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect!!!");
Console.WriteLine("Lives: " + numLives);
numLives--; // Take a life away
Console.ResetColor();
}
if (numLives == -1)
{
outOfLives();
}
if (userInput.Equals(currentWord))
{
correctWords++;
Play();
}
}
if (userInput.Equals("exit"))
{
Environment.Exit(0);
}
}
// Function for running out of lives
private static void outOfLives()
{
Console.WriteLine("Game over! You typed " + correctWords + " words correctly!");
Console.WriteLine("Type anything and press enter to retry, Press Escape to Quit!");
var endGameKey = Console.ReadKey();
if (endGameKey.Key == ConsoleKey.Escape)
{
Environment.Exit(0);
}
if (endGameKey.Key == ConsoleKey.Enter)
{
restartGame();
}
else
{
outOfLives();
}
}
// Function for running out of time
private static void outOfTime()
{
Console.WriteLine("Out of time! You typed " + correctWords + " words correctly!");
Console.WriteLine("Type anything and press enter to retry, Press Escape to Quit!");
var endGameKey = Console.ReadKey();
if (endGameKey.Key == ConsoleKey.Escape)
{
Environment.Exit(0);
}
if (endGameKey.Key == ConsoleKey.Enter)
{
restartGame();
}
else
{
outOfTime();
}
}
// Prompts user for input for difficulty along with game instructions
static void StartMessage()
{
Console.WriteLine("Welcome to my Typing Practice App!");
Console.WriteLine("Type the word displayed as fast as you can");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Difficulties are listed from hardest to easiest");
Console.WriteLine();
Console.WriteLine("Select a difficulty( 1 ,2 , or 3 ): ");
Console.WriteLine();
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("-- Press ENTER --");
Console.WriteLine("*** Satan Himself ***");
Console.WriteLine();
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("-- Type 1 --)");
Console.WriteLine("*** Software Engineer ***");
Console.WriteLine();
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("-- Type 2 --)");
Console.WriteLine("*** Social Media Fanatic ***");
Console.WriteLine();
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-- Type 3 --)");
Console.WriteLine("*** Filthy Peasant ***");
Console.WriteLine();
Console.ResetColor();
string difficultyChoice = Console.ReadLine();
switch (difficultyChoice)
{
case "1":
numLives = 1;
Console.WriteLine("You have 2 lives! Good luck!");
timeLeft = 10;
break;
case "2":
numLives = 3;
Console.WriteLine("You have 4 lives! Good luck!");
timeLeft = 20;
break;
case "3":
numLives = 5;
Console.WriteLine("You have 6 lives! Good luck!");
timeLeft = 30;
break;
default:
numLives = 0;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Miss one and you're done!!!!!");
Console.ResetColor();
timeLeft = 10;
break;
}
}
public static void restartGame()
{
Console.Clear();
//numLives = 5;
numWords = words.Length;
correctWords = 0;
Start();
}
public static void SetTimer(Object o)
{
timeLeft--;
Thread.Sleep(1000);
Console.WriteLine(timeLeft);
if (timeLeft == 0)
{
outOfTime();
}
}
public static void Start()
{
// Display start message
StartMessage();
gameActive = true;
t = new System.Threading.Timer(SetTimer, null, 0, 1250);
// While user wants to play
while (!userInput.Equals("exit"))
{
// While the game is active
while (gameActive == true)
{
// Start the game
Play();
}
}
if (userInput.Equals("exit"))
{
Environment.Exit(0);
}
}
}
}
答案 0 :(得分:0)
尝试使用this。如果协程可以正常工作,协程应该可以解决您的问题。
答案 1 :(得分:0)
您不需要程序计时器或线程即可拥有游戏计时器。只需记录游戏开始时的时间即可:
//During initialization
var startTime = DateTime.Now;
只要您希望显示或使用计时器,就可以对其进行计算:
var timerValue = DateTime.Now - startTime;
Console.WriteLine("Time elapsed: {0} seconds", timerValue.Seconds);
答案 2 :(得分:0)
您可以通过这种方式使用计时器。
它为您服务
TheLibrary.addEngine("Game", GameEngine)
答案 3 :(得分:0)
作为对所出现问题的直接解决(计时器低于0),您需要在不再需要计时器时停止计时器。为此,请使用以下行:
t.Change(Timeout.Infinite, Timeout.Infinite);
您可以将此行添加到outOfLives
和outOfTime
方法的开头:
private static void outOfLives()
{
t.Change(Timeout.Infinite, Timeout.Infinite);
Console.WriteLine("Game over! You typed " + correctWords + " words correctly!");
...
和...
private static void outOfTime()
{
t.Change(Timeout.Infinite, Timeout.Infinite);
Console.WriteLine("Out of time! You typed " + correctWords + " words correctly!");
...
编辑:对不起。只需重新阅读您的问题并意识到您已经尝试过此行。你把它放在正确的地方了吗?