def __init__(self, topleft, size): self.rect = pg.Rect(topleft, size) self.deck = Deck((20, 20), card_size=(187, 271), infinite=True) self.hand = [] self.hand_len = 0 self.card_index = None self.max_cards = None self.revealing = False self.held_cards = [] self.changing_cards = [] self.waiting = False self.playing = False self.double_up = False self.font = prepare.FONTS["Saniretro"] self.text_size = 30 self.big_text_size = 100 self.line_height = 35 self.text_color = "white" self.big_text_color = "red" self.text_bg_color = "darkblue" self.held_labels = [] self.double_up_labels = [] self.text = " insert coins " self.no_playing_label = Blinker( self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color, ) self.text = " play 1 to 5 coins " self.waiting_label = Blinker( self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color, ) self.card_spacing = 30 self.animation_speed = 170.0 self.elapsed = self.animation_speed self.deal_sound = prepare.SFX["cardplace2"] self.held_sound = prepare.SFX["bingo-ball-chosen"]
def __init__(self, topleft, size): self.rect = pg.Rect(topleft, size) self.deck = Deck((20, 20), card_size=(187, 271), infinite=True) self.hand = [] self.hand_len = 0 self.card_index = None self.max_cards = None self.revealing = False self.held_cards = [] self.changing_cards = [] self.waiting = False self.playing = False self.double_up = False self.font = prepare.FONTS["Saniretro"] self.text_size = 30 self.big_text_size = 100 self.line_height = 35 self.text_color = "white" self.big_text_color = "red" self.text_bg_color = "darkblue" self.held_labels = [] self.double_up_labels = [] self.text = " insert coins " self.no_playing_label = Blinker(self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color) self.text = " play 1 to 5 coins " self.waiting_label = Blinker(self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color) self.card_spacing = 30 self.animation_speed = 170.0 self.elapsed = self.animation_speed self.deal_sound = prepare.SFX["cardplace2"] self.held_sound = prepare.SFX["bingo-ball-chosen"]
def make_help_labels(self, rect): labels = [] text = "Double up ?" label = Blinker(self.font, 100, text, "red", { "centerx": rect.centerx, "top": rect.top }, 700) labels.append(label) text = "If selected card beats dealers, player wins. Ace is highest, two is lowest" label = MultiLineLabel(self.font, self.text_size, text, self.text_color, {"center": rect.center}, align="center") labels.append(label) return labels
class Dealer: def __init__(self, topleft, size): self.rect = pg.Rect(topleft, size) self.deck = Deck((20, 20), card_size=(187, 271), infinite=True) self.hand = [] self.hand_len = 0 self.card_index = None self.max_cards = None self.revealing = False self.held_cards = [] self.changing_cards = [] self.waiting = False self.playing = False self.double_up = False self.font = prepare.FONTS["Saniretro"] self.text_size = 30 self.big_text_size = 100 self.line_height = 35 self.text_color = "white" self.big_text_color = "red" self.text_bg_color = "darkblue" self.held_labels = [] self.double_up_labels = [] self.text = " insert coins " self.no_playing_label = Blinker( self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color, ) self.text = " play 1 to 5 coins " self.waiting_label = Blinker( self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color, ) self.card_spacing = 30 self.animation_speed = 170.0 self.elapsed = self.animation_speed self.deal_sound = prepare.SFX["cardplace2"] self.held_sound = prepare.SFX["bingo-ball-chosen"] def startup(self): self.hand = self.deck.make_hand() self.hand_len = len(self.hand) self.build() self.card_index = 0 self.max_cards = len(self.hand) self.revealing = False self.held_cards = [] self.changing_cards = list(range(5)) self.waiting = False self.playing = False self.double_up = False def start_double_up(self): for index in range(self.hand_len): self.hand[index] = self.deck.draw_card() self.held_cards = [] self.changing_cards = list(range(5)) # first card self.hand[0].face_up = True self.toggle_held(0) self.build() def draw_cards(self): for index in range(self.hand_len): if index not in self.held_cards: self.hand[index] = self.deck.draw_card() self.build() self.revealing = True def build(self): x = self.rect.left y = self.rect.top + self.line_height for index, card in enumerate(self.hand): card.rect.left = x card.rect.top = y label = Label( self.font, self.text_size, "held", self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}, ) self.held_labels.append(label) if index == 0: label = Label( self.font, self.text_size, "dealer", self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}, ) else: label = Label( self.font, self.text_size, "player", self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}, ) self.double_up_labels.append(label) x += self.card_spacing + card.rect.w def toggle_held(self, index): if index in self.held_cards: self.held_cards.remove(index) self.changing_cards.append(index) else: self.held_cards.append(index) self.changing_cards.remove(index) self.held_sound.play() self.changing_cards.sort() def select_card(self, index): self.hand[index].face_up = True self.toggle_held(index) return self.compare_cards(index) def compare_cards(self, index): val1 = self.hand[0].value val2 = self.hand[index].value if val2 == 1: return True if val1 == 1: # if dealer card is an Ace and player isn't an Ace return False else: return not val1 > val2 def evaluate_hand(self): values = [] suits = [] for card in self.hand: values.append(card.value) suits.append(card.suit) values.sort() suits.sort() # if don't match any rank rank = NO_HAND pairs = [] are_three = False for val in values: matches = values.count(val) if matches == 2: if val not in pairs: pairs.append(val) elif matches == 3: if not are_three: are_three = True pairs_len = len(pairs) if pairs_len == 1 and are_three: """Full house""" rank = HAND_RANKS["FULL_HOUSE"] elif pairs_len == 1: """ Jacks or betters""" if 1 in pairs or 11 in pairs or 12 in pairs or 13 in pairs: rank = HAND_RANKS["JACKS_OR_BETTER"] elif pairs_len == 2: """Two pair""" rank = HAND_RANKS["TWO_PAIR"] elif are_three: """Three of a kind""" rank = HAND_RANKS["THREE_OF_A_KIND"] """Straight, if is an Ace in the hand, Check if other 4 cards are K, Q, J, 10 or 2, 3, 4, 5 else Check if 5 cards are continuous in rank""" if 1 in values: a = values[1] == 2 and values[2] == 3 and values[3] == 4 and values[4] == 5 b = values[1] == 10 and values[2] == 11 and values[3] == 12 and values[4] == 13 is_straight = a or b else: test_value = values[0] + 1 is_straight = True for i in range(1, 5): if values[i] != test_value: is_straight = False test_value += 1 highest = max(values) """Flush, previously we sort it, so the array must look like this: ['Clubs', 'Diamonds', 'Diamonds', 'Hearts', 'Hearts'], so if the first and the last element are the same means that all the suits are the same""" if suits[0] == suits[4]: if is_straight: if highest == 13 and 1 in values: rank = HAND_RANKS["ROYAL_FLUSH"] else: rank = HAND_RANKS["STR_FLUSH"] else: rank = HAND_RANKS["FLUSH"] elif is_straight: rank = HAND_RANKS["STRAIGHT"] else: """4 of a kind, Check for: 4 cards of the same value + higher value unmatched card, and Check for: lower ranked unmatched card + 4 cards of the same rank""" a = values[0] == values[1] == values[2] == values[3] b = values[1] == values[2] == values[3] == values[4] if a or b: rank = HAND_RANKS["4_OF_A_KIND"] # and finally return the current rank return rank def get_event(self, mouse_pos): if self.playing: for index, card in enumerate(self.hand): if card.rect.collidepoint(mouse_pos): return index def update(self, dt): if self.revealing: self.elapsed += dt while self.elapsed >= self.animation_speed: self.elapsed -= self.animation_speed if self.changing_cards: index = self.changing_cards[self.card_index] self.hand[index].face_up = True self.deal_sound.play() self.card_index += 1 if self.card_index >= len(self.changing_cards): self.card_index = 0 self.revealing = False if not self.playing: self.no_playing_label.update(dt) if self.waiting: self.waiting_label.update(dt) def draw(self, surface): for card in self.hand: card.draw(surface) for index in self.held_cards: if self.double_up: self.double_up_labels[index].draw(surface) else: self.held_labels[index].draw(surface) if not self.playing: self.no_playing_label.draw(surface) elif self.waiting: self.waiting_label.draw(surface)
def startup(self, game): self.game = game self.game.result_labels = [] self.fade_labels = [] self.alpha = 255 total_ani_time = 2000 fade_ani = Animation(alpha=0, duration=total_ani_time) fade_ani.start(self) self.animations.add(fade_ani) self.game.tally_hands() payout = self.game.pay_out() if payout: self.coin_sound.play() hands = self.game.player.hands if len(hands) > 2: text_size = 64 elif len(hands) == 2: text_size = 80 else: text_size = 96 win_piles = [] loss_piles = [] self.winnings = [] self.losses = [] for hand in hands: amount = hand.bet.get_chip_total() hand.bet_amount = amount bl = hand.bet.stacks[0].rect.bottomleft if hand.blackjack: text, color = "Blackjack", "gold3" text_size -= 8 chips = cash_to_chips(int(amount * 2.5)) amount = int(amount * 1.5) win_piles.append(BetPile(bl, self.game.chip_size, chips)) elif hand.winner: text, color = "Win", "gold3" chips = cash_to_chips(amount * 2) win_piles.append(BetPile(bl, self.game.chip_size, chips)) elif hand.push: text, color = "Push", "gold3" chips = cash_to_chips(amount) win_piles.append(BetPile(bl, self.game.chip_size, chips)) elif hand.busted: text, color = "Bust", "darkred" chips = cash_to_chips(amount) loss_piles.append(BetPile(bl, self.game.chip_size, chips)) amount *= -1 else: text, color = "Loss", "darkred" chips = cash_to_chips(amount) loss_piles.append(BetPile(bl, self.game.chip_size, chips)) amount *= -1 centerx = (hand.slots[0].left + hand.slots[-1].right) // 2 centery = hand.slots[0].centery label = Blinker(self.font, text_size, text, color, {"center": (centerx, centery)}, 450) self.game.result_labels.append(label) amt_color = "darkgreen" if amount >= 0 else "darkred" bet_label = Label(self.font, 120, "{:+}".format(amount), amt_color, {"bottomleft": bl}, bg=prepare.FELT_GREEN) move_ani = Animation(bottom=bl[1] - 150, duration=total_ani_time, round_values=True) move_ani.start(bet_label.rect) self.animations.add(move_ani) self.fade_labels.append(bet_label) hand.bet.chips = [] hand.bet.stacks = [] payout_duration = 1000 center = self.game.player.chip_pile.rect.center for pile in win_piles: self.winnings.append(pile) for stack in pile.stacks: ani = Animation(left=center[0], bottom=center[1], duration=payout_duration, round_values=True) ani.start(stack.rect) self.animations.add(ani) center = self.game.chip_rack.rect.center for loss_pile in loss_piles: self.losses.append(loss_pile) for stack in loss_pile.stacks: ani = Animation(left=center[0], bottom=center[1], duration=payout_duration, round_values=True) ani.start(stack.rect) self.animations.add(ani) pay_ani = Task(self.game.player.chip_pile.add_chips, payout_duration, args=[payout]) remove_chips = Task(self.remove_chips, payout_duration) end_it = Task(self.end_state, total_ani_time) self.animations.add(pay_ani, remove_chips, end_it)
class Dealer: def __init__(self, topleft, size): self.rect = pg.Rect(topleft, size) self.deck = Deck((20, 20), card_size=(187, 271), infinite=True) self.hand = [] self.hand_len = 0 self.card_index = None self.max_cards = None self.revealing = False self.held_cards = [] self.changing_cards = [] self.waiting = False self.playing = False self.double_up = False self.font = prepare.FONTS["Saniretro"] self.text_size = 30 self.big_text_size = 100 self.line_height = 35 self.text_color = "white" self.big_text_color = "red" self.text_bg_color = "darkblue" self.held_labels = [] self.double_up_labels = [] self.text = " insert coins " self.no_playing_label = Blinker(self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color) self.text = " play 1 to 5 coins " self.waiting_label = Blinker(self.font, self.big_text_size, self.text, self.big_text_color, {"center": self.rect.center}, 700, self.text_bg_color) self.card_spacing = 30 self.animation_speed = 170.0 self.elapsed = self.animation_speed self.deal_sound = prepare.SFX["cardplace2"] self.held_sound = prepare.SFX["bingo-ball-chosen"] def startup(self): self.hand = self.deck.make_hand() self.hand_len = len(self.hand) self.build() self.card_index = 0 self.max_cards = len(self.hand) self.revealing = False self.held_cards = [] self.changing_cards = list(range(5)) self.waiting = False self.playing = False self.double_up = False def start_double_up(self): for index in range(self.hand_len): self.hand[index] = self.deck.draw_card() self.held_cards = [] self.changing_cards = list(range(5)) # first card self.hand[0].face_up = True self.toggle_held(0) self.build() def draw_cards(self): for index in range(self.hand_len): if index not in self.held_cards: self.hand[index] = self.deck.draw_card() self.build() self.revealing = True def build(self): x = self.rect.left y = self.rect.top + self.line_height for index, card in enumerate(self.hand): card.rect.left = x card.rect.top = y label = Label(self.font, self.text_size, 'held', self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}) self.held_labels.append(label) if index == 0: label = Label(self.font, self.text_size, 'dealer', self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}) else: label = Label(self.font, self.text_size, 'player', self.text_color, {"bottom": card.rect.top, "centerx": card.rect.centerx}) self.double_up_labels.append(label) x += self.card_spacing + card.rect.w def toggle_held(self, index): if index in self.held_cards: self.held_cards.remove(index) self.changing_cards.append(index) else: self.held_cards.append(index) self.changing_cards.remove(index) self.held_sound.play() self.changing_cards.sort() def select_card(self, index): self.hand[index].face_up = True self.toggle_held(index) return self.compare_cards(index) def compare_cards(self, index): val1 = self.hand[0].value val2 = self.hand[index].value if val2 == 1: return True if val1 == 1: # if dealer card is an Ace and player isn't an Ace return False else: return not val1 > val2 def evaluate_hand(self): values = [] suits = [] for card in self.hand: values.append(card.value) suits.append(card.suit) values.sort() suits.sort() # if don't match any rank rank = NO_HAND pairs = [] are_three = False for val in values: matches = values.count(val) if matches == 2: if val not in pairs: pairs.append(val) elif matches == 3: if not are_three: are_three = True pairs_len = len(pairs) if pairs_len == 1 and are_three: """Full house""" rank = HAND_RANKS['FULL_HOUSE'] elif pairs_len == 1: """ Jacks or betters""" if 1 in pairs or 11 in pairs or 12 in pairs or 13 in pairs: rank = HAND_RANKS['JACKS_OR_BETTER'] elif pairs_len == 2: """Two pair""" rank = HAND_RANKS['TWO_PAIR'] elif are_three: """Three of a kind""" rank = HAND_RANKS['THREE_OF_A_KIND'] """Straight, if is an Ace in the hand, Check if other 4 cards are K, Q, J, 10 or 2, 3, 4, 5 else Check if 5 cards are continuous in rank""" if 1 in values: a = values[1] == 2 and values[2] == 3 \ and values[3] == 4 and values[4] == 5 b = values[1] == 10 and values[2] == 11 \ and values[3] == 12 and values[4] == 13 is_straight = a or b else: test_value = values[0] + 1 is_straight = True for i in range(1, 5): if values[i] != test_value: is_straight = False test_value += 1 highest = max(values) """Flush, previously we sort it, so the array must look like this: ['Clubs', 'Diamonds', 'Diamonds', 'Hearts', 'Hearts'], so if the first and the last element are the same means that all the suits are the same""" if suits[0] == suits[4]: if is_straight: if highest == 13 and 1 in values: rank = HAND_RANKS['ROYAL_FLUSH'] else: rank = HAND_RANKS['STR_FLUSH'] else: rank = HAND_RANKS['FLUSH'] elif is_straight: rank = HAND_RANKS['STRAIGHT'] else: """4 of a kind, Check for: 4 cards of the same value + higher value unmatched card, and Check for: lower ranked unmatched card + 4 cards of the same rank""" a = values[0] == values[1] == values[2] == values[3] b = values[1] == values[2] == values[3] == values[4] if a or b: rank = HAND_RANKS['4_OF_A_KIND'] # and finally return the current rank return rank def get_event(self, mouse_pos): if self.playing: for index, card in enumerate(self.hand): if card.rect.collidepoint(mouse_pos): return index def update(self, dt): if self.revealing: self.elapsed += dt while self.elapsed >= self.animation_speed: self.elapsed -= self.animation_speed if self.changing_cards: index = self.changing_cards[self.card_index] self.hand[index].face_up = True self.deal_sound.play() self.card_index += 1 if self.card_index >= len(self.changing_cards): self.card_index = 0 self.revealing = False if not self.playing: self.no_playing_label.update(dt) if self.waiting: self.waiting_label.update(dt) def draw(self, surface): for card in self.hand: card.draw(surface) for index in self.held_cards: if self.double_up: self.double_up_labels[index].draw(surface) else: self.held_labels[index].draw(surface) if not self.playing: self.no_playing_label.draw(surface) elif self.waiting: self.waiting_label.draw(surface)
def startup(self, game): self.game = game self.lobby_button.call = self.warn self.toggled = False self.alpha = 255 self.labels = [] self.blinkers = [] self.calculated = False self.animations = pg.sprite.Group() stayers = [x for x in self.game.players if x.stayed] winners = self.game.get_winners() share = self.game.pot // len(winners) ani_duration = 2000 self.free_ride = False for stayer in stayers: pos = stayer.name_label.rect.center dest = pos[0], pos[1] - 120 cards_center = stayer.cards[0].rect.union( stayer.cards[1].rect).center if stayer not in winners: text = "${}".format(self.game.pot) color = "darkred" stayer.lost = self.game.pot self.free_ride = True dest = self.game.pot_label.rect.center if stayer is self.game.player: pos = self.money_icon.rect.center if self.game.player.cash < stayer.lost: amount = stayer.lost - self.game.player.cash self.cash_advance(amount) msg = "You were forced to take a cash advance to cover your loss. Visit the ATM to view your account." self.notice(msg) self.add_player_cash(-stayer.lost) task = Task(self.add_to_pot, ani_duration, args=[stayer.lost]) self.animations.add(task) lose_label = Blinker(self.font, 96, "Loser", color, {"center": cards_center}, 500) self.animations.add( Task(self.blinkers.append, ani_duration, args=[lose_label])) else: text = "${}".format(share) color = "darkgreen" stayer.won = share pos = self.game.pot_label.rect.center if stayer is self.game.player: self.cha_ching.play() dest = self.money_icon.rect.center task = Task(self.add_player_cash, ani_duration, args=[share]) self.animations.add(task) win_label = Blinker(self.font, 96, "Winner", color, {"center": cards_center}, 500) self.animations.add( Task(self.blinkers.append, ani_duration, args=[win_label])) label = Label(self.font, 128, text, color, {"center": pos}, bg=prepare.FELT_GREEN) label.image.set_colorkey(prepare.FELT_GREEN) self.labels.append(label) move = Animation(centerx=dest[0], centery=dest[1], duration=ani_duration, round_values=True, transition="in_quart") move.start(label.rect) self.animations.add(move) fader = Animation(alpha=0, duration=ani_duration + 200, round_values=True) fader.start(self) self.animations.add(fader) self.game.pot = 0 self.make_player_buttons(self.free_ride) self.toggle_buttons(False) if self.free_ride: self.advice_texts = ["Press OK to play the next round"] else: self.advice_texts = [ "Ante Up ${} to play again".format(self.game.bet) ] self.advice_texts.append("Press the Lobby button to exit")