我的第一个C#程序-智能拼图游戏

这篇博客介绍了一位作者在学习C# WPF过程中制作的一个拼图小游戏的实现过程,包括MainWindow.xaml.cs和Jigsaw.xaml.cs两个主要部分。代码展示了如何处理图像、布局、旋转和拖放等功能,允许用户选择不同难度级别的拼图并开始游戏。
摘要由CSDN通过智能技术生成

最近在学习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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值