WPF--扫雷游戏源码

有些话、适合烂在心里 提交于 2019-12-19 02:28:39

WPF–扫雷游戏源码:

XAML源码:

<Window x:Class="WPF编程.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF编程"
        mc:Ignorable="d"
        Title="扫雷" Height="600" Width="600"  Icon="img/logo.ico" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="100"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="65"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="65"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
            <MenuItem Width="60" Height="30" HorizontalContentAlignment="Center"
               VerticalContentAlignment="Center">
                <MenuItem.Icon>
                    <Image Source="img/Sysico.ico" Margin="0"></Image>
                </MenuItem.Icon>
                <MenuItem.Header>
                    <Label Content="菜单" Margin="-10,0,0,0"></Label>
                </MenuItem.Header>
                <MenuItem Header="等级" Margin="0,0,-25.667,0">
                    <MenuItem.Icon>
                        <Image Source="img/CustomerOrder.ico"></Image>
                    </MenuItem.Icon>
                    <MenuItem Header="初级" Margin="0,0,-30,0" Click="MenuItem_Click_1">
                        <MenuItem.Icon>
                            <Image Source="img/CustomerOrder.ico"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="中级" Margin="0,0,-40,0" Click="MenuItem_Click_2">
                        <MenuItem.Icon>
                            <Image Source="img/CustomerOrder.ico"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="高级" Margin="0,0,-40,0" Click="MenuItem_Click_3">
                        <MenuItem.Icon>
                            <Image Source="img/CustomerOrder.ico"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                </MenuItem>
                <MenuItem Header="设置" Margin="0,0,-40,0">
                    <MenuItem.Icon>
                        <Image Source="img/mrp.ico"></Image>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="退出" Margin="0,0,-40,0" Click="MenuItem_Click">
                    <MenuItem.Icon>
                        <Image Source="img/exit.ico"></Image>
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>
        <Border Grid.Row="1" Grid.Column="0"  Grid.RowSpan="2" Grid.ColumnSpan="3">
            <Border.Background>
                <ImageBrush ImageSource="img/bg.jpg" Stretch="Fill"></ImageBrush>
            </Border.Background>
        </Border>
        <Grid x:Name="GameBG" Grid.Row="2" Grid.Column="1" Margin="35,35,35,35">
            <Grid.Background>
                <ImageBrush ImageSource="img/main.png"></ImageBrush>
            </Grid.Background>
        </Grid>

        <StackPanel Grid.Row="1" Grid.Column="1" >

            <StackPanel.Background>
                <ImageBrush ImageSource="img/menu2.png"></ImageBrush>
            </StackPanel.Background>

        </StackPanel>
        <StackPanel Grid.Row="1" Grid.Column="1" x:Name="df">
            <Label Margin="31,30,261.333,18" RenderTransformOrigin="-2.489,0.556" x:Name="defeng" Height="33"/>
        </StackPanel>
        <StackPanel Grid.Row="1" Grid.Column="1" x:Name="tm">
            <Label Margin="293,30,31.333,18" RenderTransformOrigin="-2.489,0.556" x:Name="time" Height="33"/>
        </StackPanel>
    </Grid>
</Window>

