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?
}
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();
}