生活随笔
收集整理的這篇文章主要介紹了
WPF砖块消除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF的磚塊消除,基本實現磚塊消除公共
<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
="磚塊總數:" Foreground
="Azure"/><Label Grid
.Row
="1" Grid
.Column
="0" Content
="已消除總數:" Foreground
="Azure"/><Label Grid
.Row
="2" Grid
.Column
="0" Content
="當前用時:" 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
="彈起數:" 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
="第①步:設置磚塊數量和小球速度 第②步:點擊刷新界面生成磚塊 第③步:點擊啟動(或單擊“a”開始)開始消除磚塊 移動方式:按住鼠標左鍵即可移動擋板" 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
="球坐標:"/><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
="磚塊數量:"/><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
="啟動" Margin
="3" Height
="40" x
:Name
="bt_start" Click
="bt_start_Click"/><Label Content
="擋板中心坐標:"/><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-火狐鞥 2021-01-14" HorizontalAlignment
="Center" VerticalAlignment
="Center" TextAlignment
="Center" FontSize
="15" Foreground
="Gray"/></Grid
></Grid
>
</Window
>
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 打磚塊
{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 申明類public struct Ball
{public Point Addr
; public Point Speed
; public Point Dir
; public int Radius
; }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#regionprivate ProcessCount processCount
;private int brick_run_stpe
= 0; 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; private List
<int> brick_addr
= new List<int>();double Max_leng
= 0;#endregion#region 定時器MillisecondTimer aTimer
;private int timer_count
=0;void timer_init(){aTimer
= new MillisecondTimer();aTimer
.Tick
+= timedo
; ;aTimer
.Interval
= 1; processCount
= new ProcessCount(0);aTimer
.Stop();}private void timedo(object sender
, EventArgs e
) {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 [DllImport("kernel32.dll")]private static extern bool SetProcessWorkingSetSize(IntPtr proc
, int min
, int max
);private void ClearMemory(){GC
.Collect();GC
.WaitForPendingFinalizers();if (Environment
.OSVersion
.Platform
== PlatformID
.Win32NT
){SetProcessWorkingSetSize(System
.Diagnostics
.Process
.GetCurrentProcess().Handle
, -1, -1);}}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
++) {if (buf
[k
] == i
) {num
= num
+ 1;break;}}if (num
== 0){buf
.Add(i
);}}buf
.Sort();return buf
;}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
);T childType
= child
as T
;if (childType
== null){foundChild
= FindChild<T>(child
, childName
);if (foundChild
!= null) break;}else if (!string.IsNullOrEmpty(childName
)){var frameworkElement
= child
as FrameworkElement
;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 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
) {return re
;}else{if ((b
.Addr
.X
>= p
.Addr
.X
- p
.Width
/ 2 + b
.Radius
) && (b
.Addr
.X
<= p
.Addr
.X
+ p
.Width
/ 2 - b
.Radius
)) {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 {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
)){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 {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); 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
;}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); 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;}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
;}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); 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); 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
;}}private double wb_val
= 0;private int time_count
= 0;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 {}}else if (ball_info
.Addr
.Y
> -10){if (System
.Math
.Abs(ball_info
.Addr
.X
- wb_val
) > 50){}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
;}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() + " 總共用時:" + processCount
.TotalSecond
.ToString()+"s") ; ;}}}}}}}}}}catch{ }}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("磚過大或數量過多,請重新生成\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
== 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
= 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
};ret_t
.SetValue(Grid
.RowProperty
, i
);ret_t
.SetValue(Grid
.ColumnProperty
, j
);gd_table
.Children
.Add(ret_t
);break;}}count
++;}}}#endregion#regionprivate void sd_bool_speed_ValueChanged(object sender
, RoutedPropertyChangedEventArgs
<double> e
){ball_info
.Speed
.Y
= (int)(sd_bool_speed
.Value
);}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
;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
;}}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
;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
;}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();}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();}private void Window_Closing(object sender
, CancelEventArgs e
){aTimer
.Stop();}private void cb_start_game_Executed(object sender
, ExecutedRoutedEventArgs e
){bt_start_Click(null, null);}private void cb_start_game_CanExecute(object sender
, CanExecuteRoutedEventArgs e
){e
.CanExecute
= true;}private void Window_SizeChanged(object sender
, SizeChangedEventArgs e
){sd_x
.Maximum
= gd_ball
.ActualWidth
/ 2;sd_x
.Minimum
= -gd_ball
.ActualWidth
/ 2;}#endregion}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; }}public ProcessCount(Int32 totalSecond
, int s
= 0){this._TotalSecond
= totalSecond
;this.Stpe
= s
;}public bool ProcessCountDown(){if (_TotalSecond
== 0)return false;else{_TotalSecond
--;return true;}}public bool ProcessCountUp(){_TotalSecond
++;return true;}public string GetHour(){return String
.Format("{0:D2}", (_TotalSecond
/ 3600));}public string GetMinute(){return String
.Format("{0:D2}", (_TotalSecond
% 3600) / 60);}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("超出計時范圍!");}this.interval
= value;}}public bool IsRunning
{get{return this.isRunning
;}}public ISite Site
{set;get;}public event EventHandler Disposed
; 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); if (this.timerID
== 0){throw new Exception("無法啟動計時器");}this.isRunning
= true;}}public void Stop(){if (this.isRunning
){timeKillEvent(this.timerID
);this.isRunning
= false;}}public void Dispose(){timeKillEvent(this.timerID
);GC
.SuppressFinalize(this);EventHandler disposed
= this.Disposed
;if (disposed
!= null){disposed(this, EventArgs
.Empty
);}}[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
);private void TimerEventCallback(int id
, int msg
, int user
, int param1
, int param2
){if (this.Tick
!= null){this.Tick(this, null); }}private delegate void TimerCallback(int id
, int msg
, int user
, int param1
, int param2
); [StructLayout(LayoutKind
.Sequential
)]private struct TimerCaps
{public int periodMin
;public int periodMax
;}}
}
- 代碼下載:
打磚塊代碼V0.0.1_20210118
試用程序:
鏈接:https://pan.baidu.com/s/1HFWxnroo2Hgk6d7TtQ3p1w
提取碼:3ayx
總結
以上是生活随笔為你收集整理的WPF砖块消除的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。