C#拼图游戏

以前大学时候练图像绘制、旋转、切割的小练习,分享给学习winform的使用。贡献给.Net生态

下载地址:
https://download.csdn.net/download/zhanglianzhu_91/11055691

在这里插入图片描述
示例代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Media;

namespace Jigsaw
{
    public partial class Main : Form
    {
        Grid grid;
        int yNum = 2;
        int xNum = 2;
        int buShu = 0;
        int time = 0;
        Image image;
        List<Grade> list=new List<Grade> ();
        System.Media.SoundPlayer sundPlayer = new SoundPlayer();
        bool play = true;
        bool isEnd = false;

        public Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Load(object sender, EventArgs e)
        {
            
            image=Image.FromFile(Application.StartupPath+"\\1.jpg");
            grid=new Grid (image,xNum,yNum);
            Bitmap bit = null;
            if (image.Width < image.Height)
            {
                bit = new Bitmap(image.Width, image.Width);
            }
            else
            {
                bit = new Bitmap(image.Height, image.Height);
            }
            Graphics g = Graphics.FromImage(bit);
            g.DrawImage(image, 0, 0);
            image = bit;
            pictureBox1.Image = image;
            pictureBox1.Image = image;
            try
            {
                FileStream fs = new FileStream(Application.StartupPath + "\\grade", FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                list = (List<Grade>)formatter.Deserialize(fs);
                fs.Close();
                ShowGrad();
            }
            catch (Exception)
            {
              
            }

            
        }

        /// <summary>
        /// Paint函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Paint(object sender, PaintEventArgs e)
        {
            if (grid != null)
            {
                grid.Draw(this.CreateGraphics());
            }
        }

        /// <summary>
        /// 鼠标单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_MouseClick(object sender, MouseEventArgs e)
        { 
            if (e.X > 10 && e.X < 505&&e.Y>50&&e.Y<545)
            {
                BeginNew();
            }
            if (e.X < 10 || e.X > 505)
            {
                return;
            }
            if (e.Y < 50 || e.Y > 545)
            {
                return;
            }
           
            Rectangle rect = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum)+11, grid.Cells[0, 0].CurNum /yNum * (500 / xNum)+51, image.Width / xNum-2, 500 / yNum-2);
            Rectangle rectUp = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51 - (500 / xNum), 500 / xNum - 2, 500 / yNum - 2);
            Rectangle rectLow = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51 + (500 / xNum), 500 / xNum - 2, 500 / yNum - 2);
            Rectangle rectLeft = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) - (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51, 500 / xNum - 2, 500 / yNum - 2);
            Rectangle rectRight = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51, 500 / xNum - 2, 500 / yNum - 2);
            int x=e.X;
            int y=e.Y;
            if(IsInRect(x,y,rectUp))
            {
                int curNum = grid.Cells[0, 0].CurNum;
                Cell cell = FindCell(curNum - yNum);
                grid.Cells[0, 0].CurNum = cell.CurNum;
                cell.CurNum = curNum;
                buShu++;

            }
            if (IsInRect(x, y, rectLow))
            {
                int curNum = grid.Cells[0, 0].CurNum;
                Cell cell = FindCell(curNum+yNum);
                grid.Cells[0, 0].CurNum = cell.CurNum;
                cell.CurNum = curNum;
                buShu++;
            }
            if (IsInRect(x, y, rectLeft))
            {
                int curNum = grid.Cells[0, 0].CurNum;
                Cell cell = FindCell(curNum -1);
                grid.Cells[0, 0].CurNum = cell.CurNum;
                cell.CurNum = curNum;
                buShu++;
            }
            if (IsInRect(x, y, rectRight))
            {
                int curNum = grid.Cells[0, 0].CurNum;
                Cell cell = FindCell(curNum+1);
                grid.Cells[0, 0].CurNum = cell.CurNum;
                cell.CurNum = curNum;
                buShu++;
            }
            grid.Draw(this.CreateGraphics());
            label3.Text = "步数:" + buShu;
            if (IsEnd())
            {
                timer1.Enabled = false;
                grid.IsEnd = true;
                grid.Draw(this.CreateGraphics());
                isEnd = true;
                MessageBox.Show("完成", "完成提示");
                PaiMing paiMing = new PaiMing();
                int count = 0;
                foreach (Grade g in list)
                {
                    if(g.Level==Convert.ToInt32(comboBox1.Text))
                    {
                        count++;
                    }
                }
                
                if (count<= 5)
                {
                    if (paiMing.ShowDialog() == DialogResult.OK)
                    {
                        if (paiMing.textBox1.Text == "")
                        {
                            return;
                        }
                        Grade gra = new Grade();
                        gra.Name = paiMing.textBox1.Text;
                        gra.Step = buShu;
                        gra.Time = time;
                        time = 0;
                        gra.Level = Convert.ToInt32(comboBox1.Text);
                        list.Add(gra);
                    }
                }
                else
                {

                     foreach (Grade g in list)
                     {
                         if (g.Level != Convert.ToInt32(comboBox1.Text))
                         {
                             continue;
                         }
                         if (time < g.Time)
                         {
                             int i = 0;
                             l1:
                             Grade gr=list[i];
                             if (gr.Level != Convert.ToInt32(comboBox1.Text))
                             {
                                 if (i < list.Count-1)
                                 {
                                     i++;
                                 }
                                 goto l1;
                             }
                             foreach (Grade g1 in list)
                             {
                                 if (g1.Level != Convert.ToInt32(comboBox1.Text))
                                 {
                                     continue;
                                 }
                                 if (g1.Time > gr.Time)
                                 {
                                     gr = g1;
                                 }
                             }
                             if (paiMing.ShowDialog() == DialogResult.OK)
                             {
                                 if (paiMing.textBox1.Text == "")
                                 {
                                     return;
                                 }
                                 Grade gra = new Grade();
                                 gra.Name = paiMing.textBox1.Text;
                                 gra.Step = buShu;
                                 gra.Time = time;
                                 gra.Level = Convert.ToInt32(comboBox1.Text);
                                 time = 0;
                                 list.Add(gra);
                                 list.Remove(gr);

                             }
                             break;
                             
                         }
                     
                     }
                }
                File.Delete(Application.StartupPath + "\\grade");
                FileStream stream = new FileStream(Application.StartupPath+"\\grade", FileMode.CreateNew);
                BinaryFormatter formater = new BinaryFormatter();
                formater.Serialize(stream,list);
                stream.Close();
                ShowGrad();
                buShu = 0;
                
            }
        }

        /// <summary>
        /// 开始新的一次
        /// </summary>
        private void BeginNew()
        {
            if (isEnd)
            {
                buShu = 0;
                time = 0;
                timer1.Enabled = true;
                xNum = Convert.ToInt32(comboBox1.Text) + 1;
                yNum = xNum;
                grid = new Grid(image, xNum, yNum);
                grid.Draw(this.CreateGraphics());
                ShowGrad();
                isEnd = false;
                return;
            }
        }

        /// <summary>
        /// 判断点是否在矩形
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        private bool IsInRect(int x,int y,Rectangle rect)
        {
            if (x > rect.X && x < rect.X + rect.Width && y > rect.Y && y < rect.Y + rect.Height)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 找单元
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private Cell FindCell(int num)
        {
            for (int i = 0; i < xNum; i++)
            {
                for (int j = 0; j < yNum; j++)
                {
                    if (grid.Cells[i, j].CurNum == num)
                    {
                        return grid.Cells[i, j];
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 判断结束
        /// </summary>
        /// <returns></returns>
        private bool IsEnd()
        {
            for (int i = 0; i < xNum; i++)
            {
                for (int j = 0; j < yNum; j++)
                {
                    if (grid.Cells[i, j].Num != grid.Cells[i, j].CurNum)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 选择级别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            buShu = 0;
            time = 0;
            timer1.Enabled = true;
            xNum = Convert.ToInt32(comboBox1.Text)+1;
            yNum = xNum;
            grid = new Grid(image, xNum, yNum);
            Bitmap bk = new Bitmap(600,600);
            Graphics g = Graphics.FromImage(bk);
            Brush brush=new SolidBrush(this.BackColor);
            g.FillRectangle(brush, 0, 0, 600, 600);
            this.CreateGraphics().DrawImage(bk, 0, 0);
            grid.Draw(this.CreateGraphics());
            ShowGrad();

        }

        /// <summary>
        /// 打开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "JPG 图片|*.jpg|All Files|*.*";
            op.InitialDirectory = Application.StartupPath;
            op.ShowDialog();
            if (op.FileName != "")
            {
                image = Image.FromFile(op.FileName);
                Bitmap bit=null;
                if (image.Width < image.Height)
                {
                    bit = new Bitmap(image.Width, image.Width);
                }
                else
                {
                    bit = new Bitmap(image.Height, image.Height);
                }
                Graphics g = Graphics.FromImage(bit);
                g.DrawImage(image,0,0);
                image = bit;
                pictureBox1.Image = image;
                xNum = Convert.ToInt32(comboBox1.Text) + 1;
                yNum = xNum;
                grid = new Grid(image, xNum, yNum);
                grid.Draw(this.CreateGraphics());
                buShu = 0;
                time = 0;
                timer1.Enabled = true;
            }
        }

        /// <summary>
        /// 显示排名
        /// </summary>
        private void ShowGrad()
        {
            listBox1.Items.Clear();
            List<Grade> list1 = new List<Grade>();
            List<Grade> list2 = new List<Grade>();
            foreach (Grade g2 in list)
            {
                if (g2.Level == Convert.ToInt32(comboBox1.Text))
                {
                    list2.Add(g2);
                }
            }
            if (list2.Count == 0)
            {
                return;
            }
            l:
            Grade gr = list2[0];
            foreach (Grade g1 in list2)
            {

                if (g1.Time < gr.Time)
                {
                    gr = g1;
                }
            }
            list1.Add(gr);
            list2.Remove(gr);
            if (list2.Count != 0)
            {
                goto l;
            }
            if (list1.Count == 0)
            {
                return;
            }
            int i = 1;
            foreach (Grade g1 in list1)
            {
                listBox1.Items.Add(g1.Name);
                listBox1.Items.Add("第"+i+"名:"+"时间:" + g1.Time + "秒,   步数:" + g1.Step);
                i++;
                listBox1.Items.Add("---------------------------------");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (play)
            {
                sundPlayer.SoundLocation = Application.StartupPath + "\\voice.wav"; 
                sundPlayer.Load();
                sundPlayer.PlayLooping();
                play = false;
                this.button1.Text = "关闭";
            }
            else
            {
                sundPlayer.SoundLocation = Application.StartupPath + "\\voice.wav";
                sundPlayer.Stop();
                play = true;
                this.button1.Text = "播放";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            time++;
            label4.Text = "时间:" + time;
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHelpe_Click(object sender, EventArgs e)
        {
            Help help = new Help();
            help.Show();
        }

        private void btnBout_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
        
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值