Реализация игры "Морской бой"

Автор работы: Пользователь скрыл имя, 08 Июня 2014 в 14:18, курсовая работа

Краткое описание

Цель этой курсовой работы – детально рассмотреть этапы написания игровой программы с использованием языка программирования C# и языка разметки XAML.
Поставленная цель подразумевает решение следующих задач:
Изучение Microsoft Visual Studio, WPF, XAML;
Написание программы, моделирующей игру «Морской бой»;
Научится пользоваться анимацией
Разработка приложения, с графическим редактором.

Прикрепленные файлы: 1 файл

Курсовая работа.doc

— 3.48 Мб (Скачать документ)

            k1.StrokeThickness = 2;

            k1.BeginAnimation(Line.StrokeThicknessProperty, animation);

            pole_c.Children.Add(k1);

 

         

            Line k2 = new Line();

            k2.X1 = x*40;

            k2.X2 = (x*40)+40;

            k2.Y1 = (y*40) + 40;

           k2.Y2 = y*40 ;

            k2.Stroke = Brushes.Red;

            k2.StrokeThickness = 2;

            k2.BeginAnimation(Line.StrokeThicknessProperty, animation);

            pole_c.Children.Add(k2);

                }

        public void krest_p(int x, int y)

        {

            //анимация

            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 0;

            animation.To = 3;

            animation.Duration = TimeSpan.FromSeconds(2);

           

            Line k1 = new Line();

            k1.X1 = (x * 40);

            k1.X2 = (x * 40) + 40;

            k1.Y1 = (y * 40);

            k1.Y2 = (y * 40) + 40;

            k1.Stroke = Brushes.Red;

            k1.StrokeThickness = 2;

            k1.BeginAnimation(Line.StrokeThicknessProperty, animation);

            pole_i.Children.Add(k1);

 

           

            Line k2 = new Line();

            k2.X1 = x * 40;

            k2.X2 = (x * 40) + 40;

            k2.Y1 = (y * 40) + 40;

            k2.Y2 = y * 40;

            k2.Stroke = Brushes.Red;

            k2.StrokeThickness = 2;

            k2.BeginAnimation(Line.StrokeThicknessProperty, animation);

            pole_i.Children.Add(k2);

        }

        //рисование промаха (элипса)

        public void no_p(int x, int y)

        {

            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 0;

            animation.To = 10;

            animation.Duration = TimeSpan.FromSeconds(2);

           

            Brush color = new SolidColorBrush(Colors.Red);

            Ellipse ell = new Ellipse();

            Thickness mrgn = new Thickness(x, y, 0, 0);

            ell.Margin = mrgn;

            ell.Width = 10;

            ell.Height = 10;

            ell.Fill = color;

            ell.BeginAnimation(Ellipse.HeightProperty, animation);

            ell.BeginAnimation(Ellipse.WidthProperty, animation);

            pole_i.Children.Add(ell);

 

        }

 

        public void no(int x, int y)

        {

            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 0;

            animation.To = 10;

            animation.Duration = TimeSpan.FromSeconds(2);

 

            Brush color = new SolidColorBrush(Colors.Red);

            Ellipse ell = new Ellipse();

            Thickness mrgn = new Thickness(x, y, 0, 0);

            ell.Margin = mrgn;

            ell.Width = 10;

            ell.Height = 10;

            ell.Fill = color;

            ell.BeginAnimation(Ellipse.HeightProperty, animation);

            ell.BeginAnimation(Ellipse.WidthProperty, animation);

            pole_c.Children.Add(ell);

 

        }

        //проверка на пересечение кораблей

        public int prov(int x, int y, int b, int vert)

        {

            int otv = 0;

            if (vert == 0)

            {

               switch (b)

                {

                    case 1: if (array[x, y] != 0) otv = 1; break;

                    case 2: if (array[x, y] != 0 || array[x + 1, y] != 0) otv = 1; break;

                    case 3: if (array[x, y] != 0 || array[x + 1, y] != 0 || array[x + 2, y] != 0) otv = 1; break;

                    case 4: if (array[x, y] != 0 || array[x + 1, y] != 0 || array[x + 2, y] != 0 || array[x + 3, y] != 0) otv = 1; break;

                    default: otv = 0; break;

                }

           }

            else

            {

                switch (b)

                {

                    case 1: if (array[x, y] != 0) otv = 1; break;

                    case 2: if (array[x, y] != 0 ||array[x , y+1] != 0) otv = 1; break;

                    case 3: if (array[x, y] != 0 || array[x , y+1] != 0 || array[x , y+2] != 0) otv = 1; break;

                    case 4: if (array[x, y] != 0 || array[x , y+1] != 0 || array[x , y+2] != 0 || array[x , y+3] != 0) otv = 1; break;

                    default: otv = 0; break;

                }

            }

            return otv;

 

        }

 

        //проверка для поля противника

        public int prov_p(int x, int y, int b, int vert)

        {

            int otv = 0;

            if (vert == 0)

            {

                switch (b)

                {

                    case 1: if (array_p[x, y] != 0) otv = 1; break;

                    case 2: if (array_p[x, y] != 0 || array_p[x + 1, y] != 0) otv = 1; break;

                    case 3: if (array_p[x, y] != 0 || array_p[x + 1, y] != 0 || array_p[x + 2, y] != 0) otv = 1; break;

                    case 4: if (array_p[x, y] != 0 || array_p[x + 1, y] != 0 || array_p[x + 2, y] != 0 || array_p[x + 3, y] != 0) otv = 1; break;

                    default: otv = 0; break;

                }

            }

            else

            {

                switch (b)

                {

                    case 1: if (array_p[x, y] != 0) otv = 1; break;

                    case 2: if (array_p[x, y] != 0 || array_p[x, y + 1] != 0) otv = 1; break;

                    case 3: if (array_p[x, y] != 0 || array_p[x, y + 1] != 0 || array_p[x, y + 2] != 0) otv = 1; break;

                    case 4: if (array_p[x, y] != 0 || array_p[x, y + 1] != 0 || array_p[x, y + 2] != 0 || array_p[x, y + 3] != 0) otv = 1; break;

                    default: otv = 0; break;

                }

            }

            return otv;

 

        }

 

     

        //прорисовка кораблика

        public void korabl(double x, double y)

        {

            Brush color = new SolidColorBrush(Colors.BlanchedAlmond);

            Rectangle cube = new Rectangle();

            Thickness mrgn = new Thickness(x, y, 0, 0);

            cube.Margin = mrgn;

            cube.Width = 40;

            cube.Height = 40;

            cube.Fill = color;

            pole_c.Children.Add(cube);

        }

 

        public MainWindow()

        {

            InitializeComponent();

           setka();//вызов построоения сетки

            pole_i.Width = 0;

            t.Visibility = Visibility.Hidden;

                                

        }

