- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在阅读这个 tutorial .我在一个章节中,您可以在其中构建乒乓球游戏。但是,我在尝试制作一个可以检测球何时击中 Racket 的功能时遇到了问题?
它说我应该这样做:
Make a boolean function
hit(bx, by, r, px, py, h)
that returnsTrue
when the vertical coordinate of the ball (by
) is between the bottom and top of the paddle, and the horizontal location of the ball (bx
) is less than or equal to the radius (r
) away from the front of the paddle.
到目前为止我已经试过了:
def hit(bx, by, r, px, py, h):
if by > py and by <= h:
if bx <= r:
return True
else:
return False
...
# If this returns True then change the direction of the ball (dx).
if hit(ball_x, ball_y, radius, paddle_x, paddle_y, height):
dx *= -1
我无法将引用的段落翻译成代码。我做错了什么?
注意:此函数的预期输出为:
hit(760, 100, 10, 780, 100, 100)
False
hit(770, 100, 10, 780, 100, 100)
True
hit(770, 200, 10, 780, 100, 100)
True
hit(770, 210, 10, 780, 100, 100)
False
最佳答案
您没有考虑元素的实际位置 - 您所拥有的坐标是指球和 Racket 上的特定点。您的代码所做的是检查球是否在 Racket 上方并且半径大于从 y 原点到 Racket 的距离,然后检查它是否比从 x 原点到 Racket 的距离更宽。
假设 (px, py) 是 Racket 的左上角,您可以尝试这样的操作:
def hit(bx, by, r, px, py, h):
if by >= py and by <= py + h:
print "Y satisfied."
if bx <= px + r:
print "HIT"
return True
print "X not satisfied."
print "not hit."
return False
请记住,这并不能说明球是圆形的(或您想到的任何其他形状)。
编辑:如果你在让它工作时遇到问题,你可以尝试加入一些打印语句来让你知道参数的值是什么以及返回值是什么。这应该让您对实际发生的事情有一些了解。
再次编辑:意识到函数有可能没有返回值。此外,此代码并未通过所有测试用例 - 它会将第一个测试用例计为命中。
关于python - 检测球何时击中乒乓 Racket ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984541/
我遇到了一个问题,我无法将网络请求发送到我创建的 Docker 容器。我已经公开了正确的端口,所以我不确定这里可能有哪些其他问题。 我有一台服务器在容器中运行 alice在 localhost:100
在下面最后一行的方法中,我总是遇到异常: System.OverflowException: Value was either too large or too small for an Int32.
我正在关注 realpython article about running Flask on Ubuntu .建议在文章中检查 nginx 已通过导航到 http://localhost:8000/
给定一个条件,我想搜索一个元素列表并返回第一个达到条件的元素和前一个元素。 在 C/C++ 中,这很容易: int i = 0; for(;;i++) if (arr[i] == 0) break;
我正在使用 Firebase Firestore 我想从数据库中删除数据..删除数据工作正常,但我的进度对话框卡住了。我想我必须使用工作线程,但我不知道如何使用。 db = FirebaseFire
我按照 this page 上的说明进行操作创建推送通知。我之前实际上已经做过一次并且能够让它工作(几周前),花了一些时间,我想我现在才再次做这个教程作为复习,出于某种原因,我可以'甚至获取代码以点击
我在大学学习Java,这是我的任务。任务是创建一个由颜色方块组成的x x y网格,每个网格在单独的线程中运行,并且每k ms要么将其颜色更改为随机的颜色,要么对其邻居的颜色求平均。 现在,如果我创建一
我有一台服务器,它不断地从自身获得随机命中,IP 读取为 127.0.0.1 .我知道有各种各样的程序可以做到这一点,但服务器是带有 sendmail 和 monit 的最低限度的 LAMP 服务器。
我正在使用 sqlite 数据库并且我在这个要点中声明了模型 https://gist.github.com/mmahesh/7245561 我添加了一个带有事务管理器的模型实例作为 with tra
我是一名优秀的程序员,十分优秀!