0

私のゲームでは、スタート ボタンを押した後、ボタンを非表示にして、ユーザーがスタート ボタンをもう一度押してバブルをさらに表示させないようにしたいと考えていました。変数を 0 にしようとしました。その後、開始ボタンがクリックされるたびに、値が 1 ずつ増加します。値が 2 の場合、開始ボタンは消えるか、非アクティブになります。この「非表示ボタンコード」を追加しようとすると、スタートボタンがクリックされたときに画面にバブルが1つだけ表示されます。ああ、開始ボタンは隠れません... ._.

    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 に答える