From b07c131ff679e05812ae57e74eb08dc3e30ee87a Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Thu, 16 Oct 2025 14:20:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=A1=E7=82=B9=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dto/GroupJumpRopeContext.cs | 3 +- .../Dto/MusicJumpRopeContext.cs | 34 ++++++++++++-- .../Views/JumpRope/MusicJumpRope.xaml.cs | 46 +++++++++++-------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs b/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs index a396dd7..98dbf52 100644 --- a/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs +++ b/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs @@ -69,6 +69,7 @@ namespace Dto public int Rank { get; set; } public string Name { get; set; } public int Number { get; set; } + public double Score { get; set; } } - } + diff --git a/Wpf_AiSportsMicrospace/Dto/MusicJumpRopeContext.cs b/Wpf_AiSportsMicrospace/Dto/MusicJumpRopeContext.cs index 5da4829..8524ae2 100644 --- a/Wpf_AiSportsMicrospace/Dto/MusicJumpRopeContext.cs +++ b/Wpf_AiSportsMicrospace/Dto/MusicJumpRopeContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Text; using Wpf_AiSportsMicrospace.MyUserControl; using Yztob.AiSports.Postures.Sports; @@ -14,7 +15,7 @@ namespace Dto public List<(double XNorm, double YNorm)> CirclePositions { get; private set; } public List UserList { get; private set; } public List UserNumberList { get; private set; } - public List UserScoreList { get; private set; } + public List UserScoreList { get; private set; } public List Sports { get; private set; } public List UseNameList { get; private set; } = new() { "一号位", "二号位" }; public List UserBeatSyncList = new List(); @@ -53,13 +54,12 @@ namespace Dto CirclePositions = new List<(double XNorm, double YNorm)> { (0.21, 0.88), - (0.78, 0.88), + (0.50, 0.88), }; UserList = new List(); UserNumberList = new List(); Sports = new List(); - } // 更新排行榜方法 @@ -83,5 +83,33 @@ namespace Dto return rankList; } + public List UpdateScoreRankList() + { + int totalBeats = MusicBeats.Count; // 总节拍数 + const double maxScore = 100.0; // 满分 100 + + var rankList = UserBeatSyncList + .Select((beatCount, index) => new + { + Index = index, + Name = UseNameList[index], + BeatCount = beatCount, + // 按照 100 分制计算得分 + Score = Math.Round((beatCount / (double)totalBeats) * maxScore, 1) + }) + .OrderByDescending(x => x.Score) + .Select((x, rank) => new RankItem + { + Rank = rank + 1, + Name = x.Name, + Number = x.BeatCount, // 卡点个数 + Score = x.Score // 总分(百分制) + }) + .ToList(); + + // 同步更新 UserScoreList + UserScoreList = rankList.Select(x => x.Score).ToList(); + return rankList; + } } } diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs index 5e84d90..bfb9cb8 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/MusicJumpRope.xaml.cs @@ -271,23 +271,31 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope await Task.Delay(1000); } + // 停止音乐 + Utils.StopBackgroundMusic(); + // 停止实时检测和视频流 + countdownGrid.Visibility = Visibility.Hidden; - - _musicJumpRopeContext.UserList.ForEach(x => - { - x.ImageState = "1"; - }); - _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; _mainWin.WebcamClient.StopExtract(); - RankingItemList = _musicJumpRopeContext.UpdateRankList(); - //ShowRankingBoard(RankingItemList); - Utils.StopBackgroundMusic(); + // 计算排名 + var rankList = _musicJumpRopeContext.UpdateRankList(); + var scoreList = _musicJumpRopeContext.UpdateScoreRankList(); + + // 更新游戏状态 _currentGameState = GameState.Finished; - _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; - _mainWin.WebcamClient.StartExtract(); + + // 恢复视频流 + Application.Current.Dispatcher.Invoke(() => + { + _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; + _mainWin.WebcamClient.StartExtract(); + + // 更新 UI(排行榜或分数板) + // ShowRankingBoard(rankList); + }); } private DateTime? _raiseStartTime; @@ -435,6 +443,8 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope _musicJumpRopeContext.Sports.Clear(); _musicJumpRopeContext.UserList.Clear(); _musicJumpRopeContext.UserNumberList.Clear(); + _musicJumpRopeContext.UserBeatSyncList.Clear(); + _musicBeatTextBlock.Clear(); double imgWidth = userBox.ActualWidth; double imgHeight = userBox.ActualHeight; @@ -472,11 +482,11 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope bool isOnBeat = _musicBeats.Any(bt => Math.Abs(bt - _currentTime) <= _beatTolerance); if (isOnBeat) { - _musicJumpRopeContext.UserBeatSyncList[i]++; + _musicJumpRopeContext.UserBeatSyncList[indexCopy]++; - if (_musicJumpRopeContext.UserBeatSyncList[i] < count) + if (_musicJumpRopeContext.UserBeatSyncList[indexCopy] < count) { - _musicBeatTextBlock[i].Text = $"卡点 x{_musicJumpRopeContext.UserBeatSyncList[i]}"; + _musicBeatTextBlock[indexCopy].Text = $"卡点 x{_musicJumpRopeContext.UserBeatSyncList[indexCopy]}"; } //Application.Current.Dispatcher.BeginInvoke(() => @@ -536,10 +546,8 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope } // 推送计数 - if (hasHuman) - _musicJumpRopeContext.Sports[i].Pushing(humanInCircle); - - + //if (hasHuman) + _musicJumpRopeContext.Sports[i].Pushing(humanInCircle); } } private string GetJumpState(int circleIndex, bool humanInCircle) @@ -587,7 +595,7 @@ namespace Wpf_AiSportsMicrospace.Views.JumpRope userItem.VerticalAlignment = VerticalAlignment.Top; userItem.HorizontalAlignment = HorizontalAlignment.Left; userItem.ImageState = "1"; - userItem.Margin = new Thickness(centerX - 120, centerY - 700, 0, 0); + userItem.Margin = new Thickness(centerX + 120, centerY - 700, 0, 0); // ----------- 创建上方 TextBlock ------------ var textBlock = new TextBlock