如何在Python中使球触摸窗口边缘?

时间:2016-04-30 00:28:30

标签: python

我有一个球在窗户周围弹跳的代码。并且,它不会触及100%的窗口边缘,但我的老师会喜欢它。你怎么修改这个?球也似乎移动得非常快,所以它甚至不像一个圆圈。有谁知道为什么会这样?谢谢。

from graphics import * #import graphics file
import time #import time module
from random import randrange
def main():
    win = GraphWin('Bouncing Ball', 500, 500) #create a window to draw upon
    x=randrange(50,450)
    y=randrange(50,450)
    a=1
    b=1

    circ1 = Circle(Point(x,y),10)
    circ1.setFill("green")
    circ1.draw(win)


    for i in range(2000): #"i" establishes the amount of time the stuff below runs for
        circ1.move(a,b)
        center = circ1.getCenter()
        x = center.getX()
        y = center.getY()
        if 500-(x+25)<1:
            z=randrange(1,4)
            if (a<0):
                a=-z
            else:
                a=z
                a=-a
            #to not alter speed use this as code instead:
            # a=-a
        elif x-25<1:
            z=randrange(1,4)
            if (a<0):
                a=z
            else:
                a=-z
        if 500-(y+25)<1:
            z=randrange(1,4)
            if (b<0):
                b=-z
            else:
                b=z
            b=-b
        elif y-25<1:
            b=-b
        time.sleep(.05)

main()

0 个答案:

没有答案