Search code examples
c#if-statementrandommonogamebreakout

Trying to add new levels to my Breakout game


I have a problem with my code, check the code down below, so you understand which section i'm talking about!

I'm trying to add new block levels when "block.Count == 1", you know when all my blocks are destroyed by the ball. I want it to start a new level with a different block path. When I have:

else if (block.Count == 1 && block2.Count == 1)
{
     spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
}

It doesn't draw out my gameover_texture, when are blocks are gone? (Sorry about my bad english)

    public class Game1 : Game
    {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont spritefont;
    Texture2D paddle_texture;
    Texture2D ball_texture;
    Texture2D blockred_texture;
    Texture2D blockgreen_texture;
    Texture2D gameover_texture;
    Rectangle paddle_rect;
    Rectangle ball_rect;
    Rectangle blockred_rect;
    Rectangle blockgreen_rect;
    Rectangle gameover_rect;

    Vector2 paddle_speed;
    Vector2 ball_speed;

    Random random;

    StreamReader sr;
    StreamWriter sw;

    int lives = 3;
    int points = 0;
    int highscore;
    int counter = 0;
    int seconds = 0;

    List<Rectangle> block = new List<Rectangle>();
    List<Rectangle> block2 = new List<Rectangle>();

    bool Start = false;
    bool holdingleft = false;
    bool holdingright = false;
    bool resetballspeed = false;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.PreferredBackBufferWidth = 760;
        graphics.PreferredBackBufferHeight = 620; 
    }

    protected override void Initialize()
    {
        random = new Random();
        paddle_speed.X = 6f;
        ball_speed.X = random.Next(-1, 1); 
        ball_speed.Y = 7f;

        sr = new StreamReader("highscore.txt");
        highscore = int.Parse(sr.ReadLine());
        sr.Close();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        spritefont = Content.Load<SpriteFont>("Fonts/Myfont");
        paddle_texture = Content.Load<Texture2D>("Pics/linje");
        ball_texture = Content.Load<Texture2D>("Pics/boll");
        blockgreen_texture = Content.Load<Texture2D>("Pics/block-grön");
        blockred_texture = Content.Load<Texture2D>("Pics/block-röd");
        gameover_texture = Content.Load<Texture2D>("Pics/GameOver");
        paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 580, paddle_texture.Width, paddle_texture.Height);
        ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 556, ball_texture.Width, ball_texture.Height);
        gameover_rect = new Rectangle((Window.ClientBounds.Width / 2) - (gameover_texture.Width / 2), (Window.ClientBounds.Height / 2) - gameover_texture.Height / 2, gameover_texture.Width, gameover_texture.Height);

        block.Add(blockgreen_rect);
        block2.Add(blockred_rect);
        for (int i = 1; i < 2; i++)
        {
            for (int g = 1; g < 3; g++)
            {
                block2.Add(new Rectangle((g * 63) - 60, (i * 40), blockred_texture.Width, blockred_texture.Height));
            }
        }
        for (int i = 1; i < 2; i++)
        {
            for (int g = 1; g < 3; g++)
            {
                block.Add(new Rectangle((g * 63) - 60, (i * 20) + 40, blockgreen_texture.Width, blockgreen_texture.Height));
            }
        }
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        if (Start == true) //kolla om "Start == true", 
        {
            ball_rect.X += (int)ball_speed.X; 
            ball_rect.Y += (int)ball_speed.Y;
        }

        if (Start == false) 
        {
            ball_rect.X = paddle_rect.X + ((paddle_rect.Width / 2) - (ball_texture.Width / 2)); 
        }
        if (ball_rect.X > Window.ClientBounds.Width - ball_texture.Width || ball_rect.X < 0) 
            ball_speed.X *= -1;

        if (ball_rect.Y > Window.ClientBounds.Height - ball_texture.Height || ball_rect.Y < 0) 
            ball_speed.Y *= -1;

        if (ball_rect.Y > Window.ClientBounds.Height - ball_texture.Height)
        {
            lives -= 1;
            Start = false;
            ball_rect.X = (Window.ClientBounds.Width - ball_texture.Width) / 2; 
            ball_rect.Y = 556;
            paddle_rect.X = (Window.ClientBounds.Width - paddle_texture.Width) / 2; 
            paddle_rect.Y = 580; 
        }

        KeyboardState ks = Keyboard.GetState();
        if (ks.IsKeyDown(Keys.Left)) 
        {
            paddle_rect.X -= (int)paddle_speed.X; 
            holdingleft = true;
        }
        else if (ks.IsKeyDown(Keys.Right))
        {
            paddle_rect.X += (int)paddle_speed.X;
            holdingright = true;
        }
        else if (ks.IsKeyDown(Keys.Space)) 
        {
            Start = true;
        }
        else if (ks.Equals(new KeyboardState()))
        {
            resetballspeed = true;
        }

        if (paddle_rect.X > Window.ClientBounds.Width - paddle_rect.Width) 
            paddle_rect.X = (Window.ClientBounds.Width - paddle_rect.Width);

        if (paddle_rect.X < 0) 
            paddle_rect.X = 0;

        if (paddle_rect.Intersects(ball_rect))
        {
            ball_speed.Y *= -1;
            ball_rect.Y += (int)ball_speed.Y;
            if (holdingleft == true)
            {
                ball_speed.X -= 3;
            }
            else if (holdingright == true)
            {
                ball_speed.X += 3;
            }
            else if (resetballspeed == true)
            {
                ball_speed.X = 1;
            }
        }

        if (points == highscore)
        {
            sw = new StreamWriter("highscore.txt");
            sw.WriteLine(points);
            sw.Close();
        }

        for (int j = 1; j < block.Count; j++) 
        {
            if (ball_rect.Intersects(block[j])) 
            {
                ball_speed.Y *= -1;
                points += 1;
                block.RemoveAt(j); 
                if (points > 9)
                {
                    paddle_rect.Width = 60;
                }
                if (points > highscore)
                {
                    highscore = points;
                }
            }
        }

        for (int k = 1; k < block2.Count; k++) 
        {
            if (ball_rect.Intersects(block2[k])) 
            {
                ball_speed.Y *= -1;
                points += 5;
                block2.RemoveAt(k); 
                                    if (points > 9)
                {
                    paddle_rect.Width = 60;
                }
                if (points > highscore)
                {
                    highscore = points;
                }
            }
        }

        holdingleft = false;
        holdingright = false;

        counter++;

        if (counter == 60)
        {
            seconds++;
            counter = 0;
        }
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        if (lives > 0)
        {
            spriteBatch.Draw(ball_texture, ball_rect, Color.White);
            spriteBatch.Draw(paddle_texture, paddle_rect, Color.White);
            spriteBatch.DrawString(spritefont, "Lives left: " + lives, Vector2.Zero, Color.White);
            spriteBatch.DrawString(spritefont, "Points: " + points, new Vector2(350, 0), Color.White);
            spriteBatch.DrawString(spritefont, "Timer: " + seconds, new Vector2(350, 600), Color.White);
            spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(660, 0), Color.White);
            foreach (Rectangle g in block)
            {
                spriteBatch.Draw(blockgreen_texture, g, Color.White);
            }
            foreach (Rectangle t in block2)
            {
                spriteBatch.Draw(blockred_texture, t, Color.White);
            }
        }
        else if (block.Count == 1 && block2.Count == 1)
            {
                spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
            }
        else if (lives == 0)
        {
            spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }
    }

Solution

  • Fixed the problem now, by putting the else if-statement inside "if (lives > 0)"