Read.Key not to stop the program to keep running C# Console

Oct 22 2014 8:10 AM
Hi guys!

I am trying to code the famous Frogger game in C# - Console. I got to a point where the "cars" (reporesented as ">" and "<" wont move unless the frog (an "X") moves (console.ReadKey waits for the key pressing).
Is there anyway I could make teh cars to run on their own (keep moving non-stop) while the frog waits for the key press? I've read about thread but not sure how to apply it.

Here is my code so far:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Frogger
{
    class Program
    {
        static void Main(string[] args)
        {

            sbyte bytCar1X;
            sbyte bytCar1Y;
            sbyte bytCar2X;
            sbyte bytCar2Y;
            sbyte bytCar3X;
            sbyte bytCar3Y;
            sbyte bytFrogX;
            sbyte bytFrogY;

            
            Console.Clear();


            //Random deeclarations

            Random rndRandom = new Random();

            bytCar1X = Convert.ToSByte(rndRandom.Next(179));
            bytCar1Y = Convert.ToSByte(rndRandom.Next(16));
            bytCar2X = Convert.ToSByte(rndRandom.Next(179));
            bytCar2Y = Convert.ToSByte(rndRandom.Next(715));
            bytCar3X = Convert.ToSByte(rndRandom.Next(179));
            bytCar3Y = Convert.ToSByte(rndRandom.Next(1622));
            bytFrogX = Convert.ToSByte(rndRandom.Next(179));
            bytFrogY = 23;


            do

            {
                Console.Clear();

                //Car 1 movement

                bytCar1X = bytCar1X +=1;
                if(bytCar1X > 79)
                {
                    bytCar1X = 1;
                }

                Console.SetCursorPosition(bytCar1X, bytCar1Y);
                Console.WriteLine(">");

                //Car 2 movement

                bytCar2X = bytCar2X -= 1;
                if (bytCar2X < 0)
                {
                    bytCar2X = 79;
                }

                Console.SetCursorPosition(bytCar2X, bytCar2Y);
                Console.WriteLine("<");

                //Car 3 movement

                bytCar3X = bytCar3X += 1;
                if (bytCar3X > 79)
                {
                    bytCar3X = 1;
                }

                Console.SetCursorPosition(bytCar3X, bytCar3Y);
                Console.WriteLine(">");

                //Frog keys

                Console.SetCursorPosition(bytFrogX, bytFrogY);
                Console.WriteLine("X");

                ConsoleKeyInfo CKIKey = Console.ReadKey(true);
                Console.SetCursorPosition(00);

                if(CKIKey.Key == ConsoleKey.UpArrow)
                {
                    bytFrogY = (bytFrogY -= 1);
                }
                
                Console.SetCursorPosition(00);

                if (CKIKey.Key == ConsoleKey.DownArrow)
                {
                    bytFrogY = (bytFrogY += 1);
                }

                Console.SetCursorPosition(00);

                if (CKIKey.Key == ConsoleKey.LeftArrow)
                {
                    bytFrogX = (bytFrogX -= 1);
                }

                Console.SetCursorPosition(00);

                if (CKIKey.Key == ConsoleKey.RightArrow)
                {
                    bytFrogX = (bytFrogX += 1);
                }

                //Frog Respawn and Crashes

                if (bytFrogY == 0)
                {
                    bytFrogY = 23;
                }

                if ((bytFrogX == bytCar1X) && (bytFrogY == bytCar1Y))
                {
                    Console.Clear();
                    Console.WriteLine("CRASH! Press enter to continue");
                    Console.ReadLine();
                }

                if ((bytFrogX == bytCar2X) && (bytFrogY == bytCar2Y))
                {
                    Console.Clear();
                    Console.WriteLine("CRASH! Press enter to continue");
                    Console.ReadLine();
                }

                if ((bytFrogX == bytCar3X) && (bytFrogY == bytCar3Y))
                {
                    Console.Clear();
                    Console.WriteLine("CRASH! Press enter to continue");
                    Console.ReadLine();
                }


            } while (1 == 1);

          
        }

    }

}



Thanks in advance.

Victor.

Answers (1)