-1

私はゲームを作っていて、さまざまなスコアでテキストを表示したいと考えています。しかし、現時点では、タイマーは最初の場合にのみ機能します (テキストのみが表示されます) (つまり、スコアが 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();

}
4

3 に答える 3

1

score要件に従ってを増やしていることを確認してください。上記のコードから、スコアはどこにも更新されていません。

提案:以下のコード スニペットを使用して、コードを簡潔にします。

void startTimer()
{  
    if (score >= 100 && score <= 900 && score % 100 == 0)
    {   
        timerWIN.Start();  
    }  
    else if (score == 1000)  
    {    
        timer1000.Start();  
    } 
}
于 2012-10-30T11:15:33.380 に答える
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;
}
于 2012-10-30T11:16:22.033 に答える
0

が変更されているコード フラグメントを表示しますscore


startTimer次のようにメソッドを変更する必要があります。

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

と、

メソッド内で、 の代わりにtimer1000_Tick停止しないでください。timer1000timerWIN

timer1000.Stop();
于 2012-10-30T11:14:44.940 に答える