Пример #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Grid.Children.Clear();
            map = GameMap.LoadMapFromfile(@"..\..\Box.map");

            map.OnCollision += Collision;                                                         //Обработчик колизий

            snake = new GameAPI.Snake(new GameAPI.Point(2, 2));                                   //Создадим змею
            map.Add(snake.CurentPosition, snake);                                                 // Добавим змею на карту

            snake.OnGrow += (Object obj, EventArgs ea) => { Grid.Children.Add((SnakeBody)obj); }; //Добавим в Грид Новые куски змеи...
            snake.OnGrow += (Object obj, EventArgs ea) => { if (snake.Count % 4 == 0)
                                                            {
                                                                dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, dispatcherTimer.Interval.Milliseconds / 2);
                                                            }
            };                       //Добавим в Грид Новые куски змеи...

            RandomAppleGeneration(); //Сгенерим яблочко

            PrintMap(map, this.Grid);

            //Поехали
            dispatcherTimer       = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += (Object obj, EventArgs ea) =>
            {
                try
                {
                    snake.Move(map);
                }
                catch (ApplicationException Aex)
                {
                    dispatcherTimer.Stop();                         // Остановим таймер
                    Grid.Children.Clear();

                    Label l = new Label();
                    l.FontSize            = 48;
                    l.VerticalAlignment   = System.Windows.VerticalAlignment.Center;
                    l.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    if (Aex is GameOverExeption)
                    {
                        l.Content    = "Game Over";
                        l.Foreground = Brushes.Red;
                    }

                    if (Aex is GameWinExeption)
                    {
                        l.Content    = "Win level";
                        l.Foreground = Brushes.Green;
                    }

                    Grid.Children.Add(l);
                }
            };
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 600);
            dispatcherTimer.Start();
        }
Пример #2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Grid.Children.Clear();
            map = GameMap.LoadMapFromfile(@"..\..\Box.map");

            map.OnCollision += Collision; //Обработчик колизий

            snake = new GameAPI.Snake(new GameAPI.Point(2, 2)); //Создадим змею
            map.Add(snake.CurentPosition, snake); // Добавим змею на карту

            snake.OnGrow += (Object obj, EventArgs ea) => { Grid.Children.Add((SnakeBody)obj); };//Добавим в Грид Новые куски змеи...
            snake.OnGrow += (Object obj, EventArgs ea) => { if (snake.Count % 4 ==0 ) dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, dispatcherTimer.Interval.Milliseconds / 2); };//Добавим в Грид Новые куски змеи...
    
            RandomAppleGeneration(); //Сгенерим яблочко

            PrintMap(map, this.Grid);

            //Поехали
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += (Object obj, EventArgs ea) => 
                                    {
                                        try
                                        {
                                            snake.Move(map);
                                        }
                                        catch (ApplicationException Aex)
                                        {
                                            dispatcherTimer.Stop(); // Остановим таймер
                                            Grid.Children.Clear();

                                            Label l = new Label();
                                            l.FontSize = 48;
                                            l.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                                            l.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                                            if (Aex is GameOverExeption)
                                            {
                                                l.Content = "Game Over";
                                                l.Foreground = Brushes.Red;
                                            }

                                            if (Aex is GameWinExeption)
                                            {
                                                l.Content = "Win level";
                                                l.Foreground = Brushes.Green;
                                            }

                                            Grid.Children.Add(l);
                                        }
                                    };
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 600);
            dispatcherTimer.Start();

        }