我的问题是如何让多个“气泡”在屏幕上随机移动?
我必须做一堆 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()