我正在制作刽子手游戏,游戏可玩,但当我猜到了所有的游戏 字母没有获胜的消息框,也没有重置游戏。 看下面的代码。谢谢你帮助我Godbless!
名称空间Hangman
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string wurds = "";
List<Label> labels = new List<Label>();
int tries = 0;
void DrawingHangman()
{
Graphics g = pnl1.CreateGraphics();
Pen p = new Pen(Color.Black, 10);
pbHead.Hide();
pbBody.Hide();
pbLegs.Hide();
pbArms.Hide();
}
private void Form1_Shown(object sender, EventArgs e)
{
DrawingHangman();
MakeLabels();
}
enum BodyParts
{
head,
body,
legs,
arms
}
void DrawBodyParts(BodyParts bp)
{
Graphics g = pnl1.CreateGraphics();
if (bp == BodyParts.head)
{
pbHead.Show();
}
else if (bp == BodyParts.body)
{
pbBody.Show();
}
else if (bp == BodyParts.legs)
{
pbLegs.Show();
}
else if (bp == BodyParts.arms)
{
pbArms.Show();
}
}
void MakeLabels()
{
wurds = GetRandomWords();
Char[] chars = wurds.ToCharArray();
int between = 330 / chars.Length - 1;
for (int i = 0; i < chars.Length; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = gbWords;
labels[i].BringToFront();
labels[i].CreateControl();
}
label1.Text = ("Word Length: " + chars.Length).ToString();
}
string GetRandomWords()
{
string[] wrds = new string[3];
wrds[0] = "hello";
wrds[1] = "world";
Random ran = new Random();
return wrds[ran.Next(0, wrds.Length - 1)];
}
void btnLetter_Click(object sender, EventArgs e)
{
char letter = textBox1.Text.ToLower().ToCharArray()[0];
if (!char.IsLetter(letter))
{
MessageBox.Show("You didn't put a letter. Please input a single letter", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//What do you want it to do that it isn't already when someone wins?
if (wurds.Contains(letter))
{
char[] letters = wurds.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();
}
foreach (Label l in labels)
{
if (l.Text == "_") return;
MessageBox.Show("You've guess one letter!", "Correct");
return;
}
ResetGame();
}
else
{
MessageBox.Show("Wrong Letter!", "Careful");
label2.Text += " " + letter.ToString() + ",";
DrawBodyParts((BodyParts)tries);
tries++;
if (tries == 5)
{
MessageBox.Show("You've been abducted by the gay alien! The answer is : " + wurds, "You Lose", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
ResetGame();
}
}
}
void ResetGame()
{
GetRandomWords();
MakeLabels();
label2.Text = "Missed: ";
textBox1.Text = "";
DrawingHangman();
tries = 0;
}
}
}
答案 0 :(得分:1)
看起来你没有重置尝试...所以你可能最终得到一个没有的值(BodyParts)...
添加到您的ResetGame方法:
tries = 0;
---问题的第2部分:---
(直到我看到整个表格的代码,我不确定,但这至少会让你接近)
要处理游戏获胜方案,请尝试以下方法:
//guessing right letter code here
if (wurds.Contains(letter))
{
char[] letters = wurds.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();
}
// determine how many letters have not been guessed yet
int lettersNotGuessed = 0;
foreach (Label l in labels)
{
if (l.Text == "_") lettersNotGuessed++;
}
// Perform correct action: still more to guess or they won
if (lettersNotGuessed == 0) {
MessageBox.Show("You Won!", "Correct");
ResetGame();
return;
}
else {
MessageBox.Show("You've guessed one letter!", "Correct");
}
return;
}
当你处于这种状态时,处理可能出现意外错误的情况很有用(稍后您将了解有关错误处理的更多信息),但现在这将有所帮助:
void DrawBodyParts(BodyParts bp)
{
Graphics g = pnl1.CreateGraphics();
if (bp == BodyParts.head)
{
pbHead.Show();
}
else if (bp == BodyParts.body)
{
pbBody.Show();
}
else if (bp == BodyParts.legs)
{
pbLegs.Show();
}
else if (bp == BodyParts.arms)
{
pbArms.Show();
}
// something went wrong, let me know about it
else {
MessageBox.Show("Error: I can't determine the proper body part to draw.");
}
}