Esempio n. 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();
        }
Esempio n. 2
0
        /// <summary>Выводит на экран карту</summary>
        static void PrintMap(GameMap map, Grid Grid)
        {
            //Все обьекты на карте
            foreach (GameObject item in map)
            {
                if (item != null) //Console.Write(" ");
                    Grid.Children.Add(item);
            }
 
        }
Esempio n. 3
0
        /// <summary>Обработчик Столкновений</summary>
        static void Collision(GameAPI.GameMap map, GameAPI.Point p, Object NewObj, Object ExistObj)
        {
            if ((NewObj is GameAPI.Snake) && (ExistObj is GameAPI.Mouse))
            {
                //Удалим яблочко
                map.Remove(p);
                (NewObj as Snake).Grow(map); // Вырастим

                if ((NewObj as Snake).Count >= 12)
                {
                    throw new GameWinExeption(); //Віиграли
                }
                RandomAppleGeneration();         // Сгенерим новое яблочко
            }
            else
            {
                throw new GameOverExeption();
            }
        }
Esempio n. 4
0
        /// <summary>Создаёт Объект Карты из Файла (Размер карты КвоСтрок Х КвоСимволов в последней строке)</summary>
        /// <param name="FileName">Путь к файлу</param>
        public static GameMap LoadMapFromfile(string FileName)
        {
            if (!  File.Exists(FileName)) return null;

            GameMap map = new GameMap(int.MaxValue, int.MaxValue);

            int X = -1; // Х координата
            int Y = -1; // У Координата
            String line;

            using (StreamReader sr = new StreamReader(FileName))
            {
                
                //Переберем все строки
                while (!sr.EndOfStream)
                {
                    ++Y;
                    X = -1;

                    line = sr.ReadLine();
                    foreach (Char Symbol in line)
                    {
                        ++X;
                        if (Symbol != ' ' && !Char.IsControl(Symbol) && !Char.IsSeparator(Symbol))
                        {
                            GameAPI.Point p = new Point(X, Y);
                            map.Add(p, new Wall(p));
                        }
                    }

                }
                
                //Зададим размер карты
                map.y = ++Y;
                map.x = ++X;
            }

            return map;
        }
Esempio n. 5
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();

        }