/// <summary> /// 游戏主循环 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnGameLoop(object sender, EventArgs e) { var span = DateTime.Now - startTime; var costTime = (span.Hours * 60 * 60 * 1000 + span.Minutes * 60 * 1000 + span.Seconds * 1000 + span.Milliseconds) / 1000d; tbTimer.Text = string.Format("您已坚持:{0} 秒", costTime); hero.Updata(); hero.Draw(); foreach (var oneBullet in bullets) { oneBullet.Update(); oneBullet.Draw(); if (hero.CollisionCircle(oneBullet.CenterOfCircle)) { GameCanvas.Children.Remove(oneBullet); HeroDied(); //被子弹击中 } } #region 闪电 if ((DateTime.Now - lastLightningTime).Seconds >= secondSpan) { lastLightningTime = DateTime.Now; secondSpan = GetRandomNum(3, 7); //3-7秒产生一次闪电 var posY = GetRandomNum(0, 550); var light = new Lightning(); Canvas.SetLeft(light, 0); Canvas.SetTop(light, posY); Canvas.SetZIndex(light, -100); GameCanvas.Children.Add(light); light.Fire += () => { var lightY = Canvas.GetTop(light) + 50; if (hero.nowPosition.Y < lightY + 25 && hero.nowPosition.Y > lightY - 25) { HeroDied(); //被闪电击中 } }; light.Completed += () => { GameCanvas.Children.Remove(light); //闪电播放完成后 }; light.Play(); //开始闪电 } #endregion }