Stop a moving Python turtle when it's close to another turtle











up vote
1
down vote

favorite












How do I stop a randomly moving turtle using a while loop when it comes with 50 units of another turtle?



I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?



import random
import turtle

def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False

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

turtleX = turt.xcor()
turtleY = turt.ycor()

stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn

def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()

july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)

june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)

if dist == 'True':
break

main()









share|improve this question




























    up vote
    1
    down vote

    favorite












    How do I stop a randomly moving turtle using a while loop when it comes with 50 units of another turtle?



    I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?



    import random
    import turtle

    def turtlesClose(t1, t2):
    if t1.distance(t2)<50:
    return True
    else:
    return False

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

    turtleX = turt.xcor()
    turtleY = turt.ycor()

    stillIn = True
    if turtleX > rightBound or turtleX < leftBound:
    stillIn = False
    if turtleY > topBound or turtleY < bottomBound:
    stillIn = False
    return stillIn

    def main():
    wn = turtle.Screen()
    # Define your turtles here
    june = turtle.Turtle()
    july = turtle.Turtle()

    july.shape('turtle')
    july.up()
    july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
    july.down()
    july.dot(100)

    june.shape('turtle')
    while isInScreen(wn,june):
    coin = random.randrange(0, 2)
    dist = turtlesClose(july, june)
    if coin == 0:
    june.left(90)
    else:
    june.right(90)
    june.forward(50)

    if dist == 'True':
    break

    main()









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      How do I stop a randomly moving turtle using a while loop when it comes with 50 units of another turtle?



      I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?



      import random
      import turtle

      def turtlesClose(t1, t2):
      if t1.distance(t2)<50:
      return True
      else:
      return False

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

      turtleX = turt.xcor()
      turtleY = turt.ycor()

      stillIn = True
      if turtleX > rightBound or turtleX < leftBound:
      stillIn = False
      if turtleY > topBound or turtleY < bottomBound:
      stillIn = False
      return stillIn

      def main():
      wn = turtle.Screen()
      # Define your turtles here
      june = turtle.Turtle()
      july = turtle.Turtle()

      july.shape('turtle')
      july.up()
      july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
      july.down()
      july.dot(100)

      june.shape('turtle')
      while isInScreen(wn,june):
      coin = random.randrange(0, 2)
      dist = turtlesClose(july, june)
      if coin == 0:
      june.left(90)
      else:
      june.right(90)
      june.forward(50)

      if dist == 'True':
      break

      main()









      share|improve this question















      How do I stop a randomly moving turtle using a while loop when it comes with 50 units of another turtle?



      I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?



      import random
      import turtle

      def turtlesClose(t1, t2):
      if t1.distance(t2)<50:
      return True
      else:
      return False

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

      turtleX = turt.xcor()
      turtleY = turt.ycor()

      stillIn = True
      if turtleX > rightBound or turtleX < leftBound:
      stillIn = False
      if turtleY > topBound or turtleY < bottomBound:
      stillIn = False
      return stillIn

      def main():
      wn = turtle.Screen()
      # Define your turtles here
      june = turtle.Turtle()
      july = turtle.Turtle()

      july.shape('turtle')
      july.up()
      july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
      july.down()
      july.dot(100)

      june.shape('turtle')
      while isInScreen(wn,june):
      coin = random.randrange(0, 2)
      dist = turtlesClose(july, june)
      if coin == 0:
      june.left(90)
      else:
      june.right(90)
      june.forward(50)

      if dist == 'True':
      break

      main()






      python python-3.x turtle-graphics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 8:53









      cdlane

      16.7k21043




      16.7k21043










      asked Nov 11 at 4:25









      Stephen Dangel

      102




      102
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The problem with your code is this statement:



          if dist == 'True':


          You don't want quotes around True. Although this will work:



          if dist == True:


          The correct way to express this is:



          if dist is True:


          or better yet:



          if dist:


          Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:



          from random import randrange, choice
          from turtle import Screen, Turtle

          CURSOR_SIZE = 20

          def turtlesClose(t1, t2):
          return t1.distance(t2) < 50

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

          turtleX, turtleY = turtle.position()

          return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound

          def main():
          screen = Screen()

          july = Turtle('circle')
          july.shapesize(100 / CURSOR_SIZE)

          july.up()
          july.goto(randrange(-250, 250), randrange(-250, 250))
          july.down()

          june = Turtle('turtle')

          while isInScreen(screen, june):

          if turtlesClose(july, june):
          break

          turn = choice([june.left, june.right])

          turn(90)

          june.forward(50)

          main()





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245825%2fstop-a-moving-python-turtle-when-its-close-to-another-turtle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            The problem with your code is this statement:



            if dist == 'True':


            You don't want quotes around True. Although this will work:



            if dist == True:


            The correct way to express this is:



            if dist is True:


            or better yet:



            if dist:


            Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:



            from random import randrange, choice
            from turtle import Screen, Turtle

            CURSOR_SIZE = 20

            def turtlesClose(t1, t2):
            return t1.distance(t2) < 50

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

            turtleX, turtleY = turtle.position()

            return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound

            def main():
            screen = Screen()

            july = Turtle('circle')
            july.shapesize(100 / CURSOR_SIZE)

            july.up()
            july.goto(randrange(-250, 250), randrange(-250, 250))
            july.down()

            june = Turtle('turtle')

            while isInScreen(screen, june):

            if turtlesClose(july, june):
            break

            turn = choice([june.left, june.right])

            turn(90)

            june.forward(50)

            main()





            share|improve this answer

























              up vote
              0
              down vote



              accepted










              The problem with your code is this statement:



              if dist == 'True':


              You don't want quotes around True. Although this will work:



              if dist == True:


              The correct way to express this is:



              if dist is True:


              or better yet:



              if dist:


              Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:



              from random import randrange, choice
              from turtle import Screen, Turtle

              CURSOR_SIZE = 20

              def turtlesClose(t1, t2):
              return t1.distance(t2) < 50

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

              turtleX, turtleY = turtle.position()

              return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound

              def main():
              screen = Screen()

              july = Turtle('circle')
              july.shapesize(100 / CURSOR_SIZE)

              july.up()
              july.goto(randrange(-250, 250), randrange(-250, 250))
              july.down()

              june = Turtle('turtle')

              while isInScreen(screen, june):

              if turtlesClose(july, june):
              break

              turn = choice([june.left, june.right])

              turn(90)

              june.forward(50)

              main()





              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                The problem with your code is this statement:



                if dist == 'True':


                You don't want quotes around True. Although this will work:



                if dist == True:


                The correct way to express this is:



                if dist is True:


                or better yet:



                if dist:


                Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:



                from random import randrange, choice
                from turtle import Screen, Turtle

                CURSOR_SIZE = 20

                def turtlesClose(t1, t2):
                return t1.distance(t2) < 50

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

                turtleX, turtleY = turtle.position()

                return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound

                def main():
                screen = Screen()

                july = Turtle('circle')
                july.shapesize(100 / CURSOR_SIZE)

                july.up()
                july.goto(randrange(-250, 250), randrange(-250, 250))
                july.down()

                june = Turtle('turtle')

                while isInScreen(screen, june):

                if turtlesClose(july, june):
                break

                turn = choice([june.left, june.right])

                turn(90)

                june.forward(50)

                main()





                share|improve this answer












                The problem with your code is this statement:



                if dist == 'True':


                You don't want quotes around True. Although this will work:



                if dist == True:


                The correct way to express this is:



                if dist is True:


                or better yet:



                if dist:


                Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:



                from random import randrange, choice
                from turtle import Screen, Turtle

                CURSOR_SIZE = 20

                def turtlesClose(t1, t2):
                return t1.distance(t2) < 50

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

                turtleX, turtleY = turtle.position()

                return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound

                def main():
                screen = Screen()

                july = Turtle('circle')
                july.shapesize(100 / CURSOR_SIZE)

                july.up()
                july.goto(randrange(-250, 250), randrange(-250, 250))
                july.down()

                june = Turtle('turtle')

                while isInScreen(screen, june):

                if turtlesClose(july, june):
                break

                turn = choice([june.left, june.right])

                turn(90)

                june.forward(50)

                main()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 8:50









                cdlane

                16.7k21043




                16.7k21043






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245825%2fstop-a-moving-python-turtle-when-its-close-to-another-turtle%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)