0

这是我的代码:

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()

基本上我想做的是创建一个二十一点类型的游戏,我想要四个按钮(我已经做了),当你点击它们时它会改变bet变量。我的问题是即使我尝试将默认变量设置为 5 它也不起作用;我得到一个类型错误。我也不知道如何在资金上下波动时更新第一个窗口上的“余额”。对于凌乱的代码很抱歉,这是漫长的一天,我想我已经尽可能多地自己完成了。

编辑:弹出的错误是:

line 75, in hit
    self.money = self.money - self.bet
TypeError: unsupported operand type(s) for -: 'int' and 'instance'
4

1 回答 1

1

self.bet是一个DoubleVar。为了获取bet所持有的数据,您需要,self.bet.get()

于 2012-11-05T00:33:16.863 回答