标签:style blog class c code tar
游戏简介
类似于打地鼠的游戏,点击颜色为红色的方块,即得分。做这个游戏的原因是我们老板给我布置的作业是用flex做一个打地鼠的游戏,我先用最熟悉的C#试一试类似的功能。比较简单,我水平也很有限,有更好的实现或者建议还请大神们指点一下。
游戏展示
(初始页面) (游戏中)
(游戏结束) (点击确定后游戏重置)
游戏代码
由于游戏非常简单,加上大括号和VS自动生成的代码都还不足100行。
namespace 踩红 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int btnState = 1;//按钮状态 private int time = 30;//倒计时 private int num = 0;//击中个数 private int max = 0;//最高纪录 private void btnOK_Click(object sender, EventArgs e) { if (btnState==1) { btnOK.Text = "暂停"; btnState = 0; timerJS.Start(); timerSD.Start(); } else { btnOK.Text = "开始"; btnState = 1; timerJS.Stop(); timerSD.Stop(); } } private void timerJS_Tick(object sender, EventArgs e) { timerJS.Interval = 1000; time--; lbltime.Text = "倒计时:" + time; if (time == 0) { timerJS.Stop(); timerSD.Stop(); max = max > num ? max : num; lblmax.Text = "最高纪录:" + max; if (MessageBox.Show("游戏结束,所得分数为" + num) == DialogResult.OK) { Reset(); } } } private void Reset() { for (int i = 0; i < pnlbody.Controls.Count ; i++) { pnlbody.Controls[i].BackColor = Control .DefaultBackColor ; } btnOK.Text = "开始"; btnState = 1; lbltime.Text = "倒计时30秒开始!"; time = 30; num = 0; lblnum.Text = "击中个数为:0"; } Random random = new Random(); private int btnNum=0; private void timerSD_Tick(object sender, EventArgs e) { pnlbody.Controls[btnNum].BackColor = Control.DefaultBackColor; btnNum = random.Next(0,9); timerSD.Interval = 800; pnlbody.Controls[btnNum].BackColor = Color.Red; pnlbody.Controls[btnNum].Click += btn_Click; } void btn_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn.BackColor == Color.Red) { num += 1; lblnum.Text = "击中个数:" + num; btn.BackColor = Control.DefaultBackColor; } } } }
游戏Demo
游戏demo:http://pan.baidu.com/s/1jGuV7Ki
欢迎多多提意见~
标签:style blog class c code tar
原文地址:http://www.cnblogs.com/wangliu/p/3739793.html