Esempio n. 1
0
 public Car(Car car1)
 {
     this.x = car1.x;
     this.y = car1.y;
     this.color = car1.color;
     this.vehicle = car1.vehicle;
 }
Esempio n. 2
0
 /// <summary>
 /// Allows the game to perform any initialization it needs to before starting to run.
 /// This is where it can query for any required services and load any non-graphic
 /// related content.  Calling base.Initialize will enumerate through any components
 /// and initialize them as well.
 /// </summary>
 protected override void Initialize()
 {
     // TODO: Add your initialization logic here
     _car = new Car();
     _car.Speed = 3;
     //_car.
     base.Initialize();
 }
Esempio n. 3
0
        private void PrintCar(Car car)
        {
            int counterY = 0;
            int counterX = 0;
            for (int i = 0; i < car.Vehicle.Length; i++)
            {
                Console.SetCursorPosition(car.X + counterX, car.Y + counterY);
                Console.Write(car.Vehicle[i]);
                counterX++;
                if (car.Vehicle[i] == '\n')
                {
                    counterY++;
                    counterX = 0;

                }
            }
        }
Esempio n. 4
0
        private Car SpawnCar(int i)
        {
            int laneWidth = 4;

            List<ConsoleColor> colorPalette = new List<ConsoleColor>() { ConsoleColor.Blue, ConsoleColor.Cyan,
                                                    ConsoleColor.Green, ConsoleColor.Magenta, ConsoleColor.White };
            Random random = new Random();

            Car spawnedCar = new Car();
            spawnedCar.Y = 1;
            spawnedCar.Color = colorPalette[random.Next(colorPalette.Count)];

            //int lane = random.Next(1, 6);
            switch (i)
            {
                case 1: spawnedCar.X = trackOffsetRight + laneWidth * 0 + 1; break;
                case 2: spawnedCar.X = trackOffsetRight + laneWidth * 1 + 1; break;
                case 3: spawnedCar.X = trackOffsetRight + laneWidth * 2 + 1; break;
                case 4: spawnedCar.X = trackOffsetRight + laneWidth * 3 + 1; break;
                case 5: spawnedCar.X = trackOffsetRight + laneWidth * 4 + 1; break;

            }

            return spawnedCar;
        }
