2
\$\begingroup\$

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;
        }
\$\endgroup\$
  • 2
    \$\begingroup\$ First thing is to get rid of the ugly switch statements, use GameState classes as explained here \$\endgroup\$ – Maik Semder Jul 22 '12 at 10:10
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Austin Brunkhorst Jul 22 '12 at 10:30
4
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

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;
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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