2013-03-10 52 views
0

所以這裏是一本來自我一直在研究的書的遊戲。計時器滴答事件,是否會被其他函數中斷?

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 + "%"; 
} 

它生成隨機字母,如果按正確的字母,它具有去除按信件,從列表框中。

問題是Form的keydown屬性不起作用,它只會突出顯示按下的按鍵,但它不會將其從列表框中移除,循環將繼續,直到列表框已滿,然後它將遊戲結束。 ..

我想知道我這次做錯了什麼,因爲整個代碼都是從本書中取得的?

+1

您是否將方法添加爲窗體上相應事件的事件處理程序?另外,我認爲它應該是previewKeyDown,而不是表單有焦點。 – Servy 2013-03-10 22:44:18

+0

是的,我沒有添加該方法,只是試圖previewKeyDown,它也沒有工作。 – imdrunkisuppose 2013-03-10 22:48:39

+0

您是否嘗試過單獨的疑難解答?我的意思是使用'Form1_KeyDown'事件,並使用'MessageBox.Show(「HERE」);'或類似的東西來確保事件正在發射? – tmwoods 2013-03-10 22:53:04

回答

1

我檢查了我的兩個代碼和書籍,問題是他們的表單的KeyPreview被設置爲True,而我沒有,所以這是他們忘了書上提到不幸的答案。

相關問題