python tkinter 21点游戏,加两个变量时出错

2024-05-14 09:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

from __future__ import division 
from Tkinter import *
import tkMessageBox
import os
import subprocess
from random import randrange
from random import shuffle
from random import choice
from time import sleep
import sys, time
from select import select


class MyApp(object):

    a = choice(range(1, 12))
    b = choice(range(1, 12))
    money = 100
    bet = 5
    hand = 0


    def __init__(self):
        self.root = Tk()
        self.root.wm_title("The Game of 21")
        Label(self.root, text="Balance: %d" %(self.money)).grid(row=0, column=0, sticky=E)
        Label(self.root, text="Choose Bet:").grid(row=2, column=0, sticky=E)
        Button(self.root, text="5", command=self.bet).grid(row = 2, column = 1)  #create a button
        Button(self.root, text="10", command=self.bet).grid(row = 2, column = 2)  #create a button
        Button(self.root, text="25", command=self.bet).grid(row = 2, column = 3)  #create a button
        Button(self.root, text="50", command=self.bet).grid(row = 2, column = 4)  #create a button

        Button(self.root, text="Deal", command=self.gameOn).grid(row = 5, column = 1)  #create a button
        Button(self.root, text="Exit", command=self.root.destroy).grid(row = 5, column = 2)  #create a button

        self.root.mainloop()

    def gameOn(self):
        """Setup the application's main window as a 2x5 grid"""
        self.root = Tk()
        self.root.wm_title("The Game of 21")
        self.bet = DoubleVar()

        self.cards = StringVar()
        Label(self.root, text = 'Cards dealt:').grid(row = 1, column = 0, sticky = W) #cards dealt
        Label(self.root, text = self.a).grid(row = 1, column = 1, sticky = E)        # card 1 (aka card a)
        Label(self.root, text = ',').grid(row = 1, column = 2)      # inserting a comma between card1 and card2
        Label(self.root, text = self.b).grid(row = 1, column = 3)        # card 2 (aka card b)
        Label(self.root, text = 'Total = %d' %(self.a + self.b + self.hand)).grid(row = 1, column = 4)  # card 1 + card 2
        Label(self.root, text = "Time remaining:").grid(row = 3, column = 1) 

        Button(self.root, text="Hit", command=self.hit).grid(row = 2, column = 0)  #create a button
        Button(self.root, text="Stand" , command=self.stand).grid(row = 2, column = 1) # create a button




#   def bet(betAmount):
#       self.bet = 10

    def hit(self):
        self.hand = choice(range(1, 12))  
        total = self.hand + self.a + self.b 
        dealerHand = choice(range(14,25))


        self.root.destroy()

        result = "Dealer wins. You lose."
        if dealerHand > 21 and total < 22: 
            self.money = self.money - self.bet
            result = "Winner! Winner! Chicken Dinner!"
        if total > 21:
            result
            self.money = self.money - self.bet
        elif total > dealerHand:
            self.money = self.money + self.bet
            result = "Winner! Winner! Chicken Dinner!" 
        elif total == dealerHand: 
            self.money = self.money - self.bet
            result = "Dealer wins. You lose." 
        tkMessageBox.showinfo("Results" , "Hand = %d\nDealer = %d\n%s" %((self.hand + self.a + self.b), (dealerHand), result)) # print result


    def stand(self): 
        self.hand = 0
        total = self.hand + self.a + self.b 
        dealerHand = choice(range(14,25))    
        self.root.destroy()

        result = "Dealer wins. You lose."
        if dealerHand > 21 and total < 22: 
            self.money += bet
            result = "Winner! Winner! Chicken Dinner!"
        if total > 21: 
            self.money += bet
            result
        elif total > dealerHand: 
            self.money += bet
            result = "Winner! Winner! Chicken Dinner!" 
        elif total == dealerHand: 
            self.money += bet
            result = "Dealer wins. You lose." 
        print self.money
        tkMessageBox.showinfo("Results" , "Your hand: %d\nDealer: %d\n%s" %((self.hand + self.a + self.b), (dealerHand), result)) # print result




MyApp()

基本上我要做的是创建一个21点类型的游戏,我想有四个按钮(我已经做了),当你点击它们时,它会改变bet变量。我的问题是,即使我试图将默认变量设置为5,它也不会起作用;我得到了一个TypeError。我也不知道如何更新第一个窗口的“余额”随着资金的上升和下降。很抱歉,这是一个漫长的一天,我想我已经尽可能多地自己做了。在

编辑:弹出的错误是:

^{pr2}$

Tags: textimportselfcolumnbuttonrootresultlabel

热门问题