mardi 10 mars 2015

Matching Game Revealing Tiles and Accidently Winning

I'm very sorry to have to ask for your help again so soon, but this is the last piece for my classwork.


Basically in this part of this quiz, I've designed it so that the player matches two picture-boxes together, whenever a player clicks a box it reveals the first image, whenever the player clicks on the second box, if the tags match, the two boxes will stay visible and the player will then proceed until all pairs have been matched.


My problems are that, upon entering the form, all of the picture boxes are visible, which simply won't do. And the second, and perhaps most frustrating of all is that, whenever I click any picturebox on ANY other picturebox, the game ends, and I get an unhandled NullReferenceException that refers to the TimerClick_Tick event.


I would sincerely appreciate it if someone could assist me, or at the very least steer me in the correct direction towards fixing my errors.


Thank you for your time.



private void pbox_Click(object sender, EventArgs e)
{
if (TimerClick.Enabled == true)
return;

PictureBox clickedPBox = sender as PictureBox;

if (clickedPBox != null)
{
if (clickedPBox.ForeColor == Color.Transparent)
return;

if (FirstClickedBox == null)
{
FirstClickedBox = clickedPBox;
FirstClickedBox.ForeColor = Color.Transparent;
return;
}

SecondClickedBox = clickedPBox;
SecondClickedBox.ForeColor = Color.Transparent;

CheckingForWinner();

if (FirstClickedBox.Tag == SecondClickedBox.Tag)
{
FirstClickedBox = null;
SecondClickedBox = null;
return;
}

TimerClick.Start();
}
}

private void TimerClick_Tick(object sender, EventArgs e)
{
FirstClickedBox.ForeColor = Color.Transparent;
SecondClickedBox.ForeColor = Color.Transparent;

FirstClickedBox = null;
SecondClickedBox = null;
}

private void CheckingForWinner()
{

foreach (Control control in tableLayoutPanel1.Controls)
{
PictureBox iconBox = control as PictureBox;

if (iconBox != null)
{
if (iconBox.ForeColor == iconBox.BackColor)
return;

}



}

HasGameBeenWon = true;
MessageBox.Show("You have successfully matched all of the above icons!", "We congratulate you!");
CurrentPlayer.CurrentPlayerScore += 10;
CurrentPlayer.CheckHardMode = true;
CurrentPlayer.HardModeHasBeenChecked = false;
Close();

}

int counter = 60;
private void frmQuizThreeHardMode_Load(object sender, EventArgs e)
{
int counter = 60;
TimerEnd = new System.Windows.Forms.Timer();
TimerEnd.Tick += new EventHandler(TimerEnd_Tick);
TimerEnd.Interval = 1000;
TimerEnd.Start();
textBox1.Text = counter.ToString() + " seconds";
}

private void TimerEnd_Tick(object sender, EventArgs e)
{
counter--;
if ((counter <= 60) && (counter != 0))
{
textBox1.Text = counter.ToString() + " seconds";

if ((counter < 1) && (HasGameBeenWon == false))
{
TimerEnd.Stop();
textBox1.Text = "Time's up!";
MessageBox.Show("You didn't finish in time. 10 points will be deducted from your total.", "You're Too Slow!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.Close();
}
}


}
}


}





Aucun commentaire:

Enregistrer un commentaire