I'm relatively new to XNA and C#, so please try to use small words. I'm making a Breakout clone for my first real project (simplest thing I could think of), and have gotten fairly far. How would I implement a fade to black after the level ends or the ball flies past the paddle. I also want it to pause the game loop until the fade is complete. Any suggestions?
My whole game-state management, minus a couple if-elses:
Main Loop:
And the Reset Level code:
I'm really sorry if this is to much code, I don't know what you guys want to see...
My whole game-state management, minus a couple if-elses:
Code:
static class GlobalVars
{
public static int Lives;
public static int Points;
public static bool IsGameRunning;
public static int Level;
public static bool Sound;
public static void Reset()
{
Lives = 3;
Points = 0;
Level = 1;
}
}
Main Loop:
Code:
protected override void Update(GameTime gameTime)
{
iHelper.Update();
OtherKeyStuff();
if (GlobalVars.IsGameRunning)
{
paddle.Update();
ball.Update();
ball.PaddleCollision(paddle.GetLeftBounds(),paddle.GetRightBounds(),paddle.GetCenterBounds());
if (ball.OffBottom())
{
DoLives(true);
if (GlobalVars.Sound)
{
Kaboom.Play();
}
StartGame();
}
base.Update(gameTime);
}
}
protected override void Draw(GameTime gameTime)
{
if (GlobalVars.IsGameRunning)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
DrawBackground();
foreach (Brick brick in bricks)
brick.Draw(spriteBatch);
paddle.Draw(spriteBatch);
ball.Draw(spriteBatch);
DrawInGameText(gameTime);
spriteBatch.End();
base.Draw(gameTime);
}
else
{
//TODO
}
}
private void DoLives(bool detract) //Add pause at end/ use gamestate
{
if (detract)
{
GlobalVars.Lives--;
}
if (GlobalVars.Lives < 0)
{
string tempPoints;
tempPoints = GlobalVars.Points.ToString();
this.Exit();
}
}
Code:
private void StartGame() //add IsGameRunning stuff
{
if (MediaPlayer.State == MediaState.Stopped)
{
StartGameSoundtrack();
}
paddle.SetInStartPosition();
ball.SetInStartPosition(paddle.GetBounds());
bricks = new Brick[bricksWide, bricksHigh];
for (int y = 0; y < bricksHigh; y++)
{
Color tint = Color.White;
for (int x = 0; x < bricksWide; x++)
{
bricks[x, y] = new Brick(
brickImage,
new Rectangle(
x * brickImage.Width,
y * brickImage.Height,
brickImage.Width,
brickImage.Height),
tint, true);
}
}
}
Comment