What's wrong with this code, part 2.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccgrl451
    New Member
    • Nov 2009
    • 8

    What's wrong with this code, part 2.

    Okay, so you probably haven't seen my previous question, whats wrong with this code. For those who have, you know what I'm talking about. If not, heres what I have to do.

    Randomly place a pyramid (yellow, large turtle) on the screen. Have a second turtle move
    randomly until it is close to the pyramid. Stop the seeking turtle once it has found the pyramid.
    We have to do this using object-oriented coding. We don't have to use classes, but I just chose to do so. This also needs to happen in turtle graphics. Heres my code:

    Code:
    from turtle import *
    from random import *
    
    def find_the_pyramid():
        class Pyramid(Turtle()):
            color="yellow"
            shape="triangle"
            shapesize=4
            def __init__(self,color,shape,shapesize):
                self.color=color
                self.shape=shape
                self.shapesize=shapesize
                self.goto(randint(-100,100),randint(-100,100))
                self.goto(randint(-100,100),randint(-100,100))
    
    
        class Lost_Turtle(Turtle()):
            name="Kirk"
            color="brown"
            shape="turtle"
            shapesize=2
            def __init__(self,name,color,shape,shapesize):
                self.name=name
                self.color=color
                self.shape=shape
                self.shapesize=shapesize
    
        Sphinx=Pyramid("yellow","triangle",5)
        Billy=Lost_Turtle("Billy","blue","turtle",3)
        
            
        while(Billy.distance(Sphinx)>20):
            Billy.forward(randint(40,80))
            Billy.right(randint(0, 360))
    
    find_the_pyramid()
    The only problem is that I keep getting sent errors, which I don't understand. If so, can you take a look at my code and try to tell me what I'm doing wrong. Much appreaciated. Also, if there's an easier way to do this without using classes, that would be great. Thanks.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    ccgrl451,

    Please use code tags when posting code. See posting guidelines here. I appreciate your creative use of indentation, but code tags are much better suited to displaying code.

    To inherit from another class, leave off the parentheses:
    Code:
    class Pyramid(Turtle):
    There is no need to create class variables in your case. You can assign defaults like this:
    Code:
        def __init__(self, color='yellow', shape='triangle', shapesize=4):
    I don't see how you can be sure you will ever find the pyramid by moving only forward and right. You should check the turtle's position after every move. In your while loop, you are moving twice before checking the location. I would do something like this:

    Code:
    while True:
        # 'a' and 'b' can be any numbers of your choice
        Billy.forward(randint(a,b))
        if Billy.distance(Sphinx)>20:
            break
        Billy.right(randint(a,b))
        if Billy.distance(Sphinx)>20:
            break
        Billy.backward(randint(a,b))
        if Billy.distance(Sphinx)>20:
            break
        Billy.left(randint(a,b))
        if Billy.distance(Sphinx)>20:
            break
    # summarize the results here
    return 'summarized results'
    I would not encapsulate the classes and movement in the same function. I would suggest this format:

    imports
    class 1 definition
    class 2 definition
    define function "main()" where you create class instances and random movements and return results
    Then:
    Code:
    if __name__ == "__main__":
        main()

    Comment

    • ccgrl451
      New Member
      • Nov 2009
      • 8

      #3
      Sorry this took too long to check. I was busy. Thanks this helped me tons. Also, the link to the posting guidelines are great for future reference!

      Comment

      Working...