示例#1
0
        public void OnActionButtonClicked(object sender, EventArgs args)
        {
            // Cast the sender (object that called the action) into what we know it is to be
            StyledGameButton clicked = (StyledGameButton)sender;

            // We've clicked a button that is currently selected, so let's deselect it
            if (SelectedButton == clicked)
            {
                DescriptionLabel.Text = STARTING_TEXT; // Reset text
                clicked.MarkInactive();
                SelectedButton = null;                 // Remove our reference
                return;
            }

            // Set all children to their default colors
            foreach (StyledGameButton child in ButtonLayout.Children)
            {
                child.MarkInactive();
            }

            // Since this was the clicked one, make its colors the active colors
            clicked.MarkActive();

            DescriptionLabel.Text = ACTION_CHOSEN_TEXT;

            // Keep the reference so we can use it later (see above early exit and below OnShoot)
            SelectedButton = clicked;
        }
示例#2
0
        public void OnShootButtonClicked(object sender, EventArgs args)
        {
            // An option hasn't been clicked (otherwise this wouldn't be null)
            if (SelectedButton == null)
            {
                return;
            }

            int      repetitions = 3;
            TimeSpan span        = new TimeSpan(0, 0, 1); // Every second

            // Make a timer to show to the user
            Device.StartTimer(span, () =>
            {
                // After 3 seconds, show the "Shoot" Text
                if (repetitions == 0)
                {
                    DescriptionLabel.Text = GO_TEXT;
                }
                else if (repetitions == -1) // After shoot is displayed, actually run the game logic
                {
                    string computerChoice = GetRandomComputerChoice();

                    bool didComputerTie = SelectedButton.Text == computerChoice;
                    bool didComputerWin = DidComputerWin(SelectedButton.Text, computerChoice);

                    if (didComputerTie)
                    {
                        DescriptionLabel.Text = $"The Computer chose {computerChoice} too\n\nPlay again";
                        SelectedButton.MarkPreviouslySelected();
                    }
                    else if (didComputerWin)
                    {
                        DescriptionLabel.Text = $"The Computer chose {computerChoice}\n\nYou Lost.";
                        SelectedButton.MarkLost();
                    }
                    else
                    {
                        DescriptionLabel.Text = $"The Computer chose {computerChoice}\n\nYou Won.";
                        SelectedButton.MarkWon();
                    }

                    // Prevent the user from pressing the `Shoot` button over and over
                    SelectedButton = null;
                    return(false);
                }
                else // Otherwise we're still just diplaying the countdown
                {
                    DescriptionLabel.Text = repetitions.ToString();
                }

                repetitions = repetitions - 1; // or you could do repetitions--;

                return(true);
            });
        }
示例#3
0
        public GamePageInCSharp()
        {
            // This tells the Navigation page (App.cs:15) what title to put in the navigation bar.
            Title = "C# : RPS";


            // Create a layout that is the 'root' or 'parent' that can hold everything (as children)
            layout = new StackLayout
            {
                VerticalOptions   = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            // Create a parent layout for the buttons specifically so we can orient them horizontally in their parent
            buttonLayout = new Grid {
                HorizontalOptions = LayoutOptions.CenterAndExpand, Margin = new Thickness(10, 0)
            };

            // We want to make a 1x3 grid where the height is determined by its children
            // and the width of each column is equally divided
            buttonLayout.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Auto)
            });
            buttonLayout.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });
            buttonLayout.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });
            buttonLayout.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });

            // We make this a class level variable so we can change the text from other
            // methods later
            descriptionLabel = new Label
            {
                Text                    = STARTING_TEXT,
                FontSize                = 24,
                FontAttributes          = FontAttributes.Bold,
                HorizontalTextAlignment = TextAlignment.Center,
                Margin                  = new Thickness(0, 0, 0, 25)
            };

            // Make the buttons
            Button rockButton = new StyledGameButton {
                Text = ROCK_ACTION
            };
            Button paperButton = new StyledGameButton {
                Text = PAPER_ACTION
            };
            Button scissorsButton = new StyledGameButton {
                Text = SCISSORS_ACTION
            };
            Button shootButton = new Button
            {
                Text              = "Shoot",
                Margin            = 15,
                BorderWidth       = 1,
                BorderColor       = Color.SlateGray,
                BackgroundColor   = Color.DimGray,
                TextColor         = Color.White,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            // Register event handlers when a button is clicked (note that they use the same handler)
            rockButton.Clicked     += OnActionButtonClicked;
            paperButton.Clicked    += OnActionButtonClicked;
            scissorsButton.Clicked += OnActionButtonClicked;

            // Add event handler for when the shoot button is clicked.
            shootButton.Clicked += OnShootButtonClicked;

            // Add each button to their layout (as a child)
            buttonLayout.Children.Add(rockButton, 0, 0);
            buttonLayout.Children.Add(paperButton, 1, 0);
            buttonLayout.Children.Add(scissorsButton, 2, 0);

            // Add each child layout (or view) to the root layout
            layout.Children.Add(descriptionLabel);
            layout.Children.Add(buttonLayout);
            layout.Children.Add(shootButton);

            // The content property always has to be set to show anything on a page.
            Content = layout;
        }