第一次使用画布,还是比较新奇。
import simplegui
import random
# load card sprite - 949x392 - source: jfitz.com
CARD_CENTEN=[53,70]
CARD_SIZE =[106,140]
card_images = simplegui.load_image("http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01c0445664fbc432f8754573aaf022.png&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2070432792%2C4061676734%26fm%3D26%26gp%3D0.jpg")
CARD_BACK_SIZE = (106, 140)
CARD_BACK_CENTER = (53, 70)
width = 671
height = 1031
card_back = simplegui.load_image("http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0133ad5913038da801216a3e90f75f.jpg%401280w_1l_2o_100sh.jpg&thumburl=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D2352634760%2C2881261736%26fm%3D26%26gp%3D0.jpg")
# initialize some useful global variables
in_play = False
outcome = ""
player_score = 0
# define globals for cards
SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
# define card class
class Card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
self.suit = None
self.rank = None
print "Invalid card: ", suit, rank
def __str__(self):
return self.suit + self.rank
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def draw(self, canvas, loc):
i = RANKS.index(self.rank)
j = SUITS.index(self.suit)
card_pos = [CARD_CENTEN[0]+i *CARD_SIZE[0],
CARD_CENTEN[1]+j *CARD_SIZE[1]]
canvas.draw_image(card_images,card_pos,CARD_SIZE,loc,CARD_SIZE)
# define hand class
class Hand:
def __init__(self):
self.card_list = []
def add_card(self,add):
if bool(add):
self.card_list.append(add)
def __str__(self):
str1 = "Hand contains"
for i in self.card_list:
str1 += " " + i.suit + i.rank
return str1
def get_value(self):
# count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
score = 0
count = 0
for i in self.card_list:
score += VALUES[i.rank]
if i.rank == "A":
count += 1
if score < 12 and count > 0:
score += 10
return score
def draw(self, canvas, pos):
i = 0
for j in self.card_list:
card = Card(j.suit,j.rank) # draw a hand on the canvas, use the draw method for cards
card.draw(canvas, [pos[0]+i*100 ,pos[1]])
i += 1
# define deck class
class Deck:
def __init__(self):
self.deck_list = []
for i in SUITS:
for j in VALUES:
card = Card(i,j)
self.deck_list.append(card)
def shuffle(self):
random.shuffle(self.deck_list)
def deal_card(self):
i = len(self.deck_list)
if i > 0:
return self.deck_list.pop(random.randrange(i))
def __str__(self):
str1 = "Deck contains"
for k in self.deck_list:
str1 += " " + k.suit +k.rank
return str1
#define event handlers for buttons
def deal():
global outcome, in_play, Card_Deck, player, dealer
if in_play == False:
outcome = ""
player = Hand()
dealer = Hand()
Card_Deck = Deck()
Card_Deck.shuffle()
player.add_card(Card_Deck.deal_card())
player.add_card(Card_Deck.deal_card())
dealer.add_card(Card_Deck.deal_card())
dealer.add_card(Card_Deck.deal_card())
in_play = True
def hit():
global outcome,in_play,player_score
# if the hand is in play, hit the player
if in_play:
if player.get_value() < 21:
player.add_card(Card_Deck.deal_card())
#print "Player's",player,"Score is",player.get_value()
# if busted, assign a message to outcome, update in_play and score
if player.get_value() > 21:
outcome = "You have busted!"
player_score -= 1
in_play = False
elif player.get_value() == 21:
outcome = "Well done!"
in_play = False
def stand():
global player_score,outcome,in_play
if in_play == True:
if player.get_value() > 21:
outcome = "You have busted!"
player_score -= 1
in_play == False
return 0
while dealer.get_value() < 17:
dealer.add_card(Card_Deck.deal_card())
if dealer.get_value() > 21:
outcome = "Dealer has busted!"
player_score += 1
in_play = False
else:
if dealer.get_value() >= player.get_value():
outcome = "You lose!"
player_score -= 1
else:
outcome = "You win!"
player_score += 1
in_play = False
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
dealer.draw(canvas,[75,130])
player.draw(canvas,[75,300])
if in_play:
#print 1
canvas.draw_image(card_back,
[width //2 ,height // 2],[width,height],
[75 ,130],[95,129])
else:
canvas.draw_text("Dealer:"+str(dealer.get_value()), (25, 50), 45, 'White')
canvas.draw_text("Player:"+str(player.get_value()), (25, 450), 45, 'White')
canvas.draw_text(outcome, (200, 500), 45, 'White')
canvas.draw_text(str(player_score), (400, 75), 45, 'White')
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("Green")
#create buttons and canvas callback
frame.add_button("Deal", deal, 200)
frame.add_button("Hit", hit, 200)
frame.add_button("Stand", stand, 200)
frame.set_draw_handler(draw)
# get things rolling
deal()
frame.start()