Загрузка данных


Начало<Window x:Class="MovieQuizGame.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Кто хочет стать миллионером?" Height="450" Width="800"
        WindowStartupLocation="CenterScreen">
    <Grid Background="#0D1B2A">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Text="КИНО-ВИКТОРИНА" FontSize="48" FontWeight="Bold" 
                       Foreground="#E0E1DD" Margin="0,0,0,30" TextAlignment="Center"/>
            <Button x:Name="PlayButton" Content="ИГРАТЬ" Width="200" Height="60"
                    FontSize="24" FontWeight="Bold" Background="#1B263B"
                    Foreground="White" BorderBrush="#415A77" BorderThickness="2"
                    Click="PlayButton_Click" Cursor="Hand">
                <Button.Resources>
                    <Style TargetType="Border">
                        <Setter Property="CornerRadius" Value="10"/>
                    </Style>
                </Button.Resources>
            </Button>
        </StackPanel>
    </Grid>
</Window>


Начальный код
using System.Windows;

namespace MovieQuizGame
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            GameWindow gameWindow = new GameWindow();
            gameWindow.Show();
            this.Close();
        }
    }
}



<Window x:Class="MovieQuizGame.GameWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Игра" Height="700" Width="1000"
        WindowStartupLocation="CenterScreen">
    <Grid Background="#0D1B2A">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Заголовок -->
        <TextBlock x:Name="QuestionText" Grid.Row="0" FontSize="28" 
                   Foreground="#E0E1DD" Margin="20" TextWrapping="Wrap" 
                   HorizontalAlignment="Center" Text="Вопрос"/>

        <!-- Картинка вопроса -->
        <Border Grid.Row="1" Margin="20" BorderBrush="#415A77" BorderThickness="2" CornerRadius="10">
            <Image x:Name="QuestionImage" Stretch="Uniform" />
        </Border>

        <!-- Варианты ответов -->
        <Grid Grid.Row="2" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Button x:Name="Option1" Grid.Row="0" Grid.Column="0" Margin="10" 
                    FontSize="20" Background="#1B263B" Foreground="White"
                    BorderBrush="#415A77" BorderThickness="2" Click="Option_Click" Tag="0">
                <Button.Resources><Style TargetType="Border"><Setter Property="CornerRadius" Value="8"/></Style></Button.Resources>
            </Button>
            <Button x:Name="Option2" Grid.Row="0" Grid.Column="1" Margin="10" 
                    FontSize="20" Background="#1B263B" Foreground="White"
                    BorderBrush="#415A77" BorderThickness="2" Click="Option_Click" Tag="1">
                <Button.Resources><Style TargetType="Border"><Setter Property="CornerRadius" Value="8"/></Style></Button.Resources>
            </Button>
            <Button x:Name="Option3" Grid.Row="1" Grid.Column="0" Margin="10" 
                    FontSize="20" Background="#1B263B" Foreground="White"
                    BorderBrush="#415A77" BorderThickness="2" Click="Option_Click" Tag="2">
                <Button.Resources><Style TargetType="Border"><Setter Property="CornerRadius" Value="8"/></Style></Button.Resources>
            </Button>
            <Button x:Name="Option4" Grid.Row="1" Grid.Column="1" Margin="10" 
                    FontSize="20" Background="#1B263B" Foreground="White"
                    BorderBrush="#415A77" BorderThickness="2" Click="Option_Click" Tag="3">
                <Button.Resources><Style TargetType="Border"><Setter Property="CornerRadius" Value="8"/></Style></Button.Resources>
            </Button>
        </Grid>

        <!-- Подсказки -->
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20">
            <Button x:Name="FiftyFiftyButton" Content="50:50" Width="120" Height="40" Margin="10"
                    Background="#1B263B" Foreground="White" Click="FiftyFiftyButton_Click"/>
            <Button x:Name="PhoneFriendButton" Content="Звонок другу" Width="120" Height="40" Margin="10"
                    Background="#1B263B" Foreground="White" Click="PhoneFriendButton_Click"/>
            <Button x:Name="AskAudienceButton" Content="Помощь зала" Width="120" Height="40" Margin="10"
                    Background="#1B263B" Foreground="White" Click="AskAudienceButton_Click"/>
        </StackPanel>
    </Grid>
