c#犀利游戏.超出范围错误
本文关键字:范围 错误 游戏 | 更新日期: 2023-09-27 18:16:35
我对c#有点陌生,并且正在获得错误消息。我正在尝试重新创建一个程序,该程序被发布在youtube上:https://www.youtube.com/watch?v=E8XQ9x-7yYk
我似乎遇到了一些问题。它说这是一个超出范围的误差。我一直在尝试不同的方法来解决这个问题,但我似乎什么也做不了。public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
string word = "";
List<Label> labels = new List<Label>();
int amount = 0;
enum BodyParts
{
Head,
Left_Eye,
Right_Eye,
Mouth,
Right_Arm,
Left_Arm,
Body,
Left_Leg,
Right_Leg,
}
void drawhangpost()
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.Brown, 10);
g.DrawLine(p, new Point(130, 218), new Point(130, 5));
g.DrawLine(p, new Point(135, 5), new Point(65, 5));
g.DrawLine(p, new Point(60, 0), new Point(60, 50));
//DrawBodyPart(BodyParts.Head);//just for show
//DrawBodyPart(BodyParts.Left_Eye);
//DrawBodyPart(BodyParts.Right_Eye);
//DrawBodyPart(BodyParts.Mouth);
//DrawBodyPart(BodyParts.Right_Arm);
//DrawBodyPart(BodyParts.Left_Arm);
//DrawBodyPart(BodyParts.Body);
//DrawBodyPart(BodyParts.Left_Leg);
//DrawBodyPart(BodyParts.Right_Leg);
//MessageBox.Show(GetRandomWord());
}
void DrawBodyPart(BodyParts bp)
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.Blue, 2);
if (bp == BodyParts.Head)
g.DrawEllipse(p, 40, 50, 40, 40);
else if (bp == BodyParts.Left_Eye)
{
SolidBrush s = new SolidBrush(Color.Black);
g.FillEllipse(s, 50, 60, 5, 5);
}
else if (bp == BodyParts.Right_Eye)
{
SolidBrush s = new SolidBrush(Color.Black);
g.FillEllipse(s, 63, 60, 5, 5);
}
else if (bp == BodyParts.Mouth)
{
g.DrawArc(p, 50, 60, 20, 20, 45, 90);
}
else if (bp == BodyParts.Body)
g.DrawLine(p, new Point(60, 90), new Point(60, 170));
else if (bp == BodyParts.Left_Arm)
g.DrawLine(p, new Point(60, 100), new Point(30, 85));
else if (bp == BodyParts.Right_Arm)
g.DrawLine(p, new Point(60, 100), new Point(90, 85));
else if (bp == BodyParts.Left_Leg)
g.DrawLine(p, new Point(60, 170), new Point(30, 190));
else if (bp == BodyParts.Right_Leg)
g.DrawLine(p, new Point(60, 170), new Point(90, 190));
}
void MakeLables()
{
word = GetRandomWord();
char[] chars = word.ToCharArray();
int between = 330 / chars.Length - 1;
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = groupBox2;
labels[i].BringToFront();
labels[i].CreateControl();
}
label1.Text = "Word Length: " + (chars.Length - 1).ToString();
}
string GetRandomWord()
{
WebClient wc = new WebClient();
string wordList = wc.DownloadString("https://raw.githubusercontent.com/Tom25/Hangman/master/wordlist.txt");
string[] words = wordList.Split(''n');
Random ran = new Random();
return words[ran.Next(0, words.Length - 1)];
}
private void form3_shown(object sender, EventArgs e)
{
drawhangpost();
MakeLables();
}
private void button1_Click(object sender, EventArgs e)
{
char letter = textBox1.Text.ToLower().ToCharArray()[0];
if (!char.IsLetter(letter))
{
MessageBox.Show("You can only submit letters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();//Line gives out of rage error
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("You have won", "Congrats");
ResetGame();
}
else
{
//MessageBox.Show("The letter you guessed is wrong", "Sorry");
label2.Text += " " + letter.ToString() + ",";
DrawBodyPart((BodyParts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lost, the word was " + word);
ResetGame();
}
}
}
void ResetGame()
{
amount = 0;// testing not sure
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
GetRandomWord();
MakeLables();
drawhangpost();
label2.Text = "Missed: ";
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == word)
{
MessageBox.Show("You Have won", "Congrats");
ResetGame();
}
else
{
MessageBox.Show("The word you guest was wrong", "Sorry");
DrawBodyPart((BodyParts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lost, the word was " + word);
ResetGame();
}
}
}
}
}
当你制作标签时,你错过了最后一个:
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = groupBox2;
labels[i].BringToFront();
labels[i].CreateControl();
}
当i小于1小于单词长度时循环停止。如果你的单词有6个字符长,你的标签将在索引4处停止,因为5不小于6 - 1。将其更改为<=或仅为<然后去掉-1>然后去掉-1>
当你循环遍历标签以将文本设置为该字母时,你的操作是正确的:
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();//Line gives out of rage error
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("You have won", "Congrats");
ResetGame();
}
所以,如果你的单词有6个字母长,当你猜单词的最后一个字母i将是5,你没有为它创建一个标签,它失败了
我原来的答案完全错了。
问题在这里。labels是Label类型的List。列表长度在MakeLabels()中定义,如下:
char[] chars = word.ToCharArray();
...
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
...
labels[i].Text = "_";
...
所以,列表的长度是字符。长度- 1
然后在你的button1_Click函数中,你有:
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();
...
这里,你可以选择字母。长度,大于标签列表的长度。如果你要把一个字母分配给标签[i],你应该有尽可能多的潜在字母。文本(与字母[i]的索引相同)。这意味着您应该将MakeLabels更改为:
for(int i = 0; i < chars.Length; i++)