Package: terminal-playing-cards
terminal_playing_cardsPython 📦 for building playing card games in the terminal.Checkout how pretty these cards look in a terminal window!Checkout thisblackjackrepo for an example of how to useterminal_playing_cardsto create python card games that run in a terminal window.Getting startedCustomize a ViewNegative spacingSortingAdding cardsRemoving cardsCustomize a DeckCustomize card valuesHidden cardsAdd Joker cardsRemove face card emojisBuilt-in methodsMath with CardsIterate through a Deck/ViewCheck the length of a Deck/ViewGetting started:arrow_down: Install the package from PyPI.pip install terminal_playing_cards:hammer: Build a standard 52 playing card deck.fromterminal_playing_cardsimportDeckdeck=Deck():arrows_counterclockwise: Shuffle the deck, deal out some cards, and convert the list of cards into aViewthat can be printed to the terminal.>>>fromterminal_playing_cardsimportView>>>deck.shuffle()# Deal 5 cards>>>player_1_hand=View([deck.pop()for_inrange(5)])>>>print(player_1_hand)Customize a ViewNegative spacingIf you anticipate having a lot of cards in a singleView, specify a negative value for theView.spacingattribute.>>>player_1_hand=View(...cards=[deck.pop()for_inrange(5)],...spacing=-5...)>>>print(player_1_hand)SortingWhenever I play card games, I have a particular way I like to sort my hand. Use theView.sort()method to sort aViewby value, suit, or both!>>>player_1_hand.sort()>>>print(player_1_hand)If you prefer to sort purely by value, specify that insort_order.>>>player_1_hand.sort(sort_order=["value"])>>>print(player_1_hand)Seehelp(View.sort)for further details.Adding cardsAdding cards to aViewis easy! Just deal another card from the deck, and add it to the existing view.>>>next_card=deck.pop()>>>player_1_hand+=[next_card]>>>print(player_1_hand)Notice that this was done with alistofCardobjects. This allows multiple cards to be added into aViewat one time, and also allows twoViewobjects to be added together.Removing cardsSinceViewinherits many of it's methods fromDeck, use theView.pop()to kick cards out of aView>>>played_card=player_1_hand.pop()>>>print(played_card)And now our hand doesn't have that card in it anymore.>>>print(player_1_hand)Customize a DeckCustomize card valuesIn a game likeBlackjack, face cards are all valued at ten. But the default deck specifications assign a jack a value of eleven. Set thespecificationsparameter to customize options like card values.>>>blackjack_deck=Deck(specifications=["face_cards"])Seehelp(Deck)for more details on how to customizeDeckbuild specifications.Hidden cardsIf you were going to build a game likeTexas hold'emthen you would want to hide the turn and the river cards by default.When building theDeck, set thehiddenparameter to hide all cards  when printed to the terminal by default.>>>hidden_deck=Deck(hidden=True)>>>top_card=hidden_deck.pop()>>>print(top_card)This ensures that that "back" of a card is printed to the terminal, rather than the regular face. In order to reveal this card, set theCard.hiddenattribute toFalse.>>>top_card.hidden=False>>>print(top_card)Add Joker cardsJokersaren't included in aDeckby default. Add them in after the fact if you need them for your game.>>>fromterminal_playing_cardsimportCard>>>deck=Deck()>>>jokers=[Card("JK",suit="none")for_inrange(2)]>>>deck+=jokers>>>print(deck[53])Remove face card emojisOn some terminal windows, emoji's don't print out as expected. Check out how the queen of hearts prints out in thecmderconsole emulator.:rage: Why is it messed up??? :rage:Never fear! If this happens on your terminal, make sure to setpictureparameter to False when building theDeck.>>>no_pic_deck=Deck(picture=False)# Default position of queen of hearts in a sorted Deck>>>queen_hearts=no_pic_deck[47]>>>print(queen_hearts):+1: That's more like it!Built-in methodsMath with CardsAll cards have aCard.valueattribute that is used for logical comparisions and simple arithmetic with numbers and otherCardobjects.>>>fromterminal_playing_cardsimportCard>>>ace_spades=Card("A","spades",value=1)>>>ace_hearts=Card("A","hearts",value=1)>>>two_hearts=Card("2","hearts",value=2)>>>ace_spades<two_heartsTrue>>>ace_spades==ace_heartsTrue>>>two_hearts-11>>>sum([ace_spades,ace_hearts,two_hearts])4Note: Equality is based onvalue alone. This is why theace_spadesis equal to theace_heartseven though they have different suits.Iterate through a Deck/ViewIteration is simple, treat aDeck/Viewas a list ofCardobjects.>>>deck=Deck()>>>hand=View([deck.pop()for_inrange(3)])>>>forcardinhand:...print(repr(card))Card('A','clubs',value=1,hidden=False,picture=True)Card('A','diamonds',value=1,hidden=False,picture=True)Card('A','spades',value=1,hidden=False,picture=True)>>>forcardindeck[:3]:...print(repr(card))Card('A','hearts',value=1,hidden=False,picture=True)Card('2','clubs',value=2,hidden=False,picture=True)Card('2','diamonds',value=2,hidden=False,picture=True)Check the length of a Deck/ViewEasy-peasy.>>>deck=Deck()>>>len(deck)52>>>hand=View([deck.pop()for_inrange(3)])>>>len(hand)3