[英]Hangman - GDI+ problem
我有點發脾氣。 我開始開發基於GUI的Hangman游戲,用於娛樂。 但是,我遇到了一些問題。
需要猜測的單詞已被轉換為char數組。 但是,當用戶輸入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);
}
}
}
在表單(或任何其他控件)上繪制的正確方法是通過調用使其無效
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();
}
你能不能只使用string.Contains方法來查看字母是否存在?
public bool CheckLetter(char letter)
{
return word.Contains(letter);
}
然后,您可以使用此結果來操縱您擁有的單詞。
你的CheckLetter方法是倒退的,它應該是:
GuessedLetters[i] = WordCharactes[i];
不:
WordCharactes[i] = GuessedLetters[i];
問題是您沒有在表單/控件的Paint事件中更新繪圖邏輯。 為此添加一個事件,然后在那里處理所有繪圖。 要更新,請調用表單/控件的Invalidate()函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.