c#.NET之WPF扫雷游戏(登录+游戏+最终得分)

登录界面设计:c#.NET之WPF扫雷游戏(登录+游戏+最终得分)_第1张图片
xaml代码:

<Window x:Class="扫雷.dl"
        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:扫雷"
        mc:Ignorable="d"
        Title="扫雷"   WindowStartupLocation="CenterScreen"  WindowStyle="None"  Width="600" Height="600" Loaded="Window_Loaded">
    <Canvas Width="600" Height="600">
        <Image Source="img/bg.jpg" Width="600" Height="600" Stretch="Fill"/>
        <Button Content="开始游戏" FontSize="30" Background="Blue" Canvas.Left="200" Canvas.Top="315" Width="166" Height="47" Click="Button_Click"/>
        <Button Content="退出游戏" FontSize="30" Background="OrangeRed" Canvas.Left="200" Canvas.Top="365" Width="166" Height="48" Click="Button_Click_1"/>

        <Canvas Height="45" Canvas.Left="200" Canvas.Top="225" Width="166" Background="White"/>
        <TextBox Name="a" Width="166" Canvas.Left="200" Canvas.Top="225" Height="45" FontSize="15">
            <TextBox.Resources>
                <VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Center" AlignmentY="Center">
                    <VisualBrush.Visual>
                        <TextBlock FontStyle="Italic" Text="玩家请输入用户名"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </TextBox.Resources>
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Style.Triggers>
                        <Trigger Property="Text" Value="{x:Null}">
                            <Setter Property="Background" Value="{StaticResource HintText}"/>
                        </Trigger>
                        <Trigger Property="Text" Value="">
                            <Setter Property="Background" Value="{StaticResource HintText}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <Button Name="zhuce" FontSize="30" Content="注册" Canvas.Left="200" Canvas.Top="270" Width="166" Height="45" Background="Blue" Click="zhuce_Click"/>
    </Canvas>
</Window>

cs代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.Shapes;

namespace 扫雷
{
    /// 
    /// dl.xaml 的交互逻辑
    /// 
    public partial class dl : Window
    {
        public dl()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (zhuce.Content.ToString()=="注册成功")
            {
                MainWindow game = new MainWindow();
                this.Hide();
                game.Show();
                FileStream file = new FileStream("../../defen.txt",FileMode.Append,FileAccess.Write);
                StreamWriter writer = new StreamWriter(file);
                writer.Write("|"+a.Text);
                writer.Close();
            }
            else
            {
                MessageBox.Show("请确保您已经注册名字");
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        bool aa = true;
        private void zhuce_Click(object sender, RoutedEventArgs e)
        {
            string patten = @"^\d+$";
            while (aa)
            {
                if (string.IsNullOrWhiteSpace(a.Text))
                {
                    MessageBox.Show("玩家的姓名不能为空,请重新输入");
                    a.Text = "";
                }
                else if (Regex.IsMatch(a.Text, patten))
                {
                    MessageBox.Show("玩家的姓名不能是纯数字,请重新输入");
                    a.Text = "";
                }
                else
                {
                    aa = false;
                }
                break;
            }
           
            if (aa == false)
            {
                zhuce.Content = "注册成功";
            }
        } 
    }
}

游戏界面:
c#.NET之WPF扫雷游戏(登录+游戏+最终得分)_第2张图片
xaml代码:

<Window x:Class="扫雷.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:扫雷"
        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="123"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="35"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="35"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
            <MenuItem Width="90" Height="30" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                <MenuItem.Icon>
                    <Image Source="img/Sysico.ico" Margin="0"></Image>
                </MenuItem.Icon>
                <MenuItem.Header>
                    <Label Content="菜单" Margin="0,0,0,0"></Label>
                </MenuItem.Header>
                <MenuItem Header="等级" Margin="0,0,-6,0">
                    <MenuItem.Icon>
                        <Image Source="img/CustomerOrder.ico"></Image>
                    </MenuItem.Icon>
                    <MenuItem Header="初级" Click="MenuItem_Click_1">
                        <MenuItem.Icon >
                            <Image Source="img/brick.png" Width="10" Height="10"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="中级" Click="MenuItem_Click_2">
                        <MenuItem.Icon >
                            <Image Source="img/grass.png" Width="10" Height="10"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="高级" Click="MenuItem_Click_3">
                        <MenuItem.Icon >
                            <Image Source="img/iron.png" Width="10" Height="10"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                </MenuItem>
                <MenuItem Header="设置" Margin="0,0,-6,0">
                    <MenuItem.Icon>
                        <Image Source="img/mrp.ico" Width="20" Height="20"></Image>
                    </MenuItem.Icon>
                    <MenuItem Header="背景" Click="MenuItem_Click_1">
                        <MenuItem.Icon >
                            <Image Source="img/brick.png" Width="10" Height="10"></Image>
                        </MenuItem.Icon>
                        <MenuItem Header="春" Click="MenuItem_Click_1">
                            <MenuItem.Icon >
                                <Image Source="img/brick.png" Width="10" Height="10"></Image>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="夏" Click="MenuItem_Click_1">
                            <MenuItem.Icon >
                                <Image Source="img/brick.png" Width="10" Height="10"></Image>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="秋" Click="MenuItem_Click_1">
                            <MenuItem.Icon >
                                <Image Source="img/brick.png" Width="10" Height="10"></Image>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="冬" Click="MenuItem_Click_1">
                            <MenuItem.Icon >
                                <Image Source="img/brick.png" Width="10" Height="10"></Image>
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                </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" Name="bg">
            <Border.Background>
                <ImageBrush ImageSource="img/bg.jpg" Stretch="Fill"></ImageBrush>
            </Border.Background>
        </Border>
        <Grid x:Name="GameBG" Grid.Row="2" Grid.Column="1" Margin="43,60,27,10">
            <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>
            <Label Content="0s" Margin="47,40,395,50" FontSize="30" x:Name="lab" Height="42" RenderTransformOrigin="0.506,0.643"/>
        </StackPanel>
        <Label Content="通关关数为:0" HorizontalAlignment="Left" Margin="126,51,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0.525,0.231" Grid.Column="1" FontSize="20" Name="lab1"/>
    </Grid>
</Window>

cs代码:

using System;
using System.Collections.Generic;
using System.IO;
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 扫雷
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //网格线数量
        int rows = 10;
        //雷
        int[,] mines;
        //图片
        Image[,] images;
        //旁边的砖是否被翻过  相当于递归退出条件
        bool[,] overList;
        //雷的数量
        int mineCount = 3;
        Random r = new Random();
        DispatcherTimer time;
        int num = 0;
        int guanshu = 0;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {    //布局网格
            Creatmap(rows);
            InitialGame(rows,mineCount);
        }

