本文为coursera中An Introduction to Interactive Programming in Python第六周的project代码,勉强能用,请大家批评指导。
# -*- coding: cp936 -*-
# Mini-project #6 - Blackjack
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import random
# load card sprite - 949×392 - source: jfitz.com
CARD_SIZE = (75,116)
CARD_DIS = (77, 116)
CARD_CENTER = (38,59)
card_images = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Blackjack\\Full.jpg")
CARD_BACK_SIZE = (220,340)
CARD_BACK_CENTER = (120,179)
card_back = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Blackjack\\background.jpg")
# initialize some useful global variables
in_play = False
outcome = ""
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,pos):
card_loc = (CARD_CENTER[0] + CARD_DIS[0] * RANKS.index(self.rank),
CARD_CENTER[1] + CARD_DIS[1] * SUITS.index(self.suit))
canvas.draw_image(card_images,card_loc,CARD_SIZE,[pos[0] + CARD_CENTER[0],pos[1] + CARD_CENTER[1]],CARD_SIZE)
# define hand class
class Hand:
def __init__(self):
self.list = []
def __str__(self):
pass # replace with your code
def add_card(self,card):
self.list.append(card)
# count aces as 1, if the hand has a ace, then add 10 to hand value if it doesn't bust
def get_value(self):
hand_value = 0
hand_ace = 0
for hand_card in self.list:
hand_rank = hand_card.rank
if hand_rank == 'A':
hand_ace = 1
hand_value += VALUES[hand_rank]
if hand_ace == 0:
return hand_value
elif hand_value + 10 <= 21:
return hand_value + 10
else:
return hand_value
def busted(self):
pass # replace with your code
def draw(self,canvas,p):
pass # replace with your code
# define deck class
class Deck:
def __init__(self):
self.list = []
for suit in SUITS:
for rank in RANKS:
card = Card(suit,rank)
self.list.append(card)
def shuffle(self):
random.shuffle(self.list)
def deal_card(self):
if len(self.list) > 0:
pop_card = self.list.pop(0)
self.list.append(pop_card)
return pop_card
else:
random.shuffle(self.list)
# define event handlers for buttons
def deal():
global outcome, in_play, deck, player, dealer
deck.shuffle()
player = Hand()
dealer = Hand()
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
in_play = True
def hit():
global score,in_play,outcome
player_value = player.get_value()
if in_play == True:
card = deck.deal_card()
player.add_card(card)
if player.get_value() > 21:
outcome = 'You have busted and lose'
score = score - 1
in_play = False
def stand():
global score,outcome,in_play
if in_play == True:
player_value = player.get_value()
dealer_value = dealer.get_value()
while(dealer_value < 17):
card = deck.deal_card()
dealer.add_card(card)
dealer_value = dealer.get_value()
if dealer_value > 21:
outcome = 'The dealer have busted and You win'
score = score + 1
elif player_value > dealer_value:
outcome = 'You win'
score = score + 1
else:
outcome = 'You lose'
score = score - 1
in_play = False
# draw handler
def draw(canvas):
canvas.draw_text('Ma Junyang', (100,100),50,'Red')
canvas.draw_text('Dealer',(80,170),35,'Black')
canvas.draw_text('Player',(80,370),35,'Black')
canvas.draw_text('Score '+str(score),(400,100),35,'Black')
dealer_pos = [80,200]
player_pos = [80,400]
for player_card in player.list:
player_card.draw(canvas, player_pos)
player_pos[0] += 120
if in_play == False:
canvas.draw_text('New deal ?',(250,370),35,'Black')
for dealer_card in dealer.list:
dealer_card.draw(canvas, dealer_pos)
dealer_pos[0] += 120
canvas.draw_text(outcome,(250,170),35,'Black')
else:
canvas.draw_text('Hit or stand ?',(250,370),35,'Black')
canvas.draw_image(card_back,CARD_BACK_CENTER,CARD_BACK_SIZE,\
[dealer_pos[0] + CARD_CENTER[0],dealer_pos[1] + \
CARD_CENTER[1]],CARD_SIZE)
for dealer_card in dealer.list[1:len(dealer.list)]:
dealer_pos[0] += 120
dealer_card.draw(canvas, dealer_pos)
# initialization frame
frame = simplegui.create_frame("Blackjack", 800, 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)
# deal an initial hand
deck = Deck()
player = Hand()
dealer = Hand()
deal()
# get things rolling
frame.start()