C#帮助绘制字母(文字游戏)

C#帮助绘制字母(文字游戏),c#,C#,我正在创建一个文字游戏(行话)。它就是这样工作的:从数组中随机选择一个单词,用户必须猜出正确的单词。然后把这个字打印出来。 红色:字母错误 黄色:字母正确但位置错误 绿色:位置和字母正确 现在的问题是,它只画出猜测单词的几个字母。我还需要更改y值,使所有内容不在同一条线上 这就是我目前得到的 public string CorrectPlace; // Correct letter and postion public string WrongPlace; // Correct

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

现在的问题是,它只画出猜测单词的几个字母。我还需要更改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;
                        }

                    }

                }
            }
        }
    }
}
公共字符串更正位置;//正信
公共字符串位置错误;//正确的字母但不正确的位置
公共字符串不存在;//错
公共字符串inputwordstring;//用户输入
公共字符串更正字;//正确的词
公共字符[]修正字字符;
公共字符[]输入字;
int x;//位置
int-y//位置
公共积分=0;
char replacemt='#';
公共字符串[]Wordarray=null;//声明字符串数组
公共字符串getRandomWord()//生成随机字
{
Random ran=新的Random();
返回Wordarray[(ran.Next(0,Wordarray.Length-1));
}
public void Gamers()//
{
Wordarray=新字符串[]{“雷达”、“安达”、“阿克萨”、“拉帕”、“拉塞尔”、“马特尔”、“里卡斯”、“拉塔”、“巴卡尔”、“布鲁卡”};//字典
CorrectWord=getRandomWord();//将随机字存储在字符串中
}
公共无效校验字(字符串名称,图形g)
{
if(CorrectWord==inputwordstring)
{
点数+=100;
MessageBox.Show(“惊人”);
}
for(int i=0;i

}一些注释和示例代码

  • 你有很多重复的代码,只是因为你画的是红色和绿色。这可以概括起来

  • WinForms绘图中使用的大多数类都必须处理。否则您会遇到内存泄漏和GDI+泄漏。刷子、字体和其他应该被处理掉

  • 用于获取每个字符的大小。生成的大小可用于在X和Y方向上向前移动

  • 字符串的字符可以由索引直接访问。您不需要将它们转换为字符数组

    绘制方法(图g) { var ErrorBrush=新的SolidBrush(颜色为红色); var correctBrush=新的SolidBrush(颜色为绿色)

    var ff=新字体系列(“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;
    }
    
    }

    Dispose(); Dispose(); }


  • 谢谢你的意见。我知道代码可能写得不好,但您认为您可以通过聊天帮助我吗(我有很多问题=)。我可以上传游戏,如果可以的话,你可以试试。只要把代码放在网上的某个地方,让我知道何时何地聊天。我在维也纳时区,整个星期天下午都有空。