日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

WPF砖块消除

發(fā)布時(shí)間:2023/12/20 asp.net 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF砖块消除 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

WPF的磚塊消除,基本實(shí)現(xiàn)磚塊消除公共

  • 實(shí)現(xiàn)效果:





  • 使用方式:
    第一步:設(shè)置磚塊數(shù)量
    第二步:點(diǎn)擊刷新界面
    第三步:按住鍵盤(pán)的 “A” 或點(diǎn)擊 “啟動(dòng)”按鈕啟動(dòng)游戲
    移動(dòng)方式:在橘黃色區(qū)域按住鼠標(biāo)左鍵即可移動(dòng)擋板,或者移動(dòng)擋板左右坐標(biāo)即可

  • 前臺(tái)代碼:

<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="打磚塊V0.0.1" Height="600" Width="600" MinHeight="600" MinWidth="600" Loaded="Window_Loaded" Closing="Window_Closing" SizeChanged="Window_SizeChanged" ><Window.Resources><RoutedUICommand x:Key="Start_Game" Text="Start game" /></Window.Resources><Window.InputBindings><KeyBinding Key="A" Command="{StaticResource Start_Game}"/></Window.InputBindings><Window.CommandBindings><CommandBinding x:Name="cb_start_game" Command="{StaticResource Start_Game}" CanExecute="cb_start_game_CanExecute" Executed="cb_start_game_Executed"/></Window.CommandBindings><Grid x:Name="gd_min"><Grid.Background><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF3B41CF" Offset="0"/><GradientStop Color="#FFB82274" Offset="0.992"/><GradientStop Color="#FF4A4A4A" Offset="0.229"/><GradientStop Color="#FF7AD65E" Offset="0.562"/></LinearGradientBrush></Grid.Background><Grid.RowDefinitions><RowDefinition Height="80"/><RowDefinition Height="9*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition Width="210"/></Grid.ColumnDefinitions><Grid Grid.Row="0" Grid.Column="0"><Grid.RowDefinitions ><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition Width="0.1*"/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Label Grid.Row="0" Grid.Column="0" Content="磚塊總數(shù):" Foreground="Azure"/><Label Grid.Row="1" Grid.Column="0" Content="已消除總數(shù):" Foreground="Azure"/><Label Grid.Row="2" Grid.Column="0" Content="當(dāng)前用時(shí):" Foreground="Azure"/><Label x:Name="lb_brick_count" Grid.Row="0" Grid.Column="1" Content="-" Foreground="Azure"/><Label x:Name="lb_re_count" Grid.Row="1" Grid.Column="1" Content="-" Foreground="Azure"/><Label x:Name="lb_time" Grid.Row="2" Grid.Column="1" Content="-" Foreground="Azure"/><Label Grid.Row="0" Grid.Column="3" Content="剩余磚塊:" Foreground="Azure"/><Label Grid.Row="1" Grid.Column="3" Content="彈起數(shù):" Foreground="Azure"/><Label Grid.Row="2" Grid.Column="3" Content="-" Foreground="Azure"/><Label x:Name="lb_lost_count" Grid.Row="0" Grid.Column="4" Content="-" Foreground="Azure"/><Label x:Name="lb_run_stpe" Grid.Row="1" Grid.Column="4" Content="-" Foreground="Azure"/><Label Grid.Row="2" Grid.Column="4" Content="-" Foreground="Azure"/></Grid><TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Text="第①步:設(shè)置磚塊數(shù)量和小球速度&#13;第②步:點(diǎn)擊刷新界面生成磚塊&#13;第③步:點(diǎn)擊啟動(dòng)(或單擊“a”開(kāi)始)開(kāi)始消除磚塊&#13;移動(dòng)方式:按住鼠標(biāo)左鍵即可移動(dòng)擋板" Foreground="Red" Grid.ColumnSpan="2" /><Grid Grid.Row="1" Grid.Column="0" Background="BlanchedAlmond" MouseMove="gd_ball_MouseMove"><Grid.RowDefinitions><RowDefinition Height="2*"/><RowDefinition/></Grid.RowDefinitions><Grid Grid.Row="0"><Grid.ColumnDefinitions><ColumnDefinition Width="20"/><ColumnDefinition/><ColumnDefinition Width="20"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="20"/><RowDefinition/></Grid.RowDefinitions><Grid Grid.Row="1" Grid.Column="1" x:Name="gd_table" Background="AliceBlue"/></Grid></Grid><Grid Grid.Row="1" Grid.Column="0" x:Name="gd_ball" Background="{x:Null}" ><Grid.RowDefinitions><RowDefinition/><RowDefinition Height="10"/></Grid.RowDefinitions><Ellipse x:Name="ep_boll" Grid.Row="0" Grid.RowSpan="3" Width="10" Height="10" VerticalAlignment="Bottom" HorizontalAlignment="Center"><Ellipse.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FFF7642C" Offset="0"/><GradientStop Color="#FF4058A8" Offset="1"/><GradientStop Color="#FFA66147" Offset="0.402"/><GradientStop Color="#FF6E4739" Offset="0.546"/></LinearGradientBrush></Ellipse.Fill></Ellipse><Rectangle Visibility="Collapsed" x:Name="rt_wb" Grid.Row="1" Width="100" Height="10" VerticalAlignment="Bottom" HorizontalAlignment="Center"RadiusY="5" RadiusX="5"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF787878" Offset="0"/><GradientStop Color="Black" Offset="1"/></LinearGradientBrush></Rectangle.Fill></Rectangle><Rectangle Grid.Row="1" x:Name="rt_rect" Height="10" Width="100" Fill="#CCCD2C2C"/></Grid><Grid Grid.Row="1" Grid.Column="1"><Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition/><RowDefinition Height="40"/></Grid.RowDefinitions><StackPanel Grid.Row="0" Orientation="Vertical" Background="Aqua"><Label Content="球坐標(biāo):"/><Label Name="lb_ball_addr" Content="0:0" HorizontalAlignment="Center"/><Label Content="距離:"/><Label Name="lb_leng" Content="-" HorizontalAlignment="Center" /></StackPanel><StackPanel Grid.Row="1" VerticalAlignment="Center"><Label Content="磚塊數(shù)量:"/><Slider Margin="2" Width="50" x:Name="sd_brick_num" MinWidth="150" IsSnapToTickEnabled="True" Minimum="10" Maximum="500" Value="40" Orientation="Horizontal"/><Label Content="{Binding Path=Value,ElementName=sd_brick_num}" HorizontalAlignment="Center" VerticalAlignment="Center"/><Label Content="速度比:"/><Slider Margin="2" Width="50" x:Name="sd_bool_speed" MinWidth="150" IsSnapToTickEnabled="True" Minimum="0" Maximum="50" Value="45" Orientation="Horizontal" ValueChanged="sd_bool_speed_ValueChanged"/><Label Content="{Binding Path=Value,ElementName=sd_bool_speed}" HorizontalAlignment="Center" VerticalAlignment="Center"/><Button x:Name="bt_crt" Margin="3" Height="40" Content="刷新界面" Click="bt_crt_Click"/><Button Content="啟動(dòng)" Margin="3" Height="40" x:Name="bt_start" Click="bt_start_Click"/><Label Content="擋板中心坐標(biāo):"/><Slider Margin="2" Width="50" x:Name="sd_x" MinWidth="150" ValueChanged="sd_x_ValueChanged" IsSnapToTickEnabled="True" Minimum="-200" Maximum="200" Value="0" Orientation="Horizontal"/><Label Content="{Binding Path=Value,ElementName=sd_x}" HorizontalAlignment="Center" VerticalAlignment="Center"/></StackPanel><TextBlock Grid.Row="2" Text="Power By Bilibili-火狐鞥&#13;2021-01-14" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" FontSize="15" Foreground="Gray"/></Grid></Grid> </Window>
  • 后臺(tái)代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Timers; 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 打磚塊 {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.ep_boll.RenderTransform=new TranslateTransform();this.rt_rect.RenderTransform=new TranslateTransform();}private void Window_Loaded(object sender, RoutedEventArgs e){timer_init();CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);Max_leng = Math.Sqrt(Math.Pow(rect_info.Width / 2, 2) + Math.Pow(rect_info.Height / 2, 2)) + ball_info.Radius;sd_x.Maximum = gd_ball.ActualWidth / 2;sd_x.Minimum = -gd_ball.ActualWidth / 2;}#region 申明類(lèi)public struct Ball{public Point Addr; //位置public Point Speed; //運(yùn)行速度public Point Dir; //運(yùn)行方向public int Radius; //球半徑}//public struct Rect//{// public Point Addr; //位置// public double Width;// public double Height;// public int Stat;//}public class Rect{private Point _addr = new Point(0, 0);private double _width = 0;private double _height = 0;private int _stat = 0;private int _index = 0;public Point Addr{set { _addr = value; }get { return _addr; }}public double Width{set { _width = value; }get { return _width; }}public double Height{set { _height = value; }get { return _height; }}public int Stat{set { _stat = value; }get { return _stat; }}public int Index{set { _index = value; }get { return _index; }}}#endregion#region///全局變量private ProcessCount processCount;//統(tǒng)計(jì)運(yùn)行時(shí)間private int brick_run_stpe = 0; //運(yùn)行步數(shù)private int re_brick_count = 0;private Ball ball_info = new Ball() { Addr = new Point(0, -10), Speed = new Point(0, 0), Dir = new Point(0, -1), Radius = 5 };private Rect rect_info = new Rect() { Addr = new Point(0, 0), Height = 10, Width = 100, Stat = 99 };private List<Rect> brick_list = new List<Rect>();//磚塊列表private Point brick_info = new Point(20, 18); //生成磚的大小private int brick_num = 40; //生成磚的數(shù)量private List<int> brick_addr = new List<int>();double Max_leng = 0;#endregion#region 定時(shí)器MillisecondTimer aTimer;private int timer_count=0;void timer_init(){aTimer = new MillisecondTimer();aTimer.Tick += timedo; ;aTimer.Interval = 1; //每秒執(zhí)行processCount = new ProcessCount(0);//aTimer.Start();aTimer.Stop();}private void timedo(object sender, EventArgs e) //定時(shí)中斷{this.Dispatcher.Invoke(new Action(delegate{ball_move();timer_count++;if (timer_count > 1000){timer_count = 0;processCount.ProcessCountUp();lb_time.Content = processCount.GetHour() + ":" + processCount.GetMinute() + ":" + processCount.GetSecond();}}));}#endregion#region //功能函數(shù)[DllImport("kernel32.dll")]private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);/// <summary>/// 內(nèi)存回收/// </summary>private void ClearMemory(){GC.Collect();GC.WaitForPendingFinalizers();if (Environment.OSVersion.Platform == PlatformID.Win32NT){SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);}}/// <summary>/// 產(chǎn)生指定隨機(jī)數(shù) 且不重復(fù)/// </summary>/// <param name="Number">產(chǎn)生隨機(jī)數(shù)個(gè)數(shù)</param>/// <param name="minNum">最小值</param>/// <param name="maxNum">最大值</param>/// <returns></returns>public List<int> GetRandomArray(int Number, int minNum, int maxNum){int j;int[] b = new int[Number];List<int> buf = new List<int>();Random r = new Random();while (buf.Count < Number){int i = r.Next(minNum, maxNum);int num = 0;for (int k = 0; k < buf.Count; k++) //判斷是否和以前的數(shù)據(jù)重復(fù){if (buf[k] == i) //新生成的數(shù)據(jù)和原來(lái)的數(shù)據(jù)重復(fù){num = num + 1;break;}}if (num == 0){//b[j] = i;buf.Add(i);}}//for (j = 0; j < Number; j++)//{// int i = r.Next(minNum, maxNum + 1);// int num = 0;// for (int k = 0; k < j; k++) //判斷是否和以前的數(shù)據(jù)重復(fù)// {// if (b[k] == i) //新生成的數(shù)據(jù)和原來(lái)的數(shù)據(jù)重復(fù)// {// num = num + 1;// }// }// if (num == 0)// {// b[j] = i;// }// else// {// j = j - 1;// }//}//Array.Sort(buf);buf.Sort();return buf;}/// <summary>/// WPF控件查找/// </summary>/// <typeparam name="T">空間類(lèi)型</typeparam>/// <param name="parent">父控件</param>/// <param name="childName">查找空間名稱(chēng)</param>/// <returns></returns>public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject{if (parent == null) return null;T foundChild = null;int childrenCount = VisualTreeHelper.GetChildrenCount(parent);for (int i = 0; i < childrenCount; i++){var child = VisualTreeHelper.GetChild(parent, i);// 如果bai子控件不是需查找的控件類(lèi)型T childType = child as T;if (childType == null){// 在下一級(jí)控件中遞歸查找foundChild = FindChild<T>(child, childName);// 找到控件就可以中斷遞歸操作 if (foundChild != null) break;}else if (!string.IsNullOrEmpty(childName)){var frameworkElement = child as FrameworkElement;// 如果控件名稱(chēng)符合參數(shù)條件if (frameworkElement != null && frameworkElement.Name == childName){foundChild = (T)child;break;}}else{// 查找到了控件foundChild = (T)child;break;}}return foundChild;}#endregion#region ///界面刷新private void CompositionTarget_Rendering(object sender, EventArgs e){TranslateTransform bool_translate = (TranslateTransform)ep_boll.RenderTransform;TranslateTransform rect_translate = (TranslateTransform)rt_rect.RenderTransform;bool_translate.X = ball_info.Addr.X;bool_translate.Y = ball_info.Addr.Y;rect_translate.X = rect_info.Addr.X;rect_translate.Y = rect_info.Addr.Y;lb_ball_addr.Content = ball_info.Addr.X.ToString("0.00") + ":" + ball_info.Addr.Y.ToString("0.00");lb_leng.Content=check_leng(rect_info, ball_info)[1].ToString("0.00");}#endregion#region ///核心算法/// <summary>/// 獲取距離最近的點(diǎn)的距離/// </summary>/// <param name="p">矩形信息</param>/// <param name="b">圓信息</param>/// <returns>/// re[0] -1 錯(cuò)誤(內(nèi)部) 0 X軸方向距離最短 1 Y軸距離最短/// re[1] -1 錯(cuò)誤 其他:最短距離/// </returns>private double[] check_leng(Rect p, Ball b){double leng = Math.Sqrt(Math.Pow(p.Addr.X - b.Addr.X, 2) + Math.Pow(p.Addr.Y - b.Addr.Y, 2));double max_len = Math.Sqrt(Math.Pow(p.Width / 2, 2) + Math.Pow(p.Height / 2, 2)) + b.Radius;double min_len = Math.Min(Math.Min(p.Height / 2, p.Width / 2), b.Radius);double min_x = -1;double min_y = -1;double min_1 = -1;double min_2 = -1;double min_3 = -1;double min_4 = -1;double[] re = new double[2];re[0] = -1;re[1] = -1;if (leng < min_len) //小于最小距離,兩物體相交 或位于內(nèi)部{//邏輯上不允許出現(xiàn)此種情況return re;}else{//根據(jù)中心坐標(biāo)位置確定需要判斷的邊界(有可能交互的邊界)if ((b.Addr.X >= p.Addr.X - p.Width / 2 + b.Radius) && (b.Addr.X <= p.Addr.X + p.Width / 2 - b.Radius)) //位于Y軸方向區(qū)域類(lèi) {if (b.Addr.Y <= p.Addr.Y - p.Height / 2 - b.Radius) //圓心位于矩形的下邊沿{min_y = Math.Abs(b.Addr.Y + b.Radius - (p.Addr.Y - p.Height / 2));}else if (b.Addr.Y >= p.Addr.Y + p.Height / 2 + b.Radius) //圓心位于的上邊沿{min_y = Math.Abs(b.Addr.Y - b.Radius - (p.Addr.Y + p.Height / 2));}else //圓心位于矩形內(nèi){min_y = -1;}re[0] = 1;re[1] = min_y;return re;}else if ((b.Addr.Y >= p.Addr.Y - p.Height / 2 + b.Radius) && (b.Addr.Y <= p.Addr.Y + p.Height / 2 - b.Radius))//位于矩形X軸方向區(qū)域類(lèi) {if (b.Addr.X <= p.Addr.X - p.Width / 2 - b.Radius) //圓心位于矩形的左邊沿{min_x = Math.Abs(b.Addr.X + b.Radius - (p.Addr.X - p.Width / 2));}else if (b.Addr.X >= p.Addr.X + p.Width / 2 + b.Radius) //圓心位于矩形的右邊沿{min_x = Math.Abs(b.Addr.X - b.Radius - (p.Addr.X + p.Width / 2));}else //圓心位于矩形內(nèi){min_x = -1;}re[0] = 0;re[1] = min_x;return re;}else if ((b.Addr.X <= p.Addr.X - p.Width / 2 + b.Radius) && (b.Addr.Y <= p.Addr.Y - p.Height / 2 + b.Radius)) //第四象域{for (int rad_t = 270; rad_t <= 360; rad_t++){double x = b.Addr.X + b.Radius * Math.Cos(Math.PI * rad_t / 180);double y = b.Addr.Y - b.Radius * Math.Sin(Math.PI * rad_t / 180); //算出球邊沿的坐標(biāo)double m = p.Addr.X - p.Width / 2;double n = p.Addr.Y - p.Height / 2;double mint_t = Math.Sqrt(Math.Pow(m - x, 2) + Math.Pow(n - y, 2));re[0] = 1;if (b.Addr.X > m){mint_t = Math.Min(mint_t, Math.Abs(y - n));re[0] = 1;}else if (b.Addr.Y > n){mint_t = Math.Min(mint_t, Math.Abs(x - m));re[0] = 1;}if (min_4 != -1){if ((x > p.Addr.X - p.Width / 2) && (y > p.Addr.Y - p.Height / 2)){min_4 = -1;break;}else{if (mint_t < min_4){min_4 = mint_t;}}}else{if ((x > p.Addr.X - p.Width / 2) && (y > p.Addr.Y - p.Height / 2)){min_4 = -1;break;}else{min_4 = mint_t;}}}re[1] = min_4;return re;//return min_4;}else if ((b.Addr.X > p.Addr.X + p.Width / 2 - b.Radius) && (b.Addr.Y <= p.Addr.Y - p.Height / 2 + b.Radius)) //第三象域{for (int rad_t = 180; rad_t <= 270; rad_t++){double x = b.Addr.X + b.Radius * Math.Cos(Math.PI * rad_t / 180);double y = b.Addr.Y - b.Radius * Math.Sin(Math.PI * rad_t / 180); //算出球邊沿的坐標(biāo)double m = p.Addr.X + p.Width / 2;double n = p.Addr.Y - p.Height / 2;double mint_t = Math.Sqrt(Math.Pow(m - x, 2) + Math.Pow(n - y, 2));re[0] = 1;if (b.Addr.X < m){mint_t = Math.Min(mint_t, Math.Abs(y - n));re[0] = 1;}else if (b.Addr.Y > n){mint_t = Math.Min(mint_t, Math.Abs(x - m));re[0] = 0;}//double mint_t = Math.Sqrt(Math.Pow(p.Addr.X + p.Width / 2 - x, 2) + Math.Pow(p.Addr.Y - p.Height / 2 - y, 2));if (min_3 != -1){if ((x < p.Addr.X + p.Width / 2) && (y > p.Addr.Y - p.Height / 2)){min_3 = -1;break;}elseif (mint_t < min_3){min_3 = mint_t;}}else{if ((x < p.Addr.X + p.Width / 2) && (y > p.Addr.Y - p.Height / 2)){min_3 = -1;break;}else{}min_3 = mint_t;}}re[1] = min_3;return re;//return min_3;}else if ((b.Addr.X > p.Addr.X + p.Width / 2 - b.Radius) && (b.Addr.Y > p.Addr.Y + p.Height / 2 - b.Radius))//第二象域{for (int rad_t = 90; rad_t <= 180; rad_t++){double x = b.Addr.X + b.Radius * Math.Cos(Math.PI * rad_t / 180);double y = b.Addr.Y - b.Radius * Math.Sin(Math.PI * rad_t / 180); //算出球邊沿的坐標(biāo)double m = p.Addr.X + p.Width / 2;double n = p.Addr.Y + p.Height / 2;double mint_t = Math.Sqrt(Math.Pow(m - x, 2) + Math.Pow(n - y, 2));re[0] = 0;if (b.Addr.X < m){mint_t = Math.Min(mint_t, Math.Abs(y - n));re[0] = 1;}else if (b.Addr.Y < n){mint_t = Math.Min(mint_t, Math.Abs(x - m));re[0] = 0;}if (min_2 != -1){if ((x < p.Addr.X + p.Width / 2) && (y < p.Addr.Y + p.Height / 2)){min_2 = -1;break;}else{if (mint_t < min_2){min_2 = mint_t;}}}else{if ((x < p.Addr.X + p.Width / 2) && (y < p.Addr.Y + p.Height / 2)){min_2 = -1;break;}else{min_2 = mint_t;}}}re[1] = min_2;return re;}else if ((b.Addr.X <= p.Addr.X - p.Width / 2 + b.Radius) && (b.Addr.Y > p.Addr.Y + p.Height / 2 - b.Radius)) //第一象域{for (int rad_t = 0; rad_t <= 90; rad_t++){double x = b.Addr.X + b.Radius * Math.Cos(Math.PI * rad_t / 180);double y = b.Addr.Y - b.Radius * Math.Sin(Math.PI * rad_t / 180); //算出球邊沿的坐標(biāo)double m = p.Addr.X - p.Width / 2;double n = p.Addr.Y + p.Height / 2;double mint_t = Math.Sqrt(Math.Pow(m - x, 2) + Math.Pow(n - y, 2));re[0] = 0;if (b.Addr.X > m){mint_t = Math.Min(mint_t, Math.Abs(y - n));re[0] = 1;}else if (b.Addr.Y < n){mint_t = Math.Min(mint_t, Math.Abs(x - m));re[0] = 0;}if (min_1 != -1){if ((x > p.Addr.X - p.Width / 2) && (y < p.Addr.Y + p.Height / 2)){min_1 = -1;break;}else{if (mint_t < min_1){min_1 = mint_t;}}}else{if ((x > p.Addr.X - p.Width / 2) && (y < p.Addr.Y + p.Height / 2)){min_1 = -1;break;}else{min_1 = mint_t;}}}re[1] = min_1;return re;}return re;//return -98;}}private double wb_val = 0;private int time_count = 0;/// <summary>/// 球體移動(dòng)判斷/// </summary>private void ball_move(){try{time_count++;double max_x = (gd_ball.ActualWidth - ep_boll.ActualWidth) / 2;double max_y = gd_ball.ActualHeight - ep_boll.ActualHeight;wb_val = rect_info.Addr.X;if (time_count >= 20){time_count = 0;}if ((int)(51 - ball_info.Speed.X) != 0 && ball_info.Speed.X != 0){if (time_count % ((int)(51 - ball_info.Speed.X)) == 0){ball_info.Addr.X += ball_info.Dir.X;}}if ((int)(51 - ball_info.Speed.Y) != 0 && ball_info.Speed.Y != 0){if (time_count % ((int)(51 - ball_info.Speed.Y)) == 0){ball_info.Addr.Y += ball_info.Dir.Y;}}if (ball_info.Addr.X >= (max_x)){ball_info.Dir.X = -1;ball_info.Addr.X = max_x;}else if (ball_info.Addr.X <= -max_x){ball_info.Dir.X = 1;ball_info.Addr.X = -max_x;}if (ball_info.Addr.Y <= (-max_y)){ball_info.Dir.Y = 1;ball_info.Addr.Y = -max_y;}else if (ball_info.Addr.Y > 0){ball_info.Addr.X = 0;ball_info.Addr.Y = -10;ball_info.Speed.X = 0;ball_info.Speed.Y = 0;ball_info.Dir.X = 0;ball_info.Dir.Y = -1;brick_run_stpe++;lb_run_stpe.Content = brick_run_stpe;Debug.WriteLine("重置");}else if (ball_info.Addr.Y > -5){if (System.Math.Abs(ball_info.Addr.X - wb_val) > 50){Debug.WriteLine("掉了");}else if (System.Math.Abs(ball_info.Addr.X - wb_val) > 45){if (ball_info.Dir.Y != -1){ball_info.Dir.Y = -1;brick_run_stpe++;lb_run_stpe.Content = brick_run_stpe;}if (ball_info.Addr.X - wb_val < 0){if (ball_info.Dir.X != -1){ball_info.Dir.X = -1;}}else{if (ball_info.Dir.X != 1){ball_info.Dir.X = 1;}}ball_info.Speed.X = (Math.Abs((ball_info.Addr.X - wb_val)));Debug.WriteLine("起飛1");}else //0~45理論還是輸了。{}}else if (ball_info.Addr.Y > -10){if (System.Math.Abs(ball_info.Addr.X - wb_val) > 50){// Debug.WriteLine("掉了");}else{if (ball_info.Dir.Y != -1){ball_info.Dir.Y = -1;brick_run_stpe++;lb_run_stpe.Content = brick_run_stpe;}if (ball_info.Addr.X - wb_val < 0){if (ball_info.Dir.X != -1){ball_info.Dir.X = -1;}}else{if (ball_info.Dir.X != 1){ball_info.Dir.X = 1;}}ball_info.Speed.X = (Math.Abs((ball_info.Addr.X - wb_val)));Debug.WriteLine("起飛2");}}if (brick_list != null && brick_list.Count != 0){for (int i = 0; i < brick_list.Count; i++) //判斷磚塊與球的距離{if (brick_list[i].Stat != 0){if ((Math.Abs(brick_list[i].Addr.X - ball_info.Addr.X) < Max_leng)&&(Math.Abs(brick_list[i].Addr.Y - ball_info.Addr.Y) < Max_leng)){double[] leng = check_leng(brick_list[i], ball_info);if (leng[1] <= 0){if (leng[0] == 0){ball_info.Dir.X = -ball_info.Dir.X;}else{ball_info.Dir.Y = -ball_info.Dir.Y;}//觸發(fā)移除操作if (brick_list[i].Stat != 99){brick_list[i].Stat--;if (brick_list[i].Stat <= 0){//移除磚塊brick_list[i].Stat = 0;//界面移除Rectangle ret_t = null;string name = "rect_" + brick_list[i].Index.ToString();ret_t = FindChild<Rectangle>(gd_table, name);if (ret_t != null){BrushConverter conv = new BrushConverter();Brush bru;bru = conv.ConvertFromInvariantString("#000000FF") as Brush;ret_t.Fill = bru;ret_t.Stroke = bru;re_brick_count++;lb_re_count.Content =re_brick_count;lb_lost_count.Content = brick_num - re_brick_count;if (brick_num <= re_brick_count){aTimer.Stop();MessageBox.Show("恭喜,已完成拆除\r\n" + "拆除磚塊:" + re_brick_count.ToString() + " 總共用時(shí):" + processCount.TotalSecond.ToString()+"s") ; ;}}}}}}}}}}catch{ }}/// <summary>/// 生成磚塊/// </summary>/// <param name="brick_info">磚塊信息</param>/// <param name="num">磚塊數(shù)量</param>private void crt_zhuan(Point brick_info,int num){int widght_num = (int)((gd_ball.ActualWidth - 40) / brick_info.X);int height_num = (int)(((gd_ball.ActualHeight) / 3 * 2 - 20) / brick_info.Y);int max_num = widght_num * height_num;if (max_num < num){MessageBox.Show("磚過(guò)大或數(shù)量過(guò)多,請(qǐng)重新生成\r\n" + "最多生成 " + max_num.ToString() + " 塊");return;}Point point_start = new Point();point_start.X = 20 - (gd_ball.ActualWidth / 2);point_start.Y = 20 - gd_ball.ActualHeight; ;brick_addr = GetRandomArray(num, 0, max_num);int count = 0;brick_list.Clear();brick_list = new List<Rect>();gd_table.Children.Clear();gd_table.RowDefinitions.Clear();gd_table.ColumnDefinitions.Clear();for (int i = 0; i < height_num; i++){RowDefinition rowDefinition = new RowDefinition();rowDefinition.MinHeight = brick_info.Y;rowDefinition.MaxHeight = brick_info.Y;gd_table.RowDefinitions.Add(rowDefinition);}for (int j = 0; j < widght_num; j++){ColumnDefinition columnDefinition = new ColumnDefinition();columnDefinition.MinWidth = brick_info.X;columnDefinition.MaxWidth = brick_info.X;gd_table.ColumnDefinitions.Add(columnDefinition);}for (int i = 0; i < height_num; i++){for (int j = 0; j < widght_num; j++){for (int k = 0; k < num; k++){//if (count == addr[k])if (count == brick_addr[k]){//添加磚Rect rect_t = new Rect();Point p = new Point(0, 0);p.X= point_start.X + j * brick_info.X + brick_info.X / 2;p.Y= point_start.Y + i * brick_info.Y + brick_info.Y / 2;//rect_t.Addr.X = point_start.X + j * brick_info.X + brick_info.X / 2;//rect_t.Addr.Y = point_start.Y + i * brick_info.Y + brick_info.Y / 2;rect_t.Addr = p;rect_t.Width = brick_info.X;rect_t.Height = brick_info.Y;rect_t.Stat = 1;rect_t.Index = count;brick_list.Add(rect_t);//顯示BrushConverter conv = new BrushConverter();Brush bru;bru = conv.ConvertFromInvariantString("#FFB6BFBF") as Brush;Brush back;back = conv.ConvertFromInvariantString("#FF00BFBF") as Brush;//Rectangle ret_t = new Rectangle() { Width = rect_t.Width, Name= "rect_"+count.ToString(), Height = rect_t.Height, Fill = bru, Stroke = back };Rectangle ret_t = new Rectangle() { Width = rect_t.Width, Name = "rect_" + count.ToString(), Height = rect_t.Height, Fill = bru, Stroke = back };ret_t.SetValue(Grid.RowProperty, i);ret_t.SetValue(Grid.ColumnProperty, j);gd_table.Children.Add(ret_t);break;}}count++;}}}#endregion#region///控件事件/// <summary>/// 速度更改按鈕/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void sd_bool_speed_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e){// bool_speed = (int)(sd_bool_speed.Value);ball_info.Speed.Y = (int)(sd_bool_speed.Value);}/// <summary>/// 底板移動(dòng)事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void gd_ball_MouseMove(object sender, MouseEventArgs e){if (e.LeftButton == MouseButtonState.Pressed){Point p = e.GetPosition((IInputElement)sender);Point d = new Point(0, 0);double max_leng = (gd_ball.ActualWidth - rt_wb.ActualWidth - rt_rect.ActualWidth) / 2;d.X= p.X - max_leng;d.Y = rect_info.Addr.Y;//rect_info.Addr.X = p.X - max_leng;if (d.X < -max_leng){d.X = -max_leng;}if (d.X > max_leng){d.X = max_leng;}rect_info.Addr = d;sd_x.Value = rect_info.Addr.X;}}/// <summary>/// 更改擋板坐標(biāo)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void sd_x_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e){Point d = new Point(0, 0);d.X= sd_x.Value;d.Y = rect_info.Addr.Y;//rect_info.Addr.X = sd_x.Value;double max_leng = (gd_ball.ActualWidth - rt_wb.ActualWidth - rt_rect.ActualWidth) / 2;if (d.X < -max_leng){d.X = -max_leng;}if (d.X > max_leng){d.X = max_leng;}rect_info.Addr = d;sd_x.Value = rect_info.Addr.X;}/// <summary>/// 重新開(kāi)始游戲 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void bt_crt_Click(object sender, RoutedEventArgs e){aTimer.Stop();System.Threading.Thread.Sleep(100);ball_info.Speed.X = 0;ball_info.Addr.X = 0;ball_info.Addr.Y = -10;brick_num = (int)sd_brick_num.Value;brick_run_stpe = 0;re_brick_count = 0;processCount.TotalSecond = 0;processCount.Stpe = 0;crt_zhuan(brick_info, brick_num);lb_brick_count.Content = brick_num;lb_re_count.Content = 0;lb_lost_count.Content = brick_list.Count - re_brick_count;lb_run_stpe.Content = brick_run_stpe;lb_time.Content = processCount.GetHour() + ":" + processCount.GetMinute() + ":" + processCount.GetSecond();}/// <summary>/// 啟動(dòng)運(yùn)行/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void bt_start_Click(object sender, RoutedEventArgs e){ball_info.Speed.X = 0;ball_info.Speed.Y = sd_bool_speed.Value;brick_num = (int)sd_brick_num.Value;aTimer.Start();}/// <summary>/// 關(guān)閉窗口/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Window_Closing(object sender, CancelEventArgs e){aTimer.Stop();}/// <summary>/// 關(guān)聯(lián)A鍵/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cb_start_game_Executed(object sender, ExecutedRoutedEventArgs e){bt_start_Click(null, null);}/// <summary>/// /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cb_start_game_CanExecute(object sender, CanExecuteRoutedEventArgs e){e.CanExecute = true;}/// <summary>/// 窗體大小改變/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Window_SizeChanged(object sender, SizeChangedEventArgs e){sd_x.Maximum = gd_ball.ActualWidth / 2;sd_x.Minimum = -gd_ball.ActualWidth / 2;}#endregion}/// <summary>/// 實(shí)現(xiàn)倒計(jì)時(shí)功能的類(lèi)/// </summary>public class ProcessCount{private Int32 _TotalSecond;private int _stpe = 0;public Int32 TotalSecond{get { return _TotalSecond; }set { _TotalSecond = value; }}public int Stpe{get { return _stpe; }set { _stpe = value; }}/// <summary>/// 構(gòu)造函數(shù)/// </summary>public ProcessCount(Int32 totalSecond, int s = 0){this._TotalSecond = totalSecond;this.Stpe = s;}/// <summary>/// 減秒/// </summary>/// <returns></returns>public bool ProcessCountDown(){if (_TotalSecond == 0)return false;else{_TotalSecond--;return true;}}/// <summary>/// 加秒/// </summary>/// <returns></returns>public bool ProcessCountUp(){_TotalSecond++;return true;}/// <summary>/// 獲取小時(shí)顯示值/// </summary>/// <returns></returns>public string GetHour(){return String.Format("{0:D2}", (_TotalSecond / 3600));}/// <summary>/// 獲取分鐘顯示值/// </summary>/// <returns></returns>public string GetMinute(){return String.Format("{0:D2}", (_TotalSecond % 3600) / 60);}/// <summary>/// 獲取秒顯示值/// </summary>/// <returns></returns>public string GetSecond(){return String.Format("{0:D2}", _TotalSecond % 60);}}public sealed class MillisecondTimer : IComponent, IDisposable{private static TimerCaps caps;private int interval;private bool isRunning;private int resolution;private TimerCallback timerCallback;private int timerID;public int Interval{get{return this.interval;}set{if ((value < caps.periodMin) || (value > caps.periodMax)){throw new Exception("超出計(jì)時(shí)范圍!");}this.interval = value;}}/// <summary>/// /// </summary>public bool IsRunning{get{return this.isRunning;}}/// <summary>/// /// </summary>public ISite Site{set;get;}public event EventHandler Disposed; // 這個(gè)事件實(shí)現(xiàn)了IComponet接口public event EventHandler Tick;static MillisecondTimer(){timeGetDevCaps(ref caps, Marshal.SizeOf(caps));}public MillisecondTimer(){this.interval = caps.periodMin; // this.resolution = caps.periodMin; //this.isRunning = false;this.timerCallback = new TimerCallback(this.TimerEventCallback);}public MillisecondTimer(IContainer container): this(){container.Add(this);}~MillisecondTimer(){timeKillEvent(this.timerID);}public void Start(){if (!this.isRunning){this.timerID = timeSetEvent(this.interval, this.resolution, this.timerCallback, 0, 1); // 間隔性地運(yùn)行if (this.timerID == 0){throw new Exception("無(wú)法啟動(dòng)計(jì)時(shí)器");}this.isRunning = true;}}public void Stop(){if (this.isRunning){timeKillEvent(this.timerID);this.isRunning = false;}}/// <summary>/// 實(shí)現(xiàn)IDisposable接口/// </summary>public void Dispose(){timeKillEvent(this.timerID);GC.SuppressFinalize(this);EventHandler disposed = this.Disposed;if (disposed != null){disposed(this, EventArgs.Empty);}}//*************************************************** 內(nèi)部函數(shù) ******************************************************************[DllImport("winmm.dll")]private static extern int timeSetEvent(int delay, int resolution, TimerCallback callback, int user, int mode);[DllImport("winmm.dll")]private static extern int timeKillEvent(int id);[DllImport("winmm.dll")]private static extern int timeGetDevCaps(ref TimerCaps caps, int sizeOfTimerCaps);// The timeGetDevCaps function queries the timer device to determine its resolution. private void TimerEventCallback(int id, int msg, int user, int param1, int param2){if (this.Tick != null){this.Tick(this, null); // 引發(fā)事件}}//*************************************************** 內(nèi)部類(lèi)型 ******************************************************************private delegate void TimerCallback(int id, int msg, int user, int param1, int param2); // timeSetEvent所對(duì)應(yīng)的回調(diào)函數(shù)的簽名/// <summary>/// 定時(shí)器的分辨率(resolution)。單位是ms,毫秒/// </summary>[StructLayout(LayoutKind.Sequential)]private struct TimerCaps{public int periodMin;public int periodMax;}} }
  • 代碼下載:
    打磚塊代碼V0.0.1_20210118
    試用程序:
    鏈接:https://pan.baidu.com/s/1HFWxnroo2Hgk6d7TtQ3p1w
    提取碼:3ayx

總結(jié)

以上是生活随笔為你收集整理的WPF砖块消除的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。