我有点麻烦了。我开始开发一个基于GUI的Hangman游戏,用于娱乐。然而,我遇到了一些问题。
需要猜测的单词已转换为char数组。但是,当用户输入字符以猜测单词时,CheckLetter()方法似乎不起作用,尽管它可以正确调用。当这些字母被正确猜测时,它们不会出现在屏幕上。
如果你能指引我正确的方向,我将不胜感激。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HangmanV1._0
{
public partial class Main : Form
{
private Graphics g;
//Stores words characters
private char[] WordCharactes;
//Cloned array size of word characters, though data only appears in the elements
//when characters are matched succesfully
private char[] GuessedLetters;
public Main()
{
InitializeComponent();
}
public void GetWord(string Word, int NumberOfCharacters)
{
//Declares new char array
WordCharactes = new char[NumberOfCharacters];
GuessedLetters = new char[NumberOfCharacters];
//Converts word to char array
WordCharactes = Word.ToCharArray();
}
private void btnPlay_Click(object sender, EventArgs e)
{
//invokes the method by passing the word required to be guessed, specified by the user
GetWord(tbWordToGuess.Text, tbWordToGuess.Text.Length);
grbNewGame.Visible = false;
//Draw hangman game board
DrawWord(g);
}
public void DrawWord(Graphics e)
{
//Line Coordinates
int LinePointX = 50;
int LinePointY = 80;
int LetterPoint = 50;
for (int i = 0; i < WordCharactes.Length; i++)
{
//Draws dashed unser letters, highlights how many letters to guess
e.DrawLine(new Pen(Color.Black, 5), new PointF(LinePointX, 300), new PointF(LinePointY, 300));
//Draws letters that have been correctly guessed
e.DrawString(GuessedLetters[i].ToString(), new Font("Arial", 18), Brushes.Black, new PointF(LetterPoint, 270));
//Steadily increments line
LetterPoint += 40;
LinePointX += 40;
LinePointY += 40;
}
}
public void CheckLetter(char Letter)
{
this.Refresh(); //<-- Edit: adding this solved my problem
//Compares letters
for (int i = 0; i < WordCharactes.Length; i++)
{
if (WordCharactes[i] == Letter)
{
GuessedLetters[i] = WordCharactes[i];
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
//Exits enviroment
Environment.Exit(0);
}
private void btnInputLetter_Click(object sender, EventArgs e)
{
//Invokes the checkletter method to compare inputted char to that of the word
CheckLetter(char.Parse(tbGuessedLetter.Text));
//Redraws
DrawWord(g);
}
}
}发布于 2011-05-12 04:02:00
在窗体(或任何其他控件)上绘制的正确方法是通过调用
Invalidate();您将执行此操作,而不是在btnInputLetter_Click中使用DrawWord(g
然后,系统将调用窗体的paint事件。此事件有一个参数,该参数包含应用于绘制的Graphics对象。
所有这些将总结为以下内容:
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawWord(e.Graphics);
}
private void btnInputLetter_Click(object sender, EventArgs e)
{
//Invokes the checkletter method to compare inputted char to that of the word
CheckLetter(char.Parse(tbGuessedLetter.Text));
//Redraws
Invalidate();
}发布于 2011-05-12 03:31:09
您不能只使用string.Contains方法来查看该字母是否存在吗?
public bool CheckLetter(char letter)
{
return word.Contains(letter);
}然后,您可以使用此结果来操作您拥有的单词。
发布于 2011-05-12 03:32:32
你的CheckLetter方法是反向的,它应该是:
GuessedLetters[i] = WordCharactes[i];不是:
WordCharactes[i] = GuessedLetters[i];https://stackoverflow.com/questions/5969533
复制相似问题