Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I use a switch with enums to divide my game in different parts. In the code below, that is simplified, I have a problem, becasue I need to show the same text in two or perhaps more parts and I wonder if there is a better way to do this, to avoid the use of the same code at several cases?

 switch (currentGameState)
        {
            case GameState.Start:
                // Code
                break;

            case GameState.Info:
                // Code
                break;

            case GameState.PlayGame:
                spriteBatch.Begin();
                spriteBatch.DrawString(scoreFont, "Score : " + currentScore, new Vector2(10, 10), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.DrawString(timeFont, "Time : " + secondsToDisplay, new Vector2(10, 30), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.DrawString(timeFont, "Spaceship : " + numberOfSpaceship, new Vector2(10, 50), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.End();
                break;

            case GameState.NewSpaceship:
                spriteBatch.Begin();
                spriteBatch.DrawString(scoreFont, "Score : " + currentScore, new Vector2(10, 10), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.DrawString(timeFont, "Time : " + secondsToDisplay, new Vector2(10, 30), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.DrawString(timeFont, "Spaceship : " + numberOfSpaceship, new Vector2(10, 50), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.End();
                break;

            case GameState.GameOver:
            // Code
                break;
        }
share|improve this question
2  
First thing is to get rid of the ugly switch statements, use GameState classes as explained here –  Maik Semder Jul 22 '12 at 10:10
4  
Maik is right. But if you have to use the switch statement you can always take code that is repetitive and make it a function. –  Austin Brunkhorst Jul 22 '12 at 10:30
add comment

2 Answers

This is what functions (and methods) are for. Create a new function, add those 5 lines to it, and call the function in each switch case instead.

share|improve this answer
add comment

You have a few options. You can double up cases on switch statements. Remember if those breaks aren't there, the execution just continues along:

case GameState.PlayGame:
case GameState.NewSpaceship:
   spriteBatch.Begin();
   spriteBatch.DrawString(scoreFont, "Score : " + currentScore, new Vector2(10, 10), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.DrawString(timeFont, "Time : " + secondsToDisplay, new Vector2(10, 30), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.DrawString(timeFont, "Spaceship : " + numberOfSpaceship, new Vector2(10, 50), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.End();
   break;

Or you can pull out your duplicate code into a function:

case GameState.PlayGame:
    Draw();
    break;
case GameState.NewSpaceship:
    Draw();
    break;

Then somewhere else in your code:

public void Draw() {
   spriteBatch.Begin();
   spriteBatch.DrawString(scoreFont, "Score : " + currentScore, new Vector2(10, 10), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.DrawString(timeFont, "Time : " + secondsToDisplay, new Vector2(10, 30), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.DrawString(timeFont, "Spaceship : " + numberOfSpaceship, new Vector2(10, 50), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
   spriteBatch.End();
   break;
}

Or do both!

case GameState.PlayGame:
case GameState.NewSpaceship:
    Draw();
    break;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.