游戏介绍:

此游戏共分为四关,新手,初级,中级,高级
通过改变格子数和雷数来进行跳关;
新手:10*10格子 10颗雷
初级:12*12格子 12颗雷
中级: 14*14格子 14颗雷
高级:16*16格子 15颗雷

设计界面

<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="WPF扫雷" Icon="../../img/logo.ico" Height="800" Width="800" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"><Grid><Grid.RowDefinitions><RowDefinition Height="30"></RowDefinition><RowDefinition Height="100"></RowDefinition><RowDefinition Height="121*"></RowDefinition><RowDefinition Height="519*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="0"></ColumnDefinition><ColumnDefinition Width="10.8"/><ColumnDefinition Width="54"/><ColumnDefinition Width="31*"></ColumnDefinition><ColumnDefinition Width="135*"/><ColumnDefinition Width="65"></ColumnDefinition></Grid.ColumnDefinitions><Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" Margin="0,0,-0.4,0.4"><MenuItem Width="60" Height="30" HorizontalAlignment="Center" VerticalAlignment="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,0,0" ><MenuItem.Icon><Image Source="img/CustomerOrder.ico"></Image></MenuItem.Icon><MenuItem Header="新手" Name="Noob"><MenuItem.Icon><Image Source="img/small.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="初级" Name="Chuji"><MenuItem.Icon><Image Source="img/logo-one.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="中级" Name="Zhoji"><MenuItem.Icon><Image Source="img/logo-two.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="高级" Name="Gaoji"><MenuItem.Icon><Image Source="img/logo-fore.ico"></Image></MenuItem.Icon></MenuItem></MenuItem><MenuItem Header="排名" Margin="0,0,0,0"  Click="MenuItem_Click_1"></MenuItem><MenuItem Header="设置" Margin="0,0,0,0"><MenuItem.Icon><Image Source="img/mrp.ico"></Image></MenuItem.Icon><MenuItem Header="改变背景图"><MenuItem.Icon><Image Source="img/logo-one.ico"></Image></MenuItem.Icon><MenuItem Header="场景一" Name="photo_1"><MenuItem.Icon><Image Source="img/logo-one.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="场景二" Name="photo_2"><MenuItem.Icon><Image Source="img/logo-two.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="场景三" Name="photo_3"><MenuItem.Icon><Image Source="img/logo-treen.ico"></Image></MenuItem.Icon></MenuItem><MenuItem Header="场景四" Name="photo_4"><MenuItem.Icon><Image Source="img/logo-fore.ico"></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.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="6" Margin="0,30,-0.4,0.4"><Border.Background><ImageBrush ImageSource="img/bg.jpg" Stretch="Fill" x:Name="Bg" ></ImageBrush></Border.Background><Canvas/></Border><Grid Name="GameBG" Grid.Row="2" Grid.Column="3" Margin="35.2,34.6,34.8,35.4" Grid.RowSpan="2" Grid.ColumnSpan="2"><Grid.Background><ImageBrush ImageSource="img/main.png"></ImageBrush></Grid.Background></Grid><StackPanel x:Name="topBar" Grid.Column="3" Grid.RowSpan="2" Grid.ColumnSpan="3" Margin="0.2,30,64.6,0.4" ><StackPanel.Background><ImageBrush ImageSource="img/menu2.png"></ImageBrush></StackPanel.Background></StackPanel></Grid>
</Window>

