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+