</Window>



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MovieQuizGame
{
    public partial class GameWindow : Window
    {
        private List<Question> questions;
        private int currentQuestionIndex = 0;
        private int score = 0;
        private bool isAnswered = false;

        public GameWindow()
        {
            InitializeComponent();
            LoadQuestions();
            ShowQuestion(currentQuestionIndex);
        }

        private void LoadQuestions()
        {
            string jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Questions.json");
            if (File.Exists(jsonPath))
            {
                string json = File.ReadAllText(jsonPath);
                questions = JsonSerializer.Deserialize<List<Question>>(json);
            }
            else
            {
                MessageBox.Show("Файл Questions.json не найден.");
                this.Close();
            }
        }

        private void ShowQuestion(int index)
        {
            if (index >= questions.Count)
            {
                EndGame();
                return;
            }

            var q = questions[index];
            QuestionText.Text = q.Text;

            try
            {
                string imgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, q.ImagePath);
                QuestionImage.Source = new BitmapImage(new Uri(imgPath));
            }
            catch
            {
                QuestionImage.Source = null;
            }

            Option1.Content = q.Options[0];
            Option2.Content = q.Options[1];
            Option3.Content = q.Options[2];
            Option4.Content = q.Options[3];

            // сброс цвета кнопок
            foreach (var btn in new[] { Option1, Option2, Option3, Option4 })
            {
                btn.IsEnabled = true;
                btn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#1B263B"));
            }

            isAnswered = false;
        }

        private void Option_Click(object sender, RoutedEventArgs e)
        {
            if (isAnswered) return;
            isAnswered = true;

            var button = sender as System.Windows.Controls.Button;
            int selectedIndex = int.Parse(button.Tag.ToString());
            var q = questions[currentQuestionIndex];

            if (selectedIndex == q.CorrectAnswerIndex)
            {
                button.Background = Brushes.Green;
                score++;
            }
            else
            {
                button.Background = Brushes.Red;
                // показать правильный ответ
                var correctButton = new[] { Option1, Option2, Option3, Option4 }[q.CorrectAnswerIndex];
                correctButton.Background = Brushes.Green;
            }

            // задержка перед следующим вопросом
            var timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1.5);
            timer.Tick += (s, args) =>
            {
                timer.Stop();
                currentQuestionIndex++;
                ShowQuestion(currentQuestionIndex);
            };
            timer.Start();
        }

        private void FiftyFiftyButton_Click(object sender, RoutedEventArgs e)
        {
            if (isAnswered) return;
            FiftyFiftyButton.IsEnabled = false;

            var q = questions[currentQuestionIndex];
            var wrongIndices = Enumerable.Range(0, 4).Where(i => i != q.CorrectAnswerIndex).ToList();
            var toRemove = wrongIndices.OrderBy(x => Guid.NewGuid()).Take(2).ToList();

            foreach (int i in toRemove)
            {
                var btn = new[] { Option1, Option2, Option3, Option4 }[i];
                btn.IsEnabled = false;
            }
        }

        private void PhoneFriendButton_Click(object sender, RoutedEventArgs e)
        {
            if (isAnswered) return;
            PhoneFriendButton.IsEnabled = false;

            var q = questions[currentQuestionIndex];
            MessageBox.Show($"Друг считает, что правильный ответ: {q.Options[q.CorrectAnswerIndex]}", "Звонок другу");
        }

        private void AskAudienceButton_Click(object sender, RoutedEventArgs e)
        {
            if (isAnswered) return;
            AskAudienceButton.IsEnabled = false;

            var q = questions[currentQuestionIndex];
            MessageBox.Show($"Зал голосует за ответ: {q.Options[q.CorrectAnswerIndex]}", "Помощь зала");
        }

        private void EndGame()
        {
            EndWindow endWindow = new EndWindow(score, questions.Count);
            endWindow.Show();
            this.Close();
        }
    }
}



<Window x:Class="MovieQuizGame.EndWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Игра окончена" Height="400" Width="600"
        WindowStartupLocation="CenterScreen">
    <Grid Background="#0D1B2A">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock x:Name="ResultText" FontSize="36" FontWeight="Bold" 
                       Foreground="#E0E1DD" Margin="0,0,0,30" TextAlignment="Center"/>
            <Button x:Name="BackToMainButton" Content="ВЕРНУТЬСЯ НА ГЛАВНЫЙ ЭКРАН" 
                    Width="300" Height="60" FontSize="20" FontWeight="Bold"
                    Background="#1B263B" Foreground="White" BorderBrush="#415A77"
                    BorderThickness="2" Click="BackToMainButton_Click" Cursor="Hand">
                <Button.Resources>
                    <Style TargetType="Border">
                        <Setter Property="CornerRadius" Value="10"/>
                    </Style>
                </Button.Resources>
            </Button>
        </StackPanel>
    </Grid>
</Window>



using System.Windows;

namespace MovieQuizGame
{
    public partial class EndWindow : Window
    {
        public EndWindow(int score, int total)
        {
            InitializeComponent();
            ResultText.Text = $"Ваш счёт: {score} из {total}";
        }

        private void BackToMainButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            this.Close();
        }
    }
}


<Application x:Class="MovieQuizGame.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>

using System.Collections.Generic;

namespace MovieQuizGame
{
    public class Question
    {
        public string Text { get; set; }
        public string ImagePath { get; set; }
        public List<string> Options { get; set; }
        public int CorrectAnswerIndex { get; set; }
    }
}