Ejemplo n.º 1
0
        /// <summary> gets called every frame (выполняется для каждого кадра) </summary>
        public override void Update()
        {
            //==== game loop ====
            if (!isEnd)
            {
                currentSpeed = windowMult * (float)Math.Sqrt(Math.Pow(startSpeed, 2) + 0.02f * MainTimer.ElapsedMilliseconds);
                //background
                firstLine.MoveAside(currentSpeed, SideX.middle, SideY.down);
                secondLine.MoveAside(currentSpeed, SideX.middle, SideY.down);
                if (firstLine.Position.Y >= firstLine.Position.MaxY)
                {
                    firstLine.Position.Y = secondLine.Position.Y - road.Size.Y;
                }
                if (secondLine.Position.Y >= secondLine.Position.MaxY)
                {
                    secondLine.Position.Y = firstLine.Position.Y - road.Size.Y;
                }
                //text
                if (messageOn)
                {
                    messageA = "Press arrow keys or WASD to move.";
                    //message.WriteTo(textA);
                    if (Controller.LastKey != null)
                    {
                        //textA.IsDrawn = false;
                        messageOn = false;
                    }
                }
                else
                {
                    charCounter = (int)Math.Max(Math.Floor(currentSpeed * mphMult / 10) - 5, 0);
                    messageA    = String.Format("Current speed: {0} mph {1}", (int)(currentSpeed * mphMult), new string('!', charCounter));
                    textA.MoveTowards(textSpeed * 4, textPoint);
                }
                messageA.WriteTo(textA);
                //cars
                for (int i = 0; i < enemies.Count; i++)
                {
                    enemies[i].MoveAside(currentSpeed - startSpeed * 2, SideX.middle, SideY.down);
                    int prev = i - 1 < 0 ? enemies.Count - 1 : i - 1;
                    if (enemies[i].Position.Y >= enemies[i].Position.MaxY)
                    {
                        enemies[i].Position.X = new Random().Next((int)heroCar.Position.MinX, (int)heroCar.Position.MaxX);
                        enemies[i].Position.Y = Math.Min(car.Position.Y - new Random().Next(0, (int)currentSpeed.GridToY()), enemies[prev].Position.Y - road.Size.Y / ZeroCheck(enemies.Count));
                        enemies[i].Color      = RandomColor(ConsoleColor.Black, ConsoleColor.DarkRed, windows.Color, heroCar.Color, road.Color, firstLine.Color);
                        if (!enemies[i].IsDrawn)
                        {
                            enemies[i].IsDrawn = true;
                        }
                    }
                }

                heroCar.MoveByWASD(currentSpeed, heroStep);
                if (heroCar.TriggerEnter(out other, enemyTag))
                {
                    restartCar = other[0];
                    heroCar.ChildList[0].Color    = ConsoleColor.DarkRed;
                    restartCar.ChildList[0].Color = ConsoleColor.DarkRed;
                    Console.Beep();
                    isEnd = true;
                }
                //environment
                for (int i = 0; i < environs.Count; i++)
                {
                    environs[i].MoveAside(currentSpeed, SideX.middle, SideY.down);
                    int prev = i - 1 < 0 ? environs.Count - 1 : i - 1;
                    if (environs[i].Position.Y >= environs[i].Position.MaxY)
                    {
                        environs[i].Position.X = new Random().Next(0, 2) > 0 ? plane.Position.X - plane.Size.X / 2 : plane.Position.X + plane.Size.X / 2;
                        environs[i].Position.Y = Math.Min(environsPosY - new Random().Next(0, (int)currentSpeed.GridToY()), environs[prev].Position.Y - houseC.Size.Y * 1.5f);
                        if (environs[i].Tag == house.Tag)
                        {
                            environs[i].Color = RandomColor(ConsoleColor.Black, ConsoleColor.Red, ConsoleColor.DarkRed, windows.Color, heroCar.Color, road.Color, plane.Color);
                        }
                    }
                }
            }
            else
            {
                //text
                messageA = currentSpeed > 30 ? String.Format(" You drove at a speed of {0} mph!", (int)(currentSpeed * mphMult)): "";
                messageB = String.Format("Game over!{0}", messageA);
                messageC = "Press Enter to restart.";
                messageB.WriteTo(textA, 0, -1);
                messageC.WriteTo(textA, 0, 1);
                textA.MoveTowards(textSpeed, heroCar);
                if (Controller.LastKey == ConsoleKey.Enter)
                {
                    //System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location);
                    //Environment.Exit(0);
                    currentSpeed = startSpeed;
                    heroCar.AlignWithSide(SideX.middle, SideY.down);
                    heroCar.ChildList[0].Color    = windows.Color;
                    restartCar.ChildList[0].Color = windows.Color;
                    restartCar.Position.Y         = (int)restartCar.Position.MaxY;
                    MainTimer.Restart();
                    //SleepTime = FixedTimeMs;
                    isEnd = false;
                }
            }
        }