これは私のコードです:
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()
基本的に私がしようとしているのは、ブラックジャック タイプのゲームを作成することです。4 つのボタン (作成済み) が必要で、それらをクリックするとbet
変数が変更されます。私の問題は、デフォルトの変数を5に設定しようとしても機能しないことです。TypeError が発生します。また、お金が上下するときに最初のウィンドウで「残高」を更新する方法もわかりません。厄介なコードで申し訳ありませんが、長い一日でした。自分でできる限りのことをしたと思います。
編集:ポップアップするエラーは次のとおりです。
line 75, in hit
self.money = self.money - self.bet
TypeError: unsupported operand type(s) for -: 'int' and 'instance'