2014-07-09 4 views
-1

방금 ​​문자 게임을 만들었습니다. 타이머가 켜져 있으면 목록 상자에 글자가 인쇄됩니다. 사용자는 올바른 키를 눌러야합니다. 그가하는 경우, 정확한 프레스의 mumber와 레이블이 업데이 트됩니다. 그렇지 않은 경우, 누락 된 레이블이 갱신됩니다. 두 경우 모두 Total 및 정확도 레이블이 업데이트됩니다. 문제는 게임이 종료 된 후에도 계속 카운트된다는 것입니다. 내가하고 싶은 건 keyDown 이벤트가 폼에서 일어나지 못하게하는 것입니다. 이 코드를 작성했지만 작동하지 않습니다.timer.stop() 후 양식의 KeyDown 비활성화

누구에게 해결책이 있습니까?

이것은 내 코드입니다. 플래그를 추가하더라도 게임이 끝난 후에도 양식이 keyDown 이벤트를 사용하도록 설정되어 있습니다. 게임이 끝난 후이 발생하는 것을 막기 위해 keyDown 이벤트를 비활성화 할 수 있는지 알고 싶습니다.

스페이스 TypingGame { 공용 부분 Form1 클래스 : 폼 { 저그 = 새로운 랜덤(); 통계 통계 = 새 통계();

public Form1() 
    { 
     InitializeComponent(); 
    } 

    //Here the game starts 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     listBox1.Items.Add((Keys) random.Next(65,90)); 
     //If letters missed are more than 7 the game is over 
     //so I need a way to disable the KeyDown event from happening after the game is over 
     if (listBox1.Items.Count > 7) 
     { 
      listBox1.Items.Clear(); 
      listBox1.Items.Add("Game Over"); 
      timer1.Stop(); 
     } 
    } 
    //Here is what happens on KeyDown. 
    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 + "%"; 

    } 
} 

}

+0

는 플래그를 넣어 무엇을 의미 – Prisoner

+0

이상? 게임은 잘 작동하고 게임 오버를 라벨에 인쇄합니다. 나는 깃발과 게임이 끝난 후 사용자가 KeyDown 이벤트를 트리거 할 수 없기를 바란다. – Andreas777

답변

1

나는 코드의 코드베이스를 업데이트했습니다. 대한이 게임을 나타 내기 위해 플래그를 넣어하는 방법

MSDN Reference - Timer

//Here the game starts (?? game stop?) 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    listBox1.Items.Add((Keys) random.Next(65,90)); 
    //If letters missed are more than 7 the game is over 
    //so I need a way to disable the KeyDown event from happening after the game is over 
    if (listBox1.Items.Count > 7) 
    { 
     listBox1.Items.Clear(); 
     listBox1.Items.Add("Game Over"); 
     // timer1.Stop(); 
     // disable the timer to stop 
     timer1.Enabled = false; 
    } 
} 
//Here is what happens on KeyDown. 
private void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
    // if timer disabled, do nothing 
    if (!timer1.Enabled) return; 

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

} 
+0

Alex는 당신의 도움에 많은 감사를 드린다. 위의 코드를 추가했습니다. – Andreas777