所以我正在进行突破游戏,到目前为止一切进展顺利,但是当我将球图片框移出屏幕时,我似乎得到了无限量的消息框。
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.**`
我已经用一个按钮做了这个,它工作正常,但我需要只弹出一次。有没有办法让它只召唤一次?
答案 0 :(得分:1)
如果new_start()是Form1的一部分,
以下行看起来像罪魁祸首,但需要更多代码才能仔细查看。
if (result == DialogResult.No)
{
Form1 form1 = new Form1();
form1.Show(); //This line is creating endless messagebox on selecting NO
}
更新根据评论中的信息
您可以使用标志
控制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
}
答案 1 :(得分:0)
最有可能重启游戏的代码不会清除“屏幕外球”状态,你的游戏会立即进入游戏结束。
注意:调试应该立即显示行为的原因......