主程序设计

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;
using System.IO;
namespace WPF扫雷
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}Random r = new Random();int rows = 10; // 格子数int[,] mines; // 格子Image[,] images;// 存储图片数组bool[,] overList;int mineCount = 10;// 雷数string Jude = "deflut";  // 判断关卡string JudeName="新手"; // 判断等级名int count = 0; // 倒计时参数Label Countdown = new Label(); // 倒计时对象Label Grade = new Label(); // 等级对象DispatcherTimer countTimer = new DispatcherTimer(); // 倒计时定时器private void Window_Loaded(object sender, RoutedEventArgs e){Noob.Click += Noob_Click;//点击新手Chuji.Click += Chuji_Click; // 点击初级Zhoji.Click += Zhoji_Click; // 点击中级Gaoji.Click += Gaoji_Click; // 点击高级photo_1.Click += Photo_1_Click;// 点击改变场景一photo_2.Click += Photo_2_Click;// 点击改变场景二photo_3.Click += Photo_3_Click;// 点击改变场景三photo_4.Click += Photo_4_Click;//点击改变场景四GeZi(10);// 格子数InitialGame(10, 10); // 格子数,雷数// 倒计时对象Countdown.Width = 120;Countdown.Height = 50;Countdown.Content = count+"s";Countdown.FontSize = 30;Countdown.Margin = new Thickness(100,20,100,10);topBar.Children.Add(Countdown);countTimer.Interval =TimeSpan.FromSeconds(1);countTimer.Tick += CountTimer_Tick;// 记录等级对象Grade.Width = 180;Grade.Height = 50;Grade.Content = "等级:" + JudeName;Grade.FontSize = 30;Grade.Margin = new Thickness(240,-60,50,10);topBar.Children.Add(Grade);    }private void CountTimer_Tick(object sender, EventArgs e){count++;Countdown.Content = count + "s";}private void Photo_1_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/one.png",UriKind.Relative));}private void Photo_2_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/two.png", UriKind.Relative));}private void Photo_3_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/treen.png", UriKind.Relative));}private void Photo_4_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/fore.png", UriKind.Relative));}private void GeZi(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; // 显示网格}// 新手private void Noob_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/bg.jpg", UriKind.Relative));GameBG.RowDefinitions.Clear(); // 清除行GameBG.ColumnDefinitions.Clear(); // 清除列GameBG.Children.Clear(); // 清除之前rows = 10;mineCount = 10;InitialGame(rows, mineCount); //格子数,雷数GeZi(10); // 格子数Jude = "Noob";JudeName = "新手";Grade.Content = "等级:" + JudeName;}// 初级private void Chuji_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/one.png", UriKind.Relative));GameBG.RowDefinitions.Clear(); // 清除行GameBG.ColumnDefinitions.Clear(); // 清除列GameBG.Children.Clear(); // 清除之前rows = 12;mineCount = 12;InitialGame(rows,mineCount); //格子数,雷数GeZi(12); // 格子数Jude = "chuJi";JudeName = "初级";Grade.Content = "等级:" + JudeName;}// 中级private void Zhoji_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/two.png", UriKind.Relative));GameBG.RowDefinitions.Clear(); // 清除行GameBG.ColumnDefinitions.Clear();// 清除列GameBG.Children.Clear();mineCount = 14;InitialGame(14, mineCount);// 格子数,雷数GeZi(14);Jude = "zhoJi";JudeName = "中级";Grade.Content = "等级:" + JudeName;}// 高级private void Gaoji_Click(object sender, RoutedEventArgs e){Bg.ImageSource = new BitmapImage(new Uri("../../img/treen.png", UriKind.Relative));GameBG.RowDefinitions.Clear();// 清除行GameBG.ColumnDefinitions.Clear();// 清除列GameBG.Children.Clear();rows = 16;mineCount = 15;InitialGame(rows,mineCount);// 格子数,雷数GeZi(16);Jude = "gaoJi";JudeName = "高级";Grade.Content = "等级:" + JudeName;}private void InitialGame(int rows, int mineCount){mines = new int[rows, rows]; // 10*10的格子 images = new Image[rows, rows];//图片初始位置overList = new bool[rows, rows];//每个元素默认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) //如果与上一次随机的雷位置变为0{i--;}else{mines[row, colmun] = 1;// 随机的位置设为1}}// 初始化砖块图片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));/*if (mines[i,j]==1){images[i, j].Source = new BitmapImage(new Uri("img/lei.gif", UriKind.Relative));}else{}*/images[i, j].Margin = new Thickness(2, 2, 2, 2);// 外边距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 +=  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)); // 设置为旗子图片}MessageBoxResult rest;private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){countTimer.Start();Image img = sender as Image;int[] index = img.Tag as int[];// 给雷图片添加Tag值 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) // 如果剩余雷数等于设置的雷数 则获胜{MessageBoxResult rse= MessageBox.Show("是否进入下一关","游戏胜利!");countTimer.Stop();if (rse==MessageBoxResult.OK){int row, lei;string ss = "等级:"+ JudeName + ";用时:"+count;FileStream Jilu = new FileStream("../../word/paiming.txt", FileMode.Append, FileAccess.Write);StreamWriter strWrit = new StreamWriter(Jilu);strWrit.WriteLine(ss + "\r\n");strWrit.Close();if (Jude.ToString() == "deflut"|| Jude.ToString() == "Noob") // 判断是否默认{MessageBox.Show("进入初级关卡");row = 12;lei = 15;gameAnew(row, lei);Jude = "chuJi";JudeName = "初级";Grade.Content = "等级:" + JudeName;Bg.ImageSource = new BitmapImage(new Uri("../../img/one.png", UriKind.Relative));}else if (Jude.ToString() == "chuJi") // 判断是否初级{MessageBox.Show("进入中级关卡");row = 14;lei = 18;gameAnew(row, lei);Jude = "zhoJi";JudeName = "中级";Grade.Content = "等级:" + JudeName;Bg.ImageSource = new BitmapImage(new Uri("../../img/two.png", UriKind.Relative));}else if (Jude.ToString() == "zhoJi")// 判断是否中级{MessageBox.Show("进入高级关卡");row = 16;lei = 25;gameAnew(row, lei);Jude = "gaoJi";JudeName = "高级";Grade.Content = "等级:" + JudeName;Bg.ImageSource = new BitmapImage(new Uri("../../img/treen.png", UriKind.Relative));}else if (Jude.ToString() == "gaoJi")// 判断是否高级{MessageBox.Show("进入最高级关卡");row = 18;lei = 30;gameAnew(row, lei);JudeName = "最高级";Grade.Content = "等级:" + JudeName;Bg.ImageSource = new BitmapImage(new Uri("../../img/fore.png", UriKind.Relative));}count = 0;countTimer.Start();}}}else // 点击到是雷{for (int i = 0; i < mines.GetLength(0); i++){for (int j = 0; j < mines.GetLength(1); j++){if (mines[i, j] == 1) // 如果此位置是1,切换雷图片{images[i, j].Source = new BitmapImage(new Uri("img/lei.gif", UriKind.Relative));}}}countTimer.Stop();// 倒计时暂停rest= MessageBox.Show("是否重新开始", "游戏结束");AnewPopup();}}private void AnewPopup() // 是否重新开始弹框{if (rest == MessageBoxResult.OK){count = 0;countTimer.Start();int row, lei;if (Jude.ToString() == "deflut"|| Jude.ToString() == "Noob") // 判断是否默认{JudeName = "新手";Grade.Content = "等级:" + JudeName;row = 10;lei = 10;gameAnew(row, lei);}if (Jude.ToString() == "chuJi") // 判断是否初级{JudeName = "初级";Grade.Content = "等级:" + JudeName;row = 12;lei = 12;gameAnew(row, lei);}if (Jude.ToString() == "zhoJi")// 判断是否中级{JudeName = "中级";Grade.Content = "等级:" + JudeName;row = 14;lei = 14;gameAnew(row, lei);}if (Jude.ToString() == "gaoJi")// 判断是否高级{JudeName = "高级";Grade.Content = "等级:" + JudeName;row = 16;lei = 15;gameAnew(row, lei);}}}private void gameAnew(int row,int lei) // 此关卡重新开始方法{GameBG.RowDefinitions.Clear(); // 清除游戏区行GameBG.ColumnDefinitions.Clear(); // 清除游戏区列GameBG.Children.Clear(); // 清除图片,和labelrows = row; // 重新赋给格子方法mineCount = lei; // 雷数GeZi(rows); //格子方法InitialGame(rows, mineCount);}// 判断是否是雷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;}}}/*111101111*/void CountMine(int i,int j,Image img) // 判断点击方块后,八个方向{if (overList[i,j]==true){return;}overList[i, j] = true;int count = 0; // 初始附近雷数if (IsMine(i-1,j)) // 点击的块左边{count++;}if (IsMine(i-1,j-1)) // 点击的块左下方{count++;}if (IsMine(i - 1, j + 1))// 左上方{count++;}if (IsMine(i,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 (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();   // 将获取的附近雷数 赋给lbGrid.SetColumn(lb, j);Grid.SetRow(lb, i);lb.FontSize = 15;lb.HorizontalContentAlignment = HorizontalAlignment.Center;lb.VerticalAlignment = 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){MessageBox.Show("");}}
}

