i have created a game but it needs help, my code is really long (1000 lines). i asked around and people said that i could use methods but i do not know how to that, mainly becuase i do not want mess up my code but there are some errors in it and i could use some serious help.
the game is attached
update: i have made some serious modifications and cut the the code by 600 lines but i cannot find out why it is not working. I keep getting this error "identifier expected" i have no idea what the problem is.
if anyone could help i would appreciate it.
the source code is attached
Loading
FroglegPosted Dec 15, 2010, 12:07 AM
public Form1()
{
InitializeComponent();
Thread.Sleep(1);
pickRandom();// new new new
resetButtons();// new new new
hideControls();//put it here and then put a breakpoint on it and debug so you can see what happens
}
Kel MichaelPosted Dec 14, 2010, 11:25 PM
private void button25_Click(object sender, EventArgs e)
{
button21.Visible = true;
button22.Visible = true;
button23.Visible = true;
button24.Visible = true;
button26.Visible = true;
button29.Visible = true;
button30.Visible = true;
button31.Visible = true;
button32.Visible = true;
Label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
pickRandom();
}
for the start button
then i get a little confused. where do i put:
public void hideControls() // hideControls(); // put inside Form1()
{
foreach (Control myControl in this.Controls)
{
myControl.Visible = false;
}
button25.Visible = true;// make start button visible
}
do i delete private voide form1_load(object sender, eventargs e) and then put the code??
it is probably simple but the code is not working for me.
FroglegPosted Dec 14, 2010, 10:29 PM
private void button25_Click(object sender, EventArgs e)
{
button21.Visible = true;
button22.Visible = true;
button23.Visible = true;
button24.Visible = true;
button26.Visible = true;
button29.Visible = true;
button30.Visible = true;
button31.Visible = true;
button32.Visible = true;
Label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
pickRandom();
}
and after initialise form
public void hideControls() // hideControls(); // put inside Form1()
{
foreach (Control myControl in this.Controls)
{
myControl.Visible = false;
}
button25.Visible = true;// make start button visible
}
Kel MichaelPosted Dec 14, 2010, 7:04 PM
example. when the program starts nothing is visible except the start button, then when the start button is clicked everything else shows; so how would i do that.
FroglegPosted Dec 14, 2010, 4:51 PM
Also under the color column maybe using a label color for correct color may be a better idea
By the way congratulations - you new code is a lot easier to follow.
Kel MichaelPosted Dec 14, 2010, 3:24 PM
all of the code for the buttons are the same so i was wondering how i would make a method and call all the buttons instead of having code for each button. the code looks like this
private void button04_Click(object sender, EventArgs e)
{
this.ActiveControl.BackColor = controlColor;
this.ActiveControl.Text = controlNumber;
allCellsClicked[3] = '1';
if (all_Buttons_Clicked())
{
allCellsClicked[0] = '0';
allCellsClicked[1] = '0';
allCellsClicked[2] = '0';
allCellsClicked[3] = '0';
button04.Enabled = false;
button03.Enabled = false;
button02.Enabled = false;
button01.Enabled = false;
guess++;
Label1.Text = "Guess Number " + Convert.ToString(guess);
label4.Visible = true;
label5.Visible = true;
label4.Text = "0";
label5.Text = "0";
if (int.Parse(button01.Text) == fourth ) {label5.Text = Convert.ToString(int.Parse(label5.Text) + 1);}
if (int.Parse(button02.Text) == third ) {label5.Text = Convert.ToString(int.Parse(label5.Text) + 1); }
if (int.Parse(button03.Text) == second) {label5.Text = Convert.ToString(int.Parse(label5.Text) + 1);}
if (int.Parse(button04.Text) == first ) {label5.Text = Convert.ToString(int.Parse(label5.Text) + 1); }
int a = int.Parse(button01.Text), b = int.Parse(button02.Text), c = int.Parse(button03.Text), d = int.Parse(button04.Text);
if (fourth == a || third == a || second == a || first == a) {label4.Text = Convert.ToString(int.Parse(label4.Text) + 1);a = 10;}
if (fourth == b || third == b || second == b || first == b) {label4.Text = Convert.ToString(int.Parse(label4.Text) + 1); b = 10;}
if (fourth == c || third == c || second == c || first == c) {label4.Text = Convert.ToString(int.Parse(label4.Text) + 1);c = 10;}
if (fourth == d || third == d || second == d || first == d) {label4.Text = Convert.ToString(int.Parse(label4.Text) + 1); d = 10;}
label4.Text = Convert.ToString(int.Parse(label4.Text) - int.Parse(label5.Text));
}
}
Sam HobbsPosted Dec 13, 2010, 8:22 PM
Note however that "throws an error" implies execution and I assume that the error "does not contain a constructor" occurs when compiling.
FroglegPosted Dec 13, 2010, 2:50 PM
Error 1 'System.Drawing.Color' does not contain a constructor that takes '3' arguments - ,which can be overridden.
the correct way to call it is
darks[0] = Color.FromArgb(0, 0, 255);
Is this not the way you code it ?
Sam HobbsPosted Dec 13, 2010, 8:57 AM
FroglegPosted Dec 13, 2010, 4:11 AM
eg:
darks[0] = new Color(0, 0, 255); // Blue
should be:
darks[0] = Color.FromArgb(0, 0, 255);
is one of many errors
When in designer view- where the form is visible, open the properties windows Form1 and click on the lightening bolt icon at the top
This will show you a list of events that can be performed eg; MouseDown, MouseMove etc
An example of MouseMove
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//add two labels to form
label1.Text = Convert.ToString(e,X);
label2.Text = Convert.ToString(e,Y);
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left mouse clicked");
}
There is also a form paint event where you can use
private void Form1_Paint(object sender, PaintEventArgs e) //all control have events
{
e.Graphics.DrawLine(Pens.Red,20,20,100,20);
}
Use the intellisense and also snippets
}
Start a new project using the above methods, and repost for further assistance
By the way Graphics g means nothing without assigning to a bitmap or control (pictureBox, panel etc)
Sam HobbsPosted Dec 12, 2010, 12:22 AM
Are you near a library where you can borrow books? Are there books in your school library that you can borrow? Can your parents buy books for you? If any of those are possible then you will get much more help that way.
I have a nephew that I tried to teach programming when he was young. It was frustrating for me, since he did not understand the concept of methods and he would not learn. He is an adult now and has a job but he still does not do much programming. I don't know how old you are but whatever your age, you are headed in the right direction.