C# 点金蛋小游戏(源码)
C#点金蛋小游戏。
按钮名称分别为:btn1、btn2、btn3、btn4、btn5
标签名称:lblT
代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace 点金蛋
{
public partial class Form1 : Form
{
int count = 0;//统计点到金蛋的数量
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;//窗体最大化
lblT.Text = "已经点到 0 个金蛋.";//初始显示
}
private void btn1_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;//获取是哪个金蛋对象
int x = this.ClientSize.Width - b.Width;//获取窗体的宽度-按钮宽度的值
int y = this.ClientSize.Height - b.Height;//获取窗体的高度-按钮高度的值
Random r = new Random();//随机数
b.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1));//获取或设置该控件的左上角相对于其容器的左上角的坐标。 随机出 X和Y坐标
}
private void btn1_Click(object sender, EventArgs e)
{
Button b = (Button)sender;//获取哪个金蛋是对象
MessageBox.Show("点到" + b.Text + "了!");//提示
lblT.Focus();//标签获得焦点
count++;//数量+1
lblT.Text = "已经点到 " + count + " 个金蛋.";//标签赋值
}
}
}
输出结果:
如需本软件完整源码请留言!仅限学习交流使用!
