博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf版扫雷游戏
阅读量:5115 次
发布时间:2019-06-13

本文共 17610 字,大约阅读时间需要 58 分钟。

 

近来觉得wpf做出来的界面很拉风,自己也很喜欢搞些小游戏,感觉这做出来的会很炫,很装逼,(满足自己的一点小小的虚荣心)于是就去自学,发现感觉很不错,可是属性N多,太多了,而且质料也少,很多不会用,只会写基本的操作,样式直接百度黏贴在自己改改,于是属于自己的扫雷就出来了,也只能做这等游戏了,用的知识少,代码也不多,wpf属性真是多啊,不过还是得学啊,下面也没什么好说的了,贴代码,扫雷也就一个递归而已,而且vs真心的开发神器啊

XAML

1 
5
6 7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
View Code

后台代码

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;  6 using System.Windows;  7 using System.Windows.Controls;  8 using System.Windows.Data;  9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Animation; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16  17 namespace SweepMineUI 18 { 19     ///  20     /// MainWindow.xaml 的交互逻辑 21     ///  22     public partial class MainWindow : Window 23     { 24         private int[] SweepNum = null;//雷集合索引 25         private int GNum { get; set; }//挖雷工具数量 26         private bool BeginWa = false;//是否执行挖雷 27         private Storyboard Storyboard { get; set; } 28         public MainWindow() 29         { 30             InitializeComponent(); 31             LoadSweepArray(5); 32             CreateButton(10, 10); 33         } 34         ///  35         /// 初始化雷的数量 36         ///  37         /// 雷的数量 38         private void LoadSweepArray(int Length)  39         { 40             SweepNum = new int[Length]; 41             for (int i = 0; i < SweepNum.Length;i++ ) 42             { 43                 SweepNum[i] = -1; 44             } 45         } 46         private void btnInitial_Click(object sender, RoutedEventArgs e) 47         { 48             LoadSweepArray(5); 49             CreateButton(10, 10); 50         } 51         private void btnCenter_Click(object sender, RoutedEventArgs e)  52         { 53             LoadSweepArray(10); 54             CreateButton(12,12); 55         } 56         private void btnSenior_Click(object sender, RoutedEventArgs e) 57         { 58             LoadSweepArray(15); 59             CreateButton(14, 14); 60         } 61         private void btnEnd_Click(object sender, RoutedEventArgs e) 62         { 63             LoadSweepArray(20); 64             CreateButton(15, 15); 65         } 66         private void btnZB_Click(object sender, RoutedEventArgs e) 67         { 68             if (txtKL.Text == "0302") 69                 btn_ClickZB(); 70             else 71                 MessageBox.Show("口令无效"); 72         } 73         private void btnExit_Click(object sender, RoutedEventArgs e) 74         { 75             this.Close(); 76         } 77         private void btn_ClickZB()  78         { 79             foreach(Control c in canSweep.Children) 80             { 81                 if(c is Button) 82                 { 83                     Button b = c as Button; 84                     if(b.IsEnabled && b.Content.ToString() == "") 85                         b.Content = b.Tag; 86                 } 87             } 88         } 89         ///  90         /// 查找游戏区内所有未挖的雷的数量 91         ///  92         /// 
雷的数量
93 private int GetSweepCount() 94 { 95 int count = 0; 96 foreach(Control c in canSweep.Children) 97 { 98 if(c is Button) 99 {100 Button b = c as Button;101 //if (b.Content.ToString() == "★")102 if (b.Tag.ToString() == "★")103 count++;104 }105 }106 return count;107 }108 /// 109 /// 创建雷110 /// 111 /// x轴列数112 /// y轴列数113 private void CreateButton(int x, int y)114 {115 canSweep.Children.Clear();116 //四个方向的边距最大能多出1117 //double width = (500 - (x + 1) * 1) / x;118 //double height = (500 - (y + 1) * 1) / y;119 double width = 500/x;120 double height = 500/y;121 canSweep.Width = width * x + x;//自动调整游戏窗口的大小122 canSweep.Height = height * y + y;123 List
Sweeps = new List
();124 for (int i = 0; i < x; i++)125 {126 for (int j = 0; j < y; j++)127 {128 Button bt = new Button()129 {130 Width = width,131 Height = height,132 HorizontalAlignment = HorizontalAlignment.Center,133 VerticalAlignment = VerticalAlignment.Center,134 Content = "",135 Tag = "",136 Name = "bs" + i,137 Background = Brushes.Gray,138 Margin = new Thickness(i * 1, j * 1, i * 1, j * 1)//左上右下139 };140 Sweeps.Add(new Point(bt.Margin.Left, bt.Margin.Top));141 bt.Click += btnSweeps_Click;142 Canvas.SetTop(bt, j * height);143 Canvas.SetLeft(bt, i * width);144 canSweep.Children.Add(bt);145 btnSweeps_ClickBegin(bt);146 }147 }148 LoadSweeps(Sweeps);149 }150 ///
151 /// 初始化雷区152 /// 153 ///
雷集合154 private void LoadSweeps(List
Sweeps)155 {156 GNum = SweepNum.Length;157 string tag = "工具:";158 lg.Content = tag+GNum;//初始化挖雷次数159 Random random = new Random();160 for (int i = 0; i < SweepNum.Length; i++)//生成不同的随机数161 {162 int s = random.Next(0, Sweeps.Count);//随机数取值范围163 bool b = true;164 foreach (int num in SweepNum)165 {166 if (num == s)//表示有重复的随机数167 {168 --i;169 b = false;170 break;171 }172 }173 if (b)174 SweepNum[i] = s;175 }176 for (int i = 0; i < SweepNum.Length; i++)177 {178 foreach (Control c in canSweep.Children)179 {180 if (c is Button)181 {182 Button btn = c as Button;183 //btn.Content = btn.Margin.Left + "|" + btn.Margin.Top;184 if (btn.Margin.Left == Sweeps[SweepNum[i]].X && btn.Margin.Top == Sweeps[SweepNum[i]].Y)185 {186 //btn.Content = "★";187 btn.Tag = "★";188 break;189 }190 }191 }192 }193 LoadSweepTags();194 lw.Content = " 未挖:" + GetSweepCount() + " ";195 }196 ///
197 /// 查找指定区域内雷的数量198 /// 199 ///
判断的区域200 ///
雷数量count
201 private int GetSweepCount(Button btn)202 {203 double left = btn.Margin.Left - 1;204 double top = btn.Margin.Top - 1;205 double right = btn.Margin.Right + 1;206 double bottom = btn.Margin.Bottom + 1;207 int count = 0;208 foreach (Control c in canSweep.Children)209 {210 if (c is Button)211 {212 Button b = c as Button;213 if (b.Margin.Left >= left && b.Margin.Top >= top && b.Margin.Right <= right && b.Margin.Bottom <= bottom)214 {215 //if (b.Content.ToString() == "★")216 if (b.Tag.ToString() == "★")217 count++;218 }219 }220 }221 return count;222 }223 ///
224 /// 初始化雷区附近数量值225 /// 226 private void LoadSweepTags()227 {228 int count = 0;229 foreach (Control cl in canSweep.Children)230 {231 if (cl is Button)232 {233 Button btn = cl as Button;234 //if (btn.Content.ToString() == "★")235 if (btn.Tag.ToString() == "★")//是雷区则开始初始化数量值236 {237 foreach (Control c in canSweep.Children)238 {239 if (c is Button)240 {241 Button b = c as Button;242 double left = btn.Margin.Left - 1;243 double top = btn.Margin.Top - 1;244 double right = btn.Margin.Right + 1;245 double bottom = btn.Margin.Bottom + 1;246 count = GetSweepCount(b);//得到该区域周围雷的数量247 if (b.Margin.Left >= left && b.Margin.Top >= top && b.Margin.Right <= right && b.Margin.Bottom <= bottom)248 {249 //if (b.Content.ToString() != "★")250 //b.Content = count;251 if (b.Tag.ToString() != "★")252 b.Tag = count;253 }254 }255 }256 }257 }258 }259 }260 private void btnSweeps_ClickBegin(Button btn) 261 {262 Storyboard = FindResource("btnFZs") as Storyboard;//查找动画263 btn.BeginStoryboard(Storyboard);//启动动画264 }265 ///
266 /// 雷单击事件267 /// 268 ///
269 ///
270 private void btnSweeps_Click(object sender, RoutedEventArgs e)271 {272 Button btn = (e.Source as Button);273 btnSweeps_ClickBegin(btn);274 if (GNum < 0)275 {276 btnWa.IsEnabled = false;277 MessageBox.Show("GAME OVER");278 GameOver();279 return;280 }281 if (!BeginWa)282 {283 if (IsCorrect(btn))284 {285 ReturnFindSweeps(btn);286 }287 }288 else 289 {290 if (btn.Tag.ToString() == "★")291 {292 btn.Content = btn.Tag;293 btn.Foreground = Brushes.RoyalBlue;294 btn.Tag = "☆";295 btn.Content = btn.Tag;296 lw.Content = " 未挖:" + GetSweepCount() + " ";297 ly.Content = " 以挖:" + (SweepNum.Length - GetSweepCount());298 if (GetSweepCount() == 0) 299 {300 MessageBox.Show("过关");301 }302 }303 else 304 {305 btn.Content = btn.Tag;306 }307 }308 btn.Click -= btnSweeps_Click;//让单击的按钮失效309 BeginWa = false;//修改挖雷状态310 }311 ///
312 /// 刷新挖雷工具313 /// 314 private void SetLgContent()315 {316 if (GNum > 0)317 {318 GNum--;319 string tag = "工具:";320 lg.Content = tag + GNum;//刷新挖雷次数321 }322 }323 ///
324 /// 挖雷325 /// 326 ///
327 ///
328 private void btnWa_Click(object sender, RoutedEventArgs e)329 {330 if (!BeginWa)331 {332 Button btn = (e.Source as Button);333 btn.Click -= btnSweeps_Click;//让单击的按钮失效334 BeginWa = true;335 SetLgContent();336 }337 }338 ///
339 /// 游戏结束340 /// 341 private void GameOver() 342 {343 foreach (var c in canSweep.Children)344 {345 if (c is Button)346 {347 Button b = c as Button;348 b.IsEnabled = false;349 }350 }351 }352 ///
353 /// 判断是否正确354 /// 355 ///
单击的Button控件356 ///
是否可以继续游戏
357 private bool IsCorrect(Button button)358 {359 bool b = true;360 //if(button.Content.ToString() == "★")361 if (button.Tag.ToString() == "★")362 {363 b = false;364 button.Content = button.Tag;365 MessageBox.Show("GAME OVER");366 GameOver();367 }368 return b;369 }370 ///
371 /// 找雷372 /// 373 ///
单击的雷374 private void ReturnFindSweeps(Button btn) 375 {376 377 if (btn.Tag.ToString() == "" || btn.Tag.ToString() == " ")//表示该区域为安全区域378 {379 double left = btn.Margin.Left - 1;380 double top = btn.Margin.Top - 1;381 double right = btn.Margin.Right + 1;382 double bottom = btn.Margin.Bottom + 1;383 foreach (Control c in canSweep.Children)384 {385 if (c is Button)386 {387 Button b = c as Button;388 if (b.Margin.Left >= left && b.Margin.Top >= top && b.Margin.Right <= right && b.Margin.Bottom <= bottom)389 {390 //if (b.Content.ToString() != "")391 if (b.Tag.ToString() != "")//表示附近无雷392 {393 if(b.Tag.ToString() == "")394 b.Content = ""; 395 else if(b.Tag.ToString() != "★")396 b.Content = b.Tag;397 continue;398 }399 else400 {401 b.Click -= btnSweeps_Click;//让按钮失效402 b.Content = "";403 b.Tag = " ";//表示已经侦查过的区域404 //b.Style = new System.Windows.Style();//清除外部样式405 b.Style = FindResource("btnClickIng") as Style;406 ReturnFindSweeps(b);//递归调用407 }408 }409 }410 }411 }412 else//如果是雷所属的区域,则只显示该一块区域413 {414 //btn.Background = myBrush;415 btn.Content = btn.Tag;416 btn.Click -= btnSweeps_Click;//让按钮失效417 }418 }419 }420 }
View Code

样式

1 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 22
23
24
120
141
View Code
1 
3
21
View Code
1 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
74
View Code
1 
3
39
View Code
1 
3
31
View Code

 

转载于:https://www.cnblogs.com/LiuZhen/p/4017584.html

你可能感兴趣的文章
【2.2】创建博客文章模型
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
cassandra vs mongo (1)存储引擎
查看>>