xaml.cs源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WPF编程
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //初级
        int rows = 10;
        int[,] mines;
        Image[,] images;
        bool[,] overList;
        int mineCount = 10;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < rows; i++)
            {
                //10*10
                //行数
                RowDefinition row = new RowDefinition();
                GameBG.RowDefinitions.Add(row);
                //列数
                ColumnDefinition column = new ColumnDefinition();
                GameBG.ColumnDefinitions.Add(column);


                //得分
                defeng.Foreground = new LinearGradientBrush(Colors.Pink, Colors.Red, 0);
                //defeng.Background = Brushes.Cyan;
                defeng.Content = "得分为:" + num + "分";
                defeng.FontFamily = new FontFamily("楷体");
                defeng.FontSize = 25;

                //时间
                shitimer.Interval = TimeSpan.FromSeconds(1);
                shitimer.Tick += Shitimer_Tick;
                time.Foreground = new LinearGradientBrush(Colors.Pink, Colors.Red, 0);
                //defeng.Background = Brushes.Cyan;
                time.Content = "时间:" + time1/10 + "秒";
                time.FontFamily = new FontFamily("楷体");
                time.FontSize = 25;
              
                

            }
            GameBG.ShowGridLines = true;
            //lnitialGame();
        }
        //倒计时
        private void Shitimer_Tick(object sender, EventArgs e)
        {
            time1++;
            time.Content = "时间:" + time1/10 + "秒";
           
        }

        // 倒计时定时器
        DispatcherTimer shitimer = new DispatcherTimer();
        //倒数的盒子
        Label daoshu = new Label();
        int time1 = 0;
        int num = 0;
        //随机数
        Random r = new Random();

        //初级
        private void lnitialGame()
        {
            //把每个网格的行列放在mines里面
            mines = new int[rows, rows];
            //给每个图片添加网格
            images = new Image[rows, rows];
            //bool false
            overList = new bool[rows, rows];
            //二维数组中每一个元素默认值为0,如果是0则证明没有雷,如果是1则证明有雷
            //初始化雷
            for (int i = 0; i < mineCount; i++)//循环十次的炸弹
            {
                //随机为0代表行,1代表列
                int row = r.Next(0, mines.GetLength(0));
                
                int colmun = r.Next(0,mines.GetLength(1));
                //标记=1,这时候就是1
                if (mines[row, colmun] == 1)
                {
                    i--;
                }
                else
                {
                    mines[row,colmun] =1;
                }
            }
            //初始情况下所有的lmage全部装砖
            for (int i = 0; i < images.GetLength(0); i++)//控制行
            {
                for (int j = 0; j <images.GetLength(1); j++)//控制列
                {
                    //实例化图片
                    images[i,j] = new Image();
                    //给位置添加图片
                    images[i,j].Source = new BitmapImage(new Uri("img/zhuan.gif",UriKind.Relative));
                    //格子的间距
                    images[i,j].Margin = new Thickness(2,2,2,2);
                    Grid.SetRow(images[i,j],i);
                    Grid.SetColumn(images[i,j],j);
                    //tag数字代表
                    images[i, j].Tag = new int[2] { i, j };
                    //添加到GameBG
                    GameBG.Children.Add(images[i,j]);
                    //左键事件
                    images[i,j].MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
                   //右键点击事件
                    images[i,j].MouseRightButtonDown += MainWindow_MouseRightButtonDown;
                    
                }
            }
        }
        //中级
        private void lnitialGame1()
        {
          
            //把每个网格的行列放在mines里面
            mines = new int[rows, rows];
            //给每个图片添加网格
            images = new Image[rows, rows];
            //bool false
            overList = new bool[rows, rows];
            //二维数组中每一个元素默认值为0,如果是0则证明没有雷,如果是1则证明有雷
            //初始化雷
            for (int i = 0; i < mineCount; i++)//循环十次的炸弹
            {
                //随机为0代表行,1代表列
                int row = r.Next(0, mines.GetLength(0));

                int colmun = r.Next(0, mines.GetLength(1));
                //标记=1,这时候就是1
                if (mines[row, colmun] == 1)
                {
                    i--;
                }
                else
                {
                    mines[row, colmun] = 1;
                }
            }
            //初始情况下所有的lmage全部装砖
            for (int i = 0; i < images.GetLength(0); i++)//控制行
            {
                for (int j = 0; j < images.GetLength(1); j++)//控制列
                {
                    //实例化图片
                    images[i, j] = new Image();
                    //给位置添加图片
                    images[i, j].Source = new BitmapImage(new Uri("img/zhuan.gif", UriKind.Relative));
                    //格子的间距
                    images[i, j].Margin = new Thickness(2, 2, 2, 2);
                    Grid.SetRow(images[i, j], i);
                    Grid.SetColumn(images[i, j], j);
                    //tag数字代表
                    images[i, j].Tag = new int[2] { i, j };
                    //添加到GameBG
                    GameBG.Children.Add(images[i, j]);
                    //左键事件
                    images[i, j].MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
                    //右键点击事件
                    images[i, j].MouseRightButtonDown += MainWindow_MouseRightButtonDown;

                }
            }
        }

        //右键按下
        private void MainWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //旗子
            Image img = sender as Image;
            //旗子的图片
            img.Source = new BitmapImage(new Uri("img/qizi.gif",UriKind.Relative));

        }
        //左键按下
        private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //触发者,那个在局部,要用触发者
            Image img = sender as Image;
            //获取图片的Tag值 x,y
            int[] index = img.Tag as int[];
            //点击的这个是一个安全地区
            if (!IsMine(index[0], index[1]))//true 返回不正确的时候进去,雷数
            {
                //要判断周围的地雷数,基本上所有的雷周围至少有3个,最多有8个相邻
                //假设当前这个是i,j,则需要考虑周围的(i-1,j),(i-1,j-1),(i,j-1),(i+1,j-1),(i+1,j),(i+1,j+1),(i,j+1),(i-1,j+1)
                //int count = CountMine(index[0], index[1]);

                CountMine(index[0], index[1], img);//放雷的数
                int left = 0;//剩余数量

                for (int i = 0; i < overList.GetLength(0); i++)//默认值false
                {
                    for (int j = 0; j < overList.GetLength(1); j++)//默认值false
                    {
                        //统计剩下的false数量,格子
                        if (overList[i, j] == false)
                        {
                            left++;
                        }
                    }
                }
                if (left == mineCount)//mineCount炸弹数量等于等于剩余的
                {
                    MessageBox.Show("获胜");
                }
            }
            else
            {
                for (int i = 0; i < mines.GetLength(0); i++)
                {
                    for (int j = 0; j < mines.GetLength(1); j++)
                    {
                        if (mines[i, j] == 1)
                        {
                            //所有的雷显示
                            images[i, j].Source = new BitmapImage(new Uri("img/lei.gif", UriKind.Relative));
                        }
                    }
                }
                //弹框弹出失败
                MessageBox.Show("失败!");
                shitimer.Stop();
                time.Content = "时间:" + 0+ "秒";
                defeng.Content = "得分为:" + 0 + "分";
                GameBG.Children.Clear();
            }
        }

        bool IsMine(int i, int j)//默认值是false
        {
            
            if (i < 0 || j < 0 || i >= mines.GetLength(0) || j >= mines.GetLength(1))
            {
                return false;
            }
            else
            {
                //返回雷是正确的
                if (mines[i, j] == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //判断8个方向有没有雷
        void CountMine(int i,int j, Image img)
        {
            
            if (overList[i, j] == true)
            {
                return;
            }
            //点击后修改为true
            overList[i, j] = true;
            //用一个数定义周围的雷数
            int count = 0; 
            //左
            if (IsMine(i - 1, j))
            {
                //是炸弹+1
                count++;
            }
            //左上
            if (IsMine(i - 1, j - 1))
            {
                count++;
            }
            //上
            if (IsMine(i, j - 1))
            {
                count++;
            }
            //右上
            if (IsMine(i + 1, j - 1))
            {
                count++;
            }
            //右
            if (IsMine(i + 1, j))
            {
                count++;
            }
            //右下
            if (IsMine(i + 1, j + 1))
            {
                count++;
            }
           //下
            if (IsMine(i, j + 1))
            {
                count++;
            }
           //左下
            if (IsMine(i - 1, j + 1))
            {
                count++;
            }
            if (count == 0)
            {
                GameBG.Children.Remove(img);
                if (i > 0)
                {
                    CountMine(i - 1, j, images[i - 1, j]);
                }
                if (j > 0)
                {
                    CountMine(i, j - 1, images[i, j - 1]);
                }
                if (i > 0 && j > 0)
                {
                    CountMine(i - 1, j - 1, images[i - 1, j - 1]);
                }
                if (i < mines.GetLength(0) - 1)
                {
                    CountMine(i + 1, j, images[i + 1, j]);
                }
                if (j < mines.GetLength(1) - 1)
                {
                    CountMine(i, j + 1, images[i, j + 1]);
                }
                if (i > 0 && j < mines.GetLength(1) - 1)
                {
                    CountMine(i - 1, j + 1, images[i - 1, j + 1]);
                }
                if (i < mines.GetLength(0) - 1 && j < mines.GetLength(1) - 1)
                {
                    CountMine(i + 1, j + 1, images[i + 1, j + 1]);
                }
                if (i < mines.GetLength(0) - 1 && j > 0)
                {
                    CountMine(i + 1, j - 1, images[i + 1, j - 1]);
                }
               
            }
            else
            {
                //满足条件是把他周围的照片移除
                GameBG.Children.Remove(img);
                Label lb = new Label();
                //文本,count就是记录的雷数
                lb.Content = count.ToString();
                //位置
                Grid.SetColumn(lb, j);
                Grid.SetRow(lb, i);
                lb.FontSize = 15;
                //居中
                lb.HorizontalContentAlignment = HorizontalAlignment.Center;
                lb.VerticalContentAlignment = VerticalAlignment.Center;
                GameBG.Children.Add(lb);
                num++;
                defeng.Content = "得分为:" + num + "分";

            }
        }
        //退出
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
      
        //初级
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            lnitialGame();
            shitimer.Start();
        }

运行结果:
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!