简体   繁体   中英

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. 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. 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?

Additionally, does anyone know how to convert the number to format a timeclock like so: 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.

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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