DSP

c# 用pictureBox写的游戏

2019-07-13 16:50发布

   以前一直都是在做MFC(俺一直都是做DSP软件的,PC上跟DSP通讯等程序一直都是用MFC做的),最近在看C#方面的东西,感觉C#挺方便的,于是便随便写了这么个小游戏,里面主要是Timer的应用、delegate的应用和Graphics的应用等,原理很简单,操作也很简单(键盘操作用左、右键,鼠标操作只是左右操作就可以了),特此分享一下,有兴趣的可以一起探讨一下。程序的运行画面如下:          程序的主要数据结构如下:    (1)球体的数据结构 public struct Ball
        {
            public Point TopPoint;      //最上面坐标
            public Point BottomPoint;   //最下面坐标
            public Point LeftPoint;     //最左边坐标
            public Point RightPoint;    //最右边坐标
            public Brush BallBrush;     //球体颜 {MOD}
            public bool IsPassed;       //是否已经miss             //下降一次
            public void FollowOneStep(int nOneStep,ref int missBalls)
            {
                TopPoint.Y += nOneStep;
                BottomPoint.Y += nOneStep;
                LeftPoint.Y += nOneStep;
                RightPoint.Y += nOneStep;                 if (BottomPoint.Y >= MONITOR_BOTTOM_EDGE)
                {
                    IsPassed = true;                     missBalls++;
                }
            }             //检测球体是不是被平衡木触摸到,接触到则将IsPassed职位TRUE
            public bool CheckBallsPassState(Point topLeftPointOfBeam,ref int recvBalls)
            {
                if (BottomPoint.Y > topLeftPointOfBeam.Y && TopPoint.Y < topLeftPointOfBeam.Y + BEAM_WIDTH) //球体在平衡木可接触的水平线上
                {
                    if (RightPoint.X > topLeftPointOfBeam.X && LeftPoint.X < topLeftPointOfBeam.X + BEAM_LENGH)                       
                    {
                        IsPassed = true;                         recvBalls++;
                    }                   
                }                 return IsPassed;
            }
        }   (2)游戏难度数据结构    public struct DegreeOfDifcuty
        {
            public int Diffcuty;              //难度
            public int FollowOneTime;         //一次下降的像素点
            public int AddOneBallOfBreak;     //多少ms添加一个球
            public int FollowOfBreak;         //多少个Timer中断产生一次下降事件
        }   //主窗口的程序代码如下 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace FollowingBalls
{
    public partial class FollowingBalls : Form
    {
        public FollowingBalls()
        {
            InitializeComponent();
        }         private void FollowingBalls_Load(object sender, EventArgs e)
        {
            myTimer = new System.Timers.Timer();
            myTimer.Interval = 20;
            myTimer.Enabled = true;
            myTimer.AutoReset = true;
            myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed);             bmpMonitor = new Bitmap(pboxMonitor.Width, pboxMonitor.Height);
            bmpRecord = new Bitmap(pboxRecord.Width, pboxRecord.Height);             _ballBrushNum = _ballBrush.Length;             for (int i = 0; i < 100; i++)
            {
                _ballsFollowing[i].IsPassed = true;
            }             dgreeOfDifcuty.Diffcuty = DIFFCUTY_MIDD;
            dgreeOfDifcuty.FollowOneTime = 19;
            dgreeOfDifcuty.AddOneBallOfBreak = 10;
            dgreeOfDifcuty.FollowOfBreak = 8;             //难度选项
            MidToolStripMenuItem.CheckState = CheckState.Checked;             //控制选项
            MouseToolStripMenuItem.CheckState = CheckState.Unchecked;
            KeyBoardToolStripMenuItem.CheckState = CheckState.Checked;             //Graphics初始化
            _gMonitor = Graphics.FromImage(bmpMonitor);
            _gRecord = Graphics.FromImage(bmpRecord);             LoadRecord();
        }         //获取成绩记录
        private void LoadRecord()
        {
            try
            {
                if (File.Exists(@"c:/record.ini"))
                {
                    using (FileStream fs = new FileStream(@"c:/record.ini", FileMode.Open))
                    {
                        StreamReader sr = new StreamReader(fs);                         _hardRecord.UserName = sr.ReadLine();
                        _hardRecord.RecvBalls = int.Parse(sr.ReadLine());                         _midRecord.UserName = sr.ReadLine();
                        _midRecord.RecvBalls = int.Parse(sr.ReadLine());                         _simpRecord.UserName = sr.ReadLine();
                        _simpRecord.RecvBalls = int.Parse(sr.ReadLine());                         sr.Close();
                        sr.Dispose();
                    }
                }
                else
                {
                    _hardRecord.UserName = "";
                    _hardRecord.RecvBalls = 0;                     _midRecord.UserName = "";
                    _midRecord.RecvBalls = 0;                     _simpRecord.UserName = "";
                    _simpRecord.RecvBalls = 0;
                }
            }
            catch
            {
                _hardRecord.UserName = "";
                _hardRecord.RecvBalls = 0;                 _midRecord.UserName = "";
                _midRecord.RecvBalls = 0;                 _simpRecord.UserName = "";
                _simpRecord.RecvBalls = 0;
            }
        }         //鼠标控制
        private void MouseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!_gameGoing)
            {
                _ctrlMode = CTRLMODE_MOUSE;                 MouseToolStripMenuItem.CheckState = CheckState.Checked;
                KeyBoardToolStripMenuItem.CheckState = CheckState.Unchecked;
            }
        }         //键盘控制
        private void KeyBoardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!_gameGoing)
            {
                _ctrlMode = CTRLMODE_KEYBOARD;                 MouseToolStripMenuItem.CheckState = CheckState.Unchecked;
                KeyBoardToolStripMenuItem.CheckState = CheckState.Checked;
            }
        }         private void FollowingBalls_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                _leftKeyState = true;
            }
            else if (e.KeyCode == Keys.Right)
            {
                _rightKeyState = true;
            }
        }         private void FollowingBalls_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                _leftKeyState = false;
            }
            else if (e.KeyCode == Keys.Right)
            {
                _rightKeyState = false;
            }
            else if (e.KeyCode == Keys.Space)   //暂停
            {
                _pasued = !_pasued;
                _gameOverPausedCnt = 0;
            }
        }         private void RestartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_gameGoing)
            {
                RestartToolStripMenuItem.Text = "开始游戏";                 _gameGoing = false;
            }
            else
            {
                RestartToolStripMenuItem.Text = "结束游戏";
                _gameGoing = true;
            }             _gameOver = false;
            _recvBalls = _missBalls = 0;             _ballCnt = 0;             for (int i = 0; i < 100; i++)
            {
                _ballsFollowing[i].IsPassed = true;
            }             _pasued = false;
        }         private void pboxMonitor_MouseMove(object sender, MouseEventArgs e)
        {
            _mousePoint = e.Location;
        }         private void HardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!_gameGoing)
            {
                dgreeOfDifcuty.Diffcuty = DIFFCUTY_HARD;
                dgreeOfDifcuty.FollowOneTime = 15;
                dgreeOfDifcuty.AddOneBallOfBreak = 10;
                dgreeOfDifcuty.FollowOfBreak = 2;                 HardToolStripMenuItem.CheckState = CheckState.Checked;
                MidToolStripMenuItem.CheckState = CheckState.Unchecked;
                SimpToolStripMenuItem.CheckState = CheckState.Unchecked;
            }
        }         private void MidToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!_gameGoing)
            {
                dgreeOfDifcuty.Diffcuty = DIFFCUTY_MIDD;
                dgreeOfDifcuty.FollowOneTime = 19;
                dgreeOfDifcuty.AddOneBallOfBreak = 10;
                dgreeOfDifcuty.FollowOfBreak = 8;                 HardToolStripMenuItem.CheckState = CheckState.Unchecked;
                MidToolStripMenuItem.CheckState = CheckState.Checked;
                SimpToolStripMenuItem.CheckState = CheckState.Unchecked;
            }
        }         private void SimpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!_gameGoing)
            {
                dgreeOfDifcuty.Diffcuty = DIFFCUTY_SIMP;
                dgreeOfDifcuty.FollowOneTime = 16;
                dgreeOfDifcuty.AddOneBallOfBreak = 31;
                dgreeOfDifcuty.FollowOfBreak = 14;                 HardToolStripMenuItem.CheckState = CheckState.Unchecked;
                MidToolStripMenuItem.CheckState = CheckState.Unchecked;
                SimpToolStripMenuItem.CheckState = CheckState.Checked;
            }
        }         private void FollowingBalls_FormClosed(object sender, FormClosedEventArgs e)
        {
            _gMonitor.Dispose();
            _gRecord.Dispose();             bmpMonitor.Dispose();
            bmpRecord.Dispose();             myTimer.Dispose();             //this.Close();
        }         private void AboutFollowingBallsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FallowingBallsAbout about = new FallowingBallsAbout())
            {
                about.ShowDialog();
            }
        }     
    }
}   //该游戏用到的变量等 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace FollowingBalls
{   
    public partial class FollowingBalls : Form
    {
        #region
        public const int BALL_RADIUS = 8;               //球体半径
        public const int BALLS_NUM_ONE_LINE = 14;       //一行球体最多可以有的个数
        public const int BALL_SQURE = 20;               //球体的正方形长度
        public const int MONITOR_HEIGHT = 450;          //用于编程的屏幕高度
        public const int MONITOR_WIDTH = 280;           //用于编程的屏幕宽度
        public const int MONITOR_LEFT_EDGE = 7;         //左边界
        public const int MONITOR_TOP_EDGE = 8;          //上边界
        public const int MONITOR_BOTTOM_EDGE = 457;     //上边界
        public const int CTRLMODE_MOUSE = 0;            //鼠标控制  
        public const int CTRLMODE_KEYBOARD = 1;         //键盘控制
        public const int BEAM_MOVE_ONE_TIME = 20;       //平衡木每次移动的像素值
        public const int BEAM_LENGH = 100;              //平衡木长度
        public const int BEAM_WIDTH = 13;               //平衡木宽度
        public const int WORD_FLASH = 20;               //Game Over/paused闪烁频率
        public const int MAX_MISS_BALLS = 10;           //最大miss球数
        public const int DIFFCUTY_HARD = 0;             //困难
        public const int DIFFCUTY_MIDD = 1;             //中等
        public const int DIFFCUTY_SIMP = 2;             //简单         #endregion         //球体
        public struct Ball
        {
            public Point TopPoint;      //最上面坐标
            public Point BottomPoint;   //最下面坐标
            public Point LeftPoint;     //最左边坐标
            public Point RightPoint;    //最右边坐标
            public Brush BallBrush;     //球体颜 {MOD}
            public bool IsPassed;       //是否已经miss             //下降一次
            public void FollowOneStep(int nOneStep,ref int missBalls)
            {
                TopPoint.Y += nOneStep;
                BottomPoint.Y += nOneStep;
                LeftPoint.Y += nOneStep;
                RightPoint.Y += nOneStep;                 if (BottomPoint.Y >= MONITOR_BOTTOM_EDGE)
                {
                    IsPassed = true;                     missBalls++;
                }
            }             //检测球体是不是被平衡木触摸到,接触到则将IsPassed职位TRUE
            public bool CheckBallsPassState(Point topLeftPointOfBeam,ref int recvBalls)
            {
                if (BottomPoint.Y > topLeftPointOfBeam.Y && TopPoint.Y < topLeftPointOfBeam.Y + BEAM_WIDTH) //球体在平衡木可接触的水平线上
                {
                    if (RightPoint.X > topLeftPointOfBeam.X && LeftPoint.X < topLeftPointOfBeam.X + BEAM_LENGH)                       
                    {
                        IsPassed = true;                         recvBalls++;
                    }                   
                }                 return IsPassed;
            }
        }         //成绩记录
        public struct Record
        {
            public string UserName;
            public int RecvBalls;
        }         //难度
        public struct DegreeOfDifcuty
        {
            public int Diffcuty;              //难度
            public int FollowOneTime;         //一次下降的像素点
            public int AddOneBallOfBreak;     //多少ms添加一个球
            public int FollowOfBreak;         //多少个Timer中断产生一次下降事件
        }
       
        System.Timers.Timer myTimer;         //pboxMonitor和pboxRecord的背景图片
        Bitmap bmpMonitor,bmpRecord;         //pboxMonitor和pboxRecord的背景图片绘制用Graphics
        private Graphics _gMonitor;
        private Graphics _gRecord;         //球体的颜 {MOD}
        private Brush[] _ballBrush =
        {
            Brushes.Tomato,
            Brushes.Snow,
            Brushes.SpringGreen,
            Brushes.SkyBlue,
            Brushes.Yellow,
            Brushes.Chocolate,
            Brushes.Cyan,
            Brushes.DarkOrange,
            Brushes.DeepPink,
            Brushes.Gold
        };         //球体颜 {MOD}种类
        private int _ballBrushNum = 0;         //球体结构数组
        private Ball[] _ballsFollowing = new Ball[100];         //球体循环变量
        private int _ballCnt = 0;         //难度
        public DegreeOfDifcuty dgreeOfDifcuty;         //定时刷新进入次数
        private int _enterTimes = 0;         //控制方式
        private int _ctrlMode = CTRLMODE_KEYBOARD;         //"<—"或者"—>"状态(按下,松开)
        private bool _leftKeyState = false;
        private bool _rightKeyState = false;         //平衡木的左上角顶点坐标
        private Point _topLeftPointOfBeam = new Point((MONITOR_WIDTH - BEAM_LENGH) / 2 + MONITOR_LEFT_EDGE,380);         //游戏是否正在进行
        private bool _gameGoing = false;         //鼠标的坐标(pboxMonitor)
        private Point _mousePoint = new Point(0, 0);         //暂停标志
        private bool _pasued = false;         //游戏结束标志
        private bool _gameOver = false;         //接收、miss掉的球体数
        private int _recvBalls = 0;
        private int _missBalls = 0;         //成绩记录
        private Record _hardRecord;
        private Record _midRecord;
        private Record _simpRecord;         //Game over/Paused闪烁计数
        private int _gameOverPausedCnt = 0;         //产生下降事件的计数
        private int _followCnt = 0;         //记录写入文件标志
        private bool _writeToFileFlag = false;         //从SaveRecord获取的记录者名字的临时string
        private string _tmpRecordName = null;
    }
}   //该程序的主要操作 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.IO; namespace FollowingBalls
{  
    public partial class FollowingBalls : Form
    { 
        //定时刷新
        public void myTimer_Elapsed(Object sender, ElapsedEventArgs e)
        {
            try
            {
                //生成黑 {MOD}底布
                _gMonitor.Clear(Color.Black);
                _gRecord.Clear(Color.Black);                 //画白 {MOD}框
                _gMonitor.DrawRectangle(new Pen(Brushes.White, 2.0F), new Rectangle(4, 4, bmpMonitor.Width - 8, bmpMonitor.Height - 8));
                _gRecord.DrawRectangle(new Pen(Brushes.White, 2.0F), new Rectangle(4, 4, bmpRecord.Width - 8, bmpMonitor.Height - 8));                 if (_gameGoing)
                {
                    AddOneBall();
                }
                DrawBalls(ref _gMonitor);
                DrawBeam(ref _gMonitor);
                DrawGamePauseOver(ref _gMonitor);
                DrawRecordState();
                SetUserRecord();                 pboxMonitor.Image = bmpMonitor;
                pboxRecord.Image = bmpRecord;                 pboxMonitor.Invalidate();
                pboxRecord.Invalidate();                 _enterTimes = (_enterTimes + 1) % dgreeOfDifcuty.AddOneBallOfBreak;
                _followCnt = (_followCnt + 1) % dgreeOfDifcuty.FollowOfBreak;
                _gameOverPausedCnt = (_gameOverPausedCnt + 1) % (WORD_FLASH * 2);
            }
            catch { }
        }         //添加一个球体
        public void AddOneBall()
        {
            if (_enterTimes == 0 && !_pasued)
            {
                Random rn = new Random();
                int nBallBrush = rn.Next(_ballBrushNum);
                int nBallNum = rn.Next(BALLS_NUM_ONE_LINE);                 _ballsFollowing[_ballCnt].IsPassed = false;
                _ballsFollowing[_ballCnt].LeftPoint = new Point(nBallNum * BALL_SQURE + MONITOR_LEFT_EDGE, MONITOR_TOP_EDGE + BALL_RADIUS);
                _ballsFollowing[_ballCnt].RightPoint = new Point(nBallNum * BALL_SQURE + MONITOR_LEFT_EDGE + BALL_RADIUS * 2, MONITOR_TOP_EDGE + BALL_RADIUS);
                _ballsFollowing[_ballCnt].TopPoint = new Point(nBallNum * BALL_SQURE + MONITOR_LEFT_EDGE + BALL_RADIUS, MONITOR_TOP_EDGE);
                _ballsFollowing[_ballCnt].BottomPoint = new Point(nBallNum * BALL_SQURE + MONITOR_LEFT_EDGE + BALL_RADIUS, MONITOR_TOP_EDGE + BALL_RADIUS * 2);
                _ballsFollowing[_ballCnt].BallBrush = _ballBrush[nBallBrush];                 _ballCnt = (_ballCnt + 1) % 100;
            }
        }         //画球体
        public void DrawBalls(ref Graphics gMonitor)
        {
            for (int i = 0; i < 100;i++ )
            {
                if (!_ballsFollowing[i].IsPassed && !_ballsFollowing[i].CheckBallsPassState(_topLeftPointOfBeam,ref _recvBalls))
                {
                    gMonitor.FillEllipse(_ballsFollowing[i].BallBrush, new Rectangle(_ballsFollowing[i].LeftPoint.X, _ballsFollowing[i].TopPoint.Y, BALL_RADIUS * 2, BALL_RADIUS * 2));                     if (_followCnt == 0 && !_pasued && _gameGoing)
                        _ballsFollowing[i].FollowOneStep(dgreeOfDifcuty.FollowOneTime,ref _missBalls);
                }             }
        }         //画平衡木
        public void DrawBeam(ref Graphics gMonitor)
        {
            //键盘控制方式
            if (_ctrlMode == CTRLMODE_KEYBOARD && _gameGoing && !_pasued)
            {
                if (_leftKeyState == true && _topLeftPointOfBeam.X > MONITOR_LEFT_EDGE)
                {
                    if ((_topLeftPointOfBeam.X - BEAM_MOVE_ONE_TIME) < MONITOR_LEFT_EDGE)
                    {
                        _topLeftPointOfBeam.X = MONITOR_LEFT_EDGE;
                    }
                    else
                    {
                        _topLeftPointOfBeam.X -= BEAM_MOVE_ONE_TIME;
                    }
                }
                else if (_rightKeyState == true && _topLeftPointOfBeam.X < MONITOR_LEFT_EDGE + MONITOR_WIDTH - BEAM_LENGH)
                {
                    if ((_topLeftPointOfBeam.X + BEAM_MOVE_ONE_TIME) > MONITOR_LEFT_EDGE + MONITOR_WIDTH - BEAM_LENGH )
                    {
                        _topLeftPointOfBeam.X = MONITOR_LEFT_EDGE + MONITOR_WIDTH - BEAM_LENGH;
                    }
                    else
                    {
                        _topLeftPointOfBeam.X += BEAM_MOVE_ONE_TIME;
                    }
                }
            }
            else if (_ctrlMode == CTRLMODE_MOUSE && _gameGoing && !_pasued)
            {
                if (_mousePoint.X < MONITOR_LEFT_EDGE)
                {
                    _topLeftPointOfBeam.X = MONITOR_LEFT_EDGE;
                }
                else if (_mousePoint.X > (MONITOR_LEFT_EDGE + MONITOR_WIDTH - BEAM_LENGH))
                {
                    _topLeftPointOfBeam.X = MONITOR_LEFT_EDGE + MONITOR_WIDTH - BEAM_LENGH;
                }
                else
                {
                    _topLeftPointOfBeam.X = _mousePoint.X;
                }
            }
            gMonitor.FillRectangle(Brushes.Bisque, _topLeftPointOfBeam.X, _topLeftPointOfBeam.Y, BEAM_LENGH, BEAM_WIDTH);
        }         //画PAUSE或GAME OVER
        private void DrawGamePauseOver(ref Graphics gMonitor)
        {
            if (_missBalls >= MAX_MISS_BALLS)
            {
                _gameOver = true;
                _gameGoing = false;                 RestartToolStripMenuItem.Text = "开始游戏";
            }             if (_gameOver == true)
            { 
                if (_gameOverPausedCnt < WORD_FLASH)
                    gMonitor.DrawString("Game Over", new Font(FontFamily.GenericSansSerif, 18.0F, FontStyle.Regular), Brushes.Red, new PointF(80, 220));
            }
            else if (_pasued==true)
            {
                if (_gameOverPausedCnt < WORD_FLASH)
                    gMonitor.DrawString("Game Paused",new Font(FontFamily.GenericSansSerif,18.0F, FontStyle.Regular),Brushes.Red,new PointF(80,220));
            }
        }         //画状态
        private void DrawRecordState()
        {
            _gRecord.DrawString("接球数: " + _recvBalls.ToString(), new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 18));
            _gRecord.DrawString("丢球数: " + _missBalls.ToString(), new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 40));             _gRecord.DrawString("最高成绩: ", new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 80));             _gRecord.DrawString("困难: ", new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 122));
            _gRecord.DrawString("创造者: " + _hardRecord.UserName, new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 144));
            _gRecord.DrawString("接球数: " + _hardRecord.RecvBalls.ToString(), new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 166));             _gRecord.DrawString("中等: ", new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 206));
            _gRecord.DrawString("创造者: " + _midRecord.UserName, new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 228));
            _gRecord.DrawString("接球数: " + _midRecord.RecvBalls.ToString(), new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 250));             _gRecord.DrawString("简单: ", new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 290));
            _gRecord.DrawString("创造者: " + _simpRecord.UserName, new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 312));
            _gRecord.DrawString("接球数: " + _simpRecord.RecvBalls.ToString(), new Font(FontFamily.GenericSansSerif, 10.0F, FontStyle.Regular), Brushes.Red, new PointF(10, 334));
        }         //若是破纪录则将将破纪录者和分数更新到record.ini中
        private void SetUserRecord()
        {
            if (_gameOver && !_writeToFileFlag)
            {
                _writeToFileFlag = true;                 switch (dgreeOfDifcuty.Diffcuty)
                {
                    case DIFFCUTY_HARD:                         if (_recvBalls > _hardRecord.RecvBalls)
                        {
                            using (SaveRecord sr = new SaveRecord())
                            {
                                sr.myEvt += new GetUserNameEvt(GetRecordUserName);
                                sr.ShowDialog();                                 _hardRecord.UserName = _tmpRecordName;
                                _hardRecord.RecvBalls = _recvBalls;                                 try
                                {
                                    if (File.Exists(@"c:/record.ini"))
                                    {
                                        File.Delete(@"c:/record.ini");
                                    }                                     using (StreamWriter sw = File.CreateText(@"c:/record.ini"))
                                    {
                                        sw.WriteLine(_hardRecord.UserName);
                                        sw.WriteLine(_hardRecord.RecvBalls.ToString());                                         sw.WriteLine(_midRecord.UserName);
                                        sw.WriteLine(_midRecord.RecvBalls.ToString());                                         sw.WriteLine(_simpRecord.UserName);
                                        sw.WriteLine(_simpRecord.RecvBalls.ToString());                                         sw.Close();
                                        sw.Dispose();
                                    }                                 }
                                catch { }
                            }
                        }                         break;                     case DIFFCUTY_MIDD:                         if (_recvBalls > _midRecord.RecvBalls)
                        {
                            using (SaveRecord sr = new SaveRecord())
                            {
                                sr.myEvt += new GetUserNameEvt(GetRecordUserName);
                                sr.ShowDialog();                                 _midRecord.UserName = _tmpRecordName;
                                _midRecord.RecvBalls = _recvBalls;                                 try
                                {
                                    if (File.Exists(@"c:/record.ini"))
                                    {
                                        File.Delete(@"c:/record.ini");
                                    }                                     using (StreamWriter sw = File.CreateText(@"c:/record.ini"))
                                    {
                                        sw.WriteLine(_hardRecord.UserName);
                                        sw.WriteLine(_hardRecord.RecvBalls.ToString());                                         sw.WriteLine(_midRecord.UserName);
                                        sw.WriteLine(_midRecord.RecvBalls.ToString());                                         sw.WriteLine(_simpRecord.UserName);
                                        sw.WriteLine(_simpRecord.RecvBalls.ToString());                                         sw.Close();
                                        sw.Dispose();
                                    }                                 }
                                catch { }
                            }
                        }                         break;                     case DIFFCUTY_SIMP:                         if (_recvBalls > _simpRecord.RecvBalls)
                        {
                            using (SaveRecord sr = new SaveRecord())
                            {
                                sr.myEvt += new GetUserNameEvt(GetRecordUserName);
                                sr.ShowDialog();                                 _simpRecord.UserName = _tmpRecordName;
                                _simpRecord.RecvBalls = _recvBalls;                                 try
                                {
                                    if (File.Exists(@"c:/record.ini"))
                                    {
                                        File.Delete(@"c:/record.ini");
                                    }                                     using (StreamWriter sw = File.CreateText(@"c:/record.ini"))
                                    {
                                        sw.WriteLine(_hardRecord.UserName);
                                        sw.WriteLine(_hardRecord.RecvBalls.ToString());                                         sw.WriteLine(_midRecord.UserName);
                                        sw.WriteLine(_midRecord.RecvBalls.ToString());                                         sw.WriteLine(_simpRecord.UserName);
                                        sw.WriteLine(_simpRecord.RecvBalls.ToString());                                         sw.Close();
                                        sw.Dispose();
                                    }                                 }
                                catch { }
                            }
                        }                         break;                     default:                         break;
                }              
            }
        }         //获取破纪录的名字
        private void GetRecordUserName(string str)
        {
            _tmpRecordName = str;
        }
    }
}     程序下载地址:http://download.csdn.net/source/3301365