An Introduction to Interactive Programming in Python 第六周作业

第一次使用画布,还是比较新奇。

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值