From e202153a256995c97b165031c169a94d693a43ce Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Tue, 21 Oct 2025 17:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wpf_AiSportsMicrospace/Enum/GameState.cs | 3 +- Wpf_AiSportsMicrospace/Views/Home.xaml.cs | 7 +- .../Views/JumpRope/GroupJumpRope.xaml.cs | 81 +++++++++---------- .../Views/JumpRope/MusicJumpRope.xaml | 2 +- .../Views/JumpRope/MusicJumpRope.xaml.cs | 81 +++++++++---------- 5 files changed, 82 insertions(+), 92 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Enum/GameState.cs b/Wpf_AiSportsMicrospace/Enum/GameState.cs index 13f2ef6..dc9ca53 100644 --- a/Wpf_AiSportsMicrospace/Enum/GameState.cs +++ b/Wpf_AiSportsMicrospace/Enum/GameState.cs @@ -8,6 +8,7 @@ namespace Enum { NotStarted, // 游戏未开始 Running, // 游戏进行中 - Finished // 游戏完成 + Finished, // 游戏完成 + RaiseHand } } diff --git a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs index 52bee37..fc43f5b 100644 --- a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs @@ -142,11 +142,16 @@ namespace Wpf_AiSportsMicrospace var newPage = new GroupJumpRope(); mainWin?.SwitchPageWithMaskAnimation(newPage, true); } - else + else if (coverFlow.SelectedIndex == 1) { var newPage = new MusicJumpRope(); mainWin?.SwitchPageWithMaskAnimation(newPage, true); } + else + { + var newPage = new TrainingRecords(); + mainWin?.SwitchPageWithMaskAnimation(newPage, true); + } } //跳转二级页面 diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index 7547f4f..0b40454 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -162,64 +162,55 @@ namespace Wpf_AiSportsMicrospace.Views // 可在这里绑定抽帧事件 _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; } + + private bool _isCountdownStarted = false; // 新增标记 private void OnHumanFrameUpdated(object sender, List humans) { try { if (humans == null || humans.Count == 0) return; + switch (_currentGameState) { case GameState.NotStarted: // 未开始 - int leftWaving = DetectLeftHandRaise(humans); - if (leftWaving == 5) + if (!_isCountdownStarted) // 只有倒计时未开始才检测左手 { - _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; - _mainWin.WebcamClient.StopExtract(); - - // 举左手逻辑,例如结束动画或退出 - var newPage = new Home(); - _mainWin?.SwitchPageWithMaskAnimation(newPage, true); - } - else { - int rightWaving = DetectRightHandRaise(humans); - if (rightWaving >= 3) + int leftWaving = DetectLeftHandRaise(humans); + if (leftWaving == 5) { - switch (rightWaving) - { - case (int)WavingAction.FirstHand: - StartCountdown(3); - break; - case (int)WavingAction.Raising: - UpdateCountdown(); - break; - case (int)WavingAction.RaiseHand: - FinishCountdown(); - _currentGameState = GameState.Running; // 倒计时完成 → 游戏开始 - break; - } + _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; + _mainWin.WebcamClient.StopExtract(); + + // 举左手逻辑,例如结束动画或退出 + var newPage = new Home(); + _mainWin?.SwitchPageWithMaskAnimation(newPage, true); + return; // 提前退出 + } + } + + int rightWaving = DetectRightHandRaise(humans); + if (rightWaving >= 3) + { + switch (rightWaving) + { + case (int)WavingAction.FirstHand: + StartCountdown(3); + _isCountdownStarted = true; // 倒计时开始,停止左手检测 + break; + case (int)WavingAction.Raising: + UpdateCountdown(); + break; + case (int)WavingAction.RaiseHand: + FinishCountdown(); + _currentGameState = GameState.Running; // 倒计时完成 → 游戏开始 + break; } } break; case GameState.Running: // 游戏进行中 UpdateCircleCounts(humans); - - // 可选:判断游戏结束条件,将状态切换到 Finished - // if (CheckGameFinished()) _currentGameState = GameState.Finished; break; - - //case GameState.Finished: // 游戏完成 - // int leftWaving = DetectLeftHandRaise(humans); - // if (leftWaving == 5) - // { - // _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; - // _mainWin.WebcamClient.StopExtract(); - - // // 举左手逻辑,例如结束动画或退出 - // var newPage = new Home(); - // _mainWin?.SwitchPageWithMaskAnimation(newPage, true); - // } - // break; } } catch (Exception ex) @@ -296,6 +287,8 @@ namespace Wpf_AiSportsMicrospace.Views Utils.StopBackgroundMusic(); _currentGameState = GameState.NotStarted; + + _isCountdownStarted = false; _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; _mainWin.WebcamClient.StartExtract(); } @@ -657,7 +650,7 @@ namespace Wpf_AiSportsMicrospace.Views * time: 动画持续时间(毫秒) * name: 元素名称 */ - public void HideElement(string name , int time) + public void HideElement(string name, int time) { var Element = FindName(name) as Grid; if (Element == null) return; @@ -672,9 +665,9 @@ namespace Wpf_AiSportsMicrospace.Views * time: 动画持续时间(毫秒) * name: 元素名称 */ - public void showElement(string name, int time = 600 ) + public void showElement(string name, int time = 600) { - var Element = FindName(name) as FrameworkElement; + var Element = FindName(name) as FrameworkElement; if (Element == null) return; Element.Visibility = Visibility.Visible; diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml index bc15181..a391f22 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml @@ -25,7 +25,7 @@ humans) { try { if (humans == null || humans.Count == 0) return; + switch (_currentGameState) { case GameState.NotStarted: // 未开始 - int leftWaving = DetectLeftHandRaise(humans); - if (leftWaving == 5) + if (!_isCountdownStarted) // 只有倒计时未开始才检测左手 { - _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; - _mainWin.WebcamClient.StopExtract(); - - // 举左手逻辑,例如结束动画或退出 - var newPage = new Home(); - _mainWin?.SwitchPageWithMaskAnimation(newPage, true); - } - else - { - int rightWaving = DetectRightHandRaise(humans); - if (rightWaving >= 3) + int leftWaving = DetectLeftHandRaise(humans); + if (leftWaving == 5) { - switch (rightWaving) - { - case (int)WavingAction.FirstHand: - StartCountdown(3); - break; - case (int)WavingAction.Raising: - UpdateCountdown(); - break; - case (int)WavingAction.RaiseHand: - FinishCountdown(); - _currentGameState = GameState.Running; // 倒计时完成 → 游戏开始 - break; - } + _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; + _mainWin.WebcamClient.StopExtract(); + + // 举左手逻辑,例如结束动画或退出 + var newPage = new Home(); + _mainWin?.SwitchPageWithMaskAnimation(newPage, true); + return; // 提前退出 + } + } + + int rightWaving = DetectRightHandRaise(humans); + if (rightWaving >= 3) + { + switch (rightWaving) + { + case (int)WavingAction.FirstHand: + StartCountdown(3); + _isCountdownStarted = true; // 倒计时开始,停止左手检测 + break; + case (int)WavingAction.Raising: + UpdateCountdown(); + break; + case (int)WavingAction.RaiseHand: + FinishCountdown(); + _currentGameState = GameState.Running; // 倒计时完成 → 游戏开始 + break; } } break; case GameState.Running: // 游戏进行中 UpdateCircleCounts(humans); - - // 可选:判断游戏结束条件,将状态切换到 Finished - // if (CheckGameFinished()) _currentGameState = GameState.Finished; break; - - //case GameState.Finished: // 游戏完成 - // int leftWaving = DetectLeftHandRaise(humans); - // if (leftWaving == 5) - // { - // _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; - // _mainWin.WebcamClient.StopExtract(); - - // // 举左手逻辑,例如结束动画或退出 - // var newPage = new Home(); - // _mainWin?.SwitchPageWithMaskAnimation(newPage, true); - // } - // break; } } catch (Exception ex) @@ -229,13 +218,14 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope } } + private int _currentCountdown = 3; private DateTime _lastUpdateTime = DateTime.Now; private void StartCountdown(int start = 4) { _currentCountdown = start; - countdownText.Text = _currentCountdown.ToString(); - countdownGrid.Visibility = Visibility.Visible; + //countdownText.Text = _currentCountdown.ToString(); + //countdownGrid.Visibility = Visibility.Visible; _lastUpdateTime = DateTime.Now; _mainWin.ShowCountDownAnimation(); Utils.PlayBackgroundMusic("countdown_3.mp3", false); @@ -264,7 +254,7 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope countdownText.Text = "GO!"; countdownGrid.Visibility = Visibility.Hidden; // 启动60秒倒计时(独立任务) - StartGameCountdown(105); + StartGameCountdown(108); ShowMusicAnimation(); } @@ -301,6 +291,7 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope // 更新游戏状态 _currentGameState = GameState.NotStarted; + _isCountdownStarted = false; // 恢复视频流 Application.Current.Dispatcher.Invoke(() =>