- Tkinter GUI Programming by Example
- David Love
- 169字
- 2021-08-27 18:49:10
The Card class
The Card class will be the first class we define, as both of our other classes will need to use it. Open up a new file and type the following code:
import random
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __repr__(self):
return " of ".join((self.value, self.suit))
The only import we will need for our game is the random module. This will allow us to shuffle our virtual deck of cards at the beginning of every game.
Our first class will be one representing the playing cards. Each card will have a suit (hearts, diamonds, spades, and clubs) and a value (ace to king). We define the __repr__ function in order to change how the card is displayed when we call print on it. Our function will return the value and the suit, for example, King of Spades. This is all we need to do for a Card.
Next up, we need to create a Deck of these Card classes.
推薦閱讀
- HTML5+CSS3王者歸來
- TypeScript入門與實戰(zhàn)
- TypeScript圖形渲染實戰(zhàn):基于WebGL的3D架構(gòu)與實現(xiàn)
- 軟件項目管理實用教程
- HDInsight Essentials(Second Edition)
- 網(wǎng)站構(gòu)建技術(shù)
- Java:High-Performance Apps with Java 9
- 學習正則表達式
- 程序設(shè)計基礎(chǔ)教程:C語言
- Getting Started with Hazelcast(Second Edition)
- Python 3 數(shù)據(jù)分析與機器學習實戰(zhàn)
- Lift Application Development Cookbook
- SSH框架企業(yè)級應(yīng)用實戰(zhàn)
- HTML5 WebSocket權(quán)威指南
- Java EE 7 Development with WildFly