0

I am trying to get a play again button on a WPF Application to reset my game. I can't figure it out for the life of me. My Professor said to create a new event handler but I still just don't understand what I am doing. My code is below, any help would be appreciated!! Thanks!

public partial class MainWindow : Window
{
    //declare and initialize variables
    private int lives = 10; //used to give lives for the game
    private int random = 0; //used to assign a random number

    public MainWindow()
    {
        InitializeComponent();
        Random r = new Random();
        //sets the range for the random number
        random = r.Next(0, 100);


        //play the game
        playGame();

    } //end MainWindow Constructor

    private void btnPlayAgain_Click(object sender, RoutedEventArgs e)
    {

        //call playGame to setup a new game
        playGame();

    }

    private void playGame()
    {
        //hide play again
        btnPlayAgain.Visibility = Visibility.Hidden;

    }

    private void txtInupt_KeyDown(object sender, KeyEventArgs e)
    {
        //when you run out of lives message pops up
        if (lives == 0)
        {
            lblFrom.Content = "You";
            lblTo.Content = "lost";
            return;
        }

        //when you guess the rigiht number message pops up
        if (e.Key == Key.Enter)
        {
            lives--;
            int userGuessed = Int32.Parse(txtInupt.Text);
            if (userGuessed == random)
            {
                lblFrom.Content = "You";
                lblTo.Content = "win!!";
                btnPlayAgain.Visibility = Visibility.Visible;
                txtInupt.Visibility = Visibility.Hidden;
                return;
            }
            if (userGuessed < random)
            {
                lblFrom.Content = userGuessed;
            }
            else
            {
                lblTo.Content = userGuessed;
            }

            //this is the countdown of how many lives you have
            lblStatus.Content = "Remaining Lives: " + lives;
            //changes the color toi rend when you have 3 or less lives
            if (this.lives <= 3)
            {
                this.lblStatus.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            }
            else
            {
                this.lblStatus.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 0));
            }
        }
    } //End txtInput_KeyDown
}//End Main Window
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Paige
  • 1
  • 1
    If the code you posted is indicative of what your professor is teaching you, they are doing you a great disservice. There should be very little code in the code-behind of a window or other view element for a WPF program. The bulk of your program logic belongs in a view model, including commands for the user to execute and properties representing values to display. Data binding markup in the XAML is used to make the connections between the view (XAML) and the view model (C#). See marked duplicate for example of how binding a command to a button would work for your scenario. – Peter Duniho Apr 17 '20 at 22:16
  • 1
    Don't worry about not understanding, we were all new at this at some point and had no idea what we were doing. When the `btnPlayAgain` becomes visible and you click it, the `btnPlayAgain_Click` method is called. That method calls the `playGame()` method to make `btnPlayAgain` invisible. Nothing will happen until you type something into `txtInupt`. When you start typing into that, the `txtInupt_KeyDown` method is called. When you enter that method, you don't have any `lives` remaining so nothing happens. You should reset the `lives` when you press the `btnPlayAgain`. – Brett Wertz Apr 17 '20 at 22:17

0 Answers0