Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_playing cards.py ZZZ
- from Tkinter import *
- import sys
- import random
- import time
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- ww = 250
- hh = 400
- root=Tk()
- canvas = Canvas(root,width=ww,height=hh,bg='white')
- canvas.grid(sticky=N+S+E+W)
- CARDS = {}
- FULL_DECK = []
- class Cv():
- keyb = 0
- cv = Cv()
- def show_card(value, suit):
- canvas.delete('all')
- img = CARDS[value, suit]
- canvas.create_image(0, 0, anchor=NW, image=img)
- def build_deck(value, suit, polygon):
- if suit == "hearts":
- fill_color = "red"
- elif suit == "diamonds":
- fill_color = "red"
- else:
- fill_color = "black"
- im = Image.new("RGB", (ww, hh))
- t = font_size
- image = Image.new("RGBA", (ww, hh), None)
- try:
- del draw
- except:
- 0
- draw = ImageDraw.Draw(image)
- draw.text((15, -10), value, fill=fill_color, font=font)
- x = 175
- y = 285
- if value == "10":
- x = 130
- elif value == "J":
- x = 200
- elif value == "Q":
- x = 170
- y = 270
- draw.text((x, y), text=value, fill=fill_color, font=font)
- draw.polygon(polygon, fill=fill_color)
- id = ImageTk.PhotoImage(image)
- CARDS[value, suit] = id
- FULL_DECK.append((value, suit))
- def deck_data():
- base_cards = [
- 'A', '2', '3', '4', '5', '6', '7',
- '8', '9', 'X', 'J', 'Q', 'K' ]
- hearts = [(0, 113), (-60, 53), (-90, 8), (-105, -52), (-90, -97), (-60, -112), (-30, -112), (-15, -97), (0, -67), (15, -97), (30, -112), (60, -112), (90, -97), (105, -52), (90, 8), (60, 53)]
- clubs = [(-15, -7), (-45, -22), (-75, -22), (-105, 8), (-105, 38), (-90, 68), (-60, 83), (-30, 68), (0, 38), (-30, 113), (30, 113), (0, 38), (30, 68), (60, 83), (90, 68), (105, 38), (105, 8), (75, -22), (45, -22), (15, -7), (45, -37), (60, -67), (45, -97), (15, -112), (-15, -112), (-45, -97), (-60, -67), (-45, -37)]
- spades = [(0, -112), (-60, -67), (-90, -22), (-105, 23), (-90, 68), (-60, 83), (-30, 83), (-15, 68), (0, 38), (-30, 113), (30, 113), (0, 38), (15, 68), (30, 83), (60, 83), (90, 68), (105, 23), (90, -22), (60, -67)]
- diamonds = [(0, -112), (-75, 0), (0, 113), (75, 0)]
- suits = {
- "diamonds": diamonds,
- "hearts": hearts,
- "spades": spades,
- "clubs": clubs,
- }
- for key, value in suits.items():
- value = [(xx*0.8+ww/2,yy*0.8+hh/2+5) for xx,yy in value]
- for card in base_cards:
- build_deck(card, key, value)
- print("your deck is complete!")
- font_size = 100
- font = ImageFont.truetype("tahoma.ttf", font_size)
- deck_data()
- def keyID(event):
- try:
- ord(event)
- t = (event.char).lower()
- except:
- t = event.keycode
- return t
- def onKeyPress(event):
- cv.keyb = keyID(event), 1
- def onKeyReleased(event):
- cv.keyb = keyID(event), 0
- root.bind('<KeyPress>', onKeyPress)
- L = len(FULL_DECK)
- i = 0
- show_card('A', 'hearts')
- while 1:
- if cv.keyb:
- value, suit = FULL_DECK[i]
- show_card(value, suit)
- i = (i+1)%L
- cv.keyb = 0
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement