C# 连连看

本文介绍了作者使用C#和Microsoft Visual Studio 2022开发的一个简单的连连看小游戏的过程。游戏界面参考了其他博客,并指出代码仍需完善,目前功能不全。文章提醒读者需将图片资源添加到Resources.resx中。
摘要由CSDN通过智能技术生成

2022.04.14 / Microsoft Visual Studio 2022 / C#

游戏界面:

 

 

参考了C#长程序(留着看)_weixin_33991418的博客-CSDN博客

学习了一下,以我自己的理解,制作了一个简陋的连连看小程序,功能还不齐全,慢慢完善。

我理解不深,很多地方似懂又非懂,语言又匮乏,注释不多,原文的注释还更多。

▶特别说明:

1、我用的图片资源

2、需将图片添加到Resources.resx中

 

 

 代码:

using System;
using System.Collections;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace LineGame  // 连连看 10行*15列
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private readonly int[] map = new int[205];
        private Bitmap bitmap;  // 小图块的图片集合
        private bool selected = false;  // 小图块未被选择的状态为false,被选择的状态为true
        private int x1, y1, x2, y2;  // 两个小图块的坐标
        public Point z1, z2;  // 连接线转折点坐标
        private int beRemoved = -1;  // 被成功消除的小图块标记为-1
        public sbyte isRemovedALL = 0;  // 记录被消除的小图块数量
        public enum ConnectStyle  // 连接线方式
        {
            OneLine,
            TwoLine,
            ThreeLine
        };

        private void Form1_Load(object sender, EventArgs e)
        {
            Initialization();
        }

        /// <summary>
        /// 游戏初始化
        /// </summary>
        private void Initialization()
        {
            RandomSort();  // 随机排列小图块
            CreateGame();  // 显示游戏界面
        }

        /// <summary>
        /// 小图块随机排序
        /// </summary>
        private void RandomSort()
        {
            Random random = new Random();
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < 15; i++)  // 15列
            {
                for (int j = 0; j < 10; j++)  // 10行
                {
                    arrayList.Add(i);  // 数组存有10组0~15
                }
            }
            for (int i = 0; i < 150; i++)
            {
                int index = random.Next() % arrayList.Count;
                map[i] = (int)arrayList[index];  // 数组随机排列
                arrayList.RemoveAt(index);
            }
        }

        /// <summary>
        /// 以pictureBox1作为GDI画板
        /// </summary>
        /// <returns></returns>
        private Graphics InterFace()
        {
            if (pictureBox1.Image == null)
            {
                Bitmap bitmap = new Bitmap(1200, 916);
                pictureBox1.Image = bitmap;
            }
            Graphics graphics = Graphics.FromImage(pictureBox1.Image);
            return graphics;
        }

        /// <summary>
        /// 分割素材图片集
        /// </summary>
        /// <param name="serialNumber"></param>
        /// <returns></returns>
        private Bitmap Split(int serialNumber)
        {
            // 直接调用资源库里的图片(看文章前面▶特别说明)
            bitmap = Properties.Resources._0;  // _0是提前将0.bmp导入Form1.resx中的图片资源
            Bitmap splited = new Bitmap(50, 50);  // 将0.bmp分割成50像素*50像素的小图块
            Graphics pictures = Graphics.FromImage(splited);
            Rectangle rectangle_A = new Rectangle(0, 0, 50, 50);
            Rectangle rectangle_B = new Rectangle(serialNumber * 50, 0, 50, 50);
            pictures.DrawImage(bitmap, rectangle_A, rectangle_B, GraphicsUnit.Pixel);
            return splited;
        }

        /// <summary>
        /// 绘制游戏界面
        /// </summary>
        private void CreateGame()
        {
            Graphics graphics = InterFace();
            for (int i = 0; i < 150; i+
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值