我正在使用C#进行游戏,并且一旦生命达到0,游戏就不会结束。然而游戏确实显示结束MessageBox提醒用户他们没有剩余活动,按OK确定游戏应关闭并且Gameover对话框表格应该显示。然而,当Gameover表单显示时,游戏再次从头开始重新开始。任何人都可以帮助我,因为这个项目具有小型独立侧滚动游戏的所有元素,我将使用此游戏作为我下一个项目中的组件的介绍。 这是我用过的代码,如果你可以帮助我,我将不胜感激。请记住,这是我发布的第一个问题,并广泛搜索了我无法找到的解决方案。
using System; //--->
using System.Collections.Generic; //--->
using System.ComponentModel; //--->
using System.Data; //--->
using System.Drawing; //--->
using System.Linq; //--->
using System.Text; //--->
using System.Threading.Tasks; //--->
using System.Windows.Forms; //------>
namespace BouningBall
{
public partial class EasyGame : Form
{
public int speed_Left = 5;
public int speed_Top = 3;
public int Lifes = 3;
int Points = 0;
public EasyGame()
{
InitializeComponent();
EasyTimer.Enabled = true;
EasyLife.Text = Lifes.ToString();
this.FormBorderStyle = FormBorderStyle.Sizable;
this.TopMost = true;
this.Bounds = Screen.PrimaryScreen.Bounds;
Cursor.Hide();
Bar.Top = Playground.Bottom - (Playground.Bottom / 10);
}
private void EasyTimer_Tick(object sender, EventArgs e)
{
Bar.Left = Cursor.Position.X - (Bar.Width / 2);
Ball.Left += speed_Left;
Ball.Top += speed_Top;
if (EasyLife.Text == "0")
{
EasyTimer.Stop();
if (Lifes == 0)
EasyTimer.Stop();
this.Hide();
Cursor.Show();
string info = "lost ...";
string snippet = "all lifes have been used...";
MessageBoxButtons digits = MessageBoxButtons.OK;
DialogResult ending;
ending = MessageBox.Show(info, snippet, digits);
if (ending == System.Windows.Forms.DialogResult.OK)
{
EasyTimer.Stop();
EndLife Final = new EndLife();
Final.Show();
}
}
if (Ball.Bottom >= Bar.Top && Ball.Bottom <= Bar.Bottom && Ball.Left >= Bar.Left && Ball.Right <= Bar.Right)
{
speed_Top += 3;
speed_Left += 3;
speed_Top = -speed_Top;
Points += 1;
Easygamepoints.Text = Points.ToString();
}
if (Ball.Left <= Playground.Left)
{
speed_Left = -speed_Left;
}
if (Ball.Right >= Playground.Right)
{
speed_Left = -speed_Left;
}
if (Ball.Top <= Playground.Top)
{
speed_Top = -speed_Top;
}
if (Ball.Bottom >= Playground.Bottom)
{
EasyTimer.Stop();
Cursor.Show();
Lifes -= 1;
string message = "You have lost one of your lives...";
string caption = "You died, Life Lost...";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.OK)
{
EasyTimer.Enabled = false;
Cursor.Hide();
EasyLife.Text = Lifes.ToString();
EasyTimer.Start();
speed_Left = 5;
speed_Top = 3;
Ball.Top = 69;
Ball.Left = 128;
}
}
}
}
}
Due to Confusion, this body has been edited to allow better understanding of the objective that needs to be met.`enter code here`
答案 0 :(得分:0)
注意:可能不是答案,但我只能在评论部分发布这么多字符,所以它就在这里。
private void EasyTimer_Tick(object sender, EventArgs e)
{
Bar.Left = Cursor.Position.X - (Bar.Width / 2);
Ball.Left += speed_Left;
Ball.Top += speed_Top;
if (Ball.Bottom >= Bar.Top && Ball.Bottom <= Bar.Bottom && Ball.Left >= Bar.Left && Ball.Right <= Bar.Right)
{
speed_Top += 3;
speed_Left += 3;
speed_Top = -speed_Top;
Points += 1;
Easygamepoints.Text = Points.ToString();
}
if (Ball.Left <= Playground.Left)
...
...
if (Ball.Bottom >= Playground.Bottom)
{
EasyTimer.Stop();
Cursor.Show();
Lifes -= 1;
if (Lifes == 0)
{
DialogResult ending;
MessageBox.Show("all lifes have been used...", "lost ...", MessageBoxButtons.OK);
EndLife Final = new EndLife();
Final.Show();
// You need to return whether or not the user wants to play another game from this form
// See http://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form
if (Final.NewGame == true)
{
speed_Left = 5;
speed_Top = 3;
Ball.Top = 69;
Ball.Left = 128;
Lifes = 3;
EasyTimer.Start();
}
else
Close();
}
else
{
DialogResult result = MessageBox.Show("You have lost one of your lives...", "You died, Life Lost...", MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
EasyTimer.Start();
Cursor.Hide();
EasyLife.Text = Lifes.ToString();
speed_Left = 5;
speed_Top = 3;
Ball.Top = 69;
Ball.Left = 128;
}
else
Close();
}
}
}
这里需要注意的重要一点是,您必须从最终表单中检查用户是否想要玩另一个游戏。如果他们没有,那么关闭主表单。如果你不关闭表格,游戏永远不会结束。