c ++纸牌游戏,当创建新的圆形牌组/新轮次开始时,手牌不止一次

时间:2014-09-14 08:07:41

标签: c++

我正在编写一个视频扑克游戏,并且我在绘制一只手时遇到问题,然后在新一轮开始时被替换。

这个想法是你开始使用五张牌,你选择要保留哪些牌,然后当你点击"交易"时,其他牌都会被切换出来,然后你会看到你的新牌并告诉你&#34 39;赢了,之后你会被要求开始新一轮,当你点击"新一轮"之前使用的牌组应该被丢弃,从该牌组中取出一只新牌,然后将其拉到屏幕上。

前两件事情有效,问题是当我点击"新一轮"它很快将手拉到屏幕上,然后用另一只手替换它,这不会影响玩家的赌注,他们拥有的钱,绝对没有,我花了一段时间注意到它实际发生了。 / p>

我无法发布一个工作示例,这需要上传整个游戏(我的代码不是很优雅),但我会尝试显示相关文字。

主:

int main(int argc, char *argv[])
{
    srand(time(NULL));

    //load static cards
    SDL_Surface* deal_card = load_surface("resources/images/cards/misc/deal.png");
    SDL_Surface* round_card = load_surface("resources/images/cards/misc/new_round.png");
    SDL_Surface* held = load_surface("resources/images/cards/effect/held.png");

    //initiate standard sdl modules
    if(!init())
    {
        printf("fail init");
    }
    //initiate SDL_ttf
    else if(TTF_Init() == -1)
    {
        printf("TTF INit fail");
    }
    else
    {
        //should exit
        bool quit = false;

        //events
        SDL_Event e;

        //font and font colour to be used for rendering text1
        TTF_Font* font = TTF_OpenFont("resources/fonts/OpenSans-Regular.ttf", 18);
        SDL_Color text_colour = {236, 251, 100};

        //create a new deck, draw out a hand, sort it numerically, setup images and positions for cards
        vector<card>my_deck = new_shuffled_deck();
        vector<card>my_hand = hand(my_deck);
        sort_hand(my_hand);
        setup_hand(my_hand);

        //should switch cards that are not held and remove those used
        //must be TRUE on start otherwise the first deal will duplicate cards
        bool switch_hand = true;
        int round_number = 1;

        //get or set bet information
        read_bet(player_pot, cash_borrowed);

        while(!quit)
        {
            //starting mouse position
            int mouse_x_pos = 0;
            int mouse_y_pos = 0;
            //push current mouse position to starting mouse positions
            SDL_GetMouseState(&mouse_x_pos, &mouse_y_pos);

            //set up to blit hold icon
            update_hold_position(my_hand);

            //check for winning hand
            winning_hand hand_details = my_scores.card_check(my_hand, bet_amount);
            //setup render and blit text
            render_and_blit_text(font, hand_details, player_pot, cash_borrowed, text_colour);
            scoring_text(font, hand_details, text_colour);

            //switch out cards that are not held
            if(switch_hand == true)
            {
                swap_cards(my_hand, my_deck);
            }
            switch_hand = false;

            while(SDL_PollEvent(&e) != 0)
            {
                if(e.type == SDL_QUIT)
                {
                    quit = true;
                }
                if(e.type == SDL_MOUSEBUTTONDOWN)
                {
                    //set mouse position to carry over without resetting
                    int n_mouse_pos_x = mouse_x_pos;
                    int n_mouse_pos_y = mouse_y_pos;
                    //check if card is clicked, if is selected de-select, if not selected then select
                    for(size_t cpc = 0; cpc < my_hand.size(); cpc++)
                    {
                        // if mouse position is in range of left side of card and right side of card
                        if(n_mouse_pos_x > my_hand[cpc].position.x and n_mouse_pos_x < my_hand[cpc].position.x + my_hand[cpc].image->w &&
                           n_mouse_pos_y > my_hand[cpc].position.y and n_mouse_pos_y < my_hand[cpc].position.y + my_hand[cpc].image->h)
                        {
                            //if clicked un-click, if un-clickde click
                            if(my_hand[cpc].selected == 0)
                            {
                                my_hand[cpc].selected = 1;
                            }
                            else if(my_hand[cpc].selected == 1)
                            {
                                my_hand[cpc].selected = 0;
                            }
                        }
                    }
                    //if deal is clicked
                    if(n_mouse_pos_x > deal_rect.x and n_mouse_pos_x < deal_rect.x + deal_card->w &&
                       n_mouse_pos_y > deal_rect.y and n_mouse_pos_y < deal_rect.y + deal_card->h)
                    {
                        //switch held cards, if last round switch entire hand, update cash
                        deal_clicked(switch_hand, round_number, my_hand, my_deck, cash_borrowed, player_pot, amount_won,
                                     bet_amount, hand_details);
                    }
                }
            }
            //blit section
            //blit cards to screen
            blit_cards(my_hand, round_number, held, screen_surface, deal_rect, round_card, deal_card);

            SDL_Surface* fill_screen;
            fill_screen = SDL_CreateRGBSurface(0, screen_width, screen_height, 32, 0, 0, 0, 0);
            SDL_UpdateWindowSurface(window);
            SDL_FillRect(screen_surface, 0, SDL_MapRGB(fill_screen->format, 18, 17, 233));

            SDL_FreeSurface(fill_screen);

            SDL_Delay(30);
        }
    }
    close();
    return 0;
}

