C# 制作足球游戏控制台应用程序

Posted

技术标签:

【中文标题】C# 制作足球游戏控制台应用程序【英文标题】:C# Making a Football Game Console Application 【发布时间】:2014-10-31 21:24:35 【问题描述】:

我有一个 C# 控制台应用程序,我想使用此代码编写它,但我尝试了几件事,似乎运行了一个块。我正在尝试在顶部的游戏上制作一个计时器,但我打印到控制台并保留它的唯一方法是从本代码中消除 Console.Clear。有没有一种方法可以让倒数计时器保持在顶部,而 Console.Clear 仅对计时器有效,而不是控制台窗口中的所有内容?

另外,有谁知道如何将数字转换为像这样的时钟格式:00:05:00?

using System;
using System.Timers;

namespace TimerExample

    class Program
    
        static Timer timer = new Timer(1000);
        static int i = 10;

        static void Main(string[] args)
                    
            timer.Elapsed+=timer_Elapsed;
            timer.Start();
            Console.Read();
        

        private static void timer_Elapsed(object sender, ElapsedEventArgs e)
        
            i--;

            Console.Clear();//here is where I disable and it prints out all the timer in the widow
                            // if I leave this console window it wipes out my print?
            Console.WriteLine("=================================================");
            Console.WriteLine("                  First Quarter");
            Console.WriteLine(""); 
            Console.WriteLine("                Time Remaining:  " + i.ToString());
            Console.WriteLine("");        
            Console.WriteLine("=================================================");

            if (i == 0) 
            
                Console.Clear();
                Console.WriteLine("");
                Console.WriteLine("==============================================");
                Console.WriteLine("         Huddle ! ! ! !");
                Console.WriteLine("");
                Console.WriteLine("               Timeout");
                Console.WriteLine("==============================================");

                timer.Close();
                timer.Dispose();
            

            GC.Collect();
        
    


好的,这里是代码的扩展 我在尝试将我的游戏保持在窗口底部并真正尝试将倒数计时器编码为在游戏开始后立即激活但同时为用户保留我的游戏选择打印时遇到问题,任何建议都会有所帮助

....

++++++++++++++++++++++++

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace TimerExample

    class Program
    


            static Timer timer = new Timer(1000);
            static int i = 300;

            private void Run()
            
                Header();
                Story();
                timer.Elapsed += timer_Elapsed;
                timer.Start();

                // Console.ReadLine();

                bool appRunning = true;
                bool playerExists = false;
                //bool drinkExists = false;
                int userInput = 0;

                //loop
                while (appRunning)
                
                    Console.WriteLine("\n\n....................\n");
                    Console.WriteLine("What would you like to do?\n");
                    Console.WriteLine("1.)Choose Role 2.)Show Credits 3.)Exit Application");
                    Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    Console.WriteLine("Chose a player: ");

                    //take user input and ocnvert to string to int data type
                    userInput = Int16.Parse(Console.ReadLine());

                    //switch statement o flip through menu options

                    switch (userInput)
                    
                        case 1:
                            //intelligence to monitor that we only make one sandwich per customer
                            if (playerExists == false)
                            

                                Console.WriteLine("Does my sandwich exist? Answer is" + playerExists);
                                Console.WriteLine("Lets make you a snadwich, then shall we??\n");


                                //set default sandwich variable values
                                String Quarterback = "Quarterback";
                                String Runningback = "Runningback";
                                String Receiver = "Receiver";

                                //get type of bread from user
                                Console.Clear();
                                Console.WriteLine("What type of play would you like?");
                                Console.WriteLine("Short, Run, Pass Long");
                                Quarterback = Console.ReadLine();


                                //get type of Option from user
                                Console.Clear();
                                Console.WriteLine("What type of play would you like?");
                                Console.WriteLine("Left, Right, or Staight");
                                Runningback = Console.ReadLine();


                                //get type of jelly from user
                                Console.Clear();
                                Console.WriteLine("What type of play would you like?");
                                Console.WriteLine("Long, Short, or Mid");
                                Receiver = Console.ReadLine();

                                //create an instance of the sandwichclass
                                Player myPlayer = new Player(Quarterback, Runningback, Receiver);
                                myPlayer.AboutPlayer();
                                playerExists = true;


                            
                            
                                Console.WriteLine("You've already made your role choice!");

                            

                            break;

                        case 2:
                            ShowCredits();
                            break;

                        case 3:
                            appRunning = false;
                            break;

                        default:
                            Console.WriteLine("You have not chosen a valid option.");
                            Console.WriteLine("Please chose from the menu....");
                            break;
                    //end switch

                //end of while
            //end of private void run


            private static void timer_Elapsed(object sender, ElapsedEventArgs e)
            
                i--;

               // Console.Clear();
                Console.WriteLine("=================================================");
                Console.WriteLine("                  First Quarter");
                Console.WriteLine("");
                Console.WriteLine("                Time Remaining:  " + i.ToString());
                Console.WriteLine("");
                Console.WriteLine("=================================================");

                if (i == 0)
                
                    Console.Clear();
                    Console.WriteLine("");
                    Console.WriteLine("==============================================");
                    Console.WriteLine("         Time Out ! ! ! !");
                    Console.WriteLine("");
                    Console.WriteLine("               Huddle");
                    Console.WriteLine("==============================================");

                    timer.Close();
                    timer.Dispose();
                


                GC.Collect();



            //end ofprivatestaticvoidtimer



        class Player
        
            //set default vaulues for our sandwich type
            private String Quarterback = "";
            private String Runningback = "";
            private String Receiver = "";

            //build the constructor for the sandwich class
            public Player(String Quarterback, String Runningback, String Receiver)
            

                this.Quarterback = Quarterback;
                this.Runningback = Runningback;
                this.Receiver = Receiver;
            

            public void AboutPlayer()
            


                Console.WriteLine("You have made your selection!");
                Console.WriteLine("As");
                Console.WriteLine("Press enter to coninue...");
                Console.ReadLine();
            
        








        static void Main(string[] args)
        

            Console.ReadKey();

            Program myProgram = new Program();
            myProgram.Run();



            Console.ReadLine();





        //endofmain

         private void ShowCredits()
            
                Console.Clear();
                Console.WriteLine("An in-class assigment for Intro to Programming");
                Console.WriteLine("Cesar Mojarro");
            
         private void Header()
            

                Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                Console.WriteLine("+++++++1967 Tackle Football Championships v.1.0 ++++++");
                Console.WriteLine("++++++++++++++Studded Diamond Cup ++++++++++++++++++++");
                Console.WriteLine("++++++++++++++++++++++By IAM!+++++++++++++++++++++++++");
                Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
                Console.ReadKey();
            
         private void Story()
         

             Console.WriteLine("It's the year 1967 and in the First Quarter Steve Miller is running Quarterback for the Packs.");
             Console.WriteLine("Young Styler is running Quarterback for the Texan Ranchers.");
             Console.WriteLine("Your frontline is hung over from the party last nite, ouch! ");
             Console.WriteLine("The team owner is ready to collect on the pay day from the Studded Diamond Cup");
             Console.WriteLine("Don't let him replace you for a rookie!");
             Console.WriteLine("");
             Console.WriteLine("");
             Console.ReadKey();
             Console.WriteLine("The Coin Toss is about to take place,\nyou are nervous and are about to take a chance at this tackle unit.");
             Console.WriteLine("Rules are simple keep the ball running yards,\nstay head strong and keep your eye on defense!");
             Console.WriteLine("Don't let them tackle you down and\nreturn you all the way to the goal line by allowing your apponent to score");
             Console.WriteLine("");
             Console.WriteLine("");
             Console.WriteLine("");
             Console.ReadKey();


         









    //endofProgram

