重置c#游戏中的按钮

时间:2017-03-20 23:01:40

标签: c# forms button reset pacman

所以,我正在做一个类似于pac-man的游戏,我想添加一个重置按钮(重置游戏,而不是重新启动应用程序)。我已经尝试了很多东西,但我还是无法让它发挥作用。任何解决方案?

提前致谢,

亨克

   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?

    }

1 个答案:

答案 0 :(得分:0)

只需创建Game的新实例即可。这将为您提供一组全新的变量,从而为您提供新的游戏状态。

你需要保留对游戏的引用以防止它被GC,所以你还需要一个静态变量。

static private Game _currentGame;    

private void ResetButton_Click(object sender, EventArgs e)
{
    this.Hide();
    _currentGame = new Game();
    _currentGame.Show();
    this.Close();
}