⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameform.cs

📁 c#编写的连连看 功能和单机版的差不多 自己看吧 呵呵
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace 蒋锟连连看
{
    public partial class GameForm : Form
    {
        private Random rdImage = null;
        //private string soundFile;
        private Graphics gPic = null;
        private Image[] images = new Image[39];
        private int[,] picKey = new int[XL, YL];//图形数组
        private int[,] picKeyHalfPart = new int[XL, YL / 2];
        private int[,] key = new int[XL, YL];//记录各点是否可达
        private bool[,] isClicked = new bool[XL, YL];//记录图形的被点击状态
        private Bitmap bmp;
        private int x1, y1, x2, y2;
        private int Tx1 = -1, Ty1, Tx2, Ty2;//用于记录提示时所消除的点的位置
        private const int PicSize = 30;//图形宽度
        private const int XL = 18;//图形数组横坐标
        private const int YL = 10;//图形数组纵坐标
        private const int GameTimeSec = 60;//游戏开始时初始的时间
        //private int WMPlayerCount;//播放器标号
        private List<Point> clickedPointArray = new List<Point>();//存储被点击的并且图片相同的点
        private List<Point> pathPointArray = new List<Point>();//存储两图间可达路径经过的点
        enum directions { LeftToRight, UpToDown, RightToLeft, DownToUp, None };//缩进方向
        //enum SoundType { BGSound, WAV };//播放类别
        enum EffectType { Bomb, Path };//特效类别
        private directions dirs;
        //private SoundType soundType;
        private EffectType effectType;
        private int hintTimes = 6;
        
        public GameForm()
        {
            InitializeComponent();
            gPic = this.pictureBox1.CreateGraphics();
            rdImage = new Random();
            InitImages();
            bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
            ReDrawTimer.Interval = 30;
            dirs = directions.None;
            this.pBar.Value = GameTimeSec;
            //this.btnTip.Enabled = false;
            //this.btnReDraw.Enabled = false;
            //this.btnStart.Enabled = false;
            //bool a = loadingForm.IsDisposed;
            this.LFIsDisposeTimer.Start();
        }
        private bool win()//判断是否胜利
        {
            for (int i = 1; i < XL - 1; i++)
            {
                for (int j = 1; j < YL - 1; j++)
                {
                    if (picKey[i, j] > 0)
                        return false;
                }
            }
            return true;
        }
        private void DrawInThread(int x1, int y1, int x2, int y2, EffectType effectType)//启动绘制特效图线程
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.effectType = effectType;
            Thread drawThread = new Thread(new ThreadStart(DrawSpecialEffect));
            drawThread.Start();
        }
        public void DrawSpecialEffect()
        {
            Graphics g = pictureBox1.CreateGraphics();
            if (effectType == EffectType.Bomb)
            {
                for (int i = 1; i < 7; i++)
                {
                    Image bombImages = Image.FromFile(@"Images\B" + i.ToString() + ".bmp");
                    g.DrawImage(bombImages, x1, y1, PicSize + 1, PicSize + 1);
                    g.DrawImage(bombImages, x2, y2, PicSize + 1, PicSize + 1);
                    Thread.Sleep(50);
                }
            }
            if (effectType == EffectType.Path)
            {
                Image dotsImages = Image.FromFile(@"Images\dots.gif");
                for (int i = 0; i < pathPointArray.Count; i++)
                {
                    g.DrawImage(dotsImages, pathPointArray[i].X * PicSize, pathPointArray[i].Y * PicSize, PicSize, PicSize);
                }
            }
            try
            {
                Thread.CurrentThread.Abort();// Thread.CurrentThread.Join();
            }
            catch
            {
                return;
            }
        }
        /*[DllImport("winmm.dll", EntryPoint = "PlaySound", SetLastError = true)]
        public static extern long PlaySound(string lpszName, int hModule,
            int dwFlags);
        public void PlayASound()
        {
            if (soundType == SoundType.WAV)
            {
                PlaySound("Sounds\\" + soundFile, 0, 0);
            }
            else if (soundType == SoundType.BGSound)
            {
                if (WMPlayerCount == 1)
                {
                    this.MyMediaPlayer.URL = "Sounds\\" + soundFile;
                }
                if (WMPlayerCount == 2)
                {
                    this.MyWMPlayer2.URL = "Sounds\\" + soundFile;
                }
                this.MyMediaPlayer.settings.playCount = 10;
            }
            Thread.CurrentThread.Abort();
        }*/
        /*private void PlaySoundInThread(string waveFile, SoundType soundType, int WMPlayerCount)//启动声音播放线程
        {
            this.soundFile = waveFile;
            this.soundType = soundType;
            this.WMPlayerCount = WMPlayerCount;
            //Thread soundThread = new Thread(new ThreadStart(PlayASound));
            //soundThread.Start();
        }*/
        public void InitisClicked()//初始化图形区域被点击状态
        {
            for (int i = 0; i < XL; i++)
            {
                for (int j = 0; j < YL; j++)
                {
                    isClicked[i, j] = false;
                }
            }
        }
        public void InitpicKey()//初始化图片框,-1代表无图,其余代表所绘图形的标号
        {
            int temp, x, y;
            for (int i = 1; i < XL - 1; i++)
                for (int j = 1; j < YL / 2; j++)
                    picKeyHalfPart[i, j] = rdImage.Next(1, 39);
            for (int i = 1; i < XL - 1; i++)
            {
                for (int j = 1; j < YL / 2; j++)
                {
                    picKey[i, j] = picKeyHalfPart[i, j];
                }
            }
            for (int i = 1; i < XL - 1; i++)
                for (int j = YL / 2; j < YL - 1; j++)
                    picKey[i, j] = picKeyHalfPart[i, j - YL / 2 + 1];
            for (int i = 1; i < XL - 1; i++)
            {
                for (int j = 1; j < YL - 1; j++)
                {
                    temp = picKey[i, j];
                    x = rdImage.Next(1, XL - 1);
                    y = rdImage.Next(1, YL - 1);
                    picKey[i, j] = picKey[x, y];
                    picKey[x, y] = temp;
                }
            }
            for (int i = 0; i < XL; i++)
            {
                picKey[i, 0] = -1;
                picKey[i, YL - 1] = -1;
            }
            for (int j = 0; j < YL; j++)
            {
                picKey[0, j] = -1;
                picKey[XL - 1, j] = -1;
            }
        }
        public void InitImages()//初始化IMAGES数组
        {
            for (int i = 1; i <= 39; i++)
            {
                images[i - 1] = Image.FromFile(@"Images\" + i.ToString() + ".bmp");
            }
        }
        public void drawImage(Graphics g, int i, int j)//根据PICKEY和ISCLICKED的值来绘制图形
        {
            int imageID = picKey[i, j];
            if (picKey[i, j] >= 0)
            {
                g.DrawImage(images[imageID], i * PicSize,
                    j * PicSize, PicSize, PicSize);
                if (isClicked[i, j])
                {
                    g.DrawRectangle(new Pen(new SolidBrush(Color.Red), 2),
                        new Rectangle(i * PicSize, j * PicSize, PicSize, PicSize));
                }
            }
        }
        public void initPicBox()//设置图片框的图形
        {
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.Black);
            for (int i = 0; i < XL; i++)
            {
                for (int j = 0; j < YL; j++)
                {
                    drawImage(g, i, j);
                }
            }
            gPic.DrawImage(bmp, 0, 0);
        }
        public bool findline(bool disappear)//判断是否存在可消路径
        {
            int i1, j1, i2, j2;
            for (i1 = 1; i1 < XL - 1; i1++)
            {
                for (j1 = 1; j1 < YL - 1; j1++)
                {
                    for (i2 = 1; i2 < XL - 1; i2++)
                    {
                        for (j2 = 1; j2 < YL - 1; j2++)
                        {
                            if (picKey[i1, j1] == picKey[i2, j2])
                            {
                                if (canline(i1, j1, i2, j2))
                                {
                                    Tx1 = i1; Ty1 = j1; Tx2 = i2; Ty2 = j2;
                                    if (disappear)
                                    {
                                        picKey[i1, j1] = -1;
                                        picKey[i2, j2] = -1;
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            Tx1 = -1;
            return false;
        }
        public void Reset()//如果找不到可消路径,重置图形
        {
            int x, y, temp;
            for (int i = 1; i < XL - 1; i++)
            {
                for (int j = 1; j < YL - 1; j++)
                {
                    if (picKey[i, j] > 0)
                    {
                        while (true)
                        {
                            x = rdImage.Next(1, XL - 1);
                            y = rdImage.Next(1, YL - 1);
                            if (picKey[x, y] > 0) break;
                        }
                        temp = picKey[i, j];
                        picKey[i, j] = picKey[x, y];
                        picKey[x, y] = temp;
                    }
                }
            }
        }
        private void retractPictureBox(directions dirs)//缩进图形
        {
            int i, j, k, m;
            if (dirs == directions.DownToUp)
            {
                for (i = 1; i < XL - 1; i++)
                {
                    for (j = 1; j < YL - 1; j++)
                    {
                        if (picKey[i, j] < 0)
                        {
                            for (k = j + 1; k < YL - 1; k++)
                            {
                                if (picKey[i, k] > 0)
                                {
                                    break;
                                }
                            }
                            for (m = j; m < YL - k + j; m++)
                            {
                                picKey[i, m] = picKey[i, m + k - j];
                                picKey[i, m - j + k] = -1;
                            }
                        }
                    }
                }
            }
            if (dirs == directions.UpToDown)
            {
                for (i = 1; i < XL - 1; i++)
                {
                    for (j = YL - 2; j > 1; j--)
                    {
                        if (picKey[i, j] < 0)
                        {
                            for (k = j - 1; k > 1; k--)
                            {
                                if (picKey[i, k] > 0)
                                {
                                    break;
                                }
                            }
                            for (m = j; m > j - k - 1; m--)
                            {
                                picKey[i, m] = picKey[i, m - j + k];
                                picKey[i, m - j + k] = -1;
                            }
                        }
                    }
                }
            }
            if (dirs == directions.RightToLeft)
            {
                for (j = 1; j < YL - 1; j++)
                {
                    for (i = 1; i < XL - 1; i++)
                    {
                        if (picKey[i, j] < 0)
                        {
                            for (k = i + 1; k < XL - 1; k++)
                            {
                                if (picKey[k, j] > 0)
                                {
                                    break;
                                }
                            }
                            for (m = i; m < XL - k + i; m++)
                            {
                                picKey[m, j] = picKey[m + k - i, j];
                                picKey[m - i + k, j] = -1;
                            }
                        }
                    }
                }
            }
            if (dirs == directions.LeftToRight)
            {
                for (j = 1; j < YL - 1; j++)
                {
                    for (i = XL - 2; i > 1; i--)
                    {
                        if (picKey[i, j] < 0)
                        {
                            for (k = i - 1; k > 1; k--)
                            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -