所以我在做一个突破性的游戏,到目前为止一切都很顺利,但是当我让球图片框离开屏幕时,我似乎得到了无限数量的消息框。
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.**`
我用一个按钮完成了这个,它工作正常,但我只需要弹出一次。有没有办法只调用一次?