- 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.
推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Python概率統計
- 看透JavaScript:原理、方法與實踐
- Getting Started with SQL Server 2012 Cube Development
- Mastering Ext JS
- HTML5從入門到精通 (第2版)
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- Instant Nancy Web Development
- Learning VMware vSphere
- Node.js 6.x Blueprints
- Flask開發Web搜索引擎入門與實戰
- 企業級Java現代化:寫給開發者的云原生簡明指南
- Swift 2 Design Patterns
- C++從零開始學(視頻教學版)(第2版)
- 現代JavaScript編程:經典范例與實踐技巧