예제 #1
0
 public Coin(Coin coin1)
 {
     this.x = coin1.x;
     this.y = coin1.y;
     this.color = coin1.color;
     this.symbol = coin1.symbol;
 }
예제 #2
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

            }
        }