Esempio n. 5
0
        private void InitializeGame(int speed, int spawnCarInterval, string sound)
        {
            Console.WriteLine();
            Console.Write(new string(' ', (Console.WindowWidth - "Enter your nickname: ".Length) / 2));
            Console.Write("Enter your nickname: ");
            player = Console.ReadLine();

            score = 0;
            lives = 3;

            RestartRace:
            PlaySound(sound);

            // variables
            List<Car> carsList = new List<Car>();
            List<Coin> collectibles = new List<Coin>();

            // initialize player car
            Car myCar = new Car(34, 35, ConsoleColor.Red);

            Random random = new Random();

            int newCarInterval = 0;
            int newFastCarInterval = 0;
            int newCollectibleInterval = 0;

            while (true)
            {

                if (newCollectibleInterval > 29)
                {
                    Coin bonus = new Coin();
                    int bonusLane = random.Next(0, 5);
                    bonus.X = trackOffsetRight + 2 + 4 * bonusLane;
                    // 21-> where the first lane starts; 2-> half the width of the lane; 4-> the width of one lane
                    bonus.Y = 1;
                    collectibles.Add(bonus);
                    newCollectibleInterval = 0;
                    newCarInterval = -2;
                }

                if (newCarInterval > spawnCarInterval)
                {
                    Car addCar;

                    if (newFastCarInterval > 17)
                    {
                        List<int> freelanes = new List<int>();
                        for (int lane = 1; lane <= 5; lane++)
                        {
                            bool isFree = true;
                            for (int car = 0; car < carsList.Count; car++)
                            {
                                if (carsList[car].X == trackOffsetRight + 1 + (lane - 1) * 4 && carsList[car].Y < 25)
                                {
                                    isFree = false;
                                }
                            }
                            if (isFree)
                            {
                                freelanes.Add(lane);
                            }
                        }
                        int randomFreeLane = freelanes[random.Next(0, freelanes.Count)];
                        addCar = SpawnCar(randomFreeLane);
                        addCar.Speed = 2;
                        newFastCarInterval = 0;
                    }
                    else
                    {
                        addCar = SpawnCar(random.Next(1, 6));
                        addCar.Speed = 1;
                        newCarInterval = 0;
                    }

                    carsList.Add(addCar);
                }

                for (int i = 1; i < 45; i += 2)
                {
                    char symbol = '|';
                    string lines = string.Format("{2}{1}{0}{1}{0}{1}{0}{1}{0}{1}{0}{1}", new string(' ', 3), symbol, new string(' ', trackOffsetRight));
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(lines);

                }

                Console.SetCursorPosition(trackOffsetRight + 5 * 5, 2);
                Console.WriteLine(player);
                Console.SetCursorPosition(trackOffsetRight + 5 * 5, 4);
                Console.WriteLine("Score: {0:F2}", score);
                Console.SetCursorPosition(trackOffsetRight + 5 * 5, 6);
                Console.WriteLine("Lives: {0}", lives);
                Console.SetCursorPosition(trackOffsetRight + 5 * 5, 8);
                Console.WriteLine("Speed: {0}", speed);
                Console.SetCursorPosition(2, 1);
                Console.WriteLine("Press Esc to pause");
                // Position = track offset + 4 lanes, 5 chars each + aditional buffer 5

                PrintCar(myCar);
                //PrintCarAtPosition(myCar.X, myCar.Y, "*", myCar.Color);

                foreach (var car in carsList)
                {
                    car.Y += car.Speed;
                    PrintCar(car);
                    //PrintCarAtPosition(car.X, car.Y, "*", car.Color);

                    if (car.X == myCar.X && ((myCar.Y >= car.Y && myCar.Y <= car.Y + 3) || (myCar.Y + 3 >= car.Y && myCar.Y + 3 <= car.Y + 3)))
                    {
                        PrintCarAtPosition(myCar.X, myCar.Y, "X", ConsoleColor.DarkRed);

                        PlaySound("Crash");
                        lives--;

                        Thread.Sleep(2000);

                        if (lives <= 0)
                        {
                            GameOver(score, player);
                        }

                        carsList.Clear();
                        Console.Clear();
                        goto RestartRace;
                    }
                }
                for (int i = 0; i < carsList.Count; i++)
                {
                    if (carsList[i].Y >= Console.WindowHeight - 5)
                    {
                        carsList.Remove(carsList[i]);
                        score += 5;
                    }
                }

                foreach (var bonusCoin in collectibles)
                {
                    bonusCoin.Y++;
                    PrintAtPosition(bonusCoin.X, bonusCoin.Y, bonusCoin.Symbol, bonusCoin.Color);
                    if (bonusCoin.X >= myCar.X && bonusCoin.X <= (myCar.X + 2) && bonusCoin.Y >= myCar.Y && bonusCoin.Y <= (myCar.Y + 3))
                    {
                        PrintCarAtPosition(myCar.X, myCar.Y, "X", ConsoleColor.Yellow);
                        score += 10;
                        bonusCoin.Y = Console.WindowHeight;
                        Console.Beep(659, 125);
                    }

                }
                for (int i = 0; i < collectibles.Count; i++)
                {
                    if (collectibles[i].Y >= Console.WindowHeight - 1)
                    {
                        collectibles.Remove(collectibles[i]);
                    }
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey();
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey();
                    }

                    switch (pressedKey.Key)
                    {
                        case ConsoleKey.LeftArrow:
                            {
                                if (myCar.X > trackOffsetRight + 1)
                                {
                                    myCar.X -= 4;
                                }
                                break;
                            }
                        case ConsoleKey.UpArrow:
                            {
                                if (myCar.Y > 10)
                                {
                                    myCar.Y -= 1;
                                }
                            }
                            break;
                        case ConsoleKey.RightArrow:
                            {
                                if (myCar.X < trackOffsetRight + 16)
                                {
                                    myCar.X += 4;
                                }
                            }
                            break;
                        case ConsoleKey.DownArrow:
                            {
                                if (myCar.Y < 40)
                                {
                                    myCar.Y += 1;
                                }
                            }
                            break;

                        case ConsoleKey.Escape:
                            {
                                IngameMenu();
                            }
                            break;
                    }

                    //while (Console.KeyAvailable)
                    //{
                    //    pressedKey = Console.ReadKey();
                    //}
                }
                score += (0.2) * speed / 240;
                Thread.Sleep(250 - speed);
                Console.Clear();
                newCarInterval++;
                newFastCarInterval++;
                newCollectibleInterval++;

                // todo: collision detection

            }
        }