i wanted to implement classes on my program but im having troubles at some point
i was able to use a class to generate a random word
can anyone give me some points on how i can use more classes?
here is the code
namespace HangmanGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string word = "";
int amount = 0;
List <Label> label = new List<Label>();
GetRandomWord gw = new GetRandomWord();
Draw draw = new Draw();
enum Bodyparts
{
Head,
Left_Eye,
Right_Eye,
Mouth,
Right_Arm,
Left_Arm,
Body,
Right_Leg,
Left_Leg
}
void DrawBodyParts(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.Right_Leg)
{
g.DrawLine(p, new Point(60, 170), new Point(30, 190));
}
else if (bp == Bodyparts.Left_Leg)
{
g.DrawLine(p, new Point(60, 170), new Point(90, 190));
}
}
void MakeLabels()
{
word = gw.GetWord();
char[] chars = word.ToCharArray();
int bet = 330 / chars.Length ;
for (int i = 0; i < chars.Length; i++)
{
label.Add(new Label());
label[i].Location = new Point((i *bet)+ 10,80);
label[i].Text = "_";
label[i].Parent = groupBox2;
label[i].BringToFront();
label[i].CreateControl();
}
label1.Text = "Word Length:" + (chars.Length).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Shown(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
draw.DrawHangPost(g);
MakeLabels();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
char letter = textBox1.Text.ToLower().ToCharArray()[0];
if (!char.IsLetter(letter))
{
MessageBox.Show("You can only submit letters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
label[i].Text = letter.ToString();
}
foreach (Label l in label)
if (l.Text == "_") return;
MessageBox.Show("You have won!", "Congrats");
ResetGame();
}
else
{
MessageBox.Show("Letter not in the word", "Sorry");
label2.Text += " " + letter.ToString() + ", ";
DrawBodyParts((Bodyparts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lose!The word was: "+ word);
ResetGame();
}
}
}
void ResetGame()
{
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
gw.GetWord();
MakeLabels();
draw.DrawHangPost(g);
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 guessed is wrong!", "Sorry");
DrawBodyParts((Bodyparts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lose!The word was: " + word);
ResetGame();
}
}
}
}

New Topic/Question
Reply




MultiQuote





|