Add(Début-Tp3): on tente des trucs

Début-Tp3
Louis DUFOUR 1 year ago
parent 14a4beb21d
commit c9894be042

Binary file not shown.

@ -63,5 +63,6 @@ namespace MyGesturesBank
} }
public override string GestureName => "Clap Hands"; public override string GestureName => "Clap Hands";
} }
} }

@ -11,6 +11,7 @@
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Top"> <StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Top">
@ -24,13 +25,27 @@
<Button Content="Infrared" Margin="5" Click="ToInfraredImageStream"/> <Button Content="Infrared" Margin="5" Click="ToInfraredImageStream"/>
<Button Content="Body" Margin="5" Click="ToBodyImageStream"/> <Button Content="Body" Margin="5" Click="ToBodyImageStream"/>
<Button Content="BodyAndColor" Margin="5" Click="ToColorAndBodyImageStream"/> <Button Content="BodyAndColor" Margin="5" Click="ToColorAndBodyImageStream"/>
<Button Content="StartGame" Margin="5" Click="StartGameButton_Click"/>
<Button Content="StopGame" Margin="5" Click="StartGameButton_Click"/>
</StackPanel> </StackPanel>
<Viewbox Grid.Row="2" Stretch="Uniform"> <Viewbox Grid.Row="2" Stretch="Uniform">
<Grid> <Grid>
<Image Grid.Row="2" Source="{Binding CurrentKinectStream.Bitmap}" /> <!-- L'arrière-plan du capteur de couleur -->
<Canvas Grid.Row="2" x:Name="skeletonCanvas" /> <Image Source="{Binding CurrentKinectStream.Bitmap}" />
<!-- Le Canvas pour le squelette doit être par-dessus l'image -->
<Canvas x:Name="skeletonCanvas" />
<!-- Le Canvas pour les cercles doit être le dernier pour qu'il soit au premier plan -->
<Canvas x:Name="gameCanvas" Loaded="GameCanvas_Loaded">
<Ellipse x:Name="clapCircle" Width="100" Height="100" Fill="Purple" Opacity="0.5"/>
</Canvas>
</Grid> </Grid>
</Viewbox> </Viewbox>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10">
<TextBlock Text="Score: " FontWeight="Bold" FontSize="16"/>
<TextBlock x:Name="scoreText" Text="{Binding Score}" FontWeight="Bold" FontSize="16" Margin="5,0"/>
<TextBlock Text="Feedback: " FontWeight="Bold" FontSize="16" Margin="20,0,0,0"/>
<TextBlock x:Name="feedbackText" Text="{Binding Feedback}" FontWeight="Bold" FontSize="16" Foreground="Green" Margin="5,0"/>
</StackPanel>
</Grid> </Grid>
</Window> </Window>

