-1

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);
    }
    }
PJvG
  • 1,310
  • 3
  • 16
  • 33
Lucke GG
  • 99
  • 7
  • Yeah I can delete the second problem and make a question about it when i'm working on that. – Lucke GG Apr 05 '17 at 13:25
  • True true, I realized that. It's breakout :P – Lucke GG Apr 05 '17 at 13:25
  • 1
    Also, can you confirm by debugging that both the lines `block.RemoveAt(j);` and `block2.RemoveAt(k);` get executed when you expect them to? Also, your `Update` method is ridiculously big, your code will be better readable if you divide its code over multiple methods with logical names which should than be called by `Update`. One last remark, I would advise you to study more about [OOP](https://msdn.microsoft.com/en-us/library/mt656686.aspx) and perhaps also about [SOLID](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)). They're good things to know if you want to write good design. – PJvG Apr 05 '17 at 13:52
  • Hm, I don't know exactly how I can debug and check the values. I did "starta debug" but it doesn't say anything. Gonna look into that – Lucke GG Apr 05 '17 at 13:55
  • You've got to place breakpoints. See [Debugger Basics](https://msdn.microsoft.com/en-us/library/k0k771bt.aspx). Or you can use the [Debug.WriteLine method](https://msdn.microsoft.com/en-us/library/9z9k5ydz(v=vs.110).aspx). – PJvG Apr 05 '17 at 14:00
  • 1
    Ok, thank you. Now when I check that with breakout. I think block.Count and block2.Count have value 1. But when I change value to 1 in my code so it's "if (block.Count == 1 && block2.Count == 1), the game still won't draw out my gameover.texture. That i'm testing to draw out. – Lucke GG Apr 05 '17 at 14:19
  • You should try to [update your question](http://stackoverflow.com/posts/43232254/edit) every time you have some new information about your problem so other people can quickly see it when they find your question. Anyway, is your problem now that you cannot get in the if-statement or that your `spriteBatch.Draw` call doesn't work? Have you placed a breakpoint inside the if-statement to see if the program ever reaches the code there? – PJvG Apr 05 '17 at 14:34
  • 3
    I would advise using only English language in your code, and never to make any variable names with accents. I know this might not be the best advice in this question, but it will make your code much clearer to choose 1 language throughout your code base ;) – Icepickle Apr 05 '17 at 14:49
  • 1
    Yeah that's true, I change my information now. Basically it doesn't do spriteBatch.Draw. So it does nothing, the program doesn't stop if I place a breakpoint on that if-statement – Lucke GG Apr 05 '17 at 14:50
  • U are right Icepickle, I'm swedish so I have certain things as swedish language, gonna change that. – Lucke GG Apr 05 '17 at 14:52
  • 1
    Fixed the problem now, by putting the if-statement inside "if (lives > 0)" – Lucke GG Apr 05 '17 at 16:38

1 Answers1

0

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

Lucke GG
  • 99
  • 7