Example #1
0
        /// <summary>
        /// Updates the menu button. May highlight the button or detect button click
        /// </summary>
        /// <param name="mouse">the current mouse state</param>
        public void Update(MouseState mouse)
        {
            // only updates if button is visible
            if (visible)
            {
                // check for mouse over button
                if (drawRectangle.Contains(mouse.X, mouse.Y))
                {
                    // highlight button
                    sourceRectangle.X = buttonWidth;

                    // check for click started on button
                    if (mouse.LeftButton == ButtonState.Pressed &&
                        buttonReleased)
                    {
                        clickStarted = true;
                    }
                    else if (mouse.LeftButton == ButtonState.Released)
                    {
                        buttonReleased = true;

                        // if click finished on button, change game state
                        if (clickStarted)
                        {
                            Game1.ChangeState(clickState);

                            // click is no longer started on button
                            clickStarted = false;
                        }
                    }
                }
                else
                {
                    sourceRectangle.X = 0;

                    // no clicking on this button
                    clickStarted   = false;
                    buttonReleased = false;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }


            // update menu buttons as appropriate

            MouseState mouse = Mouse.GetState();

            foreach (MenuButton button in menuButtons)
            {
                if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults)
                {
                    button.Update(mouse);
                }
            }


            // game state-specific processing

            switch (currentState)
            {
            // case if player hitting
            case (GameState.PlayerHitting):
                numberOfCardsPlayerHand = playerHand.Count;
                Card playerCard2 = deck.TakeTopCard();
                playerCard2.FlipOver();
                playerCard2.X = HorizontalCardOffset;
                playerCard2.Y = TopCardOffset + numberOfCardsPlayerHand * VerticalCardSpacing;
                playerHand.Add(playerCard2);
                playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString();
                playerHit = true;
                Game1.ChangeState(GameState.WaitingForDealer);
                break;

            // case of player is passing
            case (GameState.PlayerPassing):
                playerDone = true;
                if (dealerDone)
                {
                    Game1.ChangeState(GameState.CheckingHandOver);
                }
                else
                {
                    Game1.ChangeState(GameState.WaitingForDealer);
                }
                break;

            // case if waiting for dealer
            case (GameState.WaitingForDealer):

                // dealer decide to hit
                if (GetBlockjuckScore(dealerHand) < MaxDealerHitValue)
                {
                    Game1.ChangeState(GameState.DealerHitting);
                }

                // dealer decide to stand or he got 21
                else
                {
                    dealerHit  = false;
                    dealerDone = true;
                    Game1.ChangeState(GameState.CheckingHandOver);
                }
                break;

            // case if dealer hitting
            case (GameState.DealerHitting):

                numberOfCardsDealerHand = dealerHand.Count;
                Card dealerCard2 = deck.TakeTopCard();
                dealerCard2.FlipOver();
                dealerCard2.X = WindowWidth - HorizontalCardOffset;
                dealerCard2.Y = TopCardOffset + numberOfCardsDealerHand * VerticalCardSpacing;
                dealerHand.Add(dealerCard2);
                dealerHit = true;
                if (GetBlockjuckScore(dealerHand) >= MaxDealerHitValue && GetBlockjuckScore(dealerHand) <= MaxHandValue)
                {
                    dealerDone = true;
                }
                else if (GetBlockjuckScore(dealerHand) > MaxHandValue)
                {
                    dealerDone = true;
                    playerDone = true;
                }
                Game1.ChangeState(GameState.CheckingHandOver);
                break;

            // case checking hand over

            case (GameState.CheckingHandOver):

                if (!playerDone && GetBlockjuckScore(playerHand) <= MaxHandValue)
                {
                    dealerHit = false;
                    playerHit = false;
                    Game1.ChangeState(GameState.WaitingForPlayer);
                }
                else if (playerDone && !dealerDone)
                {
                    dealerHit = false;
                    playerHit = false;
                    Game1.ChangeState(GameState.WaitingForDealer);
                }
                else
                {
                    if (GetBlockjuckScore(playerHand) <= MaxHandValue &&
                        (GetBlockjuckScore(dealerHand) > MaxHandValue ||
                         dealerDone && playerDone && GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand)))
                    {
                        winnerText    = "YOU WON!";
                        winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation);
                        messages.Add(winnerMessage);
                    }
                    else if (GetBlockjuckScore(dealerHand) <= MaxHandValue &&
                             (GetBlockjuckScore(playerHand) > MaxHandValue ||
                              dealerDone && playerDone &&
                              GetBlockjuckScore(playerHand) < GetBlockjuckScore(dealerHand)))
                    {
                        winnerText    = "DEALER WON!";
                        winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation);
                        messages.Add(winnerMessage);
                    }
                    else if (GetBlockjuckScore(dealerHand) > MaxHandValue &&
                             GetBlockjuckScore(playerHand) > MaxHandValue ||
                             dealerDone && playerDone &&
                             GetBlockjuckScore(dealerHand) == GetBlockjuckScore(playerHand))
                    {
                        winnerText    = "TIE!";
                        winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation);
                        messages.Add(winnerMessage);
                    }

                    dealerHand[0].FlipOver();
                    dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(),
                                                     messageFont,
                                                     new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset));
                    messages.Add(dealerScoreMessage);
                    for (int i = 0; i < menuButtons.Count; i++)
                    {
                        menuButtons.RemoveAt(i);
                    }
                    MenuButton exitButton0 = new MenuButton(quitButtonSprite,
                                                            new Vector2(HorizontalMenuButtonOffset, TopMenuButtonOffset + VerticalMenuButtonSpacing),
                                                            GameState.Exiting);
                    menuButtons.Add(exitButton0);
                    Game1.ChangeState(GameState.DisplayingHandResults);
                }
                break;

            // case for exit
            case (GameState.Exiting):

                this.Exit();
                break;
            }

            base.Update(gameTime);
        }
