2017-03-20 4 views
-1

Donc, je fais un jeu qui ressemble un peu à pac-man et je veux ajouter un bouton de réinitialisation (réinitialisation du jeu, pas de redémarrage de l'application). J'ai essayé beaucoup de choses mais je n'arrive toujours pas à le faire fonctionner. Des solutions?Réinitialiser le bouton dans un jeu de C#

Merci à l'avance,

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? 

    } 
+1

Juste réinitialiser l'état du jeu. Tous les éléments qui sont suivis changent au fur et à mesure que le jeu progresse et les définissent sur leurs valeurs initiales. – itsme86

+1

vous devez recréer l'état initial du jeu; mais il n'y a pas assez d'informations ici pour décrire cela. – BradleyDotNET

+0

Je ne suis pas sûr de suivre, mais il n'y a pas de simple commande "Reset". Si votre fonction 'StartGame();' ne réinitialise pas tout à son état initial, alors vous devrez écrire une fonction qui réinitialise tout. – Santi

Répondre

0

Il suffit de créer une nouvelle instance de Game. Cela vous donnera un nouvel ensemble de variables et donc un nouvel état de jeu.

Vous devez garder une référence au jeu pour l'empêcher d'être GC'd, vous avez donc besoin d'une variable statique.

static private Game _currentGame;  

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

merci, il fonctionne maintenant, mais quand j'appuie sur réinitialiser la fenêtre entière rafraîchit. Comment puis-je le modifier afin que seule la matrice (plateau de jeu va se réinitialiser) (avec reset je veux dire générer un nouveau champ avec des objets aléatoires) –

+0

avez-vous des idées? –