-1

So, I'm making a game which is kind of similar to pac-man and I want to add a reset-button (resetting the game, NOT restarting the application). I've tried a lot of things but I still can't get it working. Any solutions?

Thanks in advance,

Henk

   public partial class Game : Form
{

    public int GameHeigth = 45;
    public int GameWidth = 45;
    private Hokje[,] matrix = new Hokje[15, 15]; //creates the "game board"
    private List<VObject> XObjects = new List<VObject>(); //list of the objects that 
the game has(unmoveable boxes and moveable by the player boxes)
    private Hero Maxim; // the hero of the game
    private Random rnd = new Random();

    public Reaper Linde { get; private set; } // the enemy

    public Game()
    {
        InitializeComponent();
        GenerateField();
        NeighbourBase();
        StartGame();
    } 
private void GenerateField()
    {
        int newpointY = 0;
        int newpointX = 0;
        for (int y = 0; y < 15; y++)
        {
            for (int x = 0; x < 15; x++)
            {
                int choise = rnd.Next(0, 99);
                Hokje green = new Hokje();
                matrix[y, x] = green;
                green.Location = new Point(newpointX, newpointY);
                Controls.Add(green);
                if (choise < 20)
                {
                    Doos box = new Doos(green);
                }
                if (choise >= 20 && choise <= 25)
                {
                    Muur wall = new Muur(green);
                } 
                newpointX = newpointX + GameWidth;
            }
            newpointX = 0;
            newpointY = newpointY + GameHeigth;
        }
    }

    private void NeighbourBase()
    {
        for (int y = 0; y < 15; y++)
        {
            for (int x = 0; x < 15; x++)
            {
                try
                {
                    matrix[y, x].Buren[Direction.Up] = matrix[y - 1, x];
                }
                catch (IndexOutOfRangeException)
                {
                    matrix[y, x].Buren[Direction.Up] = null;
                }
                try
                {
                    matrix[y, x].Buren[Direction.Down] = matrix[y + 1, x];
                }
                catch (IndexOutOfRangeException)
                {
                    matrix[y, x].Buren[Direction.Down] = null;
                }
                try
                {
                    matrix[y, x].Buren[Direction.Left] = matrix[y, x - 1];
                }
                catch (IndexOutOfRangeException)
                {
                    matrix[y, x].Buren[Direction.Left] = null;
                }
                try
                {
                    matrix[y, x].Buren[Direction.Right] = matrix[y, x + 1];
                }
                catch (IndexOutOfRangeException)
                {
                    matrix[y, x].Buren[Direction.Right] = null;
                }
            }
        }
    }

    private void StartGame()
    {
        Maxim = new Hero(matrix[0, 0]);
        Linde = new Reaper(matrix[14, 14]);
 private void Game_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            Maxim.direction = Direction.Up;
        }
        if (e.KeyCode == Keys.Down)
        {
            Maxim.direction = Direction.Down;
        }
        if (e.KeyCode == Keys.Left)
        {
            Maxim.direction = Direction.Left;
        }
        if (e.KeyCode == Keys.Right)
        {
            Maxim.direction = Direction.Right;
        }
        Maxim.Move();
    }

    private void Game_Load(object sender, EventArgs e)
    {
        foreach (Control control in Controls)
        {
            control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
        }
    }

    private void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)  //overrides browsing buttons focus while pressing on arrows keys
    {
        if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right) 
        {
            e.IsInputKey = true;
        }
    }

  .......//code missing here

   private void ResetButton_Click(object sender, EventArgs e)
    {
      //what do I set here?

    }
henk_bae
  • 25
  • 7
  • 1
    Just reset the game state. All of the stuff that tracks changes as the game progresses, set them to their initial values. – itsme86 Mar 20 '17 at 23:05
  • 1
    you need to recreate the initial game state; but there isn't enough info here to describe that. – BradleyDotNET Mar 20 '17 at 23:05
  • I'm not sure I follow, but there isn't any simple "Reset" command. If your `StartGame();` function doesn't reset everything to its initial state, then you'll have to write a function that resets everything. – Tyler Roper Mar 20 '17 at 23:06
  • @itsme86 could you give me an example on how to do that? I'm a bit of a newbie:/ – henk_bae Mar 20 '17 at 23:12
  • @Santi I added a few more lines of code there 'startgame();' will actually only spawn the heroes – henk_bae Mar 20 '17 at 23:13
  • @itsme86 when i tried to do that, i got a system breakout – henk_bae Mar 20 '17 at 23:24
  • @henk_bae It's not uncommon to miss some things. Just address them one at a time after your initial attempt. Like "oops! I forgot to set the score back to 0" or "oops! I forgot to move the hero back to the starting location" – itsme86 Mar 20 '17 at 23:26
  • @itsme86 The generate field method, generates a field with objects, when i try to reset it, It just stays the same. How can I get rid of the old list with objects and call the new one? – henk_bae Mar 20 '17 at 23:32
  • Just re-new it: `matrix = new Hokje[15, 15];` before calling `GenerateField()`. – itsme86 Mar 21 '17 at 00:01

1 Answers1

0

Just create a new instance of Game. This will give you a whole new set of variables and therefore a new game state.

You need to keep a reference to the game to keep it from being GC'd, so you also need a static variable.

static private Game _currentGame;    

private void ResetButton_Click(object sender, EventArgs e)
{
    this.Hide();
    _currentGame = new Game();
    _currentGame.Show();
    this.Close();
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • thanks it is working now but when i press reset the whole window refreshes. How can i modify it so that only the matrix(game board will reset)(with reset i mean generate a new field with random objects) – henk_bae Mar 21 '17 at 13:44
  • do you got any ideas? – henk_bae Mar 22 '17 at 00:22