WPF扫雷游戏(简略版)相关推荐

  1. 【180928】WPF扫雷游戏源码

    一.源码特点    采用WPF进行开发,实现了扫雷游戏的功能,看看你能否过关 二.功能介绍    本源码是一个WPF扫雷游戏源码,刚学WPF时做的,做的不是很好,请大家见谅,仅供参考. 三.菜单功能 ...

  2. java控制台扫雷_java实现扫雷游戏控制台版

    本文实例为大家分享了java实现扫雷游戏控制台版,供大家参考,具体内容如下 扫雷游戏 a.游戏的分析 在游戏中需要存在对象包含哪些. 格子对象(grid): 属性:内容(content).状态(typ ...

  3. java扫雷雷区的统计数据代码_java实现扫雷游戏控制台版

    本文实例为大家分享了java实现扫雷游戏控制台版,供大家参考,具体内容如下 扫雷游戏 a.游戏的分析 在游戏中需要存在对象包含哪些. 格子对象(Grid): 属性:内容(content).状态(typ ...

  4. 两款扫雷游戏3D版源码

    14号,不该胡说,今天锅真坏了,研究下怎么用茶壶煮水饺..... 这是两种类型的扫雷游戏,说是3D版其实还是2D版贴在了桌面上而已.有部分提示功能,无联机功能. 第一种类型是经典玩法,每个格子中的数字 ...

  5. 扫雷游戏网页版_做游戏,单人行动可行吗(3.让我试试扫雷)

    17年底,三国依然在线稳定运行.一个偶然的机会发现了一个网页的扫雷游戏(mienfield),是一个无限边界的扫雷游戏,全世界所有人玩一盘没有边界的扫雷.看着非常酷,我搜索了一下各大手机平台,没有同款 ...

  6. 【C语言】扫雷游戏——控制台版

    目录: 1.设计思路 1.1函数概要设计 1.2流程 2.函数实现 3.文件及函数布置 1.设计思路 1.1流程 扫雷游戏,首先让玩家知晓如何进入游戏,退出游戏,这里需要给玩家提示,设计一个菜单选项函 ...

  7. Java扫雷游戏代码

    实现扫雷游戏控制台版 扫雷游戏 a.游戏的分析 在游戏中需要存在对象包含哪些. 格子对象(Grid): 属性:内容(content).状态(type) b.工程架构 设计工程包结构 bean:存放实体 ...

  8. java开源游戏下载安装_开源java扫雷游戏,Swing版。

    开源java扫雷游戏,Swing版. xC6JnAIE.zip (60.07 KB) 开源java扫雷游戏,Swing版. 程序文件说明 文件 内容 备注 AboutFrame.java 程序&quo ...

  9. 游戏编程学Python(8)— 扫雷(文字版)

    通过游戏编程学Python(7)- 井字棋(下) 通过游戏编程学Python(7)- 井字棋(上) 通过游戏编程学Python(番外篇)- 单词小测验 前言 前一段时间扫雷游戏挺火的,可惜问哥没有赶上 ...

  10. 扫雷计算机教案,四年级上信息技术教案-游戏——扫雷辽师大版

    <四年级上信息技术教案-游戏--扫雷辽师大版>由会员分享,可在线阅读,更多相关<四年级上信息技术教案-游戏--扫雷辽师大版(2页珍藏版)>请在人人文库网上搜索. 1.第六课游戏 ...

最新文章

  1. linux tar打包大文件并分割传输另一台linux服务器
  2. Java内存模型与happens-before原则
  3. 移动端隐藏滚动条(最全面)
  4. bezier曲线_套娃成神:贝塞尔曲线
  5. ASP.NET Core 新建项目(Windows) - ASP.NET Core 基础教程 - 简单教程,简单编程
  6. 获取URL各项参数(Java)
  7. UI设计素材干货,字体设计灵感酷站
  8. suitecrm 如何backup and restore ,从一个server 转移到另一个 server . 并保证customer package , customer module 不丢...
  9. java于网络:P2P聊天系统
  10. 哪种销售方式更能深入人心?
  11. 荣耀30青春版怎么样?到手后远超预期!
  12. 欧氏空间距离和内积_欧式空间、内积空间和赋范空间之间的关系
  13. 集成redis,删除key报“srem“异常
  14. 如何将图片压缩到100K以内,教你几种免费方法
  15. java面向对象实验结论及心得_20162305 实验二 Java面向对象程序设计 实验报告
  16. SEO—搜索引擎优化
  17. 求职迷茫?看看这篇为你准备的求职指北吧!
  18. 再看《蜂蜜与四叶草》
  19. 8-9(CCPD车牌数据集)
  20. 火车票退票费计算,2013年起,火车票退票费比例下调:票面乘车站开车时间前48小时以上的按票价5%计退票费。同时,车票退票费的计算方法不再四舍五入到元为单位,而以5角为单位:尾数小于0.25元的舍去

热门文章

  1. Win10系统winload.efi丢失或损坏怎么办?修复步骤(以联想笔记本为例)
  2. 黑苹果efi文件_台式机华硕主板黑苹果EFI引导文件分享amp;2020.12.2
  3. Mysql授权远程登陆
  4. 【CSDN软件工程师能力认证学习精选】Python网络编程之初识
  5. 蓝牙音箱连接成功但没有声音还是电脑的声音
  6. windows下载安装adb(极其简单)
  7. 主成分分析R语言实现
  8. PC批量转换网易ncm音乐
  9. 蓝牙AVRCP协议解析
  10. MATLAB-画图汇总