@ -1,5 +1,7 @@
using Lib; using KinectUtils;
using Lib;
using Microsoft.Kinect; using Microsoft.Kinect;
using MyGesturesBank;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -16,6 +18,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Threading;
namespace WpfApp namespace WpfApp
{ {
@ -24,7 +28,25 @@ namespace WpfApp
/// </summary> /// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged public partial class MainWindow : Window, INotifyPropertyChanged
{ {
// Déclaration des variables nécessaires
private int score = 0;
private string feedback = "";
private DispatcherTimer gameTimer;
private MediaPlayer mediaPlayer = new MediaPlayer();
private DispatcherTimer circleTimer;
private double circleSize = 100; // Taille initiale du cercle
private Random random = new Random();
private const double someThreshold = 50.0; // Par exemple, 50 pixels
private const int MaxCircles = 5; // Maximum de 5 cercles à l'écran
private const double CircleStartSize = 50.0; // Taille initiale du cercle
private const double ReductionAmount = 2.0; // De combien le cercle réduit à chaque tick
private const double MinimumCircleSize = 5.0; // Taille minimale avant de disparaître
private KinectManager kinectManager; private KinectManager kinectManager;
public event PropertyChangedEventHandler PropertyChanged;
public KinectManager KinectManager public KinectManager KinectManager
{ {
get { return kinectManager; } get { return kinectManager; }
@ -40,7 +62,29 @@ namespace WpfApp
OnPropertyChanged(nameof(CurrentKinectStream)); OnPropertyChanged(nameof(CurrentKinectStream));
} }
} }
public event PropertyChangedEventHandler PropertyChanged;
// Propriétés pour le score et le feedback
public int Score
{
get => score;
set
{
score = value;
OnPropertyChanged(nameof(Score));
scoreText.Text = score.ToString(); // Mise à jour de l'interface utilisateur
}
}
public string Feedback
{
get => feedback;
set
{
feedback = value;
OnPropertyChanged(nameof(Feedback));
feedbackText.Text = feedback; // Mise à jour de l'interface utilisateur
}
}
public void OnPropertyChanged(string propertyName) public void OnPropertyChanged(string propertyName)
{ {
@ -58,15 +102,51 @@ namespace WpfApp
{ {
InitializeComponent(); InitializeComponent();
this.DataContext = this; this.DataContext = this;
// Ajout d'un gestionnaire d'erreurs média
mediaPlayer.MediaFailed += MediaFailed;
KinectManager = new KinectManager(); KinectManager = new KinectManager();
Factory = new KinectStreamsFactory(KinectManager, skeletonCanvas); Factory = new KinectStreamsFactory(KinectManager, skeletonCanvas);
CurrentKinectStream = Factory[KinectStreams.Color]; CurrentKinectStream = Factory[KinectStreams.Color];
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
CurrentKinectStream.Start(); CurrentKinectStream.Start();
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
InitializeGame();
// Abonnement à l'événement GestureRecognized
// GestureManager.GestureRecognized += OnGestureRecognized;
}
private void InitializeGame()
{
// Configuration initiale de Kinect et préparation de l'UI
KinectManager = new KinectManager();
Factory = new KinectStreamsFactory(KinectManager, skeletonCanvas);
CurrentKinectStream = Factory[KinectStreams.Color];
CurrentKinectStream.Start();
// Initialisation des gestes Kinect ici
AllGesturesFactory allGesturesFactory = new AllGesturesFactory();
GestureManager.AddGestures(allGesturesFactory);
GestureManager.StartAcquiringFrames(KinectManager);
// Préparation des timers sans les démarrer
gameTimer = new DispatcherTimer();
gameTimer.Interval = TimeSpan.FromMilliseconds(50);
gameTimer.Tick += GameTimer_Tick;
// Ne pas démarrer le timer ici avec gameTimer.Start();
circleTimer = new DispatcherTimer();
circleTimer.Interval = TimeSpan.FromMilliseconds(100);
circleTimer.Tick += CircleTimer_Tick;
// Ne pas démarrer le timer ici avec circleTimer.Start();
// D'autres configurations initiales peuvent être ajoutées ici
} }
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
private void MainWindow_Closing(object sender, CancelEventArgs e)
{ {
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText); Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
CurrentKinectStream.Stop(); CurrentKinectStream.Stop();
@ -111,5 +191,229 @@ namespace WpfApp
Debug.WriteLine(CurrentKinectStream.GetType().Name); Debug.WriteLine(CurrentKinectStream.GetType().Name);
CurrentKinectStream.Start(); CurrentKinectStream.Start();
} }
private void GameTimer_Tick(object sender, EventArgs e)
{
// C'est ici que vous pouvez appeler CreateRandomCircle à des intervalles réguliers
CreateRandomCircle();
}
private void CircleTimer_Tick(object sender, EventArgs e)
{
// Réduisez la taille du cercle pour donner l'impression qu'il se "ferme"
if (circleSize > 0)
{
circleSize -= 2; // Ajustez cette valeur pour modifier la vitesse de réduction du cercle
clapCircle.Width = circleSize;
clapCircle.Height = circleSize;
}
else
{
// Si le cercle est trop petit, réinitialisez-le pour le prochain clap
ResetClapCircle();
}
}
private void CheckClapTiming()
{
// Supposons que le timing parfait est lorsque le cercle est entre 40 et 60 de taille
if (circleSize >= 40 && circleSize <= 60)
{
UpdateGameStatus("Parfait!", 100);
}
else if (circleSize > 60 && circleSize <= 80)
{
UpdateGameStatus("Bien", 50);
}
else
{
UpdateGameStatus("Raté", 0);
}
// Réinitialisez la taille du cercle pour le prochain clap
ResetClapCircle();
}
private void GameCanvas_Loaded(object sender, RoutedEventArgs e)
{
CenterClapCircle();
}
private void CenterClapCircle()
{
if (gameCanvas.ActualWidth == 0 || gameCanvas.ActualHeight == 0) return;
double centerX = (gameCanvas.ActualWidth - clapCircle.Width) / 2;
double centerY = (gameCanvas.ActualHeight - clapCircle.Height) / 2;
Canvas.SetLeft(clapCircle, centerX);
Canvas.SetTop(clapCircle, centerY);
}
private void ResetClapCircle()
{
circleSize = 100; // Réinitialiser la taille
clapCircle.Width = circleSize;
clapCircle.Height = circleSize;
circleTimer.Start(); // Redémarrer le timer pour réduire le cercle
}
private void UpdateGameStatus(string feedback, int points)
{
Feedback = feedback; // "Parfait!", "Bien", "Raté", etc.
Score += points; // Mettre à jour le score basé sur le feedback
}
private void StartGame()
{
try
{
// Chemin relatif au répertoire de sortie de l'application
mediaPlayer.Open(new Uri("Tutti-Frutti.mp3", UriKind.Relative));
mediaPlayer.MediaOpened += MediaOpened;
}
catch (Exception ex)
{
MessageBox.Show($"Erreur lors de l'ouverture du fichier de musique : {ex.Message}");
}
// Configurez et démarrez le timer pour le cercle qui se réduit
circleTimer = new DispatcherTimer();
circleTimer.Interval = TimeSpan.FromMilliseconds(100); // Ajustez selon le rythme de la musique
circleTimer.Tick += CircleTimer_Tick;
circleTimer.Start();
}
private void MediaOpened(object sender, EventArgs e)
{
mediaPlayer.Play();
ResetClapCircle();
CenterClapCircle(); // Appelez cette méthode pour centrer le cercle
Score = 0;
Feedback = "";
if (!gameTimer.IsEnabled) gameTimer.Start();
if (!circleTimer.IsEnabled) circleTimer.Start();
}
private void StopGame()
{
mediaPlayer.Stop();
gameTimer.Stop(); // Arrêter le timer du jeu
circleTimer.Stop(); // Arrêter le timer du cercle
Feedback = "Jeu terminé"; // Mettre à jour le feedback
}
private void RemoveCircleAtPosition(double x, double y)
{
Ellipse circleToRemove = null;
double minDistance = double.MaxValue;
foreach (Ellipse circle in gameCanvas.Children.OfType<Ellipse>())
{
double centerX = Canvas.GetLeft(circle) + circle.Width / 2;
double centerY = Canvas.GetTop(circle) + circle.Height / 2;
double distance = Math.Sqrt(Math.Pow(centerX - x, 2) + Math.Pow(centerY - y, 2));
if (distance < minDistance)
{
minDistance = distance;
circleToRemove = circle;
}
}
if (circleToRemove != null && minDistance < someThreshold)
{
gameCanvas.Children.Remove(circleToRemove);
}
}
// Vous pouvez lier cette méthode à un bouton dans l'interface utilisateur pour démarrer le jeu
private void StartGameButton_Click(object sender, RoutedEventArgs e)
{
try
{
StartGame();
}
catch (Exception ex)
{
MessageBox.Show($"Une erreur s'est produite : {ex.Message}");
}
}
private void MediaFailed(object sender, ExceptionEventArgs e)
{
MessageBox.Show($"Échec de la lecture de la musique : {e.ErrorException.Message}");
}
// Optionnel: Ajouter un bouton d'arrêt dans l'interface utilisateur et lier cette méthode
private void StopGameButton_Click(object sender, RoutedEventArgs e)
{
StopGame();
}
private void CreateRandomCircle()
{
// Vérifier si nous avons déjà le nombre maximum de cercles à l'écran
if (gameCanvas.Children.OfType<Ellipse>().Count() < MaxCircles)
{
// Générer une position aléatoire dans les limites du Canvas
double x = random.NextDouble() * (gameCanvas.ActualWidth - CircleStartSize);
double y = random.NextDouble() * (gameCanvas.ActualHeight - CircleStartSize);
// Créer un nouveau cercle
Ellipse newCircle = new Ellipse
{
Width = CircleStartSize,
Height = CircleStartSize,
Fill = Brushes.Red,
Opacity = 0.5
};
// Ajouter le cercle au Canvas
gameCanvas.Children.Add(newCircle);
Canvas.SetLeft(newCircle, x);
Canvas.SetTop(newCircle, y);
// Créer et démarrer un timer pour ce cercle
DispatcherTimer circleTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100)
};
circleTimer.Tick += (sender, e) => ReduceCircleSize(sender, e, newCircle);
circleTimer.Start();
}
}
private void ReduceCircleSize(object sender, EventArgs e, Ellipse circle)
{
if (circle.Width > MinimumCircleSize && circle.Height > MinimumCircleSize)
{
circle.Width -= ReductionAmount;
circle.Height -= ReductionAmount;
}
else
{
DispatcherTimer timer = sender as DispatcherTimer;
timer.Stop(); // Arrêter le timer
gameCanvas.Children.Remove(circle); // Supprimer le cercle du Canvas
}
}
private void OnGestureRecognized(object sender, GestureRecognizedEventArgs e)
{
if (e.GestureName == "Clap Hands")
{
// Ici, vous devez récupérer les coordonnées de la main de l'utilisateur via Kinect
// double handX = ...; // Coordonnée X de la main
// double handY = ...; // Coordonnée Y de la main
// RemoveCircleAtPosition(handX, handY);
}
}
} }
} }

Binary file not shown.

@ -102,10 +102,23 @@
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\KinectUtils\KinectUtils.csproj">
<Project>{2d44487e-f514-4063-9494-2af1e8c9e9c8}</Project>
<Name>KinectUtils</Name>
</ProjectReference>
<ProjectReference Include="..\LibMyGesturesBank\MyGesturesBank.csproj">
<Project>{2496dfb1-eb55-47a1-a780-211e079b289d}</Project>
<Name>MyGesturesBank</Name>
</ProjectReference>
<ProjectReference Include="..\Lib\Lib.csproj"> <ProjectReference Include="..\Lib\Lib.csproj">
<Project>{0751c83e-7845-4e5f-a5d3-e11aba393aca}</Project> <Project>{0751c83e-7845-4e5f-a5d3-e11aba393aca}</Project>
<Name>Lib</Name> <Name>Lib</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="Tutti-Frutti.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
Loading…
Cancel
Save