C# Winform翻牌子记忆小游戏

效果

源码

新建一个winform项目命名为Matching Game,选用.net core 6框架

并把Form1.cs代码修改为

using Timer = System.Windows.Forms.Timer;namespace Matching_Game
{public partial class Form1 : Form{private const int row = 4;private const int col = 4;private TableLayoutPanel panel = new(){RowCount = row,ColumnCount = col,};private Label[,] labels = new Label[row, col];private readonly Random random = new();private readonly List<string> icons = new(){"!", "!", "N", "N", ",", ",", "k", "k","b", "b", "v", "v", "w", "w", "z", "z"};private readonly Timer timer = new(){Interval = 750,};private Label? firstClicked = null;private Label? secondClicked = null;public Form1(){InitializeComponent();InitializeControls();timer.Tick += Timer_Tick;AssignIconsToSquares();}/// <summary>/// 窗体和它的子窗体都开启双缓冲,防止重新开始游戏时重新绘制控件闪屏/// </summary>protected override CreateParams CreateParams{get{CreateParams cp = base.CreateParams;cp.ExStyle |= 0x02000000;return cp;}}/// <summary>/// 初始化控件/// </summary>public void InitializeControls(){#region initialize formText = "Matching Game";Size = new Size(550, 550);StartPosition = FormStartPosition.CenterScreen;Controls.Clear();#endregion#region initialize panelpanel.BackColor = Color.CornflowerBlue;for (int i = 0; i < row; i++){panel.RowStyles.Add(new RowStyle(SizeType.Percent, 25));}for (int j = 0; j < col; j++){panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));}panel.Dock = DockStyle.Fill;panel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;panel.Padding = new Padding(0, 0, 0, 0);Controls.Add(panel);#endregion#region initialize labelsfor (int i = 0; i < row; i++){for (int j = 0; j < col; j++){Label label = new(){BackColor = Color.CornflowerBlue,ForeColor = Color.CornflowerBlue,AutoSize = false,Dock = DockStyle.Fill,TextAlign = ContentAlignment.MiddleCenter,Font = new Font("Webdings", 64, FontStyle.Regular),//Font = new Font("Times New Roman", 48, FontStyle.Bold),Margin = new Padding(0, 0, 0, 0)};labels[i, j] = label;label.Click += Label_Click;panel.Controls.Add(label);panel.SetCellPosition(label, new TableLayoutPanelCellPosition(j, i));}}#endregion}/// <summary>/// 定时器任务/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Timer_Tick(object? sender, EventArgs e){timer.Stop();CheckForWinner();if (firstClicked != null){firstClicked.ForeColor = firstClicked.BackColor;}if (secondClicked != null){secondClicked.ForeColor = secondClicked.BackColor;}firstClicked = null;secondClicked = null;}/// <summary>/// 标签被点击时触发/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Label_Click(object? sender, EventArgs e){// The timer is only on after two non-matching // icons have been shown to the player, // so ignore any clicks if the timer is runningif (timer.Enabled == true)return;if (sender is Label clickedLabel){// If the clicked label is black, the player clicked// an icon that's already been revealed --// ignore the clickif (clickedLabel.ForeColor == Color.Black)return;// If firstClicked is null, this is the first icon// in the pair that the player clicked, // so set firstClicked to the label that the player // clicked, change its color to black, and returnif (firstClicked == null){firstClicked = clickedLabel;firstClicked.ForeColor = Color.Black;return;}// If the player gets this far, the timer isn't// running and firstClicked isn't null,// so this must be the second icon the player clicked// Set its color to blacksecondClicked = clickedLabel;secondClicked.ForeColor = Color.Black;if (firstClicked.Text == secondClicked.Text){firstClicked = null;secondClicked = null;}// If the player gets this far, the player // clicked two different icons, so start the // timer (which will wait three quarters of // a second, and then hide the icons)timer.Start();}}/// <summary>/// Assign each icon from the list of icons to a random square/// </summary>private void AssignIconsToSquares(){List<string> iList = icons.ToList();// The TableLayoutPanel has 16 labels,// and the icon list has 16 icons,// so an icon is pulled at random from the list// and added to each labelforeach (Control control in panel.Controls){if (control is Label iconLabel){int randomNumber = random.Next(iList.Count);iconLabel.Text = iList[randomNumber];// iconLabel.ForeColor = iconLabel.BackColor;iList.RemoveAt(randomNumber);}}}/// <summary>/// Check every icon to see if it is matched, by /// comparing its foreground color to its background color. /// If all of the icons are matched, the player wins/// </summary>private void CheckForWinner(){// Go through all of the labels in the TableLayoutPanel, // checking each one to see if its icon is matchedforeach (Control control in panel.Controls){if (control is Label iconLabel){if (iconLabel.ForeColor == iconLabel.BackColor)return;}}// If the loop didn’t return, it didn't find// any unmatched icons// That means the user won. Show a message and close the formDialogResult result = MessageBox.Show("You matched all the icons! Restart?", "Congratulations", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (result != DialogResult.Yes){Close();}else{panel = new TableLayoutPanel() { RowCount = row, ColumnCount = col };labels = new Label[row, col];InitializeControls();AssignIconsToSquares();}}}
}

修改完代码后直接启动即可。

有兴趣可以到微软官网查看相关细节教程:创建匹配游戏 - Visual Studio (Windows) | Microsoft Learn

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/628158.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简单介绍JDK、JRE、JVM三者区别

简单介绍JDK vs JRE vs JVM三者区别 文编|JavaBuild 哈喽&#xff0c;大家好呀&#xff01;我是JavaBuild&#xff0c;以后可以喊我鸟哥&#xff0c;嘿嘿&#xff01;俺滴座右铭是不在沉默中爆发&#xff0c;就在沉默中灭亡&#xff0c;一起加油学习&#xff0c;珍惜现在来之不…

【samba】Ubuntu20.04安装 error255解决方法

目录 使用samba报错 net usershare returned error 255时&#xff08;如下图&#xff09;解决方法如下&#xff1a; 1、安装 Samba 服务&#xff1a; 2、配置 Samba 共享&#xff1a; 3、设置 Samba 用户密码&#xff1a; 4、重启 Samba 服务&#xff1a; 6、在 Windows 上…

mysql 下载和安装和修改MYSQL8.0 数据库存储文件的路径

一、第一步:下载步骤 下载链接&#xff1a;MySQL :: Download MySQL Installer 选择版本8.0.35&#xff0c;社区版&#xff0c; 点击 Download 下载 安装包 二、第二步:安装步骤 添加环境变量&#xff0c;C:\Program Files\MySQL\MySQL Server 8.0\bin 可以点开MySQL 8.0 Co…

C++基础1

一、形参带默认值的函数 二、inline内联函数 内联函数是一种在编译器处理时&#xff0c;将函数的实际代码插入到调用处的方法。通常&#xff0c;函数调用涉及一定的开销&#xff0c;包括保存和恢复调用现场、跳转到函数的代码位置等。而内联函数通过在调用处直接插入函数的代码…

Centos7安装K8S

Centos7安装K8S 安装过程中没有出现的错误可以往下 根据以前一些博主写的博客&#xff0c;在小阳翻了不下几十篇博客之后&#xff0c;我果断是放弃了&#xff0c;于是找到了官网地址&#xff0c;然后也有坑 1. 关闭防火墙 systemctl stop firewalld systemctl disable firew…

MySQL的各种日志

目录 一、错误日志 二、二进制日志 1、介绍 2、作用 3、相关信息 4、日志格式 5、查看二进制文件 6、二进制日志文件删除 三、查询日志 四、慢日志 一、错误日志 记录MySQL在启动和停止时&#xff0c;以及服务器运行过程中发生的严重错误的相关信息&#xff0c;当数据库…

Hive使用shell调用命令行特殊字符处理

1.场景分析 数据处理常用hive -e的方式&#xff0c;通过脚本操作数仓&#xff0c;过程中常常遇到特殊字符的处理&#xff0c;如单双引号、反斜杠、换行符等&#xff0c;现将特殊字符用法总结使用如下&#xff0c;可直接引用&#xff0c;避免自行测试的繁琐。 2.特殊字符处理 …

16.桥接模式

桥接模式 介绍 桥接模式是一种结构型设计模式&#xff0c;它通过将抽象部分与实现部分分离&#xff0c;使它们可以独立变化。这种模式通过组合的方式来实现&#xff0c;而不是继承。桥接模式通过将抽象和实现解耦&#xff0c;从而实现抽象和实现的分离&#xff0c;使得系统更加…

Java零基础——Vue基础篇

1.【熟悉】Vue简介 1.1 简介 它是一个构建用户界面单页面的框架 Vue是一个前端框架 https://www.pmdaniu.com/#file UI网站 UI 一般开发者使用蓝湖 工具 看着UI图 写接口 https://lanhuapp.com/web/#/item 是一个轻量级的MVVM&#xff08;Model-View-ViewModel&#xff…

Spring Boot - Application Events 的发布顺序_ApplicationStartingEvent

文章目录 概述Code源码分析 概述 Spring Boot 的广播机制是基于观察者模式实现的&#xff0c;它允许在 Spring 应用程序中发布和监听事件。这种机制的主要目的是为了实现解耦&#xff0c;使得应用程序中的不同组件可以独立地改变和复用逻辑&#xff0c;而无需直接进行通信。 …

VMware虚拟机忘记密码操作方法

下面已openEuler虚拟机为例&#xff1a; 1、点击重启时&#xff0c;一直按esc&#xff08;鼠标点击一下&#xff0c;确保鼠标在你的虚拟机里面&#xff09; 2、一直到进入到如下页面按e键&#xff08;可能会略有不同&#xff09; 3、按e键后跳转到如下页面 4、在该页面输入 in…

高级定时器

本节主要介绍以下内容&#xff1a; 定时器简介 高级定时器功能框图讲解 一、定时器简介 定时器功能 &#xff1a;定时、输出比较、输入捕获、断路输入 定时器分类 &#xff1a;基本定时器、通用定时器、高级定时器 定时器资源 &#xff1a;F103有2个高级定时器、4个通…

c语言-数据类型(下)

目录 4.实型变量 5.字符常量 直接常量&#xff1a; 转义字符&#xff1a; 6.字符变量 7.字符串常量 五、输出格式总结 整型&#xff1a; 浮点型&#xff1a; 字符及字符串&#xff1a; 指针&#xff08;地址&#xff09;&#xff1a; 六、typedef 七、sizeof一个问…

Matlab并行编程之GPU

Matlab并行编程之GPU Matlab提供GPU上计算支持: 基础数据类型(gpuArray和对应API),支持GPU计算的内置函数和多个工具包,支持PTX内核对象加载,支持MEX函数使用CUDA C/C开发等。对大规模数据处理&#xff0c;复杂计算&#xff0c;利用GPU计算能提供显著的性能加速效果. Matlab同…

Docker之网络配置的使用

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《Docker之网络配置的使用》。&#x1f3af;&…

OpenHarmony——Linux之IR驱动

Linux之IR驱动 背景 在光谱中波长自760nm至400um的电磁波称为红外线&#xff0c;它是一种不可见光。红外遥控成本很低&#xff0c;以前广泛应用在电视&#xff0c;空调等电器的控制上面&#xff0c;现在随着蓝牙遥控器慢慢普及&#xff0c;红外遥控越来越少&#xff0c;但在某…

图像处理:孤立点的检测

图像处理-孤立点的检测 孤立点的检测在图像处理中通常涉及到检测图像中的突变或者边缘&#xff0c;而使用二阶导数是一种常见的方法。一阶导数可以帮助找到图像中的边缘&#xff0c;而二阶导数则有助于检测边缘上的峰值&#xff0c;这些峰值可能对应于孤立点或者特殊的图像结构…

AI大模型预先学习笔记二:prompt提问大模型、langchain使用大模型框架、fine tune微调大模型

文章目录 一、Prompt Engineering&#xff08;怎么去提问大模型&#xff09;1&#xff09;环境准备2&#xff09;交互代码的参数备注3&#xff09;交互代码 二、LangChain&#xff08;一个框架去使用大模型&#xff09;1&#xff09;LangChain核心介绍&#xff1a;I/O模块、数据…

TypeError: (0 , _ahooks.createUpdateEffect) is not a function

版本 "next": "14.0.4", "antd-mobile": "^5.34.0", next中使用antd-mobile可困难了.主要是因为antd-mobile不支持ssr 1.下载antd-mobile包,在next.config.js中加入 transpilePackages: [antd-mobile,], 2.在页面中引入antd-mobi…

蓝桥杯备赛day02 -- 算法训练题 拿金币Java

目录 题目&#xff1a; 问题描述 输入格式 输出格式 解题过程 第一步 定义dp数组 第二步 确定 dp 数组递推公式 第三步 dp数组的初始化 第四步 dp数组的遍历顺序 第五步 举例说明 报错&#xff1a;内存超限 用dp数组去存储位置上的金币 dp数组从二维降为一维 收获&a…