WPF扫雷游戏(简略版)
生活随笔
收集整理的這篇文章主要介紹了
WPF扫雷游戏(简略版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
游戲介紹:
此游戲共分為四關,新手,初級,中級,高級
通過改變格子數和雷數來進行跳關;
新手: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扫雷游戏(简略版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软考计算机网络与最大流量,软考程序员考点
- 下一篇: 综合评价法之秩和比法(RSR)