0

所以我在做一个突破性的游戏,到目前为止一切都很顺利,但是当我让球图片框离开屏幕时,我似乎得到了无限数量的消息框。

here is the code snippet

   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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
    List<Brick> bricks = new List<Brick>();
    Ball _ball;
    Paddle _paddle;
    DateTime startTime = DateTime.Now;
    DateTime startTime2 = DateTime.Now;

    float ballVelocityX = 0;
    float ballVelocityY = -1;
    float ballSpeed = 20;

    public Form1()
    {
        InitializeComponent();
        timer1.Enabled = true;
        timer2.Enabled = true;
        startTime = DateTime.Now;
        System.Media.SoundPlayer sndPlayer;
        sndPlayer = new System.Media.SoundPlayer(Properties.Resources._01_Calm_1);
        foreach (PictureBox brick in panel1.Controls)
        {
            if (brick.Name != "ball" && brick.Name != "paddle")
                bricks.Add(new Brick(brick));
            else if (brick.Name == "ball")
                _ball = new Ball(brick);
            else if (brick.Name == "paddle")
                _paddle = new Paddle(brick);
            //sndPlayer.Play();

        }
    }

    private void gameTimer_Tick(object sender, EventArgs e)
    {
        checkBrickCollision();
        checkPaddleCollision();
        checkBallScreenBounds();

        ball.SetBounds(ball.Location.X + (int)(ballVelocityX * ballSpeed),
            ball.Location.Y + (int)(ballVelocityY * ballSpeed),
            ball.Size.Width, ball.Size.Height);
        paddle.SetBounds(Cursor.Position.X - panel1.Location.X, paddle.Location.Y,
            paddle.Size.Width, paddle.Size.Height);
    }

    private void checkBallScreenBounds()
    {
        if (_ball.BallRectangle.Location.X + 25 > panel1.Size.Width)
            ballVelocityX *= -1;
        else if (_ball.BallRectangle.Location.X < 0)
            ballVelocityX *= -1;
        else if (_ball.BallRectangle.Location.Y < 0)
            ballVelocityY *= -1;
        else if (_ball.BallRectangle.Location.Y > panel1.Size.Height)
            ballOffScreen();
    }

    private void ballOffScreen()
    {
        new_start();
        Startnew startnew = new Startnew();
        startnew.Show();
        this.Close();
    }

    private void new_start()
    {            
        TimeSpan elapsedTime = DateTime.Now - startTime2;
        DialogResult result;
        result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes + elapsedTime.Seconds
            + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo);
        if (result == DialogResult.No)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
        else if (result == DialogResult.Yes)
        {
            this.Close();
        }

        //MessageBox.Show("Total time is " + elapsedTime.Minutes + ":" + elapsedTime.Seconds);

    }

    private void checkPaddleCollision()
    {
        int tmpBallVelocityX = _paddle.CheckPaddleMovement();

        if (_ball.BallRectangle.IntersectsWith(_paddle.PaddleRectangle))
        {
            ballVelocityX = tmpBallVelocityX;
            ballVelocityY *= -1;
        }
    }

    private void checkBrickCollision()
    {
        Rectangle ballRectangle = _ball.BallRectangle;

        foreach (Brick brick in bricks)
            if (brick.IntersectWith(ballRectangle))
            {
                bricks.Remove(brick);
                ballVelocityX *= -1;
                ballVelocityY *= -1;
                break;
            }
    }

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        Cursor.Hide();
    }

    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        Cursor.Show();
    }
    public void Form_closse(object sender, FormClosedEventArgs e)
    {
        //ProgramInfo.ProgramState = State.Splash_Screen;
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }
    private void timer1_tick(object sender, EventArgs e)
    {
        LabelTime.Text = "It is " + DateTime.Now;
    }
    private void timer2_tick(object sender, EventArgs e)
    {
        TimeSpan elapsedTime = DateTime.Now - startTime;
        labelElapsedTime.Text = "Time Elapsed " + elapsedTime.Minutes + ":" + elapsedTime.Seconds;
      }
    }
  }
    ` #**EDIT**#`
    `**because things were getting a bit confusing i posted all my code.**`

我用一个按钮完成了这个,它工作正常,但我只需要弹出一次。有没有办法只调用一次?

4

2 回答 2

1

如果 new_start() 是 Form1 的一部分,

以下行看起来像是罪魁祸首,但需要更多代码才能仔细查看。

if (result == DialogResult.No)
    {
        Form1 form1 = new Form1();
        form1.Show(); //This line is creating endless messagebox on selecting NO
    }

更新根据评论中的信息,

你可以用 flag 控制 new_start 函数

private bool checkNewGame = false;
private void new_start()
{        
    if(checkNewGame) return; 
    checkNewGame = true;    
    TimeSpan elapsedTime = DateTime.Now - startTime2;
    DialogResult result;
    result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes +   elapsedTime.Seconds
        + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo);
    if (result == DialogResult.No)
    {
        Form1 form1 = new Form1();
        form1.Show();
    }
    else if (result == DialogResult.Yes)
    {
       checkNewGame = true; 
       this.Close();

    }

更新 2

代替

if (result == DialogResult.No)
{
    Form1 form1 = new Form1();
    form1.Show(); //This line is creating endless messagebox on selecting NO
}

做类似的事情

if (result == DialogResult.No)
{
    this.Clear(); // Create new function Clear, and clear all the Form state.
}

void clear()
{
  ... //RESET all form state
}
于 2012-05-17T04:11:17.913 回答
0

很可能应该重新启动游戏的代码没有清除“ball-Off-Screen”情况,您的游戏立即进入游戏结束。

注意:调试应该立即显示行为的原因......

于 2012-05-17T04:22:07.220 回答