输入b的扭结文字

2024-04-18 04:48:32 发布

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

我正在追踪另一个允许用户在10&100个硬币之间下注的想法。然后需要保存到GUI的下一页。我已经尝试了一些代码,但我不能得到任何东西来覆盖什么是有任何尝试删除它,并添加一个代码,让用户输入他们想下注多少。这样的代码是否可以放入类中以使其与其他代码不同?你知道吗

import tkinter as tk
import random

intro = """Panther's Den Slot Machine.


Welcome to my den!

You can win by rolling Ocelots, Jaguars, Boas, Caimans, Macaws or Tapirs.
You can also with big with three Ibis.

You'll lose a coin for anything else, and if you roll three Scorpions say good bye to 500 coins

Good luck kit!"""


root = tk.Tk()
root.geometry("700x500")
root.title("Slot Machine")
root.configure(background='seagreen')

INIT_STAKE = 100
ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]

first = None
second = None
third = None
stake = INIT_STAKE

nameLabel = tk.Label(root, text="PANTHER DEN", font=('Cambria', 60))
nameLabel.pack()
lbl = tk.Label(root, text=intro, background='seagreen', font=('Cambria', 12))
lbl.pack()
lbl2 = tk.Label(root, text=stake)
lbl2.pack()

def play():
    global first, second, third
    first = spin()
    second = spin()
    third = spin()
    score()

def quit_play():
    lbl.config(text="Game has ended. You won a total of " + str(stake) + " coins")

def spin():
    randomnumber = random.randint(0, 10)
    return ITEMS[randomnumber]

def score():
    global stake, first, second, third
    if((first == "OCELOT") and (second != "MACAW")):
        win = 5
    elif((first == "JAGUAR") and (second == "JAGUAR") and (third != "JAGUAR")):
        win = 8
    elif((first == "BOA") and (second == "BOA") and (third == "BOA")):
        win = 10
    elif((first == "CAIMAN") and (second == "CAIMAN") and ((third == "CAIMAN") or (third == "BOA"))):
        win = 8
    elif((first == "MACAW") and (second == "IBIS") and ((third == "MACAW"))):
        win = 15
    elif((first == "TAPIR") and (second == "TAPIR") and ((third == "TAPIR"))):
        win = 20
    elif((first == "IBIS") and (second == "IBIS") and (third == "IBIS")):
        win = 300
    elif((first == "SCORPION") and (second == "SCORPION") and (third == "SCORPION")):
        win = -500
    else:
        win = -1

    stake += win

    if(win > 0):
        lbl.config(text="{}\t{}\t{} -- You win {} Coins".format(first, second, third, win))
        lbl2.config(text=stake)
    else:
        lbl.config(text="{}\t{}\t{} -- You lose".format(first, second, third))
        lbl2.config(text=stake)


tk.Button(root, text="Play", command=play).pack()
tk.Button(root, text="Quit", command=quit_play).pack()
tk.Button(root, text="Exit", command=quit).pack()

root.mainloop()

Tags: andtextyourootwinpacktkfirst
1条回答
网友
1楼 · 发布于 2024-04-18 04:48:32

你可以这样使用:

from tkinter import *
import random

class RollMachine(Frame):
    def __init__(self, parent=None):
    Frame.__init__(self, parent)
    self.first = self.second = self.third = None
    self.stake = 0
    self.intro = """Panther's Den Slot Machine.


    Welcome to my den!

    You can win by rolling Ocelots, Jaguars, Boas, Caimans, Macaws or Tapirs.
    You can also with big with three Ibis.

    You'll lose a coin for anything else, and if you roll three Scorpions say 
    good bye to 500 coins

    Good luck kit!"""
    self.ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]

    self.makeWidgets()
    self.pack()

def makeWidgets(self):
    nameLabel = Label(self, text="PANTHER DEN", font=('Cambria', 60))
    nameLabel.pack()
    self.lbl = Label(self, text=self.intro, background='seagreen', font=('Cambria', 12))
    self.lbl.pack()
    self.lbl2 = Label(self, text="Enter the stake")
    self.lbl2.pack()
    self.stake_input = Entry(self)
    self.stake_input.pack()
    Button(self, text="Play", command=self.play).pack()
    Button(self, text="Quit", command=self.quit_play).pack()
    Button(self, text="Exit", command=self.quit).pack()

def play(self):
            if self.stake == 0:
                self.stake = int(self.stake_input.get())
            self.first = self.spin()
            self.second = self.spin()
            self.third = self.spin()
            self.score()

def spin(self):
    randomnumber = random.randint(0, 10)
    return self.ITEMS[randomnumber]

def score(self):
    if((self.first == "OCELOT") and (self.second != "MACAW")):
        win = 5
    elif((self.first == "JAGUAR") and (self.second == "JAGUAR") and (self.third != "JAGUAR")):
        win = 8 
    elif((self.first == "BOA") and (self.second == "BOA") and (self.third == "BOA")):
        win = 10
    elif((self.first == "CAIMAN") and (self.second == "CAIMAN") and ((self.third == "CAIMAN") or (self.third == "BOA"))):
        win = 8 
    elif((self.first == "MACAW") and (self.second == "IBIS") and ((self.third == "MACAW"))):
        win = 15 
    elif((self.first == "TAPIR") and (self.second == "TAPIR") and ((self.third == "TAPIR"))):
        win = 20 
    elif((self.first == "IBIS") and (self.second == "IBIS") and (self.third == "IBIS")):
        win = 300 
    elif((self.first == "SCORPION") and (self.second == "SCORPION") and (self.third == "SCORPION")):
        win = -500 
    else:
        win = -1 

    self.stake += win

    if(win > 0):
        self.lbl.config(text="{}\t{}\t{}   You win {} Coins".format(self.first, self.second, self.third, win))
        self.lbl2.config(text=self.stake)
    else:
        self.lbl.config(text="{}\t{}\t{}   You lose".format(self.first, self.second, self.third))
        self.lbl2.config(text=self.stake)

def quit_play(self):
    self.lbl.config(text="Game has ended. You won a total of " + str(self.stake) + " coins")




root = Tk()
RollMachine(root)

相关问题 更多 >