Advertisement
here2share

# Tk_playing cards.py

Mar 21st, 2021
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # Tk_playing cards.py ZZZ
  2.  
  3. from Tkinter import *
  4. import sys
  5. import random
  6. import time
  7. from PIL import Image, ImageTk, ImageDraw, ImageFont
  8.  
  9. ww = 250
  10. hh = 400
  11.  
  12. root=Tk()
  13. canvas = Canvas(root,width=ww,height=hh,bg='white')
  14. canvas.grid(sticky=N+S+E+W)
  15.  
  16. CARDS = {}
  17. FULL_DECK = []
  18.  
  19. class Cv():
  20.     keyb = 0
  21. cv = Cv()
  22.  
  23. def show_card(value, suit):
  24.     canvas.delete('all')
  25.     img = CARDS[value, suit]
  26.     canvas.create_image(0, 0, anchor=NW, image=img)
  27.  
  28. def build_deck(value, suit, polygon):
  29.     if suit == "hearts":
  30.         fill_color = "red"
  31.     elif suit == "diamonds":
  32.         fill_color = "red"
  33.     else:
  34.         fill_color = "black"
  35.     im = Image.new("RGB", (ww, hh))
  36.    
  37.     t = font_size
  38.    
  39.     image = Image.new("RGBA", (ww, hh), None)
  40.     try:
  41.         del draw
  42.     except:
  43.         0
  44.     draw = ImageDraw.Draw(image)
  45.  
  46.     draw.text((15, -10), value, fill=fill_color, font=font)
  47.     x = 175
  48.     y = 285
  49.     if value == "10":
  50.         x = 130
  51.     elif value == "J":
  52.         x = 200
  53.     elif value == "Q":
  54.         x = 170
  55.         y = 270
  56.     draw.text((x, y), text=value, fill=fill_color, font=font)
  57.     draw.polygon(polygon, fill=fill_color)
  58.     id = ImageTk.PhotoImage(image)
  59.     CARDS[value, suit] = id
  60.     FULL_DECK.append((value, suit))
  61.  
  62. def deck_data():
  63.     base_cards = [
  64.         'A', '2', '3', '4', '5', '6', '7',
  65.         '8', '9', 'X', 'J', 'Q', 'K' ]
  66.  
  67.     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)]
  68.  
  69.     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)]
  70.  
  71.     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)]
  72.  
  73.     diamonds = [(0, -112), (-75, 0), (0, 113), (75, 0)]
  74.  
  75.     suits = {
  76.         "diamonds": diamonds,
  77.         "hearts": hearts,
  78.         "spades": spades,
  79.         "clubs": clubs,
  80.     }
  81.     for key, value in suits.items():
  82.         value = [(xx*0.8+ww/2,yy*0.8+hh/2+5) for xx,yy in value]
  83.         for card in base_cards:
  84.             build_deck(card, key, value)
  85.     print("your deck is complete!")
  86.  
  87. font_size = 100
  88. font = ImageFont.truetype("tahoma.ttf", font_size)
  89. deck_data()
  90.  
  91. def keyID(event):
  92.     try:
  93.         ord(event)
  94.         t = (event.char).lower()
  95.     except:
  96.         t = event.keycode
  97.     return t
  98.  
  99. def onKeyPress(event):
  100.     cv.keyb = keyID(event), 1
  101.  
  102. def onKeyReleased(event):
  103.     cv.keyb = keyID(event), 0
  104.  
  105. root.bind('<KeyPress>', onKeyPress)
  106.  
  107. L = len(FULL_DECK)
  108. i = 0
  109. show_card('A', 'hearts')
  110.  
  111. while 1:
  112.     if cv.keyb:
  113.         value, suit = FULL_DECK[i]
  114.         show_card(value, suit)
  115.         i = (i+1)%L
  116.         cv.keyb = 0
  117.     canvas.update()
  118.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement