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]這是我上面的代碼,用于在窗口內(nèi)彈跳的程序。我的問題是我只用什么來替換if (center[index] <= radius) or (center[index] + radius >= size[index]):線,以便它的中心接觸窗口邊緣而不是在其邊緣之一接觸窗口邊緣時(shí)彈跳。(我將在下面附上完整的問題,以便任何人更好地理解我正在嘗試做的事情。[如果您希望點(diǎn)在其中心接觸窗口邊緣時(shí)彈跳而不是在其邊緣之一接觸窗口邊緣時(shí)彈跳,應(yīng)該用什么代碼替換“if 語句條件”?你只能改變條件。您不能添加或刪除任何其他代碼。]我嘗試使用代碼if (center[index] <= 0) or (center[index] + radius >= size(window(500,400))):&if (center[index] <= 0) or (center[index] + radius >= size[window]):我會(huì)得到錯(cuò)誤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.。有人可以指導(dǎo)我或幫助我嗎?謝謝
1 回答

湖上湖
TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
基本上,您希望球在球中心碰到窗邊時(shí)反彈。它目前被編碼為在球邊緣擊中時(shí)反彈。
檢查球位置時(shí)只需刪除半徑間隙:
if (center[index] <= 0) or (center[index] >= size[index]):
添加回答
舉報(bào)
0/150
提交
取消