开发者

C# WPF Game advice

I want to create a game, where the word is given, but there's one letter missing and you need to choose from one of the letters given below. Being a beginner with C#, I find very difficult to make this work. Right now, I have a word class, which has WordFull, LetterA, LetterB, LetterC, index (where I need to put the letter in) and a CorrectLetter. Then, I load this word object, where I put letter one by one in textboxes and if letter's index in the word (h[开发者_C百科e]llo = 1) is equal to the current letter's index property (index = 1), then it displays blank underlined textbox. When you click on that letter, then it checks whether that letter is correct with CorrectLetter property and that's the place where I'm stuck. I want to put that letter in place of empty textbox. But how do I choose it? I think I'm doing something wrong. TL;DR I want to make a letter game and I need an advice how to do it. My XAML grid:

<TabItem Name="zaisti" Header="Vykdyti" IsSelected="True">
    <Grid Name="Grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="7*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>

        <Viewbox Grid.Row="0"  Grid.Column="0">
            <StackPanel Name="letters" Orientation="Horizontal">

            </StackPanel>
        </Viewbox>

        <Image Grid.Row="0" Grid.Column="1" Name="img" Margin="10" Source="pack://siteoforigin:,,,/pic.jpg"/>

        <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Button.Click="Grid_Click">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Margin="10">
                <Button.Content>
                    <Viewbox>
                        <Label Name="Option1" Content="{Binding LetterA}"></Label>
                    </Viewbox>
                </Button.Content>
            </Button>
            <Button Grid.Column="1" Margin="10">
                <Button.Content>
                    <Viewbox>
                        <Label Name="Option2" Content="{Binding LetterB}"></Label>
                    </Viewbox>
                </Button.Content>
            </Button>
            <Button Grid.Column="2" Margin="10">
                <Button.Content>
                    <Viewbox>
                        <Label Name="Option3" Content="{Binding LetterC}"></Label>
                    </Viewbox>
                </Button.Content>
            </Button>
        </Grid>

Code behind:

public partial class MainWindow : Window
{
    List<Word> Words = new List<Word>()
    {
        ... data ...
    };
    int index = 0;

    public MainWindow()
    {
        InitializeComponent();
        pradzia.IsSelected = true;
        zaisti.IsEnabled = false;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        zaisti.IsSelected = true;
        zaisti.IsEnabled = true;
        letters.Children.Clear();
        LoadWord(index);
        this.DataContext = Words[index];
    }

    private void Grid_Click(object sender, RoutedEventArgs e)
    {
        if (index == Words.Count() - 1) return;
        MessageBox.Show((((e.Source as Button).Content as Viewbox).Child as Label).Content.ToString());
        if ((((e.Source as Button).Content as Viewbox).Child as Label).Content.ToString() == Words[index].LetterCorrect)
        {
            letters.Children.Clear();
            LoadWord(++index);
            this.DataContext = Words[index];
        }
    }

    private void LoadWord(int i)
    {
        int a = 0;
        foreach (char l in Words[i].WordFull)
        {
            TextBlock letter = new TextBlock();
            letter.Foreground = new SolidColorBrush(Colors.Gray);
            letter.Text = l.ToString();
            letter.Margin = new Thickness(2);

            if (Words[i].index == a)
            {
                letter.Text = ((char)160).ToString() + ((char)160).ToString();

                // Create an underline text decoration. Default is underline.
                TextDecoration myUnderline = new TextDecoration();

                // Create a solid color brush pen for the text decoration.
                myUnderline.Pen = new Pen(Brushes.Red, 1);
                myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

                // Set the underline decoration to a TextDecorationCollection and add it to the text block.
                TextDecorationCollection myCollection = new TextDecorationCollection();
                myCollection.Add(myUnderline);
                letter.TextDecorations = myCollection;
            }
            a++;

            letters.Children.Add(letter);
        }
    }

}

Word class:

class Word
{
    public string WordFull { get; set; }
    public string LetterA { get; set; }
    public string LetterB { get; set; }
    public string LetterC { get; set; }
    public string LetterCorrect { get; set; }
    public int index { get; set; }
}


Based on what I'm seeing, I would do the following

  • move the creation of the individual letter elements (including the underline) into their own methods that return the component to display.
  • Then when the player picks the correct letter,
    • find the underline element,
    • remove it from the letters visual control,
    • and replace it with the the correct letter element.

edit - based on comment There are several ways of getting to the elements in the Children collection. If you know the actual element,

letters.Children.Remove(element);

will allow you to remove the specified element, or

letters.Children[index];

will work if you know the index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