【问题标题】:Randomly walking turtle function not doing what I want it to随机行走的乌龟功能没有做我想做的事
【发布时间】:2013-08-05 16:36:18
【问题描述】:

我必须编写一个程序,让乌龟在屏幕周围旋转 90 度,随机选择左或右,直到它撞到墙壁,旋转 180 度,然后回到屏幕周围行走。当它撞墙 4 次时,循环终止。我遇到的问题是,当它从墙上反弹时,它会停止行走,并且循环显然已经终止,因为我可以通过单击它来关闭窗口(@98​​7654321@)。这是完整的程序:

import turtle
import random

def isInScreen(w,t):
    leftBound = w.window_width() / -2
    rightBound = w.window_width() / 2
    bottomBound = w.window_height() / -2
    topBound = w.window_height() / 2

    turtlex = t.xcor()
    turtley = t.ycor()

    stillIn = True

    if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound:
        stillIn = False

    return(stillIn)

def randomWalk(t,w):
    counter = 0

    while isInScreen(w,t) and counter < 4:
        coin = random.randrange(0,2)
        if coin == 0:
            t.left(90)
        else:
            t.right(90)
        t.forward(50)

    t.left(180)
    t.forward(50)
    counter = counter+1

wn = turtle.Screen()
wn.bgcolor('lightcyan')

steklovata = turtle.Turtle()
steklovata.color('darkslategray')
steklovata.shape('turtle')

randomWalk(steklovata,wn)

wn.exitonclick()

我对它为什么停止感到困惑,考虑到一旦乌龟反弹回来,它的 x 和 y 坐标满足 isInScreen(w,t) 为真的要求,因此又回到了行走状态。有什么想法吗?

编辑: 接受了 Sukrit 的回答,因为它最容易与我已经编程的内容联系起来,并在其他方面给了我一些指示,但 Brian 的回答非常也很有用,如果可能的话,我会接受两者。非常感谢你们!

【问题讨论】:

  • 好的,看起来你的while循环在它离开屏幕的那一刻失败了,它反弹回来,是的,因为它在第一个while循环之外。如果您尝试在计数器

标签: python python-3.x turtle-graphics


【解决方案1】:

您的counter = counter + 1 错误。当您的isInScreen 返回False 时,while 循环中断并且代码结束,因为计数器正在递增,但您不会再次循环。请参阅以下代码 -

import turtle
import random

def isInScreen(w,t):
    leftBound = w.window_width() / -2.0
    rightBound = w.window_width() / 2.0
    bottomBound = w.window_height() / -2.0
    topBound = w.window_height() / 2.0

    turtlex = t.xcor()
    turtley = t.ycor()

    if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound:
        return False

    return True

def randomWalk(t,w):
    counter = 0

    while True:
        while isInScreen(w,t):
            coin = random.randrange(0,2)
            if coin == 0:
                t.left(90)
            else:
                t.right(90)
            t.forward(50)
        t.left(180)
        t.forward(50)
        counter += 1
        if counter == 4:
            break

wn = turtle.Screen()
wn.bgcolor('lightcyan')

steklovata = turtle.Turtle()
steklovata.color('darkslategray')
steklovata.shape('turtle')

randomWalk(steklovata,wn)

wn.exitonclick()

P.S - 你不需要变量来存储stillIn,如果if 条件计算为True,则只需return False,如果不是@987654329 @。 (变化反映在上面的代码中)。

【讨论】:

  • 效果很好,非常感谢!我不知道break,我会阅读它,以便将来使用它。
  • 难道不能在 counter
  • 这种方法没有错。事实上,我认为嵌套循环更好地模拟了我们从行为角度看待您的应用程序的方式。即“只要我们在屏幕中,就继续随机移动。一旦我们离开它,转身,然后继续随机行走。”不过,作为一个好习惯,我倾向于避免使用while True: 循环和嵌套循环,除非真的有必要。
  • @Brian:我也是。但是,在这种情况下,我认为最好使用while True:,因为它更清晰,并且 OP 可以立即与之关联并找出他的问题。 :)
  • @SukritKalra 我同意,它更清楚地传达了最初的问题,这就是为什么我很高兴他现在可以看到这两种方法。 +1
【解决方案2】:

作为嵌套循环的替代方案并避免一些多余的语句,以下应该产生与 Sukrit 的答案相同的结果。

def randomWalk(t,w):
    counter = 0

    while counter < 4:
        if not isInScreen(w,t):
            t.left(180)
            counter += 1
        else:
            coin = random.randrange(0,2)
            if coin == 0:
                t.left(90)
            else:
                t.right(90)
        t.forward(50)

核心问题是确保 isInScreen 返回 false 不会导致您的 while 循环终止,同时在循环体内增加 counter

【讨论】:

    【解决方案3】:

    你的大while循环是失败的原因,你知道的。所以你可以做的是让while循环只检查计数器,而不是把isInScreen()作为while循环的一部分。现在为了检查你是否可以前进,你可以在你跳跃之前通过查看来作弊,即在你当前的位置加上五十的值并检查你是否会在屏幕中,如果不前进,否则尽可能接近你可以,增加碰撞计数器,然后转身。或者,Sukrit Kalra 的答案可能更容易实现。

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2015-11-13
      相关资源
      最近更新 更多