mardi 18 avril 2017

Having trouble with reset method

I'm having trouble with my method "ResetButton()", that basically gonna reset the whole game back to start position. But my levels are still there when I reset back to starting point, they are visible or invisible.

Gonna fix my OOP in code soon, so sorry if it's a bit messy.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;

namespace Spelprojekt
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont spritefont;
    Texture2D paddle_texture;
    Texture2D ball_texture;
    Texture2D blockred_texture;
    Texture2D blockgreen_texture;
    Texture2D blockblue_texture;
    Texture2D blockyellow_texture;
    Texture2D blockorange_texture;
    Texture2D blockpink_texture;
    Texture2D gameover_texture;
    Texture2D play_texture;
    Texture2D quit_texture;

    Rectangle paddle_rect;
    Rectangle ball_rect;
    Rectangle blockred_rect;
    Rectangle blockgreen_rect;
    Rectangle blockblue_rect;
    Rectangle blockyellow_rect;
    Rectangle blockorange_rect;
    Rectangle blockpink_rect;
    Rectangle gameover_rect;
    Rectangle play_rect;
    Rectangle quit_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;
    int level = 1;

    List<Rectangle> block = new List<Rectangle>();
    List<Rectangle> block2 = new List<Rectangle>();
    List<Rectangle> block3 = new List<Rectangle>();
    List<Rectangle> block4 = new List<Rectangle>();
    List<Rectangle> block5 = new List<Rectangle>();
    List<Rectangle> block6 = new List<Rectangle>();

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

    float timer = 2;
    float timer2 = 2;

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

        graphics.PreferredBackBufferWidth = 760; //ändrar fönstrets bredd
        graphics.PreferredBackBufferHeight = 620; //ändrar fönstrets höjd
    }

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

        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");
        blockred_texture = Content.Load<Texture2D>("Pics/block");
        blockblue_texture = Content.Load<Texture2D>("Pics/block");
        blockyellow_texture = Content.Load<Texture2D>("Pics/block");
        blockorange_texture = Content.Load<Texture2D>("Pics/block");
        blockpink_texture = Content.Load<Texture2D>("Pics/block");
        gameover_texture = Content.Load<Texture2D>("Pics/GameOver");
        play_texture = Content.Load<Texture2D>("Pics/play");
        quit_texture = Content.Load<Texture2D>("Pics/quit");

        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) - 70, gameover_texture.Width, gameover_texture.Height);
        quit_rect = new Rectangle((Window.ClientBounds.Width / 2) - (quit_texture.Width / 2), ((Window.ClientBounds.Height / 2) - quit_texture.Height / 2) + 150, quit_texture.Width, quit_texture.Height);

        block.Add(blockgreen_rect);
        block2.Add(blockred_rect);
        block3.Add(blockblue_rect);
        block4.Add(blockyellow_rect);
        block5.Add(blockorange_rect);
        block6.Add(blockpink_rect);

        //LEVEL 1
        for (int i = 1; i < 3; i++)
        {
            for (int f = 1; f < 8; f++)
            {
                block.Add(new Rectangle((f * 63) + 94, (i * 40) + 20, blockgreen_texture.Width, blockgreen_texture.Height));
            }
        }
        for (int i = 1; i < 3; i++)
        {
            for (int g = 1; g < 8; g++)
            {
                block2.Add(new Rectangle((g * 63) + 94, (i * 40), blockred_texture.Width, blockred_texture.Height));
            }
        }

        //LEVEL 2
        for (int i = 1; i < 3; i++)
        {
            for (int j = 1; j < 10; j++)
            {
                block3.Add(new Rectangle((j * 63) + 34, (i * 200) - 120, blockblue_texture.Width, blockblue_texture.Height));
            }
        }
        for (int i = 1; i < 10; i++)
        {
            for (int k = 1; k < 4; k++)
            {
                block4.Add(new Rectangle((k * 103) + 143, (i * 20) + 80, blockyellow_texture.Width, blockyellow_texture.Height));
            }
        }

        //LEVEL 3
        for (int i = 1; i < 7; i++)
        {
            for (int j = 1; j < 7; j++)
            {
                block5.Add(new Rectangle((j * 63) + 127, (i * 20) + 160, blockorange_texture.Width, blockorange_texture.Height));
            }
        }
        for (int i = 1; i < 10; i++)
        {
            for (int k = 1; k < 3; k++)
            {
                block6.Add(new Rectangle((k * 443) - 317, (i * 20) + 130, blockpink_texture.Width, blockpink_texture.Height));
            }
        }
    }

    protected override void UnloadContent()
    {

    }

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

        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.Equals(new KeyboardState()))
        {
            resetballspeed = true;
        }

        else if (ks.IsKeyDown(Keys.Space))
        {
            Start = true;
        }
        else if (ks.IsKeyDown(Keys.R))
        {
            ResetButton();
        }

        if (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;
        }

        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;

            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 > highscore)
                {
                    highscore = points;
                }
            }
        }

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

        if (timer < 0)
        {
            for (int l = 1; l < block3.Count; l++)
            {
                if (ball_rect.Intersects(block3[l]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block3.RemoveAt(l);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }

            for (int m = 1; m < block4.Count; m++)
            {
                if (ball_rect.Intersects(block4[m]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block4.RemoveAt(m);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
        }

        if (timer2 < 0)
        {
            for (int n = 1; n < block5.Count; n++)
            {
                if (ball_rect.Intersects(block5[n]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block5.RemoveAt(n);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }

            for (int o = 1; o < block6.Count; o++)
            {
                if (ball_rect.Intersects(block6[o]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block6.RemoveAt(o);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
        }

        holdingleft = false;
        holdingright = false;

        counter++;
        if (counter == 60)
        {
            seconds++;
            counter = 0;
        }

        if (block.Count == 1 && block2.Count == 1)
        {
            level++;
            float timerlength = (float)gameTime.ElapsedGameTime.TotalSeconds;
            timer -= timerlength;
        }

        if (block3.Count == 1 && block4.Count == 1)
        {
            level++;
            float timerlength2 = (float)gameTime.ElapsedGameTime.TotalSeconds;
            timer2 -= timerlength2;
        }

        if (lives == 0)
        {
            this.IsMouseVisible = true;
        }

        base.Update(gameTime);
    }

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

        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, new Vector2(2, 0), Color.White);
            spriteBatch.DrawString(spritefont, "Points: " + points, new Vector2(350, 0), Color.White);
            spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(652, 0), Color.White);
            spriteBatch.DrawString(spritefont, "Timer: " + seconds, new Vector2(350, 600), Color.White);

            foreach (Rectangle g in block)
            {
                spriteBatch.Draw(blockgreen_texture, g, Color.LimeGreen);
            }
            foreach (Rectangle r in block2)
            {
                spriteBatch.Draw(blockred_texture, r, Color.IndianRed);
            }

            if (block.Count == 15 && block2.Count == 15)
            {
                spriteBatch.DrawString(spritefont, "LEVEL " + level, Vector2.Zero, Color.White);
            }
            else if (level == 2)
            {
                spriteBatch.DrawString(spritefont, "LEVEL " + level, Vector2.Zero, Color.White);
            }

            if (timer < 0)
            {
                foreach (Rectangle b in block3)
                {
                    spriteBatch.Draw(blockblue_texture, b, Color.CornflowerBlue);
                }
                foreach (Rectangle y in block4)
                {
                    spriteBatch.Draw(blockyellow_texture, y, Color.Yellow);
                }
            }

            if (timer2 < 0)
            {
                foreach (Rectangle o in block5)
                {
                    spriteBatch.Draw(blockorange_texture, o, Color.Orange);
                }
                foreach (Rectangle p in block6)
                {
                    spriteBatch.Draw(blockpink_texture, p, Color.HotPink);
                }
            }
        }

        else if (lives == 0)
        {
            spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
            spriteBatch.Draw(quit_texture, quit_rect, Color.White);
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

    void ResetButton()
    {
        lives = 3;
        points = 0;
        level = 1;
        counter = 0;
        seconds = 0;
        timer = 2;
        timer2 = 2;

        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);

        Start = false;

        for (int i = 1; i < 3; i++)
        {
            for (int f = 1; f < 8; f++)
            {
                block.Add(new Rectangle((f * 63) + 94, (i * 40) + 20, blockgreen_texture.Width, blockgreen_texture.Height));
            }
        }
        for (int i = 1; i < 3; i++)
        {
            for (int g = 1; g < 8; g++)
            {
                block2.Add(new Rectangle((g * 63) + 94, (i * 40), blockred_texture.Width, blockred_texture.Height));
            }
        }
    }
}
}

Having trouble with:

    else if (ks.IsKeyDown(Keys.R))
    {
            ResetButton();
    }


    void ResetButton()
    {
        lives = 3;
        points = 0;
        level = 1;
        counter = 0;
        seconds = 0;
        timer = 2;
        timer2 = 2;

        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);

        Start = false;

        for (int i = 1; i < 3; i++)
        {
            for (int f = 1; f < 8; f++)
            {
                block.Add(new Rectangle((f * 63) + 94, (i * 40) + 20, blockgreen_texture.Width, blockgreen_texture.Height));
            }
        }
        for (int i = 1; i < 3; i++)
        {
            for (int g = 1; g < 8; g++)
            {
                block2.Add(new Rectangle((g * 63) + 94, (i * 40), blockred_texture.Width, blockred_texture.Height));
            }
    }


    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, new Vector2(2, 0), Color.White);
        spriteBatch.DrawString(spritefont, "Points: " + points, new Vector2(350, 0), Color.White);
        spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(652, 0), Color.White);
        spriteBatch.DrawString(spritefont, "Timer: " + seconds, new Vector2(350, 600), Color.White);

        foreach (Rectangle g in block)
        {
            spriteBatch.Draw(blockgreen_texture, g, Color.LimeGreen);
        }
        foreach (Rectangle r in block2)
        {
            spriteBatch.Draw(blockred_texture, r, Color.IndianRed);
        }

        if (block.Count == 15 && block2.Count == 15)
        {
            spriteBatch.DrawString(spritefont, "LEVEL " + level, Vector2.Zero, Color.White);
        }
        else if (level == 2)
        {
            spriteBatch.DrawString(spritefont, "LEVEL " + level, Vector2.Zero, Color.White);
        }

        if (timer < 0)
        {
            foreach (Rectangle b in block3)
            {
                spriteBatch.Draw(blockblue_texture, b, Color.CornflowerBlue);
            }
            foreach (Rectangle y in block4)
            {
                spriteBatch.Draw(blockyellow_texture, y, Color.Yellow);
            }
        }

        if (timer2 < 0)
        {
            foreach (Rectangle o in block5)
            {
                spriteBatch.Draw(blockorange_texture, o, Color.Orange);
            }
            foreach (Rectangle p in block6)
            {
                spriteBatch.Draw(blockpink_texture, p, Color.HotPink);
            }
        }
    }

    else if (lives == 0)
    {
        spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
        spriteBatch.Draw(quit_texture, quit_rect, Color.White);
    }

Aucun commentaire:

Enregistrer un commentaire