[英]Struggling to implement a timer to my C# CONSOLE Typing Game
The goal of my current Typing Game feature (code provided below) is to countdown from either 10(hard), 20(medium), or 30(easy), depending on the users difficulty selection once the game has started. 我目前的打字游戏功能(下面提供的代码)的目标是从10(硬),20(中)或30(简易)倒数,具体取决于游戏开始后用户的选择难度。 Once the countdown reaches zero OR the user runs out of lives, the game is over (which stops the countdown).
一旦倒计时达到零或用户的生命耗尽,游戏结束(停止倒计时)。 The game starts properly, displays the first word to be typed by the user, and begins counting down from 10. My current problem is that I can't figure out how to stop the timer from counting down past 0. I have tried using t.Change(Timeout.Infinite , Timeout.Infinite) and checking when timeLeft == 0 with no prevail.
游戏正常启动,显示用户要输入的第一个单词,并从10开始倒数。我当前的问题是我无法弄清楚如何停止计时器倒数超过0。我尝试使用t .Change(Timeout.Infinite,Timeout.Infinite)并检查timeLeft == 0时不优先。 I really feel like I am overthinking this (or underthinking), some help or a nudge in the right direction would be greatly appreciated!
我真的觉得我想得太对(或想不到),在正确的方向上提供一些帮助或推动将不胜感激!
my code is provided below: 我的代码如下:
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);
}
}
}
} }
You don't need a progammatic timer or thread to have a game timer. 您不需要编程计时器或线程即可拥有游戏计时器。 Just record the time at the beginning of the game:
只需记录游戏开始时的时间即可:
//During initialization
var startTime = DateTime.Now;
And any time you wish to display or use a timer, compute it: 而且,只要您希望显示或使用计时器,就可以对其进行计算:
var timerValue = DateTime.Now - startTime;
Console.WriteLine("Time elapsed: {0} seconds", timerValue.Seconds);
you can use Timer in this way. 您可以通过这种方式使用Timer。
It works for you 它为你工作
var timer2 = new Timer();
timer2.Elapsed += (o, e) =>
{
Console.WriteLine("Time Elapsed {0}", e.SignalTime);
timer2.Stop();
};
timer2.Interval = 1000;
timer2.Start();
As a direct fix to the problem presented (timer going below 0), you need to stop the timer when it is no longer needed. 作为对所出现问题的直接解决(计时器低于0),需要在不再需要计时器时停止计时器。 For that, use this line:
为此,请使用以下行:
t.Change(Timeout.Infinite, Timeout.Infinite);
You could add this line to the start of your outOfLives
and outOfTime
methods: 您可以将此行添加到
outOfLives
和outOfTime
方法的outOfTime
:
private static void outOfLives()
{
t.Change(Timeout.Infinite, Timeout.Infinite);
Console.WriteLine("Game over! You typed " + correctWords + " words correctly!");
...
and... 和...
private static void outOfTime()
{
t.Change(Timeout.Infinite, Timeout.Infinite);
Console.WriteLine("Out of time! You typed " + correctWords + " words correctly!");
...
EDIT: Sorry. 编辑:对不起。 Just re-read your question and realised you had already tried this line.
只需重新阅读您的问题并意识到您已经尝试过此行。 Did you have it in the correct place(s)?
您是否把它放在正确的地方?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.