2012-11-05 29 views
0

時,這是我的代碼:蟒蛇Tkinter的二十一點遊戲,typerror增加兩個變量

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它不會工作;我得到一個TypeError。我也不知道如何更新第一個窗口的「餘額」,因爲金錢上漲和下跌。對不起,這段亂七八糟的代碼是漫長的一天,我想我已經儘可能地完成了自己的工作。

編輯:彈出的錯誤是:

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

您能否顯示確切的錯誤? –

+0

@BryanOakley我不好,我編輯了這個操作。 – iamtesla

回答

1

self.betDoubleVar。爲了獲得bet所持有的數據,您需要self.bet.get()

+0

似乎工作!非常感謝。你有什麼想法,我怎麼能得到按鈕來改變下注變量? – iamtesla

+0

@iamtesla - 我確實 - 有很多不同的方式可以做到。基本上,你需要寫一個函數來接受一個金額並相應地設置賭注:'self.bet.set(amount)'。然後你可以創建一個按鈕:'bet10 = Button(...,command = lambda:self.set_bet(10)'。 – mgilson