        private void Creatmap(int rows)
        {
            for (int i = 0; i < rows; i++)
            {
                RowDefinition row = new RowDefinition();
                GameBG.RowDefinitions.Add(row);
                ColumnDefinition column = new ColumnDefinition();
                GameBG.ColumnDefinitions.Add(column);
            }
            GameBG.ShowGridLines = true;
            time = new DispatcherTimer();
            time.Interval = TimeSpan.FromMilliseconds(1000);
            time.Tick += Time_Tick;
        }

        private void Time_Tick(object sender, EventArgs e)
        {
            num += 1;
            lab.Content = num + "s";

            FileStream file2 = new FileStream("../../defen.txt",FileMode.Open,FileAccess.Read);
            StreamReader reader = new StreamReader(file2);
            string str = reader.ReadLine();
            string[] strlist = str.Split(new char[] { '|' });
            int len = strlist.Length;
            lab1.Content = strlist[len - 1] + "通关分数为:" + guanshu;
            reader.Close();
        }

        //初始化地图
        private void InitialGame(int rows,int mineCount)
        {
            mines = new int[rows, rows];
            images = new Image[rows, rows];
            overList = new bool[rows, rows];
            //二维数组中每个元素默认值是0,如果是0的话证明没有雷,如果是1的话证明有雷
            //初始化雷
            for (int i = 0; i < mineCount; i++)
            {
                int row = r.Next(0, mines.GetLength(0));
                int colmun = r.Next(0, mines.GetLength(1));
                //判断是否已经有雷 有的话重新随机
                if (mines[row, colmun] == 1)
                {
                    i--;
                }
                else
                {
                    mines[row, colmun] = 1;
                }
            }
            //初始情况下所有的Image全部是砖的图片
            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);
                    images[i, j].Width = images[i, j].Height = 30;
                    Grid.SetRow(images[i, j], i);
                    Grid.SetColumn(images[i, j], j);
                    images[i, j].Tag = new int[2] { i, j };
                    GameBG.Children.Add(images[i, j]);
                    //鼠标左键点击事件
                    images[i, j].MouseLeftButtonDown += Img_MouseLeftButtonDown;
                    //鼠标右键点击事件
                    images[i, j].MouseRightButtonDown += Img_MouseRightButtonDown;
                }
            }
        }
        //鼠标左键点击
        private void Img_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            //点击右键换成旗子图片
            img.Source = new BitmapImage(new Uri("img/qizi.gif", UriKind.Relative));
            time.Start();
        }
        //鼠标点击左键事件
        private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            time.Start(); 
            Image img = sender as Image;
            int[] index = img.Tag as int[];
            //点击的这个是一个安全地区
            //要判断周围的地雷数,基本上所有的雷周围至少有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)
            if (!IsMine(index[0], index[1]))
            {
                CountMine(index[0], index[1], img);
                //剩余雷的数量
                int left = 0;
                for (int i = 0; i < overList.GetLength(0); i++)
                {
                    for (int j = 0; j < overList.GetLength(1); j++)
                    {
                        if (overList[i, j] == false)
                        {
                            left++;
                        }
                    }
                }
                if (left == mineCount)
                {
                        guanshu++;
                        time.Stop();
                    MessageBoxResult rlt = MessageBox.Show("通关啦,游戏结束", "Game,Over", MessageBoxButton.OKCancel, MessageBoxImage.Question);
                    if (rlt==MessageBoxResult.OK)
                    {
                        GameBG.ColumnDefinitions.Clear();
                        GameBG.RowDefinitions.Clear();
                        GameBG.Children.Clear();
                        rows++;
                        mineCount += 1;
                        Creatmap(rows);
                        InitialGame(rows,mineCount);
                        num = 0;
                        lab.Content = "0s";
                        
                    }
                    else
                    {
                        this.Hide();
                        Cj cj = new Cj();
                        cj.Show();
                        FileStream file3 = new FileStream("../../defen.txt",FileMode.Append,FileAccess.Write);
                        StreamWriter writer = new StreamWriter(file3);
                        writer.Write("|" + ":" + lab1.Content + "分" + ":" + DateTime.Now.ToString("MM月dd日 dddd HH:mm:ss"));
                        writer.Close();
                    }
                }
            }
            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));
                        }
                    }
                }
                    time.Stop();
                MessageBoxResult  re = MessageBox.Show("踩到雷了,游戏结束","Game,Over",MessageBoxButton.OKCancel,MessageBoxImage.Question);
                if (re==MessageBoxResult.OK)
                {
                    GameBG.RowDefinitions.Clear();
                    GameBG.ColumnDefinitions.Clear();
                    GameBG.Children.Clear();
                    Creatmap(rows);
                    InitialGame(rows,mineCount);
                    num = 0;
                    lab.Content = "0s";
                }
                else
                {
                    this.Hide();
                    Cj cj = new Cj();
                    cj.Show();
                    FileStream file3 = new FileStream("../../defen.txt", FileMode.Append, FileAccess.Write);
                    StreamWriter writer = new StreamWriter(file3);
                    writer.Write("|" + ":" + lab1.Content + "分" + ":" + DateTime.Now.ToString("MM月dd日 dddd HH:mm:ss"));
                    writer.Close();
                }
            }
        }
        //判断是不是雷
        bool IsMine(int i, int j)
        {
            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;
                }
            }
        }

        void CountMine(int i, int j, Image img)
        {
            //如果被判断过,就返回   没有这个条件程序会进入死循环,两个之间一直进行相互判断
            if (overList[i, j] == true)
            {
                return;
            }
            overList[i, j] = true;
            //雷的数量,判断周围8个方向,有雷数字+1
            int count = 0;
            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 (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();
                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);
            }
        }
        //点击退出
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //初
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            GameBG.RowDefinitions.Clear();
            GameBG.ColumnDefinitions.Clear();
            GameBG.Children.Clear();
            Creatmap(8);
            InitialGame(8, 10);
            this.Width = 600;
            this.Height = 600;
            num = 0;
            lab.Content = num + "s";
        }
        //中
        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            GameBG.RowDefinitions.Clear();
            GameBG.ColumnDefinitions.Clear();
            GameBG.Children.Clear();
            Creatmap(12);
            InitialGame(12, 20);
            this.Width = 600;
            this.Height = 620;
            num = 0;
            lab.Content = num + "s";
        }
        //高
        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            GameBG.RowDefinitions.Clear();
            GameBG.ColumnDefinitions.Clear();
            GameBG.Children.Clear();
            Creatmap(15);
            InitialGame(15, 30);
            this.Width = 800;
            this.Height = 670;
            num = 0;
            lab.Content = num + "s";
        }
    }
}

