文章目录
前言
一、准备工具
二、使用步骤
三、使用步骤
1.引入库
2.核心代码
代码展示:
提示:这里可以添加本文要记录的大概内容:
基于C#的Winform的简单消除小游戏开发,适合新手快速上手。
提示:以下是本篇文章正文内容,下面案例可供参考
VS2019,作为一个非常人性化的开发平台,能支持和搭载其他拓展库,完成快速的功能开发,个人学习非常推荐。
简单的winform界面,需要两个timer和两个textbox和游戏界面panel,按开发的六大原则来说,建议一个模块负责单独的一个功能。
第一个timer用于界面随机字符串的形成并让其不断的移动;
第二个timer用于检查,如果落下的字符串超出窗口,则停止timer1,宣布GAME OVER,其次,检查用户输入是否对应某字符串,如果对应,则清除用户输入,消除对应的字符串,分数加一;
panel 作为游戏的主界面,用于接收随机生成的label,考虑用panel作为界面,是因为在form界面下使用Controls.Clear()非常不方便,而使用遍历组件进行一一删除Controls.Remove()又删不干净,故而使用panel作为游戏主界面。记得设置panel的Dock,设置为Top。
如下图所示,代码详见后文。
代码如下(示例):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
代码如下:
private void timer1_Tick(object sender, EventArgs e) { Random rd = new Random(); Label lb = new Label(); lb.Text = Str_char(6,true); //获取随机字符串 lb.Font = new Font("宋体", 18); panel1.Controls.Add(lb); lb.Location = new Point(rd.Next(30, 400), 0); lb.AutoSize = true; foreach(Control c in panel1.Controls) { if(c is Label) { c.Location = new Point(c.Location.X, c.Location.Y + 40); } } } private void timer2_Tick(object sender, EventArgs e) { foreach(Control c in panel1.Controls) { if(c is Label) { if(c.Location.Y>250 ) { timer1.Stop(); panel1.Controls.Clear(); Label lb = new Label(); lb.Text = "Game Over!"; lb.Location = new Point(150,150); lb.Font = new Font("Microsoft Yahei", 30); lb.AutoSize = true; panel1.Controls.Add(lb); button1.Visible = true; } else if (textBox1.Text == c.Text) { panel1.Controls.Remove(c); Grade += 1; textBox2.Text = Convert.ToString(Grade); textBox1.Text = ""; } } } } //以下为获取随机字符串字段,转载自:
C#生成随机数或随机字母 - 时光博客 - 博客园 (cnblogs.com)public static string Str_char(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; }
该处为此项目的核心内容,负责随机字符串的产生、移动、消除以及规则。如有改进或问题,欢迎下方留言≡(*v*)≡
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
int Grade = 0;
public MainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
textBox2.Enabled = false;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
textBox1.Text = "";
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 2500; //生成速度
timer2.Interval = 100; //检查速度
Grade = 0; //分数清零
timer1.Start();
timer2.Start();
button1.Visible = false;
panel1.Controls.Clear();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rd = new Random();
Label lb = new Label();
lb.Text = Str_char(6,true); //获取随机字符串
lb.Font = new Font("宋体", 18);
panel1.Controls.Add(lb);
lb.Location = new Point(rd.Next(30, 400), 0);
lb.AutoSize = true;
foreach(Control c in panel1.Controls)
{
if(c is Label)
{
c.Location = new Point(c.Location.X, c.Location.Y + 40);
}
}
}
private void timer2_Tick(object sender, EventArgs e)
{
foreach(Control c in panel1.Controls)
{
if(c is Label)
{
if(c.Location.Y>250 )
{
timer1.Stop();
panel1.Controls.Clear();
Label lb = new Label();
lb.Text = "Game Over!";
lb.Location = new Point(150,150);
lb.Font = new Font("Microsoft Yahei", 30);
lb.AutoSize = true;
panel1.Controls.Add(lb);
button1.Visible = true;
}
else if (textBox1.Text == c.Text)
{
panel1.Controls.Remove(c);
Grade += 1;
textBox2.Text = Convert.ToString(Grade);
textBox1.Text = "";
}
}
}
}
//以下为获取随机字符串字段,转载自:
C#生成随机数或随机字母 - 时光博客 - 博客园 (cnblogs.com)
public static string Str_char(int Length, bool Sleep)
{
if (Sleep) System.Threading.Thread.Sleep(3);
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
}
}