【问题讨论】:

IIRC,您可以使用 ANSI 代码在控制台上着色、定位和做一些时髦的事情 如果i 以秒为单位,您可以使用string time = new DateTime(0).AddSeconds(i).TimeOfDay.ToString(); 获得格式良好的时间。 @MatthewStrawbridge 直接使用 TimeSpan。 new DateTime(0).AddSeconds(i).TimeOfDay 等同于 TimeSpan.FromSeconds(i) @ScottChamberlain 谢谢,你说得对——我在发帖前查看了TimeSpan 的构造函数,但错过了静态方法。 【参考方案1】:

使用Console.CursorLeftConsole.CursorTop 将光标移动到带有时间的行,然后您可以用新值覆盖该行。

private void UpdateTime(int i)

    //Move to the first column.
    Console.CursorLeft = 0;

    //Move to the 4th row
    Console.CursorTop = 3;

    //Write your text.                                                  Extra spaces to overwrite old numbers
    Console.Write("                Time Remaining:  " + i.ToString() + "          ");

您可以通过找出写入列 i 并直接覆盖它来提高效率。

private string _timeRemainingString = "                Time Remaining:  ";

private void UpdateTime(int i)

    //Move to the first column that would have had numbers.
    Console.CursorLeft = _timeRemainingString.Length;

    //Move to the 4th row
    Console.CursorTop = 3;

    //overwrite just the number portion.
    Console.Write(i.ToString() + "          ");


另外,有谁知道如何将数字转换为像这样的时钟格式:00:05:00

如果i 表示游戏中剩余的秒数,那么最简单的解决方案是将其转换为TimeSpan,那么您可以使用时间跨度的格式来获得您想要的格式。

private void UpdateTime(int i)

    Console.CursorLeft = _timeRemainingString.Length;
    Console.CursorTop = 3;

    var time = TimeSpan.FromSeconds(i);
    //the `.ToString("c")` on a TimeSpan will give you a time in the format "00:00:00"
    //you don't need the extra spaces anymore because the time will always be 8 digits long.
    Console.Write(time.ToString("c"));

【讨论】:

我不明白我一直在尝试在窗口上推送打印,同时在顶部保持计时器处于活动状态。 我怎样才能重新发布我正在处理的代码...这件事很复杂! Scott 我不知道如何应用你所说的你能告诉我吗?

以上是关于C# 制作足球游戏控制台应用程序的主要内容,如果未能解决你的问题,请参考以下文章

C# 控制台应用程序 - gameloop / 多个对象

简单的控制台乐透游戏 c# 匹配数组

Process.Start 没有启动游戏应用程序?

在 C# 中模拟键盘输入

ChatGPT介绍世界杯历史与编写足球游戏python程序

为啥它不起作用,控制台程序转换为 Windows Form C# [关闭]