private void Button_Click(object sender, RoutedEventArgs e)

        {

                      

            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 0;

            animation.To = 400;

            animation.Duration = TimeSpan.FromSeconds(3);

            pole_i.BeginAnimation(Grid.WidthProperty, animation);

           pole_i.BeginAnimation(Grid.HeightProperty, animation);

 

            array_prot();

            string prov = "";

            for (int n=0; n< 10; n++)

            {

              

                for (int l = 0; l < 10; l++)

                {

                    prov = prov +" "+ array_p[n,l];

                }

 

                 prov=prov+"\n";

            }

            t.Text = prov;

 

 

                btn.Visibility = Visibility.Hidden;

            vert.Visibility = Visibility.Hidden;

        }

//по нажатию клавиши определяем выбранную ячейку

        private void pole_c_MouseUp(object sender, MouseButtonEventArgs e)

        {

         

            int b = 0;//значение корабля

           

            Point pt = e.GetPosition(this);//функция получения координат щелчка мыши

            int x=Convert.ToInt32(pt.X), y=Convert.ToInt32(pt.Y);

            int x_k = 0, y_k = 0;

          

            x = (x - 560) / 40;

            y = (y - 40) / 40;

 

            x_k = -360 +x*80;

            y_k = -360+ y*80;

 

           

             //  a++; для счета клеток, определения количества кораблей разных типов

            if (a <= 4)

            {

                if (prov(x, y, 1, 0) == 0)

                {

                    b = 1;

 

                    game_zone(x, y, b);//добавлем элементы в массив, где хранятся корабли игрока

 

                    korabl(x_k, y_k);

                  

                }

                else

                {

                    MessageBox.Show("Не правильно расположен корабль");

                    a--;

                }

 

            }

            if (a >= 5 && a < 8)

            {

 

                b = 2;

                if (vert.IsChecked == false)

                {

                   

                        try

                        {

                            if (prov(x, y, 2, 0) == 0)

                            {

                            game_zone(x + 1, y, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k + 80, y_k);

                            }

                            else

                            {

                                MessageBox.Show("пересечение с другим кораблем"); a--;

                            }

 

                        }

                        catch { MessageBox.Show("выход за пределы поля"); a--; }

                  

                }

                else

                {

                        try

                        {

                            if (prov(x, y, 2, 1) == 0)

                            {

                            game_zone(x, y + 1, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k, y_k + 80);

                            }

                            else

                            {

                                MessageBox.Show("пересечение с другим кораблем"); a--;

                            }

                        }

                        catch { MessageBox.Show("выход за пределы поля"); a--; }

                 

                }

             

            }

            if (a >= 8 && a < 10)

            {

                b = 3;

                if (vert.IsChecked == false)

                {

                   

                        try

                        {

                        if (prov(x, y, 3, 0) == 0)

                        {

                            game_zone(x + 2, y, b);

                            game_zone(x + 1, y, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k + 80, y_k);

                            korabl(x_k + 160, y_k);

                        }

                        else

                        {

                            MessageBox.Show("пересечение с другим кораблем"); a--;

                        }

                        }

                        catch { MessageBox.Show("выход за пределы поля"); a--; }

                                  }

                else

                {

                                            try

                        {

                        if (prov(x, y, 3, 1) == 0)

                        {

                            game_zone(x, y + 2, b);

                            game_zone(x, y + 1, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k, y_k + 80);

                            korabl(x_k, y_k + 160);

                        }

                        else

                        {

                            MessageBox.Show("пересечение с другим кораблем"); a--;

                        }

                        }

                        catch { MessageBox.Show("выход за пределы поля"); a--; }

                  

                }

            }

            if (a == 10 )

            {

                b = 4;

               

                if (vert.IsChecked == false)

                {

                   

                        try

                        {

                            if (prov(x, y, 4, 0) == 0)

                            {

                            game_zone(x + 3, y, b);

                            game_zone(x + 2, y, b);

                            game_zone(x + 1, y, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k + 80, y_k);

                            korabl(x_k + 160, y_k);

                            korabl(x_k + 240, y_k);

                            MessageBox.Show("Все готово!\n Начать игру!");

                            }

                            else

                            {

                                MessageBox.Show("пересечение с другим кораблем"); a--;

                            }

                        }

                       catch { MessageBox.Show("выход за пределы поля"); a--; ; }

                   

                }

                else

                {

                   

                        try

                        {

                            if (prov(x, y, 4, 1) == 0)

                            {

                            game_zone(x, y + 3, b);

                            game_zone(x, y + 2, b);

                            game_zone(x, y + 1, b);

                            game_zone(x, y, b);

 

                            korabl(x_k, y_k);

                            korabl(x_k, y_k + 80);

                            korabl(x_k, y_k + 160);

                            korabl(x_k, y_k + 240);

                            MessageBox.Show("Все готово!\n Начать игру!");

                            }

                            else

                            {

                                MessageBox.Show("пересечение с другим кораблем"); a--;

                            }

                        }

                        catch { MessageBox.Show("выход за пределы поля"); a--; }

                   

                }

            }

            if (a > 10) b=0;

       

            a++;

           

 

        }

 

        private void pole_i_MouseUp(object sender, MouseButtonEventArgs e)

        {

           

            Point pt = e.GetPosition(this);

            int x = Convert.ToInt32(pt.X), y = Convert.ToInt32(pt.Y);

            int x_k = 0, y_k = 0;

                      

            x = (x - 40) / 40;

            y = (y - 40) / 40;

 

            x_k = -360 + x * 80;

            y_k = -360 + y * 80;

             

                if (array_p[x, y] != 0)

                {

                    if (array_p[x, y] == 5)

                    {

                        MessageBox.Show("Уже здесь был!");

                    }

                    else

                    {

                        array_p[x, y] = 5;

                        krest_p(x, y);

                        hod++;

                    }

Информация о работе Реализация игры "Морской бой"