1

私の質問は、乱数を使用して複数の「バブル」を画面上で移動させるにはどうすればよいですか?

def bubble(): のヒープを実行する必要がありますか?それとももっと簡単な方法がありますか? また、キャンバス上で「泡」をランダムに移動すると、困惑します。

これまでのコーディング:

from tkinter import *
import random

def quit():
    root.destroy()


def bubble():
    xval = random.randint(5,765)
    yval = random.randint(5,615)
    canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
    canvas.create_text(xval+15,yval+15,text=number)
    canvas.update()

def main():
    global root
    global tkinter
    global canvas
    root = Tk()
    root.title("Math Bubbles")
    Button(root, text="Quit", width=8, command=quit).pack()
    Button(root, text="Start", width=8, command=bubble).pack()
    canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
    canvas.pack()
    root.mainloop()

# Create a sequence of numbers to choose from. CAPS denotes a constant variable
NUMBERS = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

# Pick numbers randomly from the sequence with the help of a random.choice function
number = random.choice(NUMBERS)

# Create a variable to use later to see if the guess is correct
correct = number

main()
4

1 に答える 1

1

これでうまくいくはずです:

import Tkinter, random

class BubbleFrame:

    def __init__(self, root):
        root.title("Math Bubbles")
        Tkinter.Button(root, text="Add Bubbles", width=8, command=self.bubble).pack()
        Tkinter.Button(root, text="Quit", width=8, command=quit).pack()
        self.canvas = Tkinter.Canvas(root, width=800, height=650, bg = '#afeeee')
        self.canvas.pack()
        self.bubbles = {} # this will hold bubbles ids, positions and velocities

    def bubble(self):
        # add bubbles for numbers from 1 to 20
        for number in range(1, 20+1):
            xval = random.randint(5,765)
            yval = random.randint(5,615)
            s1 = self.canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
            s2 = self.canvas.create_text(xval+15,yval+15, text=number)
            self.bubbles[(s1, s2)] = (xval, yval, 0, 0) # add bubbles to dictionary

    def loop(self, root):
        for (s1, s2), (x, y, dx, dy) 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(s1, dx, dy)
            self.canvas.move(s2, dx, dy)
            self.bubbles[(s1, s2)] = (x + dx, y + dy, dx, dy)
        # have mainloop repeat this after 100 ms
        root.after(100, self.loop, root)

if __name__ == "__main__":
    root = Tkinter.Tk()
    frame = BubbleFrame(root)
    frame.loop(root)
    root.mainloop()

これらのメソッドと属性をラップするクラスを使用して、コードを少し再構築したことに注意してください。ただし、それは完全にあなた次第です。また、私は Python 2.7 でこれを行ったので、Tkinter パッケージの名前など、いくつかの小さな調整が必要になる場合があることに注意してください。

bubble追加するバブルごとに別の関数を定義する必要はありません。実際、コードはすでに多くのバブルをキャンバスに追加しているので、この部分は問題ありません。トリッキーなビットは、泡を動かすことです。

このために、最初にself.bubblesディクショナリを追加し、バブルの ID とそのラベル (canvas.create...メソッドによって返される) を現在の位置と速度にマッピングします。最後に、loopメソッドはバブルの位置と速度を更新し、キャンバスを再描画します。最後に、このメソッドはメソッドを使用して次の実行をスケジュールしafterます。

于 2013-07-17T11:36:44.323 に答える