成绩xaml代码:

<Window x:Class="扫雷.Cj"
        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:扫雷"
        mc:Ignorable="d"
        Title="Cj" Height="450" Width="800" Name="chengji" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" AllowsTransparency="True" Background="Transparent" Loaded="chengji_Loaded" KeyDown="chengji_KeyDown">
    <Canvas x:Name="canv">
        
    </Canvas>
</Window>

cs代码;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
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.Shapes;
using System.Windows.Threading;

namespace 扫雷
{
    /// 
    /// Cj.xaml 的交互逻辑
    /// 
    public partial class Cj : Window
    {
        public Cj()
        {
            InitializeComponent();
        }
        //重新开始或退出游戏按钮
        Button btn1 = new Button();
        Button btn2 = new Button();
        //查看成绩
        Button btn3 = new Button();
        //胜负,比分
        TextBox txt1 = new TextBox();
        //历史记录
        TextBox txt2 = new TextBox();
        //计时器
        //DispatcherTimer timer = new DispatcherTimer();
        Dictionary<string, int> lt = new Dictionary<string, int>();
        private void chengji_Loaded(object sender, RoutedEventArgs e)
        {
            canv.Width = this.Width;
            canv.Height = this.Height;
            canv.Background = new RadialGradientBrush(Colors.Yellow, Colors.Blue);

            txt1.Width = this.Width / 2 - 100;
            txt1.Height = 600;
            txt1.Opacity = 0.7;
            txt1.BorderBrush = Brushes.Black;
            txt1.FontFamily = new FontFamily("楷体");
            txt1.FontSize = 40;
            txt1.TextWrapping = TextWrapping.Wrap;
            txt1.TextAlignment = TextAlignment.Center;
            txt1.Text += "\n" + "本次扫雷情况" + "\n" + "\n";
            Canvas.SetLeft(txt1, 100);
            Canvas.SetTop(txt1, 50);
            canv.Children.Add(txt1);

            txt2.Width = this.Width / 2 - 100;
            txt2.Height = 600;
            txt2.Opacity = 0.7;
            txt2.BorderBrush = Brushes.Black;
            txt2.FontFamily = new FontFamily("楷体");
            txt2.TextWrapping = TextWrapping.Wrap;
            txt2.FontSize = 40;
            txt2.TextAlignment = TextAlignment.Center;
            Canvas.SetLeft(txt2, this.Width - txt2.Width - 100);
            Canvas.SetTop(txt2, 50);
            canv.Children.Add(txt2);
            txt2.Text += "历史前10得分榜:" + "\n";

            btn1.Width = 200;
            btn1.Height = 60;
            btn1.FontFamily = new FontFamily("楷体");
            btn1.FontSize = 30;
            btn1.Background = Brushes.Orange;
            btn1.Content = "再玩一次";
            Canvas.SetLeft(btn1, this.Width / 2 - btn1.Width - 20);
            Canvas.SetTop(btn1, this.Height - 120);
            canv.Children.Add(btn1);
            btn1.Click += Btn1_Click;

            btn2.Width = 200;
            btn2.Height = 60;
            btn2.FontFamily = new FontFamily("楷体");
            btn2.FontSize = 30;
            btn2.Background = Brushes.Orange;
            btn2.Content = "退出游戏";
            Canvas.SetLeft(btn2, this.Width / 2 + 50);
            Canvas.SetTop(btn2, this.Height - 120);
            canv.Children.Add(btn2);
            btn2.Click += Btn2_Click;
           
            btn3.Width = 200;
            btn3.Height = 60;
            btn3.FontFamily = new FontFamily("楷体");
            btn3.FontSize = 30;
            btn3.Background = Brushes.Orange;
            btn3.Content = "查看成绩";
            Canvas.SetLeft(btn3, this.Width / 2 - btn1.Width / 2);
            Canvas.SetTop(btn3, this.Height - 190);
            canv.Children.Add(btn3);
            btn3.Click += Btn3_Click;
        }

