我遇到了产生敌人的问题。我试图用Timer和for循环生成它们,但似乎它不会在游戏中产生我的敌人。这是代码,我现在不知道如何产生它们:
private void startButton_Click(object sender, EventArgs e)
{
startButtonWasClicked = true;
logo.Visible = false;
startButton.Visible = false;
startButton.Enabled = false;
spaceship.Visible = true;
PictureBox enemy = new PictureBox();
enemy.BackColor = Color.White;
enemy.Height = 20;
enemy.Width = 20;
Random position = new Random();
int enemyPositionX = position.Next(0, 330);
enemy.Location = new Point(enemyPositionX, 5);
this.Controls.Add(enemy);
System.Timers.Timer t3 = new System.Timers.Timer();
t3.Interval = 500;
t3.Elapsed += (sender3, args) =>
{
if (enemy.Location.Y <= 400)
{
enemy.Location = new Point(enemy.Location.X, enemy.Location.Y + 2);
}
else
{
enemy.Visible = false;
}
}; t3.Start();
}
我还有一个导弹射击问题。我试图连续5次阻挡我的拍摄作为最大值,它只能在第一次使用,然后用户可以快速按空间然后轻松赢得游戏。
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
e.SuppressKeyPress = true;
if (startButtonWasClicked == true)
{
if (shoots < 5)
{
shoots++;
PictureBox missile = new PictureBox();
missile.Location = new Point(spaceship.Location.X+28, spaceship.Location.Y+5);
missile.Image = (Image)Space_Defender_3.Properties.Resources.ResourceManager.GetObject("Missile");
missile.Height = 42;
missile.Width = 12;
this.Controls.Add(missile);
System.Timers.Timer t1 = new System.Timers.Timer();
t1.Interval = 50;
t1.Elapsed += (sender1, args) =>
{
if (missile.Location.Y > 0)
{
missile.Location = new Point(missile.Location.X, missile.Location.Y - 5);
}
else
{
missile.Visible = false;
}
};
t1.Start();
}
else if(shoots >= 5)
{
System.Timers.Timer t2 = new System.Timers.Timer();
t2.Interval = 2000;
t2.Elapsed += (sender2, args) =>
{
shoots = 0;
};
t2.Start();
}
请帮助我,如果有人可以帮助我,我会很高兴。
更新
我在想它,但它仍然没有产生我的敌人。
private void startButton_Click(object sender, EventArgs e)
{
startButtonWasClicked = true;
logo.Visible = false;
startButton.Visible = false;
startButton.Enabled = false;
spaceship.Visible = true;
enemy.BackColor = Color.Black;
enemy.Image = Space_Defender_3.Properties.Resources.Enemy;
enemy.Height = 36;
enemy.Width = 29;
System.Timers.Timer t4 = new System.Timers.Timer();
t4.Interval = 500;
t4.Elapsed += (sender4, args) =>
{
for(int i = 0; i<50; i++)
{
Random position = new Random();
int enemyPositionX = position.Next(0, 330);
enemy.Location = new Point(enemyPositionX, 5);
this.Controls.Add(enemy);
}
};
t4.Start();
System.Timers.Timer t3 = new System.Timers.Timer();
t3.Interval = 500;
t3.Elapsed += (sender3, args) =>
{
if (enemy.Location.Y <= 400)
{
enemy.Location = new Point(enemy.Location.X, enemy.Location.Y + 2);
}
else
{
enemy.Visible = false;
}
}; t3.Start();
}