c#帮助画字母(文字游戏)
本文关键字:文字游戏 帮助 | 更新日期: 2023-09-27 17:50:54
我正在创建一个文字游戏(行话)。它的工作原理是这样的:从数组中随机选择一个单词,用户必须猜出正确的单词。然后把单词打印出来。红色:字母错误黄色:字母正确但位置错误绿色:位置和字母正确
现在的问题是它只画出猜出来的单词的几个字母。我还需要改变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;
}
}
}
}
}
}
}
}
一些注释和示例代码。
-
你有很多重复的代码只是因为你画红色和绿色。
-
大多数在WinForms绘图中使用的类必须被处理掉。否则你会遇到内存泄漏和GDI+泄漏。
-
使用图形。MeasureString获取每个字符的大小。生成的大小可用于X和y方向的转发
-
字符串的字符可以通过索引直接访问。您不需要将它们转换为字符数组。
无效YourDrawMethod(图形g){var wrongBrush = new SolidBrush(Color.Red);var correctBrush = new SolidBrush(Color.Green);
var ff = new FontFamily("Arial");使用(var font = new System.Drawing)字体(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 ();}