private void GameOver()  
{
gameClock.Stop();
gameClock.Dispose();
MessageBox.Show("游戏结束", "游戏结束", MessageBoxButtons.OK);
clrForm = true;
Controls.Clear();//加了一句!就好了。******
this.Invalidate();
MainMenu();
}
好运!!!!!!!!!!!!

解决方案 »

  1.   

    snewxf(心疤) ,虽然可以了,但是随着按的ESC的累计的次数的增多.按一次ESC就会出现N++的MessageBox了.请问怎么解决?
      

  2.   

    我改了一下,关键是消息的重复订阅,我用一个变量来控制,不过应该还用更好的方法,你应该将它做为一个独立的类来写。
    using System;
    using System.Windows.Forms;
    using System.Drawing;public class OldGame : Form  
    {
    int ball_x, ball_y, p_x, p_y;
    int x_vel=-5, y_vel=-5;
    int score = 0;
    int n=0;
    Pen ballPen, paddlePen;
    Graphics g;
    Timer gameClock;
    Random randNum;
    Button play, quit;
    Label lbl1, lblScore;
    bool clrForm = false;

    public OldGame()  
    {
    MainMenu();


    }

    private void MainMenu()  
    {
    //初始化窗体
    n++;
    this.Text = "弹球游戏";
    this.Size = new Size(400, 400);
    this.BackColor = Color.White;
    this.MaximizeBox = false;
    this.MaximumSize = new Size(400, 400);
    this.MinimumSize = new Size(400, 400); //初始化控件
    lbl1 = new Label();
    lbl1.Text = "main menu";
    lbl1.Size = new Size(300, 30);
    lbl1.Location = new Point(165, 75);

    lblScore = new Label();
    lblScore.Text = "积分: " + score;
    lblScore.ForeColor = Color.Green ;
    lblScore.Size = new Size(60, 20);
    lblScore.Location = new Point(175, 120);

    play = new Button();
    play.Text = "start game";
    play.BackColor = Color.WhiteSmoke;
    play.Size = new Size(100, 100);
    play.Location = new Point(50, 150);


    quit = new Button();
    quit.Text = "exit game";
    quit.BackColor = Color.WhiteSmoke;
    quit.Size = new Size(100, 100);
    quit.Location = new Point(250, 150);

    eAdd();
    //添加控件
    Controls.Add(lbl1);
    Controls.Add(lblScore);
    Controls.Add(play);
    Controls.Add(quit);
    }
    private void eAdd()
    {

    play.Click += new EventHandler(myPlay);
    quit.Click += new EventHandler(myQuit);

    }
    private void eClear()
    {

    Controls.Remove(lbl1);
    Controls.Remove(lblScore);
    Controls.Remove(play);
    Controls.Remove(quit);

    quit.Dispose();
    }

    private void myPlay(Object sender, EventArgs e)  
    {
    //在游戏进行期间移除控件
    Controls.Remove(lbl1);
    Controls.Remove(play);
    Controls.Remove(quit);
    Controls.Remove(lblScore);

    gameClock = new Timer();
    randNum = new Random();

    ball_x = randNum.Next(295)+10;
    ball_y = randNum.Next(295)+10;
    p_x = 100;
    p_y = 100;
    ballPen = new Pen(Color.Blue , 1);
    paddlePen = new Pen(Color.Blue,5);

    score = 0;

    //建立事件处理关联
    if(n<2)
    {
    this.MouseMove += new MouseEventHandler(myMouse);
    this.Paint += new PaintEventHandler(myPaint);
    this.KeyUp += new KeyEventHandler(keyHandler);
    }
    clrForm = false; //启动游戏时钟
    gameClock.Interval = 30;
    gameClock.Tick += new EventHandler(gameLoop);
    gameClock.Start();
    }

    //若用户按下Esc键,则结束游戏
    private void keyHandler(Object sender, KeyEventArgs e)  
    {
    if ( e.KeyCode == Keys.Escape )
    GameOver();
    }

    //退出游戏
    private void myQuit(Object sender, EventArgs e)  
    {
    Application.Exit();
    }

    //根据鼠标位置移动挡板
    private void myMouse(Object sender, MouseEventArgs e)  
    {
    p_x = e.X;
    p_y = e.Y;
    }

    //界面重画
    private void myPaint(Object sender, PaintEventArgs e)  
    {
    g = e.Graphics;
    GameDraw();
    }

    //更新游戏界面
    private void GameDraw()  
    {
    if ( !clrForm )  
    {
    g.DrawEllipse(ballPen, ball_x, ball_y, 5, 5);
    g.DrawLine(paddlePen, p_x, 350, p_x+50, 350);
    //g.DrawLine(paddlePen, 350, p_y, 350, p_y+50);
    }
    }

    //更新游戏AI
    private void gameLoop(Object sender, EventArgs e)  
    {
    //实现镜面反弹
    if ( ball_x <= 1 )  
    x_vel *= -1;
    if ( ball_y <= 1 )
    y_vel *= -1;
    if (ball_x>= 375 )
    x_vel *= -1;

    //若小球被下挡板挡住,加分
    if ( ball_y+5 >= 350 && ball_y+5 <= 355 && ball_x+3 >= p_x && ball_x+3 <= p_x+50 )
    {
    y_vel *= -1;
    score++;
    //lblScore.text ="7";
    }

    //小球越界,游戏结束
    if ( ball_y >= 380 || ball_x >= 380)
    GameOver();

    //设定小球的下一位置
    ball_x += x_vel;
    ball_y += y_vel;

    //进行界面重画
    Invalidate();
    }

    //结束当前游戏
    private void GameOver()  
    {
    gameClock.Stop();
    gameClock.Dispose();
    MessageBox.Show("游戏结束", "游戏结束", MessageBoxButtons.OK);
    //eClear();
    clrForm = true;
    Invalidate();
    MainMenu();
    } private void InitializeComponent()
    {
    // 
    // OldGame
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "OldGame"; } public static void Main()  
    {
    //启动程序
    Application.Run(new OldGame());
    }
    }
      

  3.   

    其实设一标志位private bool blnTerminate;//Default is false
    //若用户按下Esc键,则结束游戏
    private void keyHandler(Object sender, KeyEventArgs e)  
    {
    if(blnTerminate==false)
    if ( e.KeyCode == Keys.Escape )
    {
    blnTerminate=true;
    GameOver();
    }
    }