Hej,
Jeg er igang med at læse Head frist c#..
Jeg er igang med at lave en opgave som står i bogen..Men spillet vil ikke starte...?
- class Stats
- {
- public int Total = 0;
- public int Missed = 0;
- public int Correct = 0;
- public int Accuracy = 0;
-
- public void Update(bool correctKey)
- {
- Total++;
-
- if (!correctKey)
- {
- Missed++;
- }
- else
- {
- Correct++;
- }
-
- Accuracy = 100 * Correct / (Missed + Correct);
-
-
- }
- public partial class Form1 : Form
- {
- Random random = new Random();
- Stats stats = new Stats();
-
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- listBox1.Items.Add((Keys)random.Next(65, 90));
- if (listBox1.Items.Count > 7)
- {
- listBox1.Items.Clear();
- listBox1.Items.Add("Game over");
- timer1.Stop();
- }
- }
-
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- 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;
- stats.Update(true);
-
- }
- else
- {
- stats.Update(false);
- }
-
- correctLabel.Text = "Correct:" + stats.Correct;
- missedLabel.Text = "Missed:" + stats.Missed;
- totalLabel.Text = "Total:" + stats.Total;
- accuracyLabel.Text = "Accuracy:" + stats.Accuracy + "%";
- }
Koden kan downloades her:
http://headfirstlabs.com/books/hfcsharp/ch04.phpSpillet hedder Hit the Keys!(zip)
Indlæg senest redigeret d. 13.02.2012 00:38 af Bruger #16802