在Python中,如何从画布边缘向后转动椭圆?

时间:2014-06-03 21:18:50

标签: python tkinter

from tkinter import *

def mozog():
    global x

    if x == wmax:
        x = x - 20

    can.coords(oval, x-r, y-r, x+r, y+r)

###

r = 10
x = 200
y = 100

wmax = 400
wmin = 0

#

win = Tk()
win.title("Canvas")

can = Canvas(win, width = wmax, height = 200, bg = "gray")
can.grid(row = 1, column = 1)

oval = can.create_oval(x-r, y-r, x+r, y+r, fill= "red")

but = Button(win, text = "Move", command = mozog)
but.grid(row = 2, column = 1)

我的任务是创建一个椭圆形并将其向右移动,当它到达画布的边缘时,它应该返回到左侧等。我尝试了ifs,但那些并不好。我无法弄清楚解决方案,但我确信这很容易。 你能帮我解决一下这个问题吗?

1 个答案:

答案 0 :(得分:0)

现在你的椭圆没有做任何事情。

这是让椭圆向右移动然后向左反弹的一种方法。

delta = 10

def mozog():
    global x, delta

    x += delta

    if x >= wmax:
        delta *= -1

    can.coords(oval, x-r, y-r, x+r, y+r)