替换“ if语句条件”

时间:2020-07-29 02:22:48

标签: python python-3.x python-2.7

def move_dot(window, center, radius, velocity):
    size = (window.get_width(), window.get_height())
    for index in range(0, 2):
        # update center at coordinate
        center[index] = center[index] + velocity[index]
        # dot edge outside window?
        if (center[index] <= radius) or (center[index] + radius >= size[index]):
        # change direction
            velocity[index] = - velocity[index]

这是我上面在窗口内反弹的程序的代码。我的问题是,我该用什么来替换if (center[index] <= radius) or (center[index] + radius >= size[index]): ONLY ,以便其中心触摸窗口边缘,而不是在其中一个边缘触摸窗口边缘时弹起。 (我将在下面附上完整的问题,以便任何人对我正在尝试做的事情有更好的了解。


[如果您希望点在其中心接触到窗口边缘时反弹而不是在其边缘之一接触到窗口边缘时反弹,那么什么代码应该替换'if语句条件?您只能更改条件。您无法添加或删除任何其他代码。]


我尝试使用代码if (center[index] <= 0) or (center[index] + radius >= size(window(500,400))):if (center[index] <= 0) or (center[index] + radius >= size[window]):,但出现错误Incorrect: It should bounce when either coordinate of the center is less than or equal to 0 or larger than or equal to the size (width or height) of the window.。有人可以指导我还是请帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

基本上,您希望球 center 击中窗口边缘时球反弹。当前已编码为当球 edge 命中时弹跳。

在检查球的位置时只需去除半径间隙:

if (center[index] <= 0) or (center[index] >= size[index]):