最近在学习C#WPF,边学习边写了一个拼图的小游戏
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace JigsawGame
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private string currentImageFile;
private static double jigsawWidthMax = 450.0;
private double jigsawHeight;
private Layout layout;
private Jigsaw currentJigsaw;
public MainWindow()
{
InitializeComponent();
layout = new Layout(2, 2);
currentImageFile = "image/leaf.jpg";
ImageZoom igZoom = new ImageZoom(currentImageFile);
igZoom.ZoomOutTo(jigsawWidthMax, ImageFormat.Jpeg, "tmp.jpg");
string currentImageFileTmp = "tmp.jpg";
Stream imageStreamSource = new FileStream(currentImageFileTmp, FileMode.Open, FileAccess.Read, FileShare.Read);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bitmapSource = decoder.Frames[0];
imagePuzzle.Source = bitmapSource;
Bitmap bitmap = new Bitmap(imageStreamSource);
imageStreamSource.Close();
File.Delete(currentImageFileTmp);
Jigsaw js = new Jigsaw(
bitmapSource,
new System.Windows.Point(200, 15),
new System.Windows.Size(ScreenUnitConverter.GetScreenWindth() - 230, ScreenUnitConverter.GetScreenHeight() - 130),
new System.Windows.Point(100, 100),
new System.Windows.Size(jigsawWidthMax, ScreenUnitConverter.PixelToInch(bitmap.Height)),
layout);
contentGrid.Children.Add(js);
currentJigsaw = js;
jigsawHeight = ScreenUnitConverter.PixelToInch(bitmap.Height);
textBox1.Visibility = System.Windows.Visibility.Hidden;
}
private void ImageChanged(object sender, MouseButtonEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Image Files|*.jpg;*.bmp;*.Tif;*.Png";
if (dlg.ShowDialog() == true)
{
currentImageFile = dlg.FileName;
}
ImageZoom igZoom = new ImageZoom(currentImageFile);
igZoom.ZoomOutTo(jigsawWidthMax, ImageFormat.Jpeg, "tmp.jpg");
string currentImageFileTmp = "tmp.jpg";
Stream imageStreamSource = new FileStream(currentImageFileTmp, FileMode.Open, FileAccess.Read, FileShare.Read);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bitmapSource = decoder.Frames[0];
imageStreamSource.Close();
File.Delete(currentImageFileTmp);
imagePuzzle.Source = bitmapSource;
currentJigsaw.Image = bitmapSource;
}
private void LayoutChanged(object sender, RoutedEventArgs e)
{
if (currentJigsaw != null)
{
RadioButton rdb = (RadioButton)sender;
if (e.Source is RadioButton && rdb != other)
{
string sss = rdb.Content.ToString();
layout.RowCount = Convert.ToInt32(sss.Substring(0, sss.LastIndexOf("x")));
layout.ColCount = Convert.ToInt32(sss.Substring(sss.LastIndexOf("x") + 1, sss.Length - sss.IndexOf("x") - 1));
currentJigsaw.Layout = layout;
}
if (e.Source is RadioButton && rdb == other)
{
string row = tbxLayoutRow.Text;
string col = tbxLayoutCol.Text;
layout.RowCount = Convert.ToInt32(row);
layout.ColCount = Convert.ToInt32(col);
currentJigsaw.Layout = layout;
}
}
}
private void GroupChanged(object sender, RoutedEventArgs e)
{
if (currentJigsaw != null)
{
RadioButton rdb = (RadioButton)sender;
if (e.Source is RadioButton)
{
string sss = rdb.Content.ToString();
if (sss == "组 合")
{
currentJigsaw.Group = true;
}
else
{
currentJigsaw.Group = false;
}
}
}
}
private void buttonStart_Click(object sender, RoutedEventArgs e)
{
if ((string)(buttonStart.Content) == "开 始")
{
imagePuzzle.IsEnabled = false;
radioButton2.IsEnabled = false;
radioButton3.IsEnabled = false;
radioButton4.IsEnabled = false;
radioButton5.IsEnabled = false;
radioButton6.IsEnabled = false;
other.IsEnabled = false;
rBNGroup.IsEnabled = false;
rBNNoGroup.IsEnabled = false;
buttonStart.Content = "结 束";
currentJigsaw.Spread();
}
else
{
imagePuzzle.IsEnabled = true;
radioButton2.IsEnabled = true;
radioButton3.IsEnabled = true;
radioButton4.IsEnabled = true;
radioButton5.IsEnabled = true;
radioButton6.IsEnabled = true;
other.IsEnabled = true;
rBNGroup.IsEnabled = true;
rBNNoGroup.IsEnabled = true;
buttonStart.Content = "开 始";
currentJigsaw.Reset();
}
}
}
}
Jigsaw.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Media.Effects;
namespace JigsawGame
{
public class Layout
{
public int RowCount;
public int ColCount;
public Layout()
{
RowCount = 2;
ColCount = 2;
}
public Layout(int rowCount, int colCount)
{
RowCount = rowCount;
ColCount = colCount;
}
}
/// <summary>
/// Jigsaw.xaml 的交互逻辑
/// </summary>
public partial class Jigsaw : UserControl
{
private BitmapSource image;
private Layout layout;
private Point position;
private Size size;
private bool isGroup;
private SetOfSets<PieceOfJigsaw> children;
private List<PieceOfJigsaw> availbleChildren;
private Random random = new Random();
public Jigsaw(
BitmapSource bitmapSource,
Point motherBoardPosition,
Size motherBoardSize,
Point jigsawPosition,
Size jigsawSize,
Layout lay)
{
InitializeComponent();
image = bitmapSource;
Width = motherBoardSize.Width;
Height = motherBoardSize.Height;
Horizont