Puzzle,這的確是個最簡單的遊戲....... 等WPF再熟悉點,做個踩雷遊戲或貪吃蛇來玩玩看好了..... using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Controls.Primitives; using System.Windows.Threading; namespace Cloud.PlayPuzzle { public class PlayPuzzle : Window { const int NumRows = 4; const int NumCols = 4; int xEmpty, yEmpty, iCounter; Key[] keys = { Key.Left,Key.Right,Key.Up,Key.Down}; Random rand; UIElement elEmptySpare = new Empty(); UniformGrid unigrid; [STAThread] public static void Main() { Application app = new Application(); app.Run(new PlayPuzzle()); } public PlayPuzzle() { Title = "Cloud Orz"; SizeToContent = SizeToContent.WidthAndHeight; ResizeMode = ResizeMode.CanMinimize; Background = SystemColors.ControlBrush; // create stack panel StackPanel stack = new StackPanel(); this.Content = stack; Button btn = new Button(); btn.Content = "_Scramble"; btn.Margin = new Thickness(10); btn.HorizontalAlignment = HorizontalAlignment.Center; btn.Click += new RoutedEventHandler(btn_Click); stack.Children.Add(btn); Border bord = new Border(); bord.BorderBrush = SystemColors.ControlDarkDarkBrush; bord.BorderThickness = new Thickness(1); stack.Children.Add(bord); unigrid = new UniformGrid(); unigrid.Rows = NumRows; unigrid.Columns = NumCols; bord.Child = unigrid; int total = NumCols * NumRows - 1; for (int i = 0; i < total; i++) { Tile tile = new Tile(); tile.Text = (i + 1).ToString(); tile.MouseLeftButtonDown += new MouseButtonEventHandler(tile_MouseLeftButtonDown); unigrid.Children.Add(tile); } unigrid.Children.Add(new Empty()); xEmpty = NumCols - 1; yEmpty = NumRows - 1; } void tile_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Tile tile = sender as Tile; int iMove = unigrid.Children.IndexOf(tile); int xMove = iMove % NumCols; int yMove = iMove / NumCols; if (xMove == xEmpty) while (yMove != yEmpty) MoveTile(xMove, yEmpty + (yMove - yEmpty) / Math.Abs(yMove - yEmpty)); if (yMove == yEmpty) while (xMove != xEmpty) MoveTile(xEmpty + (xMove - xEmpty) / Math.Abs(xMove - xEmpty),yMove ); } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); switch (e.Key) { case Key.Right: MoveTile(xEmpty - 1, yEmpty); break; case Key.Left: MoveTile(xEmpty + 1, yEmpty); break; case Key.Up: MoveTile(xEmpty, yEmpty + 1); break; case Key.Down: MoveTile(xEmpty, yEmpty - 1); break; } } void btn_Click(object sender, RoutedEventArgs e) { rand = new Random(); iCounter = 16 * NumCols * NumRows; DispatcherTimer tmr = new DispatcherTimer(); tmr.Interval = TimeSpan.FromMilliseconds(10); tmr.Tick += new EventHandler(tmr_Tick); tmr.Start(); } void tmr_Tick(object sender, EventArgs e) { for (int i = 0; i < 5; i++) { MoveTile(xEmpty, yEmpty + rand.Next(3) - 1); MoveTile(xEmpty + rand.Next(3) - 1, yEmpty); } if (0 == iCounter--) (sender as DispatcherTimer).Stop(); } void MoveTile(int xTile,int yTile) { if ((xTile == xEmpty && yTile == yEmpty) || xTile < 0 || xTile >= NumCols || yTile < 0 || yTile >= NumRows) return; int iTile = NumCols * yTile + xTile; int iEmpty = NumCols * yEmpty + xEmpty; UIElement elTile = unigrid.Children[iTile]; UIElement elEmpty = unigrid.Children[iEmpty]; unigrid.Children.RemoveAt(iTile); unigrid.Children.Insert(iTile, elEmptySpare); unigrid.Children.RemoveAt(iEmpty); unigrid.Children.Insert(iEmpty, elTile); xEmpty = xTile; yEmpty = yTile; elEmptySpare = elEmpty; } } / class Empty : FrameworkElement { } / class Tile : Canvas { const int SIZE = 64; const int BORD = 6; TextBlock txtblk; public Tile() { Width = SIZE; Height = SIZE; Polygon poly = new Polygon(); poly.Points = new PointCollection(new Point[] { new Point(0,0),new Point(SIZE,0),new Point(SIZE-BORD,BORD), new Point(BORD,BORD),new Point(BORD,SIZE-BORD),new Point(0,SIZE) }); poly.Fill = SystemColors.ControlLightBrush; Children.Add(poly); poly = new Polygon(); poly.Points = new PointCollection(new Point[] { new Point(SIZE,SIZE),new Point(SIZE,0),new Point(SIZE-BORD,BORD), new Point(SIZE-BORD,SIZE-BORD),new Point(BORD,SIZE-BORD),new Point(0,SIZE) }); poly.Fill = SystemColors.ControlDarkBrush; Children.Add(poly); Border bord = new Border(); bord.Width = SIZE - 2 * BORD; bord.Height = SIZE - 2 * BORD; bord.Background = SystemColors.ControlBrush; Children.Add(bord); SetLeft(bord, BORD); SetTop(bord, BORD); txtblk = new TextBlock(); txtblk.FontSize = 32; txtblk.Foreground = SystemColors.ControlTextBrush; txtblk.HorizontalAlignment = HorizontalAlignment.Center; txtblk.VerticalAlignment = VerticalAlignment.Center; bord.Child = txtblk; } public string Text { set { txtblk.Text = value; } get { return txtblk.Text; } } } / }