C#Timer只在第一个“if”开始

时间:2012-10-30 11:04:38

标签: c# if-statement timer

我正在制作游戏并想要以各种分数显示文本。 但目前计时器只在第一个工作时(文本只显示)(如果得分等于100) 我需要一些帮助,因为它也需要使用200,300,400,...... -1000。

以下是代码:

void startTimer()
{  
    if (score == 100)
    {   
        timerWIN.Start();  
    } 
    else if (score == 200)   
    {    
        timerWIN.Start();  
    }  
    else if (score == 300)  
    {  
        timerWIN.Start();  
    }  
    else if (score == 400)  
    {    
        timerWIN.Start();  
    }  
    else if (score == 500)  
    {    
        timerWIN.Start();  
    }  
    else if (score == 600)  
    {    
        timerWIN.Start();  
    }  
    else if (score == 700)  
    {    
        timerWIN.Start();  
    }  
    else if (score == 900)  
    {    
        timerWIN.Start();  
    }  
    else if (score == 1000)  
    {    
        timer1000.Start();  
    } 
}


private void timerWIN_Tick_1(object sender, EventArgs e) 
{   
    if (timerTick == 1)   
    {
        lblWin1.Visible = true;
        lblWin2.Visible = true;   
    }   
    else if (timerTick == 15)   
    {
        lblWin1.Visible = false;
        lblWin2.Visible = false;
        timerWIN.Stop();   
    }

    timerTick++; 
}

private void timer1000_Tick(object sender, EventArgs e) 
{   
    if (timerTick == 1)   
    {
        lblWin1.Text = "500 points!";
        lblWin2.Text = "You're doing great.";
        lblWin1.Visible = true;
        lblWin2.Visible = true;   
    }   
    else if (timerTick == 15)   
    {
        lblWin1.Visible = false;
        lblWin2.Visible = false;
        lblWin1.Text = "Yeah that's it.";
        lblWin2.Text = "Keep feeding me baby.";
        timer1000.Stop();   
    }

     timerTick++; 
}

根据要求,这是我给出得分的方式:(每当我碰撞,我得分)

    private void timer1_Tick(object sender, EventArgs e)
{
  snakeScoreLabel.Text = Convert.ToString(score);

  if (down) { snake.moveDown(); }
  if (up) {snake.moveUp(); }
  if (right) {snake.moveRight(); }
  if (left) {snake.moveLeft(); }

  for (int i = 0; i < snake.SnakeRec.Length; i++)
  {
    if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
    {
      score += points;
      snake.growSnake();
      food.foodLocation(randFood);
      startTimer();
    }

  }


  collission();

  this.Invalidate();

}

3 个答案:

答案 0 :(得分:1)

确保根据您的要求增加score。从上面给出的代码得分无处更新。

建议:使用下面的代码段简化您的代码:

void startTimer()
{  
    if (score >= 100 && score <= 900 && score % 100 == 0)
    {   
        timerWIN.Start();  
    }  
    else if (score == 1000)  
    {    
        timer1000.Start();  
    } 
}

答案 1 :(得分:1)

我一开始误解了这个问题,你可以通过使用switch语句来简化代码,它可以帮助你在逻辑中找到错误,因为从提供的代码中很难看出你出错的地方。

switch (score)
{
    case 100:
    case 200:
    case 300:
    case 400:
    case 500:
    case 600:
    case 700:
    case 800:
    case 900:
        timerWIN.Start();
        break;
    case 1000:
        timer1000.Start();
        break;
}

答案 2 :(得分:0)

显示代码片段,其中score正在被修改。


您应该按照以下方式更改startTimer方法:

void startTimer()
{
    if(score == 1000)
    {
        timer1000.Start();  
    }
    else if(score % 100 == 0 && score < 1000)
    {
        timerWIN.Start();  
    }
}

timer1000_Tick方法中,您不应该timer1000代替timerWIN

timer1000.Stop();