2

我在python 3.3中制作了一个“数学泡泡”游戏,它的椭圆(泡泡)在画布上随机移动,这些泡泡上有一个数字,例如2,用户必须弹出/点击与问题相关的泡泡,例如倍数2,用户将点击2、4、6、8等气泡。

麻烦的是,我不知道如何或是否可以在我创建的椭圆上放一个数字。请帮帮我>.<

到目前为止的代码:

数学泡泡

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.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()

main()
4

1 回答 1

5

要将文本放入 tkinter 画布中,请使用 create_text 方法。要将其放置在您制作的椭圆上,请将位置设置为椭圆的中心。在这种情况下,您制作的每个椭圆的中心是 (xval+15, yval+15)。见下文:

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="mytext")
    canvas.update()

您现在制作的每个椭圆都将写有“mytext”。

但是,您可能希望研究动画软件以用于涉及运动的更复杂的应用程序。一些很好的例子是 Pygame 和 Livewires。但这取决于你。

于 2013-07-17T01:45:15.517 回答