猿问

替换“if 语句条件”

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]):线,以便它的中心接触窗口边缘而不是在其边缘之一接触窗口边缘时弹跳。(我将在下面附上完整的问题,以便任何人更好地理解我正在尝试做的事情。


[如果您希望点在其中心接触窗口边缘时弹跳而不是在其边缘之一接触窗口边缘时弹跳,应该用什么代码替换“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.。有人可以指导我或帮助我吗?谢谢


Helenr
浏览 92回答 1
1回答

湖上湖

基本上,您希望球在球中心碰到窗边时反弹。它目前被编码为在球边缘击中时反弹。检查球位置时只需删除半径间隙:if&nbsp;(center[index]&nbsp;<=&nbsp;0)&nbsp;or&nbsp;(center[index]&nbsp;>=&nbsp;size[index]):
随时随地看视频慕课网APP

相关分类

Python
我要回答