Example #3
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            MouseState mouse = Mouse.GetState();

            // update menu buttons as appropriate
            if (currentState == GameState.WaitingForPlayer)
            {
                foreach (MenuButton button in menuButtons)
                {
                    button.Update(mouse);
                }
            }
            else if (currentState == GameState.DisplayingHandResults)
            {
                foreach (MenuButton button in menuButtons)
                {
                    button.Update(mouse);
                }
            }

            // game state-specific processing
            switch (currentState)
            {
            case GameState.PlayerHitting:
                Card cardPlayer = deck.TakeTopCard();
                cardPlayer.FlipOver();
                cardPlayer.X = HORIZONTAL_CARD_OFFSET;
                cardPlayer.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * playerHand.Count);
                playerHand.Add(cardPlayer);
                playerScoreMessage.Text = SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString();
                playerHit = true;
                Game1.ChangeState(GameState.WaitingForDealer);
                break;

            case GameState.WaitingForDealer:
                if (GetBlackjackScore(dealerHand) <= 16)
                {
                    Game1.ChangeState(GameState.DealerHitting);
                }
                else
                {
                    Game1.ChangeState(GameState.CheckingHandOver);
                }
                break;

            case GameState.DealerHitting:
                Card cardDealer = deck.TakeTopCard();
                cardDealer.FlipOver();
                cardDealer.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET;
                cardDealer.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * dealerHand.Count);
                dealerHand.Add(cardDealer);
                dealerHit = true;
                Game1.ChangeState(GameState.CheckingHandOver);
                break;

            case GameState.CheckingHandOver:
                int playerScore = GetBlackjackScore(playerHand);
                int dealerScore = GetBlackjackScore(dealerHand);
                if (playerScore > MAX_HAND_VALUE ||
                    dealerScore > MAX_HAND_VALUE ||
                    (!playerHit && !dealerHit))
                {
                    String resultText;

                    //Player and dealer busted
                    if (playerScore > MAX_HAND_VALUE &&
                        dealerScore > MAX_HAND_VALUE)
                    {
                        resultText = "It's a tie";
                    }
                    //Dealer busted
                    else if (dealerScore > MAX_HAND_VALUE)
                    {
                        resultText = "Player won";
                    }
                    //Player busted
                    else if (playerScore > MAX_HAND_VALUE)
                    {
                        resultText = "Dealer won";
                    }
                    // No one was busted, so we check the scores
                    else if (playerScore > dealerScore)
                    {
                        resultText = "Player won";
                    }
                    else if (playerScore < dealerScore)
                    {
                        resultText = "Dealer won";
                    }
                    else     //playerScore == dealerScore
                    {
                        resultText = "It's a tie";
                    }
                    messageFont = Content.Load <SpriteFont>("Arial24");
                    Message resultMessage = new Message(resultText,
                                                        messageFont,
                                                        winnerMessageLocation);
                    messages.Add(resultMessage);
                    //Flip over the dealer first card
                    dealerHand[0].FlipOver();
                    //Create message for dealer score
                    Message dealerScoreMessage = new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(dealerHand).ToString(),
                                                             messageFont,
                                                             new Vector2(WINDOW_WIDTH - HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET));
                    messages.Add(dealerScoreMessage);
                    //remove hit and stand buttons
                    menuButtons.Clear();
                    //Add quit button
                    MenuButton hitButton = new MenuButton(
                        quitButtonSprite,
                        new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, QUIT_MENU_BUTTON_OFFSET),
                        GameState.Exiting);
                    menuButtons.Add(hitButton);
                    //change game state
                    Game1.ChangeState(GameState.DisplayingHandResults);
                }
                else
                {
                    playerHit = false;
                    dealerHit = false;
                    Game1.ChangeState(GameState.WaitingForPlayer);
                }
                break;

            case GameState.Exiting:
                this.Exit();
                break;
            }

            base.Update(gameTime);
        }