I'm in a slight pickle. I started development on a GUI based Hangman game, for recreation. However, I've run into a few issues.
The word required to be guessed has been converted to a char array. However, when the user inputs the char in order to guess the word, the CheckLetter() Methods doesn't seem to work, though it invokes correctly. As the letters do not appear drawn on screen, when they've been guessed correctly.
I would be grateful if you could guide in the right direction...
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);
}
}
}
The correct way to draw on a form (or any other control) would be to invalidate it by calling
Invalidate();
you would do this instead of the DrawWord(g) in btnInputLetter_Click
The system will then call the paint event of the form. This event has an argument that contains the Graphics object that you should use for painting.
All this would sum up to something like this:
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();
}
Can you not just use the string.Contains method to see if the letter exists?
public bool CheckLetter(char letter)
{
return word.Contains(letter);
}
You can then use the result of this to manipulate the words you have.
Your CheckLetter method is backwards, it should be:
GuessedLetters[i] = WordCharactes[i];
not:
WordCharactes[i] = GuessedLetters[i];
The problem is that you don't update your drawing logic in your form/control's Paint event. Add an event for this, then handle all your drawing there. To update, call the form/control's Invalidate() function.
精彩评论