简体   繁体   English

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

[英]C# Help drawing letter (Word Game)

I'm creating a word game(lingo).This is how it works: A random word is selected from an array and the user has to guess the correct word. 我正在创建一个文字游戏(术语)。这就是它的工作原理:从数组中选择一个随机单词,用户必须猜出正确的单词。 Then the word is printed out. 然后打印出这个词。 Red : wrong letter Yellow : correct letter but wrong position Green :position and letter correct 红色 :错误的字母黄色 :正确的字母但错误的位置绿色 :位置和字母正确

Right now the problem is that it only draws a few letter of the guessed word. 现在的问题是,它只绘制了猜测字的几个字母。 I also need to change the y value so that everything is not on the same line. 我还需要更改y值,以便所有内容都不在同一行。

This is what i got so far. 这是我到目前为止所得到的。

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

                    }

                }
            }
        }
    }
}

} }

Some comments and sample code. 一些评论和示例代码。

  1. You have lots of duplicated code just because you are drawing red and green. 您有很多重复的代码,因为您正在绘制红色和绿色。 This can be summarized. 这可以概括。

  2. Most of the classes used in WinForms drawing have to be disposed. WinForms绘图中使用的大多数类都必须处理掉。 Otherwise you run into memory leaks and GDI+ leaks. 否则会遇到内存泄漏和GDI +泄漏。 Brushes, Fonts and others should be disposed. 应该处理刷子,字体和其他。

  3. Use Graphics.MeasureString to get the size of each character. 使用Graphics.MeasureString获取每个字符的大小。 The resulting size can be used to forward in X and Y. 结果大小可用于在X和Y中前进。

  4. The chars of a string can be accessed by an index directly. 可以通过索引直接访问字符串的字符。 You don't need to convert them into char arrays. 您不需要将它们转换为char数组。

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

    var ff = new FontFamily("Arial"); var ff = new FontFamily(“Arial”); using(var font = new System.Drawing.Font(ff, 20)) { int x = 0; using(var font = new System.Drawing.Font(ff,20)){int x = 0; int y = 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(); wrongBrush.Dispose(); correctBrush.Dispose(); correctBrush.Dispose(); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM