简体   繁体   English

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

[英]C# Making a Football Game Console Application

I have a C# Console application that I would like to write using this code but I have tried a couple of things and seem to run a block. 我有一个C#控制台应用程序,我想使用此代码编写,但是我尝试了几件事,并且似乎在运行一个块。 I am trying to make a timer on a game at the top but the only way for me to print to the console and sort of keep it is by eliminating the Console.Clear from this Code. 我试图在最上面的游戏上设置一个计时器,但是我唯一可以打印到控制台并保持该状态的方法是消除此代码中的Console.Clear。 Is there a way that I can keep the Countdown timer at the top while Console.Clear is only active for the timer not everything in the console window? 当Console.Clear仅针对计时器而不是控制台窗口中的所有计时器处于活动状态时,有没有办法使Countdown计时器保持在顶部?

Additionally, does anyone know how to convert the number to format a timeclock like so: 00:05:00? 此外,有人知道如何将数字转换为格式为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();
        }
    }
}

Okay here is the extension of the code I am having issues trying to keep my game on the bottom of the window and really trying to code the countdown timer to be active as soon as the game starts but also keeping my game selection print for the user any suggestions would help 好的,这是代码的扩展,我遇到了一些问题,试图将我的游戏保持在窗口底部,并且实际上试图对倒数计时器进行编码,以便在游戏开始后立即将其激活,同时还要为用户保留我的游戏选择打印任何建议都会有所帮助

{}.... {} ....

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

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
}

Use Console.CursorLeft and Console.CursorTop to move the cursor to the line with the time, you can then overwrite that line with your new value. 使用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() + "          ");
}

You can be even more efficient by figuring out the column i is written to and just overwrite that. 你可以搞清楚列更有效率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() + "          ");
}

Additionally, does anyone know how to convert the number to format a timeclock like so: 00:05:00 此外,是否有人知道如何将数字转换为格式这样的时钟:00:05:00

If i represents the number of seconds left in the game it the easiest solultion would be convert it in to a TimeSpan then you can use time span's formatting to get the format you want. 如果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"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM