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

时间:2014-10-31 21:24:36

标签: c#

我有一个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
}

1 个答案:

答案 0 :(得分:4)

使用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"));
}