- 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.
推薦閱讀
- Vue.js 3.x快速入門
- Python數據分析基礎
- Getting Started with ResearchKit
- Visual Basic程序設計(第3版):學習指導與練習
- 高級C/C++編譯技術(典藏版)
- JSP開發案例教程
- Python編程:從入門到實踐
- INSTANT Passbook App Development for iOS How-to
- Mastering JBoss Enterprise Application Platform 7
- Unreal Engine 4 Shaders and Effects Cookbook
- 軟件品質之完美管理:實戰經典
- 深度學習:Java語言實現
- Mastering Akka
- RESTful Web Clients:基于超媒體的可復用客戶端
- 精通Spring:Java Web開發與Spring Boot高級功能