1+ import time
2+ import displayio , terminalio , adafruit_imageload
3+ from adafruit_display_text import label
4+ from random import choice as rc
5+
6+ def total (hand ):
7+ if len (hand ) >= 3 and 11 in hand :
8+ t = sum (hand ) - 10
9+ else :
10+ t = sum (hand )
11+ return t
12+
13+ def cardmap (value ):
14+ thisdict = {
15+ 2 : 0 ,
16+ 3 : 1 ,
17+ 4 : 2 ,
18+ 5 : 3 ,
19+ 6 : 4 ,
20+ 7 : 5 ,
21+ 8 : 6 ,
22+ 9 : 7 ,
23+ 10 : 8 ,
24+ "J" : 9 ,
25+ "Q" : 10 ,
26+ "K" : 11 ,
27+ 11 : 12
28+ }
29+ cardIndex = thisdict [value ]
30+ return cardIndex
31+
32+ def playHand (minitft , companbot_x ):
33+ #----------------------------------
34+ bj_screen = displayio .Group (max_size = 8 )
35+ cardslot1 = displayio .Group (max_size = 2 )
36+ cardslot2 = displayio .Group (max_size = 1 )
37+ cardslot3 = displayio .Group (max_size = 1 )
38+ cardslot4 = displayio .Group (max_size = 1 )
39+ cardslot5 = displayio .Group (max_size = 1 )
40+ text_group_bj = displayio .Group (max_size = 3 )
41+ card2BMP = "/bmp/cards.bmp"
42+
43+ color_bitmap = displayio .Bitmap (160 , 80 , 1 )
44+ color_palette = displayio .Palette (1 )
45+ color_palette [0 ] = 0x20c040
46+
47+ bjbgDisp = displayio .TileGrid (color_bitmap , pixel_shader = color_palette , x = 0 , y = 0 )
48+
49+ text_area1_bj = label .Label (terminalio .FONT , text = "Dealing" , color = 0xFFFFFF , max_glyphs = 20 , x = 5 , y = 55 )
50+ text_area2_bj = label .Label (terminalio .FONT , text = "Cards" , color = 0xFFFFFF , max_glyphs = 20 , x = 80 , y = 55 )
51+ text_area3_bj = label .Label (terminalio .FONT , text = " " , color = 0xFFFFFF , max_glyphs = 20 , x = 50 , y = 70 )
52+
53+ card2_SP , card2_Pal = adafruit_imageload .load (card2BMP ,bitmap = displayio .Bitmap ,palette = displayio .Palette )
54+ cardSlot1_Disp = displayio .TileGrid (card2_SP , pixel_shader = card2_Pal , width = 1 , height = 1 , tile_width = 25 , tile_height = 30 , x = 5 , y = 2 )
55+ cardSlot2_Disp = displayio .TileGrid (card2_SP , pixel_shader = card2_Pal , width = 1 , height = 1 , tile_width = 25 , tile_height = 30 , x = 35 , y = 2 )
56+ cardSlot3_Disp = displayio .TileGrid (card2_SP , pixel_shader = card2_Pal , width = 1 , height = 1 , tile_width = 25 , tile_height = 30 , x = 65 , y = 2 )
57+ cardSlot4_Disp = displayio .TileGrid (card2_SP , pixel_shader = card2_Pal , width = 1 , height = 1 , tile_width = 25 , tile_height = 30 , x = 95 , y = 2 )
58+ cardSlot5_Disp = displayio .TileGrid (card2_SP , pixel_shader = card2_Pal , width = 1 , height = 1 , tile_width = 25 , tile_height = 30 , x = 125 , y = 2 )
59+ cardslot1 .append (cardSlot1_Disp )
60+ cardslot2 .append (cardSlot2_Disp )
61+ cardslot3 .append (cardSlot3_Disp )
62+ cardslot4 .append (cardSlot4_Disp )
63+ cardslot5 .append (cardSlot5_Disp )
64+
65+ text_group_bj .append (text_area1_bj )
66+ text_group_bj .append (text_area2_bj )
67+ text_group_bj .append (text_area3_bj )
68+
69+ #text_area2_bj.x = 80
70+ #text_area2_bj.y = 55
71+ #text_area3_bj.x = 50
72+ #text_area3_bj.y = 70
73+
74+ bj_screen .append (bjbgDisp )
75+ bj_screen .append (cardslot1 )
76+ bj_screen .append (cardslot2 )
77+ bj_screen .append (cardslot3 )
78+ bj_screen .append (cardslot4 )
79+ bj_screen .append (cardslot5 )
80+ bj_screen .append (text_group_bj )
81+ #----------------------------------
82+ minitft .display .show (bj_screen )
83+ cards = [2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,10 ,10 ,10 ,11 ]
84+ player = []
85+ # draw 2 cards for the player to start
86+ player .append (rc (cards ))
87+ player .append (rc (cards ))
88+ pbust = False # player busted flag
89+ cbust = False # computer busted flag
90+ playerTurn = True
91+ #text_area1_bj.text = " "
92+ #text_area2_bj.text = " "
93+ cardslot1 .hidden = True
94+ cardslot2 .hidden = True
95+ cardslot3 .hidden = True
96+ cardslot4 .hidden = True
97+ cardslot5 .hidden = True
98+
99+ cardSlot1_Disp [0 ] = cardmap (int (player [0 ]))
100+ cardslot1 .hidden = False
101+ time .sleep (1 )
102+ cardSlot2_Disp [0 ] = cardmap (int (player [1 ]))
103+ cardslot2 .hidden = False
104+ time .sleep (1 )
105+
106+ #Input eater
107+ #time.sleep(0.4)
108+
109+ while playerTurn :
110+ playerInput = False
111+ tp = total (player )
112+ text_area1_bj .text = " "
113+ text_area2_bj .text = " "
114+ text_area3_bj .text = "Total: {}" .format (tp )
115+ time .sleep (2 )
116+ if tp > 21 :
117+ text_area1_bj .text = "Player bust!"
118+ time .sleep (2 )
119+ pbust = True
120+ playerTurn = False
121+ elif tp == 21 :
122+ text_area1_bj .text = "BLACKJACK!!!"
123+ time .sleep (2 )
124+ playerTurn = False
125+ else :
126+ text_area1_bj .text = "Hit (A)"
127+ text_area2_bj .text = "Stand (B)"
128+ while not playerInput :
129+ buttons = minitft .buttons
130+ if buttons .a :
131+ print ("Awaiting User" )
132+ playerInput = True
133+ text_area1_bj .text = "Hit"
134+ text_area2_bj .text = ""
135+ player .append (rc (cards ))
136+ if len (player ) == 3 :
137+ cardslot3 .hidden = False
138+ cardSlot3_Disp [0 ] = cardmap (int (player [2 ]))
139+ time .sleep (1 )
140+ elif len (player ) == 4 :
141+ cardslot4 .hidden = False
142+ cardSlot4_Disp [0 ] = cardmap (int (player [3 ]))
143+ time .sleep (1 )
144+ elif len (player ) == 5 :
145+ cardslot5 .hidden = False
146+ cardSlot5_Disp [0 ] = cardmap (int (player [4 ]))
147+ time .sleep (1 )
148+ playerTurn = False
149+ if buttons .b :
150+ text_area1_bj .text = "Stand"
151+ text_area2_bj .text = ""
152+ time .sleep (1 )
153+ playerInput = True
154+ playerTurn = False
155+ while True :
156+ # loop for the computer's play ...
157+ cardslot1 .hidden = True
158+ cardslot2 .hidden = True
159+ cardslot3 .hidden = True
160+ cardslot4 .hidden = True
161+ cardslot5 .hidden = True
162+ text_area1_bj .text = "Dealer's Turn"
163+ text_area2_bj .text = " "
164+ text_area3_bj .text = " "
165+ comp = []
166+ time .sleep (1 )
167+ comp .append (rc (cards ))
168+ cardslot1 .hidden = False
169+ cardSlot1_Disp [0 ] = cardmap (int (comp [0 ]))
170+ time .sleep (1 )
171+ comp .append (rc (cards ))
172+ cardslot2 .hidden = False
173+ cardSlot2_Disp [0 ] = cardmap (int (comp [1 ]))
174+ time .sleep (1 )
175+ while not pbust or cbust :
176+ tc = total (comp )
177+ text_area3_bj .text = "Total: {}" .format (tc )
178+ if tc < 17 :
179+ comp .append (rc (cards ))
180+ if cardslot3 .hidden :
181+ cardslot3 .hidden = False
182+ cardSlot3_Disp [0 ] = cardmap (int (comp [2 ]))
183+ time .sleep (2 )
184+ elif cardslot4 .hidden and not cardslot3 .hidden :
185+ cardslot4 .hidden = False
186+ cardSlot4_Disp [0 ] = cardmap (int (comp [3 ]))
187+ time .sleep (2 )
188+ elif cardslot5 .hidden and not cardslot4 .hidden :
189+ cardslot5 .hidden = False
190+ cardSlot5_Disp [0 ] = cardmap (int (comp [4 ]))
191+ time .sleep (2 )
192+ else :
193+ break
194+ text_area3_bj .text = " "
195+ # now figure out who won ...
196+ if tc > 21 :
197+ text_area1_bj .text = "Dealer bust!"
198+ cbust = True
199+ if pbust == False :
200+ text_area1_bj .text = "Player wins!"
201+ companbot_x .cred = companbot_x .cred + 20
202+ elif tc > tp :
203+ text_area1_bj .text = "Dealer Wins!"
204+ elif tc == tp :
205+ text_area1_bj .text = "It's a draw!"
206+ elif tp > tc :
207+ if pbust == False :
208+ text_area1_bj .text = "Player wins!"
209+ companbot_x .cred = companbot_x .cred + 10
210+ elif cbust == False :
211+ text_area1_bj .text = "Dealer Wins!"
212+ break
213+ time .sleep (2 )
214+ return companbot_x
0 commit comments