亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

努力為我的 C# CONSOLE 打字游戲實現計時器

努力為我的 C# CONSOLE 打字游戲實現計時器

C#
慕蓋茨4494581 2022-07-23 09:35:06
我當前的打字游戲功能(下面提供的代碼)的目標是從 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();    } 
查看完整描述

4 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

您不需要程序計時器或線程來擁有游戲計時器。只記錄游戲開始時的時間:


//During initialization

var startTime = DateTime.Now;

并且任何時候您希望顯示或使用計時器,計算它:


var timerValue = DateTime.Now - startTime;

Console.WriteLine("Time elapsed: {0} seconds", timerValue.Seconds);


查看完整回答
反對 回復 2022-07-23
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

嘗試使用這個。如果你能讓它工作,協程應該可以解決你的問題。



查看完整回答
反對 回復 2022-07-23
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

您可以通過這種方式使用 Timer。


它對你有用


var timer2 = new Timer();

timer2.Elapsed += (o, e) =>

{

    Console.WriteLine("Time Elapsed {0}", e.SignalTime);

    timer2.Stop();

};

timer2.Interval = 1000;

timer2.Start();


查看完整回答
反對 回復 2022-07-23
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

作為對出現的問題的直接修復(定時器低于 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!");

    ...

編輯:對不起。只需重新閱讀您的問題并意識到您已經嘗試過這條線。你把它放在正確的地方了嗎?


查看完整回答
反對 回復 2022-07-23
  • 4 回答
  • 0 關注
  • 166 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號