c#拼图游戏

有的小伙伴说,c#不做桌面应用程序,都对不起c#这一门语言。。。

行!加班加点的,咱们也来一个小游戏~

先看看运行效果~

还行吧,哈哈哈,技术有限,别碰我奥~

先说下用到写啥,首先,看到计时没!!!诶,对,第一个就是线程~

看到左边这些小块没,没错,就是用右边的图切出来的,图片处理是吧,还有,emmm,好多好多,哈哈哈!

咱们先来一波,猜猜我多少时间完成~

等等,先说下游戏规则,哈哈哈,开始之后,wsad分别控制块上下左右移动,当图片拼接与右图一致时,获得胜利!

正式开始了!

emmm,九牛二虎之力啊....

接下来,咱们就开始说怎么做,第一步嘛,当然是从工具箱拖拖拖空间到form窗口上啦!

怎么拖得我就不细说了,咳咳~

看到红箭头指着的文件了吗?他们时要写代码的地方!

先写最简单的把,哈哈哈!ButtonPoint.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 拼图游戏
{
    class ButtonPoint
    {
        public int[,] buttonArray;
    }
}

怎么样,是不是超简单,哈哈哈!(我也不知道为啥要加这么一个类。。。)

下面这个是一个技术点!那就是切图~也就是图中第三个红箭头指着的ImageManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace 拼图游戏
{
    public class ImageManager
    {


        /// <summary>
        /// 图像切割
        /// </summary>
        /// <param name="url">图像文件名称</param>
        /// <param name="width">切割后图像宽度</param>
        /// <param name="height">切割后图像高度</param>
        /// <param name="savePath">切割后图像文件保存路径</param>
        /// <param name="fileExt">切割后图像文件扩展名</param>
        public void Cut(string url, int width, int height, string savePath, string fileExt, string logofile)
        {
            Bitmap bitmap = new Bitmap(url);
            Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
            Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
            for (decimal i = 0; i < MaxRow; i++)
            {
                for (decimal j = 0; j < MaxColumn; j++)
                {
                    string filename = ((i * 4) + j + 1) + fileExt;
                    Bitmap bmp = new Bitmap(width, height);

                    for (int offsetX = 0; offsetX < width; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < height; offsetY++)
                        {
                            if (((j * width + offsetX) < bitmap.Width) && ((i * height + offsetY) < bitmap.Height))
                            {
                                bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
                            }
                        }
                    }
                    Graphics g = Graphics.FromImage(bmp);
                    ImageFormat format = ImageFormat.Png;
                    switch (fileExt.ToLower())
                    {
                        case "png":
                            format = ImageFormat.Png;
                            break;
                        case "bmp":
                            format = ImageFormat.Bmp;
                            break;
                        case "gif":
                            format = ImageFormat.Gif;
                            break;
                    }
                    bmp.Save(savePath + "//" + filename, format);
                }
            }
        }
    }
}

咱们能切图了,下面就是一些逻辑部分,很头疼的!!!就是图中第二个箭头,右键单击选择查看代码之后编写~

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.Threading;

namespace 拼图游戏
{
    public partial class Form1 : Form
    {
        ButtonPoint bp;
        Thread t;
        string image_format;//切割后图像的后缀名
        int[,] endArray = new int[4, 4]{
            {1, 2, 3, 4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
        };
        int len = 60;
        int[,] button_Array = new int[4, 4]{            
            {1, 6, 8, 13},
            {2, 5, 10, 14},
            {3, 7, 11, 15},
            {4, 9, 0, 12}};

        public Form1()
        {
            InitializeComponent();

            bp = new ButtonPoint();
            bp.buttonArray = new int[4, 4];
            image_format = ".png";
        }
        int sec ;
        private void button1_Click(object sender, EventArgs e)
        {
            //开始游戏
            setAndReset();
            button1.Enabled = false;

            //启动计时
            sec = 0;
            t = new Thread(delegate()
            {
                while (true)
                {
                    this.Invoke(//执行委托
                        (MethodInvoker)delegate//匿名方法
                        {
                            bq_time.Text = sec.ToString();
                        });
                    Thread.Sleep(1000);//睡眠1s
                    sec++;
                }
            });
            t.IsBackground = true;
            t.Start();
            this.KeyPreview = true;//得到按键焦点
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化
            ImageManager im = new ImageManager();//切割图像对象

            string url = "bk.png";//图像地址
            int width = 146;//图像宽/4
            int height = 146;//图像高/4
            string saveUrl = ".//";//存储位置

            //设置当前操作图像
            an_tp.BackgroundImage = Image.FromFile(url);


            //切割图像
            im.Cut(url, width, height, saveUrl, image_format, null);

            //增加格子
            for (int q = 0; q < 4; q++)
            {
                for (int h = 0; h < 4; h++)
                {
                    bp.buttonArray[q, h] = button_Array[q, h];
                }
            }

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (bp.buttonArray[i, j] != 0)//留出一个位置方便移动
                    {
                        Button b = new Button();//创建一个新按钮
                        b.Name = bp.buttonArray[i, j].ToString();
                        b.Text = string.Empty;
                        b.Width = len;
                        b.Height = len;
                        b.Location = new Point(len * j, len * i + 8);
                        b.Enabled = false;
                        b.BackgroundImage = Image.FromFile(bp.buttonArray[i, j].ToString() + image_format);
                        b.BackgroundImageLayout = ImageLayout.Stretch;
                        gb_bk.Controls.Add(b);
                    }
                }
            }
        }

