Zapfhahn_
Zapfhahn_

Reputation: 31

How to use variable from a method from another file?

I made a Snake game with the help of Angela Wu's 100 days of Coding Python course on Udemy. Every time the "food" gets eaten, the snake gets longer. So far so good. I changed the color of the new food to a random color using the function make_color in food.py:

    def make_color(self):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        random_color = r, g, b
        return random_color

In the following method, where the new snake segment gets created, I called this method:

    def refresh(self):
        self.setheading(random.randint(0, 360))
        self.color(self.make_color())
        random_x = random.randint(-280, 280)
        random_y = random.randint(-280, 280)
        self.goto(random_x, random_y)

Both are inside the Food class.

So now every new food has a random color, as desired.

When the food gets eaten, the snake gains another segment; this is handled in snake.py inside the Snake class:

    def add_segment(self, position):
        new_segment = Turtle(shape="square")
        new_segment.color(255, 255, 255)
        new_segment.penup()
        new_segment.goto(position)
        self.segments.append(new_segment)

This works totally fine, the new segment is white since I used 255, 255, 255. But I want it to have the same random color as the food it just ate. And I just can't get it to work.

I guess I need to use the random_color variable from food.py but I can't figure out how. food is imported in the snake.py via from food import Food

Upvotes: 2

Views: 77

Answers (3)

Zapfhahn_
Zapfhahn_

Reputation: 31

Thanks guys for alle the help and comments! Ill try to continue just showing relevant code.

In the end I found a different way to solve my problem:

I added an empty list to the top of the file color_list = []. In the make_color method I used list.append() to add the random_color to the list:

def make_color(self):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        random_color = r, g, b
        color_list.append(random_color)

Where the new snake segment gets created, I changed it to this:

    def add_segment(self, position):
        new_segment = Turtle(shape="square")
        new_segment.color(color_list[0])
        new_segment.penup()
        new_segment.goto(position)
        self.segments.append(new_segment)

So now it takes the first color from the color list and uses it for the snake segment. First, I used [-1] so it always used the last one that has been added. But with that, the list gets longer and longer. So before the new food gets created, I added color_list.clear(). Now the list only consists of the current color that gets created for the food, used for the snake segment when the snake ate the food and then gets deleted.

Upvotes: 1

Mad Physicist
Mad Physicist

Reputation: 114250

Notice that in make_color, you don't use the argument self. That's a good rule of thumb that something shouldn't be in your class, since it's a utility function. And as you've found, utility functions are useful everywhere, not just the first place you wrote them.

The first step is to take make_color outside the class and remove the self argument:

def make_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    random_color = r, g, b
    return random_color

Now in snake.py, you import it along with Food:

from food import Food, make_color

And voila, you can use it anywhere in the snake module now. Don't forget to splat the arguments:

new_segment.color(*make_color())

Upvotes: 0

Matt Pitkin
Matt Pitkin

Reputation: 6392

You'll need the add_segment method to take another argument along with position, in this case that argument should be a Food object (so maybe call the argument food). Then, within add_segment you can extract the colour of the food (assuming the color method in Food sets an attribute containing the colour tuple, or there's a method called, say, get_color() that gets the tuple). E.g.:

def add_segment(self, position, food):
    new_segment = Turtle(shape="square")
    new_segment.color(food.get_colour())
    new_segment.penup()
    new_segment.goto(position)
    self.segments.append(new_segment)

This does mean you'll need to change wherever in your code add_segment is called to pass it the food object that has just been consumed.

Upvotes: 2

Related Questions