为了账号安全,请及时绑定邮箱和手机立即绑定

替换“if 语句条件”

替换“if 语句条件”

Helenr 2023-04-11 16:00:18
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.。有人可以指导我或帮助我吗?谢谢
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

基本上,您希望球在球中心碰到窗边时反弹。它目前被编码为在球边缘击中时反弹。

检查球位置时只需删除半径间隙:

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


查看完整回答
反对 回复 2023-04-11
  • 1 回答
  • 0 关注
  • 63 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信