0

在我的游戏中按下开始按钮后,我想隐藏按钮以阻止用户再次按下开始按钮并出现更多气泡。我试图使变量等于 0。然后每次单击开始按钮时,该值都会增加 1。如果值为 2,那么开始按钮将消失或变为非活动状态。通过尝试添加此“隐藏按钮代码”,当单击开始按钮时,它只会在我的屏幕上显示一个气泡。哦,开始按钮并没有隐藏...... ._。

    from tkinter import *
    import random
    import tkinter as tk
    # Message box for score to tell user if they have won or lost
    from tkinter.messagebox import showinfo



    class BubbleFrame:


        def __init__(self, root, name):
            self.name = name
            root.title("Math Bubbles")
            self.bubbles = {} # this will hold bubbles ids, positions and velocities
            self.score = 0
            self.buttonclick = 0 # Newly added in attempt to hide button
            Button(root, text="Start", width=8, bg="Pink", command=self.make_bubbles).pack() # This button starts the game, making the bubbles move across the screen
            Button(root, text="Quit", width=8, bg="Yellow",command=quit).pack()
            self.canvas = Canvas(root, width=800, height=650, bg='#afeeee')
            self.canvas.create_text(400, 30, fill="darkblue", font="Times 20 italic bold", text="Click the bubbles that are answers in the two times tables.")
            #Shows score at beinging of the game
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: 0")
            self.canvas.pack()



        def make_bubbles(self):
            for each_no in range(1, 21):
                self.buttonclick += 1 #Newly added in attempt to hide button
                xval = random.randint(5, 765)
                yval = random.randint(5, 615)
                COLOURS = ('#00ff7f', '#ffff00', '#ee82ee', '#ff69b4', '#fff0f5') # CAPS represents a constant variable
                colour = random.choice(COLOURS) # This picks a colour randomly
                oval_id = self.canvas.create_oval(xval, yval, xval + 60, yval + 60,fill=colour, outline="#000000", width=5, tags="bubble")
                text_id = self.canvas.create_text(xval + 30, yval + 30, text=each_no, tags="bubble")
                self.canvas.tag_bind("bubble", "<Button-1>", lambda x: self.click(x))
                self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id) # add bubbles to dictionary
                if buttonclick == 2: #Newly added in attempt to hide button
                    showinfo("Oh No", "Sorry %s, but the game is already started" % self.name)

        def click(self, event):
            if self.canvas.find_withtag(CURRENT):
                item_uid = event.widget.find_closest(event.x, event.y)[0]
                is_even = False
                try: # clicked oval
                    self.bubbles[item_uid]
                except KeyError: # clicked oval
                    for key, value in self.bubbles.iteritems():
                        if item_uid == value[5]: # comparing to text_id
                           if value[4] % 2 == 0:
                                is_even = True
                            self.canvas.delete(key) # deleting oval
                            self.canvas.delete(item_uid) # deleting text
                else:
                    if self.bubbles[item_uid][4] % 2 == 0:
                        is_even = True
                    self.canvas.delete(item_uid) # deleting oval
                    self.canvas.delete(self.bubbles[item_uid][5]) # deleting text

                if is_even:
                    self.score += 1
                else:
                    self.score -= 1
                    showinfo("Oh no!", "%s! You clicked the wrong bubble, please start again." % self.name)

                if self.score == 10:
                    #Tells user You won! if score is 10
                    showinfo("Winner", "You won %s!" % self.name)


            self.canvas.delete(self.current_score)
            #Shows updated score on canvas
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)

        def bubble_move(self, root):
            for oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items():
                # update velocities and positions
                dx += random.randint(-1, 1)
                dy += random.randint(-1, 1)
                # dx and dy should not be too large
                dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
                # bounce off walls
                if not 0 < x < 770:
                    dx = -dx
                if not 0 < y < 620:
                    dy = -dy
                # apply new velocities
                self.canvas.move(oval_id, dx, dy)
                self.canvas.move(text_id, dx, dy)
                self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id)
            # have mainloop repeat this after 100 ms
            root.after(100, self.bubble_move, root)

    if __name__ == "__main__":

        root = Tk()
        root.title("Welcome")
        Label(root, text="Welcome to Math bubbles, what is your name?").pack()
        name = Entry(root)
        name.pack()
        def submit(name, root):
        root.destroy()
        root = Tk()
        Label(root, text="Hello %s, press the Start button to begin.\n" % name).pack()
        BubbleFrame(root, name).bubble_move(root)
        Button(root, text="Ok", command=lambda: submit(name.get(), root)).pack()
        root.mainloop()
4

1 回答 1

0

我在第 103 行遇到 IndentationError,在第 39 行遇到 NameError(self.buttonclick不是buttonclick)。下次在发布之前检查您的代码。

好吧,每次单击“开始”按钮时都会调用 20 次按钮单击,并且值检查发生在过程中间。

我为你修好了make_bubbles方法。

def make_bubbles(self):
    self.buttonclick += 1 #Newly added in attempt to hide button
    if self.buttonclick == 2: #Newly added in attempt to hide button
        self.startbutton.configure(state="disabled")
        showinfo("Oh No", "Sorry %s, but the game is already started" % self.name)
        return
    for each_no in range(1, 21):
        ...

缺少在游戏结束时启用按钮的逻辑,或者可能根本不需要。也许我宁愿每次单击“开始”按钮时清除所有气泡并生成 20 个新气泡。无论如何,我推荐这个站点用于 tkinter 编程。

这里还有一些需要解决的问题。iteritems()在 Python 3 中不再使用。self.bubbles.items()改为使用。祝你好运!

于 2013-10-29T06:29:17.887 回答