2011-05-11 52 views
1

我在一个小小的泡菜。我开始基于GUI的Hangman游戏开发,以进行娱乐。但是,我遇到了一些问题。Hang子手 - GDI +问题

需要猜测的单词已转换为char数组。但是,当用户输入字符以猜测单词时,CheckLetter()方法似乎不起作用,尽管它调用正确。由于这些字母不会在屏幕上显示,因此当他们被正确猜测时。

我将不胜感激,如果你能在正确的方向引导...

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace HangmanV1._0 
{ 
    public partial class Main : Form 
    { 
     private Graphics g; 

     //Stores words characters 
     private char[] WordCharactes; 

     //Cloned array size of word characters, though data only appears in the elements 
     //when characters are matched succesfully 
     private char[] GuessedLetters; 

     public Main() 
     { 
      InitializeComponent(); 
     } 

     public void GetWord(string Word, int NumberOfCharacters) 
     { 
      //Declares new char array 
      WordCharactes = new char[NumberOfCharacters]; 
      GuessedLetters = new char[NumberOfCharacters]; 

      //Converts word to char array 
      WordCharactes = Word.ToCharArray(); 
     } 

     private void btnPlay_Click(object sender, EventArgs e) 
     { 
      //invokes the method by passing the word required to be guessed, specified by the user 
      GetWord(tbWordToGuess.Text, tbWordToGuess.Text.Length); 
      grbNewGame.Visible = false; 

      //Draw hangman game board 
      DrawWord(g); 
     } 

     public void DrawWord(Graphics e) 
     { 
      //Line Coordinates 
      int LinePointX = 50; 
      int LinePointY = 80; 
      int LetterPoint = 50; 

      for (int i = 0; i < WordCharactes.Length; i++) 
      { 
       //Draws dashed unser letters, highlights how many letters to guess 
       e.DrawLine(new Pen(Color.Black, 5), new PointF(LinePointX, 300), new PointF(LinePointY, 300)); 


       //Draws letters that have been correctly guessed 
       e.DrawString(GuessedLetters[i].ToString(), new Font("Arial", 18), Brushes.Black, new PointF(LetterPoint, 270)); 

       //Steadily increments line 
       LetterPoint += 40; 
       LinePointX += 40; 
       LinePointY += 40; 
      } 
     } 

     public void CheckLetter(char Letter) 
     { 
      this.Refresh(); //<-- Edit: adding this solved my problem 

      //Compares letters 
      for (int i = 0; i < WordCharactes.Length; i++) 
      { 
       if (WordCharactes[i] == Letter) 
       { 
        GuessedLetters[i] = WordCharactes[i]; 
       } 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      g = this.CreateGraphics(); 
     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      //Exits enviroment 
      Environment.Exit(0); 
     } 

     private void btnInputLetter_Click(object sender, EventArgs e) 
     { 
      //Invokes the checkletter method to compare inputted char to that of the word 
      CheckLetter(char.Parse(tbGuessedLetter.Text)); 

      //Redraws 
      DrawWord(g); 
     } 
    } 
} 

回答

2

绘制表格(或任何其他控件)上的正确的方法是通过以使它无效调用

Invalidate(); 

你会在btnInputLetter_Click

012做到这一点,而不是DrawWord(G)

然后系统将调用窗体的绘画事件。该事件有一个参数,其中包含您应该用于绘制的Graphics对象。

所有这些都总结到这样的事情:

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    DrawWord(e.Graphics); 
} 

private void btnInputLetter_Click(object sender, EventArgs e) 
{ 
    //Invokes the checkletter method to compare inputted char to that of the word 
    CheckLetter(char.Parse(tbGuessedLetter.Text)); 

    //Redraws 
    Invalidate(); 
} 
+0

这会在DrawWord()方法内引起问题(空引用异常),因为WordCharacters []对象引用没有实例化,直到用户输入一个单词为止。 – Hmm 2011-05-11 20:18:04

+0

这应该可以在for循环中使用“if(WordCharacters!= null)”来轻松解决。 – 2011-05-11 20:30:56

1

难道你们就不能仅仅使用string.Contains方法,看看是否存在信?

public bool CheckLetter(char letter) 
{ 
    return word.Contains(letter); 
} 

然后,您可以使用此结果操纵您拥有的单词。

1

你CheckLetter方法是倒退,它应该是:

GuessedLetters[i] = WordCharactes[i]; 

不是:

WordCharactes[i] = GuessedLetters[i]; 
+0

谢谢,虽然问题仍然无法在屏幕上绘制匹配的字符... – Hmm 2011-05-11 19:38:57

+0

您是否调试过CheckLetter和DrawWord方法?猜测的内容是你期望的内容吗? – 2011-05-11 20:03:16

+0

是的,谢谢你设法解决问题的信息。 – Hmm 2011-05-11 21:44:57

0

的问题是,你不要在你的窗体/控件的Paint事件更新您的绘图逻辑。为此添加一个事件,然后处理您的所有绘图。要更新,请调用窗体/控件的Invalidate()函数。