        private void Btn3_Click(object sender, RoutedEventArgs e)
        {
            if (txt2.Text == "历史前10得分榜:" + "\n")
            {
                FileStream file = new FileStream("../../defen.txt", FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(file);
                string str = reader.ReadLine();
                string[] strlist = str.Split(new char[] { '|' });
                int len = strlist.Length;
                string a = strlist[len - 1];
                string[] alist = a.Split(new char[] { ':' });

                txt1.Text += alist[1] + ":" + alist[2] + "\n" + alist[3] + ":" + alist[4] + ":" + alist[5] + "\n" + "\n";



              /*  for (int i = 1; i < strlist.Length; i++)
                {
                    string[] alllist = strlist[i].Split(new char[] { ':' });

                    lt.Add(alllist[1] + alllist[3].Remove(0, 10) + ":" + alllist[4] + ":" + alllist[5], Convert.ToInt32(alllist[2].Remove(alllist[2].Length - 1, 1)));
                }
                var dicSort = from objDic in lt orderby objDic.Value descending select objDic;
                int p = 0;
                foreach (KeyValuePair kvp in dicSort)
                {
                    p++;
                    if (p < 11)
                    {
                        txt2.Text += "第" + p + "名:" + kvp.Key + "   " + kvp.Value + "分" + "\n";
                    }
                    reader.Close();
                }*/
            }
        }

        private void Btn2_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            dl jm = new dl();
            this.Hide();
            jm.Show();
        }

        private void chengji_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key==Key.Escape)
            {
                this.WindowState = WindowState.Minimized;
            }
        }
    }
}

你可能感兴趣的:(c#.NET之WPF扫雷游戏(登录+游戏+最终得分))