        private void setAndReset()
        {
            for (int q = 0; q < 4; q++)
            {
                for (int h = 0; h < 4; h++)
                {
                    bp.buttonArray[q, h] = button_Array[q, h];
                }
            }

            int i = 0;
            int j = 0;
            for (int k = 0; k < gb_bk.Controls.Count; k++)
            {
                if (bp.buttonArray[i, j] != 0)
                {
                    gb_bk.Controls[k].Location = new Point(len * j, len * i + 8);
                    j++;
                    if (j > 3)
                    {
                        j = 0;
                        i++;
                    }
                }
                else
                {
                    j++;
                    if (j > 3)
                    {
                        j = 0;
                        i++;
                    }
                    k--;
                }
            }
        }

        private bool isEnd()
        {
            for (int i = 0; i < 4; i++ )
            {
                for (int j = 0; j < 4; j++)
                {
                    if (endArray[i, j] != bp.buttonArray[i, j])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.W://上
                    bool flag = false;
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (bp.buttonArray[i, j] == 0 && i < 3)//找到空白的格子判断是否能移动
                            {
                                int temp = bp.buttonArray[i + 1, j];
                                bp.buttonArray[i + 1, j] = bp.buttonArray[i, j];
                                bp.buttonArray[i, j] = temp;
                                flag = true;
                                foreach (Control b in gb_bk.Controls)
                                {
                                    //MessageBox.Show(b.Name.ToString());
                                    if (b.Name.Equals(temp.ToString()))
                                    {
                                        b.Location = new Point(b.Location.X, b.Location.Y - 60);
                                        if (isEnd())
                                        {
                                            t.Abort();
                                            button1.Enabled = true;
                                            button1.Text = "再玩一次";
                                            this.KeyPreview = false;//得到按键焦点
                                            MessageBox.Show("恭喜你,游戏结束,总计用时" + sec + "s");
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        if (flag)
                        {
                            break;
                        }
                    }
                    break;
                case Keys.A://左
                    flag = false;
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (bp.buttonArray[i, j] == 0 && j < 3)
                            {
                                int temp = bp.buttonArray[i, j + 1];
                                bp.buttonArray[i, j + 1] = bp.buttonArray[i, j];
                                bp.buttonArray[i, j] = temp;
                                flag = true;
                                foreach (Control b in gb_bk.Controls)
                                {
                                    //MessageBox.Show("ssss");
                                    if (b.Name.Equals(temp.ToString()))
                                    {
                                        b.Location = new Point(b.Location.X - 60, b.Location.Y);
                                        if (isEnd())
                                        {
                                            t.Abort();
                                            button1.Enabled = true;
                                            button1.Text = "再玩一次";
                                            this.KeyPreview = false;//得到按键焦点
                                            MessageBox.Show("恭喜你,游戏结束,总计用时" + sec + "s");
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        if (flag)
                        {
                            break;
                        }
                    }
                    break;
                case Keys.S://下
                    //MessageBox.Show("Test");
                    flag = false;
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (bp.buttonArray[i, j] == 0 && i > 0)
                            {
                                int temp = bp.buttonArray[i - 1, j];
                                bp.buttonArray[i - 1, j] = bp.buttonArray[i, j];
                                bp.buttonArray[i, j] = temp;
                                flag = true;
                                foreach (Control b in gb_bk.Controls)
                                {
                                    //MessageBox.Show(b.Name.ToString());
                                    if (b.Name.Equals(temp.ToString()))
                                    {
                                        b.Location = new Point(b.Location.X, b.Location.Y + 60);
                                        if (isEnd())
                                        {
                                            t.Abort();
                                            button1.Enabled = true;
                                            button1.Text = "再玩一次";
                                            this.KeyPreview = false;//得到按键焦点
                                            MessageBox.Show("恭喜你,游戏结束,总计用时" + sec + "s");
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        if (flag)
                        {
                            break;
                        }
                    }
                    break;
                case Keys.D://右
                    flag = false;
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (bp.buttonArray[i, j] == 0 && j > 0)
                            {
                                int temp = bp.buttonArray[i, j - 1];
                                bp.buttonArray[i, j - 1] = bp.buttonArray[i, j];
                                bp.buttonArray[i, j] = temp;
                                flag = true;
                                foreach (Control b in gb_bk.Controls)
                                {
                                    //MessageBox.Show(b.Name.ToString());
                                    if (b.Name.Equals(temp.ToString()))
                                    {
                                        b.Location = new Point(b.Location.X + 60, b.Location.Y);
                                        if (isEnd())
                                        {
                                            t.Abort();
                                            button1.Enabled = true;
                                            button1.Text = "再玩一次";
                                            this.KeyPreview = false;//得到按键焦点
                                            MessageBox.Show("恭喜你,游戏结束,总计用时" + sec + "s");
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        if (flag)
                        {
                            break;
                        }
                    }
                    break;
            }
        }
    }
}

好了,代码都在上面的,大家学习的同时能够获得欢乐,这才是最重要的!

如果你不想部署项目,想想体验一下,好吧。。。

在这下载

c#做的拼图游戏-C#其他资源-CSDN下载

最后~祝大家学习愉快!

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

java拾荒者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值