所以我正在为学校项目创建一个二十一点游戏。我想做的是让用户玩二十一点游戏以及让他/她使用其所有功能。在过去的三天里,我一直在试图弄清楚如何将对话框放入我的下拉菜单中,而我却无法让它们工作。我从教科书,youtube教程,这个网站和谷歌尝试了一切,但仍然没有帮助。如果有人可以帮助我,我会非常感激!到目前为止,这是我的代码:
from Tkinter import * #Imports all definitions from tkinter
import tkMessageBox
import random
class BlackJack:
def __init__(self):
#player 1 is you, player 0 is dealer
self.playerNum = 1
self.gameIsPlaying = True
# Create a deck of cards
self.suits = ['Clubs', 'Spades', 'Hearts', 'Diamonds'] # Assigning Suits
self.ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] # Assigning Ranks
self.values = {'Ace':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'Jack':10, 'Queen':10, 'King':10} #Assignign Values
tkMessageBox.showinfo("Welcome!", "Lets play Blackjack!") # Displays message "Lets play Blackjack"
# Creating position of cards
deckPosition = [400,800]
playerPosition = [2,700]
dealerPosition = [2,90]
self.playerHand = {'Clubs':[], 'Spades':[], 'Hearts':[], 'Diamonds':[]} # Assigning layout of player 1 hand
self.dealerHand = {'Clubs':[], 'Spades':[], 'Hearts':[], 'Diamonds':[]} # Assigning layout of Dealers hand
self.window = Tk()
self.window.title("Blackjack 1.0")
#Create a menu bar
menubar = Menu(self.window)
self.window.config(menu = menubar) #Displays the menu bar
#Create Canvas
canvas = Canvas(self.window, bg = "dark green", width = 400, height = 500)
canvas.pack()
#Creating frames for widgets
frame1= Frame(self.window)
frame1.pack()
frame2 = Frame(self.window)
Label2 = Label(frame2, text = "Player 1", justify = "left")
Label2.pack()
Deal = Button(frame2, text = "New Game", command = self.deal).pack(side = LEFT) # Button to start a new game
Hit = Button(frame2, text = "Hit", command = self.hit).pack(side = LEFT) # Button to draw another card
Stand= Button(frame2, text = "Stand", command = self.stand).pack(side = LEFT) #Button to stop drawing new cards
frame2.pack()
# Creates background of GUI
Photo = PhotoImage(file = "b2fv.gif") # Uses image from "blackjack.gif" file
label3 = Label(self.window, image = Photo)
label3.pack()# Holds image in thelabel
#Create a pull-down menu
fileMenu = Menu(menubar, tearoff = 0) # Creates file menu
menubar.add_cascade(label = "File", menu = fileMenu)
fileMenu.add_command(label = "Options", command = self.openOptions)
fileMenu.add_command(label = "Exit", command = self.window.quit)
helpMenu = Menu(menubar, tearoff = 0) # Creates help menu
menubar.add_cascade(label = "Help", menu = helpMenu)
helpMenu.add_command(label = "Tutorial", command = self.openTutorial)
helpMenu.add_command(label = "About", command = self.openAbout)
self.window.mainloop() #Create an event loop
# Defines drop down menu
def openOptions(self):
return False
def openTutorial(self):
return False
def openAbout(self):
return False
def deal(self):
self.playerNum = 1
self.playerHand = {'Clubs':[], 'Spades':[], 'Hearts':[], 'Diamonds':[]} # Deals the cards to player 1
self.dealerHand = {'Clubs':[], 'Spades':[], 'Hearts':[], 'Diamonds':[]} # Deals the cards to Dealer
random.seed()
#assign random cards to dealer and player
self.playerHand.get(self.suits[random.randrange(len(self.suits))]).append(self.ranks[random.randrange(len(self.ranks))])
self.playerHand.get(self.suits[random.randrange(len(self.suits))]).append(self.ranks[random.randrange(len(self.ranks))])
self.dealerHand.get(self.suits[random.randrange(len(self.suits))]).append(self.ranks[random.randrange(len(self.ranks))])
self.dealerHand.get(self.suits[random.randrange(len(self.suits))]).append(self.ranks[random.randrange(len(self.ranks))])
print ""
print ""
print "Dealer"
print self.dealerHand
print "Player"
print self.playerHand
def hit(self):
# 0 for dealer, 1 for player
if self.playerNum == 0:
player = self.dealerHand
print 'dealer'
else:
player = self.playerHand
print 'player'
player.get(self.suits[random.randrange(len(self.suits))]).append(self.ranks[random.randrange(len(self.ranks))])
print player
self.assessWin(0)
def stand(self):
self.playerNum = 0
while self.countCardValue(0) < 17:
self.hit()
print "dealer"
print self.dealerHand
self.assessWin(1)
def countCardValue(self, localPlayerNum):
numOfAs = 0
runningSum = 0
# 0 for dealer, 1 for player
if localPlayerNum == 0:
player = self.dealerHand
else:
player = self.playerHand
for suit, ranks in player.items():
for rank in ranks:
if rank == 'A':
numOfAs += 1
runningSum += 11
else:
runningSum += int(self.values.get(rank))
#factor out 10 if runningSum over 21 and numOfAs != 0
while runningSum > 21 and numOfAs != 0:
runningSum -= 10
numOfAs -= 1
return runningSum
def assessWin(self, mode):
#mode 0, to see if you bust
#mode 1, after you stand
if mode == 0:
sum = self.countCardValue(1)
if sum > 21:
print "You Lose"
else:
print sum
else:
if self.countCardValue(0) > 21:
print "You Win"
elif self.countCardValue(1) > 21 or self.countCardValue(0) > self.countCardValue(1):
print "You Lose"
elif self.countCardValue(0) == self.countCardValue(1):
print "Draw"
elif self.countCardValue(0) < self.countCardValue(1):
print "You Win"