在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#基础:简易拼图小游戏源码

C#基础:简易拼图小游戏源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.54M
  • 下载次数:135
  • 浏览次数:768
  • 发布时间:2019-09-11
  • 实例类别:C#语言基础
  • 发 布 人:water&Y
  • 文件格式:.zip
  • 所需积分:0
 相关标签: 小游戏 基础 C# 拼图 游戏

实例介绍

【实例简介】

基于VS15 的 C# .NET Framework 4.5 ,简易拼图小游戏

【实例截图】

from clipboardfrom clipboard

from clipboard


from clipboard





【核心代码】

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PuzzleGame
{
    public class Puzzle
    {
        //游戏难度设置
        public enum Diff         
        {
            simple,//简单
            ordinary,//一般
            difficulty//困难
        }
        private struct Node      //单元格结构体
        {
            public Image Img;
            public int Num;
        }
        private Image _img;      //拼图图片
        public int Width;       //拼图边长
        private Diff _gameDif;   //游戏难度
        private Node[,] node;    //单元格数组
        public int N;           //单元格数组行列数

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Img">拼图大图</param>
        /// <param name="GameDif">游戏难度,该类下结构体Diff</param>
        public Puzzle(Image Img,int Width, Diff GameDif)
        {
            this._gameDif = GameDif;
            this._img = Img;
            this.Width = Width;
            switch(this._gameDif)
            {
                case Diff.simple:
                    this.N = 3;
                    node=new Node[3,3];//简单关卡 3*3 的九宫格
                    break;
                case Diff.ordinary:
                    this.N = 4;
                    node = new Node[4, 4];//一般难度的则为 4*4
                    break;
                case Diff.difficulty:
                    this.N = 9;
                    node = new Node[9, 9];//困难的则为 9*9
                    break;
            }
            
            //分割图片形成各单元保存在数组中
            int Count = 0;
            for (int x = 0; x < this.N; x )
            {
                for (int y = 0; y < this.N; y )
                {

                    node[x, y].Img = CaptureImage(this._img, this.Width / this.N, this.Width / this.N, x * (this.Width / this.N), y * (this.Width / this.N));
                    node[x, y].Num = Count;
                    Count ;
                }
            }
            
            for (int x = 0; x < this.N; x )
            {
                for (int y = 0; y < this.N; y )
                {

                    Graphics newGra = Graphics.FromImage(node[x, y].Img);
                    newGra.DrawLine(new Pen(Color.White), new Point(0, 0), new Point(0, this.Width / this.N));
                    newGra.DrawLine(new Pen(Color.White), new Point(0, 0), new Point(this.Width / this.N, 0));
                    newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(this.Width / this.N, 0));
                    newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(0,this.Width / this.N));
                }
            }
            //(最后一项为空单独处理)
            node[N - 1, N - 1].Img = Image.FromFile(" D:\\images\\end.png"); //图片路径: D:\\images\\puzzle.jpg
             Graphics newGra2 = Graphics.FromImage(node[N - 1, N - 1].Img);
            newGra2.DrawLine(new Pen(Color.Red), new Point(1, 1), new Point(1, this.Width / this.N - 1));
            newGra2.DrawLine(new Pen(Color.Red), new Point(1, 1), new Point(this.Width / this.N - 1, 1));
            newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - 1, this.Width / this.N - 1), new Point(this.Width / this.N - 1, 1));
            newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - 1, this.Width / this.N - 1), new Point( 1,this.Width / this.N - 1));
            //打乱拼图
            this.Upset();

        }


        /// <summary>
        /// 由图片fromImage中截图并返回
        /// </summary>
        /// <param name="fromImage">原图片</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="spaceX">起始X坐标</param>
        /// <param name="spaceY">起始Y坐标</param>
        /// <returns></returns>
        public  Image CaptureImage(Image fromImage, int width, int height, int spaceX, int spaceY)
        {
            int x = 0;
            int y = 0;
            int sX = fromImage.Width - width;
            int sY = fromImage.Height - height;
            if (sX > 0)
            {
                x = sX > spaceX ? spaceX : sX;
            }
            else
            {
                width = fromImage.Width;
            }
            if (sY > 0)
            {
                y = sY > spaceY ? spaceY : sY;
            }
            else
            {
                height = fromImage.Height;
            }

            //创建新图位图   
            Bitmap bitmap = new Bitmap(width, height);
            //创建作图区域   
            Graphics graphic = Graphics.FromImage(bitmap);
            //截取原图相应区域写入作图区   
            graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            //从作图区生成新图   
            Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
            return saveImage;
        }
        /// <summary>
        /// 移动坐标(x,y)拼图单元
        /// </summary>
        /// <param name="x">拼图单元x坐标</param>
        /// <param name="y">拼图单元y坐标</param>
        public bool Move(int x,int y)
        {
            if (x 1 != N && node[x 1, y].Num ==  N * N - 1)
            {
                Swap(new Point(x 1, y), new Point(x, y));
                return true;
            }
            if (y 1 != N && node[x, y 1].Num ==  N * N - 1)
            {
                Swap(new Point(x, y 1), new Point(x, y));
                return true;
            }                
            if (x - 1 != -1 && node[x - 1, y].Num == N * N - 1)
            {
                Swap(new Point(x - 1, y), new Point(x, y));
                return true;
            }   
            if (y - 1 != -1 && node[x, y - 1].Num == N * N - 1)
            {
                Swap(new Point(x, y - 1), new Point(x, y));
                return true;
            }
            return false;
                
        }
        //交换图片
        private  void Swap(Point a, Point b)
        {
            Node temp = new Node();
            temp = this.node[a.X, a.Y];
            this.node[a.X, a.Y] = this.node[b.X, b.Y];
            this.node[b.X, b.Y] = temp;
        }
        public bool Judge()
        {
            int count=0;
            for (int x = 0; x < this.N; x )
            {
                for (int y = 0; y < this.N; y )
                {
                    if (this.node[x, y].Num != count)
                        return false;
                    count ;
                }
            }
            return true;
        }
        public Image Display()
        {
            Bitmap bitmap = new Bitmap(this.Width, this.Width);
            //创建作图区域   
            Graphics newGra = Graphics.FromImage(bitmap);
            for (int x = 0; x < this.N; x )
                for (int y = 0; y < this.N; y )
                    newGra.DrawImage(node[x, y].Img, new Point(x * this.Width / this.N, y * this.Width / this.N));
            return bitmap;
        }
        /// <summary>
        /// 打乱图片
        /// </summary>
        public void Upset()
        {
            int sum = 100000;
            if (this._gameDif == Diff.simple) sum = 10000;
            Random ran = new Random();
            for (int i = 0, x = N - 1, y = N - 1; i < sum; i )
            {
                long tick = DateTime.Now.Ticks;
                ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)|ran.Next());
                switch (ran.Next(0, 4))
                {
                    case 0:
                        if (x 1 != N)
                        {
                            Move(x 1, y);
                            x = x 1;
                        }
                            
                        break;
                    case 1:
                        if (y 1 != N)
                        {
                            Move(x, y 1);
                            y = y 1;
                        } 
                        break;
                    case 2:
                        if (x - 1 != -1)
                        {
                            Move(x - 1, y);
                            x = x - 1;
                        }      
                        break;
                    case 3:
                        if (y - 1 != -1)
                        {
                            Move(x, y - 1);
                            y = y - 1;
                        }
                        break;
                }

            }
        }

        

    }
}



实例下载地址

C#基础:简易拼图小游戏源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警