2
\$\begingroup\$

I am having a problem drawing the "Game Over!" text once the user is on the last tile.

This is what I have:

The Update and drawText methods are in a class named turtle:

public void Update(float scalingFactor, int[,] map, SpriteBatch batch, SpriteFont font)
{
    if (isMovable(mapX, mapY - 1, map))
    {
        position.Y = position.Y - (int)scalingFactor;
        angle = 0.0f;
        Program.form.direction = "";

        if (mapX == 17 && mapY == 1)// This is the last tile(Tested)
        {
            Program.form.BackColor = System.Drawing.Color.Red;
            drawText(batch, font);
        }
    }
}                    

public void drawText(SpriteBatch spritebatch, SpriteFont spriteFont)
{
    textPosition.X = 200; // a vector2
    textPosition.Y = 200;
    spritebatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
    spritebatch.DrawString(spriteFont, "Game Over!!!", textPosition, Color.Red);
    spritebatch.End();
}

This update is in the Game1 class:

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    turtle.Update(scalingFactor, map, spriteBatch, font);         

    base.Update(gameTime);
}

I have also added the font content to LoadContent:

font = Content.Load<SpriteFont>("fontType");

What am I doing wrong? Why does the text not want to show on game completion? If I call the turtle.draw() in the main Draw method. The "Game Over" text stays on screen from the beggining. What am I missing?

Thanks

\$\endgroup\$
5
  • \$\begingroup\$ Without knowing more, have you tested this line? if (mapX == 17 && mapY == 1) \$\endgroup\$ Commented Jun 1, 2012 at 18:39
  • \$\begingroup\$ Yes ive tested it, that is the last tile. The way i tested it was when the sprite moved onto that tile, my WinForm will change color. \$\endgroup\$
    – Sigh-AniDe
    Commented Jun 1, 2012 at 18:41
  • \$\begingroup\$ Your font is being initialized correctly? \$\endgroup\$ Commented Jun 1, 2012 at 18:45
  • \$\begingroup\$ Yes, it is, Byte56 answer is correct \$\endgroup\$
    – Sigh-AniDe
    Commented Jun 1, 2012 at 18:50
  • \$\begingroup\$ The question was pretty much answered ,but i just wanted to say that probably the text does get drawn the thing is the update loop continues and you receive input and the if statement becomes false and you don't notice the text being drawn. \$\endgroup\$
    – Artii
    Commented Jun 3, 2012 at 12:12

1 Answer 1

5
\$\begingroup\$

You want to keep your drawing and update loops separate. Attempting to draw inside the update loop is likely to cause issues. Additionally, putting logic for drawing a game over message inside the update loop of a game character isn't the best place for it.

You can try using a global variable for setting when your game is over:

if (mapX == 17 && mapY == 1)
{
    GameOver = true;
}

Then in your draw loop,

if (GameOver)
{
    Program.form.BackColor = System.Drawing.Color.Red;
    drawText(batch, font);
}

This will allow you to have a bit more control in other aspects as well. For example you can check to see if the game is over before checking for input. That would disallow players from moving around after the game ended.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .