From 1bec6bbb84e543aa16eaefe566c87999dc71ccae Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Mon, 30 Mar 2026 16:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=8C=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Views/SquatFu/WhackAMole.xaml.cs | 86 +++++++++---------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs b/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs index dd7f0e0..eb7dd4f 100644 --- a/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/SquatFu/WhackAMole.xaml.cs @@ -42,8 +42,6 @@ namespace Views.SquatFu PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private GameState _currentGameState = GameState.NotStarted; - private readonly object _updateLock = new object(); - private readonly Dictionary _jumpStatus = new Dictionary(); private WhackAMoleContext _WhackAMoleContext; Image tipImage; @@ -80,11 +78,14 @@ namespace Views.SquatFu _WhackAMoleContext.CirclePositions = Utils.GetPointsList(pointCount, cfg); - DrawCirclesWithText(); - // 播放音乐 - PlayMusic("raisehand.mp3"); + _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; _mainWin.WebcamClient.StartExtract(); + + DrawCirclesWithText(); + + // 播放音乐 + PlayMusic("raisehand.mp3"); } private void UserControl_Unloaded(object sender, RoutedEventArgs e) @@ -180,61 +181,51 @@ namespace Views.SquatFu private bool _isCountdownStarted = false; // 新增标记 private void OnHumanFrameUpdated(object sender, List humans) { - try + if (humans == null || humans.Count == 0) return; + + switch (_currentGameState) { - if (humans == null || humans.Count == 0) return; - - switch (_currentGameState) - { - case GameState.NotStarted: // 未开始 - if (!_isCountdownStarted) // 只有倒计时未开始才检测左手 + case GameState.NotStarted: // 未开始 + if (!_isCountdownStarted) // 只有倒计时未开始才检测左手 + { + int leftWaving = sportOperate.VerifyLeftWavingAction(humans); + if (leftWaving == 5) { - int leftWaving = sportOperate.VerifyLeftWavingAction(humans); - if (leftWaving == 5) - { - _isCountdownStarted = true; - _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; + _isCountdownStarted = true; + _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; - // 举左手逻辑,例如结束动画或退出 - var newPage = new Home(); - _mainWin?.SwitchPageWithMaskAnimation(newPage, true); - return; // 提前退出 - } + // 举左手逻辑,例如结束动画或退出 + var newPage = new Home(); + _mainWin?.SwitchPageWithMaskAnimation(newPage, true); + return; // 提前退出 } + } - int rightWaving = sportOperate.VerifyRightWavingAction(humans); - if (rightWaving >= 3 && RankingGrid.Visibility != Visibility.Visible) + int rightWaving = sportOperate.VerifyRightWavingAction(humans); + if (rightWaving >= 3 && RankingGrid.Visibility != Visibility.Visible) + { + switch (rightWaving) { - switch (rightWaving) - { - case (int)WavingAction.FirstHand: - _currentGameState = GameState.Running; - StartCountdown(3); - _isCountdownStarted = true; // 倒计时开始,停止左手检测 - break; - } + case (int)WavingAction.FirstHand: + _currentGameState = GameState.Running; + StartCountdown(3); + _isCountdownStarted = true; // 倒计时开始,停止左手检测 + break; } - break; + } + break; - case GameState.Running: // 游戏进行中 - UpdateCircleCounts(humans); - break; - } - } - catch (Exception ex) - { - Console.WriteLine("OnFrameExtracted error: " + ex.Message); + case GameState.Running: // 游戏进行中 + UpdateCircleCounts(humans); + break; } } - private double _currentCountdown = 3600; - private DateTime _lastUpdateTime = DateTime.Now; private async void StartCountdown(int start = 3) { _mainWin.ShowCountDownAnimation(); - Utils.PlayBackgroundMusic("whackamole.mp3", false); - + Utils.PlayBackgroundMusic("countdown_3.mp3", false); //开始人脸识别 ============================================= //Action < VideoFrame > handler = null; @@ -279,9 +270,10 @@ namespace Views.SquatFu countdownText.Text = "GO!"; countdownGrid.Visibility = Visibility.Hidden; - // 播放背景音乐(循环) - Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true); + + Utils.PlayBackgroundMusic("whackamole.mp3", false); + // 启动60秒倒计时(独立任务) StartGameCountdown(60);