2011-07-25 10 views
0

私は、単語が与えられているゲームを作りたいと思いますが、欠落している文字が1つあり、下に示す文字から選択する必要があります。 C#の初心者であるため、私はこの作業を行うのが非常に難しいと思っています。今、私はWordFull、LetterA、LetterB、LetterC、インデックス(どこに手紙を入れる必要があるか)とCorrectLetterを持つ単語クラスを持っています。次に、この単語オブジェクトを読み込み、テキストボックスに文字を1つずつ入れ、単語の文字インデックス(h [e] llo = 1)が現在の文字のインデックスプロパティ(index = 1)と等しい場合は、空白の下線付きテキストボックス。その文字をクリックすると、その文字がCorrectLetterプロパティで正しいかどうかがチェックされます。これは私が詰まっている場所です。私は空のテキストボックスの代わりにその手紙を入れたい。しかし、私はどのようにそれを選ぶのですか?私は何か間違っていると思う。 TL; DR 私は手紙ゲームをしたいと思いますし、それを行う方法のアドバイスが必要です。 私のXAMLグリッド:背後C#WPFゲームのアドバイス

<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> 

コード:

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); 
     } 
    } 

} 

単語クラス:私が見ているものに基づい

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; } 
} 
+4

コードの記述ではなく、実際のコードサンプルを投稿すると、より良い回答を得ることができます。 –

+0

私はコードを追加しました。 –

+0

あなたの質問が何か分かりません。私はあなたが立ち往生しているが、正確にあなたをブロックしているものではありません。 –

答えて

0

、私は次の

    を行うだろう
  • 個々の文字要素の作成を移動します(アンダーラインを含む)を表示するコンポーネントを返す独自のメソッドに追加します。
  • プレイヤーが正しい文字を選ぶときに、
    • は、手紙から視覚的なコントロールを、それを削除
    • 、正しい文字要素と交換
    • 、下線の要素を見つけます。

編集 - コメント に基づいて子供のコレクションの要素になってのいくつかの方法があります。あなたは、実際の要素を知っている場合は、

letters.Children.Remove(element); 

を使用すると、指定した要素を削除することができます、またはあなたは、インデックスを知っていれば

letters.Children[index]; 

は動作します。

+0

しかしそれはどのように見えるでしょうか? wpfで子供のようなものをletters.Children(3)のように選択する方法はありますか? –