我在工作中很无聊,所以我试着制作一个简单的飞船射击游戏。 当我按空格时,一个矩形被解雇了#34;在页面上,每隔一个矩形从上到下。
我希望能够检查这两个是否相交,但因为两者都采用不同的方法,如果它们相交需要定期检查,我不知道如何解决这个问题。
这是我的代码,直到现在:
if (e.Key == Key.Space)
{
Rectangle rect = new Rectangle()
{
Width = 10,
Height = 30,
Fill = Brushes.Green,
Stroke = Brushes.Red,
StrokeThickness = 2,
};
Maingrid.Children.Add(rect);
double countMult = countMove * 20;
rect.Margin = new Thickness(countMult + 15, 350, 0, 0);
double b = rect.Margin.Top;
while(b>= -600){
await Task.Delay(50);
rect.Margin = new Thickness(countMult+15, b - 40, 0, 0);
b = b-40;
}
Maingrid.Children.Remove(rect);
}
}
private async void Mainframe_Loaded(object sender, RoutedEventArgs e)
{
}
public async void enemyMethod(int width, int height, int speed, int x)
{
Rectangle enemy = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Green,
Stroke = Brushes.Blue,
StrokeThickness = 2,
};
Maingrid.Children.Add(enemy);
enemy.Margin = new Thickness(x, -590, 0, 0);
double b = enemy.Margin.Top;
while (b >= -600)
{
await Task.Delay(50);
enemy.Margin = new Thickness(x, b + speed, 0, 0);
b = b + speed;
}
Maingrid.Children.Remove(enemy);
}
private async void ButtonStart_Click(object sender, RoutedEventArgs e)
{
ButtonStart.Visibility = Visibility.Hidden;
Random rnd = new Random();
await Task.Delay(50);
while (true)
{
int random_x = rnd.Next(-550, 550);
enemyMethod(50, 30, 5, random_x); //width, height, speed, x-position
await Task.Delay(1000);
}
}
我知道如何检查它们是否与代码相交,但问题在逻辑上是如何将它拉下来的。