C#帮助绘图信(文字游戏)

时间:2013-01-05 07:47:22

标签: c#

我正在创建一个文字游戏(术语)。这就是它的工作原理:从数组中选择一个随机单词,用户必须猜出正确的单词。然后打印出这个词。 红色:错误的信件 黄色:正确的字母,但错误的位置 绿色:位置和字母正确

现在的问题是它只画了几个猜测的字母。我还需要更改y值,以便所有内容都不在同一行。

这是我到目前为止所得到的。

    public string CorrectPlace; // Correct letter and postion
    public string WrongPlace; // Correct letter but incorrect postion
    public string NoneExist;  // Wrong
    public string inputwordstring; // user input
    public string CorrectWord; // Correct word
    public char[] CorrectWordchar;
    public char[] InputWord;
    int x;// position
    int y; //position
    public int points = 0;
    char replacemt = '#'; 
    public string[] Wordarray = null; // Declare string array
    public string getRandomWord() // generate random word
    {
        Random ran = new Random();
        return Wordarray[(ran.Next(0, Wordarray.Length - 1))];

    }

    public void Gamers() // 
    {
        Wordarray = new string[] { "radar", "andar", "axars", "rapar", "raser", "matar", "rikas", "ratas", "bakar", "bruka" }; // Dictionary
        CorrectWord = getRandomWord(); // store the random word in a string
    }
    public void CheckWords(string name, Graphics g)
    {

        if (CorrectWord == inputwordstring)
        {

            points += 100;
            MessageBox.Show("AMAZING");




        }
        for (int i = 0; i < CorrectWord.Length; i++)  
        {

            for (int b = 0; b < CorrectWord.Length; b++)
            {

                CorrectWordchar = CorrectWord.ToCharArray();
                InputWord = inputwordstring.ToCharArray();

                if (InputWord[i] == CorrectWordchar[b]) // check if the have the same index
                {
                    if (i == b)
                    {
                        CorrectPlace = CorrectWord;
                        SolidBrush s = new SolidBrush(Color.Green);
                        FontFamily ff = new FontFamily("Arial");
                        System.Drawing.Font font = new System.Drawing.Font(ff, 20);
                        g.DrawString(InputWord[i].ToString(), font, s, new PointF(x, y));
                        x += 20;
                        // draw out green letters
                        break;
                    }
                    else
                    {
                        if (InputWord[b] != CorrectWordchar[b])
                        {
                            SolidBrush s = new SolidBrush(Color.DarkOrange);
                            FontFamily ff = new FontFamily("Arial");
                            System.Drawing.Font font = new System.Drawing.Font(ff, 20);

                            g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y));
                            x += 20;
                            // Yellow letters

                            CorrectWordchar[b] = replacemt; // ersätter rätt bokstav med #
                            break;
                        }
                        else
                        {
                            b = 4;
                            SolidBrush s = new SolidBrush(Color.Red);
                            FontFamily ff = new FontFamily("Arial");
                            System.Drawing.Font font = new System.Drawing.Font(ff, 20);
                            g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y));
                            x += 20;
                            // Red letters
                            break;
                        }

                    }

                }
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

一些评论和示例代码。

  1. 您有很多重复的代码,因为您正在绘制红色和绿色。这可以概括。

  2. 必须处理WinForms绘图中使用的大多数类。否则会遇到内存泄漏和GDI +泄漏。应丢弃刷子,字体等。

  3. 使用Graphics.MeasureString获取每个字符的大小。结果大小可用于在X和Y中转发。

  4. 字符串的字符可以直接通过索引访问。您不需要将它们转换为char数组。

    void YourDrawMethod(Graphics g) {   var wrongBrush = new SolidBrush(Color.Red);   var correctBrush = new SolidBrush(Color.Green);

    var ff = new FontFamily(“Arial”);   使用(var font = new System.Drawing.Font(ff,20))   {     int x = 0;     int y = 0;

    foreach(car letter in InputWord)
    {
      SolidBrush brush = InputWord[i] == CorrectWord[b] ? correctBrush : wrongBrush;
      g.DrawString(letter.ToString(), font, brush, new PointF(x, y));         
      Size sizeOfLetter = g.MeasureString(letter.ToString(), font);
      x += sizeOfLetter.Width;
    }
    

    }

    wrongBrush.Dispose();   correctBrush.Dispose(); }