- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以,我正在尝试制作一个逼真的弹跳功能, turtle 撞到墙壁并以相应的角度弹开。我的代码如下所示:
def bounce(num_steps, step_size, initial_heading):
turtle.reset()
top = turtle.window_height()/2
bottom = -top
right = turtle.window_width()/2
left = -right
turtle.left(initial_heading)
for step in range(num_steps):
turtle.forward(step_size)
x, y = turtle.position()
if left <= x <= right and bottom <= y <= top:
pass
else:
turtle.left(180-2 * (turtle.heading()))
所以,这适用于侧壁,但我不知道如何让它从顶部/底部正确弹起。有什么建议吗?
最佳答案
尝试这样的事情:
if not (left <= x <= right):
turtle.left(180 - 2 * turtle.heading())
elif not (bottom <= y <= top):
turtle.left(-2 * turtle.heading())
else:
pass
我的 python 语法有点生疏,抱歉:P。但水平翻转和垂直翻转的数学计算略有不同。
编辑:
我怀疑发生的情况是你的 turtle 陷入了一种指向上方并卡在顶墙上方的情况。这将导致它无限期地翻转。您可以尝试添加以下条件:
if (x <= left and 90 <= turtle.heading() <= 270) or (right <= x and not 90 <= turtle.heading() <= 270):
turtle.left(180 - 2 * turtle.heading())
elif (y <= bottom and turtle.heading() >= 180) or (top <= y and turtle.heading <= 180):
turtle.left(-2 * turtle.heading())
else:
pass
如果有效,则您的代码中其他地方可能存在错误。边缘处理很难正确处理。我假设 Turtle.heading() 总是会返回 0 到 360 之间的值 - 如果不是,那么正确的结果会更加棘手。
关于python - TurtleGraphics Python : Bouncing turtle off the walls?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1457332/
-o 更改输出文件名(我发现使用--help) 但我不知道 -Wall 是做什么的? 最佳答案 它是“warn all”的缩写——它打开(几乎)所有 g++ 可以告诉你的警告。通常是个好主意,尤其是如
我的墙脚本存在问题,单个帖子在用户自己的墙上显示多次,在他的 friend 墙上显示一次。例如,如果我有 5 个 friend ,脚本将显示单个帖子 5 次。 由于它在 friend 墙上显示过一次,
我有一个基本程序,您可以在其中使用箭头键绕圆圈移动。我正在努力使球(蓝色矩形)无法进入某个区域,但它不起作用。我正在使用 Eclipse。 package TLOT; import java.awt.
我正试图在我的网站上展示我的墙上最近的一些帖子。当搜索遍了我得到的结果张贴到墙上的网站。 我查看了Facebook开发人员的网站,在那里我无法得到明确的想法。 请专家帮忙。 编辑1: Likebox插
我正在使用 GCC 4.8 用 C 编写一个项目,我想查看所有警告(希望消除它们),但问题是我 #include 一些旧的、未维护的库,这给了我巨大的警告墙以应对 -墙选项。我没有办法解决这些问题,我
在 GCC 中编译时是否可以包含 -Wall 标志,但随后从该子集中排除单个标志,例如 -Wattributes?或者,可以包括除 -Wattributes 之外的所有警告标志,但我认为必须有一种更简
好吧,我知道这个问题已经在很多地方被问过很多次了,但我还无法解决这个问题。我尝试了 java.util.Date 以及 JodaTime API。 我的问题是如何考虑应用程序运行所在的时区返回当前日期
我在这里使用jquery插件Facebook Wall jquery plugin我想在 jquery Facebox 中显示帖子墙。由于某种原因,如果 fb.wall 不是从 Facebox 中加载
我的函数不断出错,我检查了其他答案以寻求帮助,但找不到任何答案。 #include double celsius_at_depth(double celtemp); double fahre
目前我正在使用此命令在 Mint 中编译我的 .c 文件 gcc -std=gnu99 -Wall -Werror filename.c -o filename [-lm] 如何将这些参数设为默认值,
我想在 Facebook 墙上发布一些文字。 我看过“Facebook 开发人员”的教程,但他们建议使用一个按钮登录,另一个按钮发布提要。不幸的是,这不是我想要的。 我想要的是单击我制作的按钮并直接在
所以我想让用户通过粘贴或使用 wall 自己键入来输入特定消息,然后使用 wall 将其广播给所有用户。我的想法是,我不想只有一行消息,而是允许在不使用文本文件的情况下提供他们想要的尽可能大的消息。
我正在编写一个模块来计算给定日期的半小时交易时段。交易时段从半小时开始,从 1(00:00 开始)到 48(23:30 开始)连续编号。通常一天有 48 个交易时段,但在夏令时开始的那一天有 46 个
我在一个编码类(class)中,我们通过命令行参数用 C++ 进行编码,但她正在使用某些我无法通过谷歌搜索找到的东西,但我在本网站的其他帖子中看到过这些东西。我的虚拟机是 Linux。 -Wall 在
假设我有: class Vector3 { float x, y, z; ... bunch of cuntions .. static operator+(const Vector3&,
我正在尝试在 Keil IDE 中为 stm32f103 微 Controller 使用 gcc 编译器。我正在编译一个相对较小的项目,其中包含一些模板代码和几个纯虚拟类。没有花哨的 C++11 东西
我想知道如何使用 spritekit 创建一堵墙。物体上的东西不能移动过去。我知道我可以使用这段代码: self.physicsBody = [SKPhysicsBody bodyWithEdgeLo
我最近开始使用 -Wall 编译器开关来尝试提高我的代码质量。它(正确地)给出了关于这个小片段的警告...... int i; for (i = start - 1; i >= 0;
我有一个有缺陷的程序示例,它应该给出一个关于未初始化变量的警告,但是当我编译它时 gcc 没有给我任何警告。 代码如下: #include int main() { int foo;
这是源文件 get.c 的内容: #include int main(){ //int i = 0; char b[10]; gets(b); puts(b); return 0
我是一名优秀的程序员,十分优秀!