O'Reilly Forums: Hit The Keys! - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Hit The Keys! Stats shouldn't be updated after Game Over

#1 User is offline   VaLee 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 24-September 12

Posted 24 September 2012 - 07:48 AM

Didn't find any topic where we can put suggestions for a possible 3rd edition so sorry if this is wrong place to make a new topic

As it currently stands with the code in the book for the "Hit The Keys!" game at the end of Chapter 4, the Stats class' members are updated even after the game is over. I know this is not really that important for the purpose of the exercise but i thought might be nice to fix this, I'm the type of guy with the principle of "If you do something, do it right.".
My solution was to add an if statement at the beggining of the KeyDown function that surrounds all the code inside to check if the time is still running. Not sure if it's the best way to do it but at least I hope you get the problem.
Great book by the way.
Cheers.

                private void listBox1_KeyDown(object sender, KeyEventArgs e)
		{
			if (timer1.Enabled)
			{
                                // If the user pressed a key that's in the ListBox, remove it
			        // and then make the game a little faster
				if (listBox1.Items.Contains(e.KeyCode))
				{
					listBox1.Items.Remove(e.KeyCode);
					listBox1.Refresh();
					if (timer1.Interval > 400)
						timer1.Interval -= 10;
					if (timer1.Interval > 250)
						timer1.Interval -= 7;
					if (timer1.Interval > 100)
						timer1.Interval -= 2;
					difficultyProgressBar.Value = 800 - timer1.Interval;
					// The user pressed a correct key, so update the Stats object
					// by calling its Update() method with the argument true
					stats.Update(true);
				}
				else
				{
					// The user pressed an incorrect key, so update the Stats object
					// by calling its Update() method with the argument false
					stats.Update(false);
				}
				// Update the labels on the StatusStrip
				correctLabel.Text = "Correct: " + stats.Correct;
				missedLabel.Text = "Missed: " + stats.Missed;
				totalLabel.Text = "Total: " + stats.Total;
				accuracyLabel.Text = "Accuracy: " + stats.Accuracy + "%";
			}
		}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users