交换卡:

void swap_cards(vector<card>&my_hand, vector<card>&my_deck)
{
    for(size_t b = 0; b < my_hand.size(); b++)
    {
        if(my_hand[b].selected == false)
        {
            SDL_FreeSurface(my_hand[b].image);
            //replace card with card of the same index from the deck
            my_hand[b] = my_deck[b];
            // remove card from deck so it cannot be chosen again
            my_deck.erase(my_deck.begin() + b);
        }
        else
        {
            // this prevents memory leak on held cards, no idea why.
            SDL_FreeSurface(my_hand[b].image);
        }
    }
    //set up images and position for cards again
    setup_hand(my_hand);
}

点击交易:

void deal_clicked(bool &switch_hand, int &round_number, vector<card>&my_hand,  vector<card>&my_deck,
                  int &cash_borrowed, int &player_pot, int &amount_won, int& bet_amount, winning_hand &hand_details)
{
    switch_hand = true;
    round_number++;
    // aka if(round_number % 2 == 0 and round_number != 0)
    if(round_number == 3)
    {
        //free card surface images
        for(size_t d = 0; d < my_hand.size(); d++)
        {
            SDL_FreeSurface(my_hand[d].image);
        }

        vector<card>().swap(my_deck);
        //replace deck with new deck
        my_deck = new_shuffled_deck();

        //draw new hand
        vector<card>().swap(my_hand);
        my_hand = hand(my_deck);
        //sort hand by card number
        sort_hand(my_hand);
        //load images and position cards
        setup_hand(my_hand);

        //set round number back to beginning
        round_number = 1;

        //distribute winnings and take next bet amount
        amount_won = hand_details.hand_score;
        if(cash_borrowed > 0)
        {
            cash_borrowed -= amount_won;
        }
        else if(cash_borrowed < 0)
        {
            player_pot += abs(cash_borrowed);
            cash_borrowed = 0;
        }
        else
        {
            player_pot += amount_won;
        }
        if(player_pot <= 0)
        {
            cash_borrowed +=5;
        }
        else
        {
            player_pot -= bet_amount;
        }
        write_bet(player_pot, cash_borrowed);
    }
}

希望这应该足以让人知道我的问题来自何处。

如果有人想要更多的代码我可以发布它,它会变得更加混乱,这些是我认为可能导致问题的唯一区域,然后我再也无法弄清楚它是什么。

编辑:

解决了对setup_hand函数的重复调用。

1 个答案:

答案 0 :(得分:0)

重复调用setup_hand函数。