Question: I keep having issues with the code. It keeps giving me a accident immediately after running the code. namespace Chapter 2 Example 6 { public

I keep having issues with the code. It keeps giving me a accident immediately after running the code.
namespace Chapter2Example6
{
public class Program
{
static void Main()
{
Console.CursorVisible = false;
const int ROAD_WIDTH =20;
Vehicle playerVehicle = new Vehicle
(10,20,'^', ConsoleColor.Cyan, 0);
Vehicle computerVehicle = new Vehicle
(10,0,'8', ConsoleColor.Green, 0);
//vehicle computerVehicle = new Vehicle
//(10,0,'8', ConsoleColor.Grren, 0);
Random random = new Random();
List list = new List();
while (true)
{
Console.Clear();
int newXposition = random.Next(ROAD_WIDTH);
Vehicle vehicle = new Vehicle(newXposition,0,'8',
ConsoleColor.Green, 0);
list.Add(vehicle);
foreach (var computerVehicle in list)
{
if (playerVehicle.XPosition == computerVehicle.XPosition
&& playerVehicle.YPosition == computerVehicle.YPosition)
{
playerVehicle.Collide();
}
if (computerVehicle.YPosition <
Console.WindowHeight -1)
computerVehicle.YPosition++;
else
computerVehicle.YPosition =1;
computerVehicle.Display();
playerVehicle.Display();
while (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.LeftArrow)
{
if (playerVehicle.XPosition >0)
playerVehicle.MoveLeft();
}
else if (key.Key == ConsoleKey.RightArrow)
{
if (playerVehicle.XPosition +1< ROAD_WIDTH)
playerVehicle.MoveRight();
}
else if (key.Key == ConsoleKey.UpArrow)
{
if (playerVehicle.Speed +10<200)
playerVehicle.Accelerate();
}
else if (key.Key == ConsoleKey.DownArrow)
{
if (playerVehicle.Speed -10>0)
playerVehicle.Brake();
}
}
Thread.Sleep(200- playerVehicle.Speed);
}
}
}
internal class Vehicle
{
//auto-implemented properties
public int XPosition { get; set; }
public int YPosition { get; set; }
public char VehicleSymbol { get; set; }
public ConsoleColor VehicleColor { get; set; }
public int Speed { get; set; }
//constructor
public Vehicle(int xPosition, int yPosition,
char vehicleSymbol,
ConsoleColor vehicleColor, int speed)
{
XPosition = xPosition;
YPosition = yPosition;
VehicleSymbol = vehicleSymbol;
VehicleColor = vehicleColor;
Speed = speed;
}
//methods
public void MoveLeft()
{
XPosition--;
}
public void MoveRight()
{
XPosition++;
}
public void Accelerate()
{
Speed +=10;
}
public void Brake()
{
Speed -=10;
}
public void Display()
{
Console.SetCursorPosition(XPosition, YPosition);
Console.ForegroundColor = VehicleColor;
Console.Write(VehicleSymbol);
}
public void Collide()
{
Console.SetCursorPosition(XPosition, YPosition);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("X");
Console.SetCursorPosition(XPosition +2, YPosition);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Accident
Press Enter to Quit.");
Console.WriteLine();
Environment.Exit(0);
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!